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 __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
144 /* We want more bytes than seem to be available.
145 * Maybe we need a new page, maybe we have just run out
147 unsigned int avail = (char *)argp->end - (char *)argp->p;
149 if (avail + argp->pagelen < nbytes)
151 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
153 /* ok, we can do it with the current plus the next page */
154 if (nbytes <= sizeof(argp->tmp))
158 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
164 * The following memcpy is safe because read_buf is always
165 * called with nbytes > avail, and the two cases above both
166 * guarantee p points to at least nbytes bytes.
168 memcpy(p, argp->p, avail);
169 /* step to next page */
171 argp->p = page_address(argp->pagelist[0]);
172 if (argp->pagelen < PAGE_SIZE) {
173 argp->end = argp->p + (argp->pagelen>>2);
176 argp->end = argp->p + (PAGE_SIZE>>2);
177 argp->pagelen -= PAGE_SIZE;
179 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
180 argp->p += XDR_QUADLEN(nbytes - avail);
184 static int zero_clientid(clientid_t *clid)
186 return (clid->cl_boot == 0) && (clid->cl_id == 0);
190 defer_free(struct nfsd4_compoundargs *argp,
191 void (*release)(const void *), void *p)
195 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
199 tb->release = release;
200 tb->next = argp->to_free;
205 static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
207 if (p == argp->tmp) {
208 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
212 BUG_ON(p != argp->tmpp);
215 if (defer_free(argp, kfree, p)) {
223 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
237 READ_BUF(bmlen << 2);
249 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
250 struct iattr *iattr, struct nfs4_acl **acl,
251 struct xdr_netobj *label)
253 int expected_len, len = 0;
260 if ((status = nfsd4_decode_bitmap(argp, bmval)))
264 READ32(expected_len);
266 if (bmval[0] & FATTR4_WORD0_SIZE) {
269 READ64(iattr->ia_size);
270 iattr->ia_valid |= ATTR_SIZE;
272 if (bmval[0] & FATTR4_WORD0_ACL) {
274 struct nfs4_ace *ace;
276 READ_BUF(4); len += 4;
279 if (nace > NFS4_ACL_MAX)
280 return nfserr_resource;
282 *acl = nfs4_acl_new(nace);
287 defer_free(argp, kfree, *acl);
289 (*acl)->naces = nace;
290 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
291 READ_BUF(16); len += 16;
294 READ32(ace->access_mask);
297 len += XDR_QUADLEN(dummy32) << 2;
298 READMEM(buf, dummy32);
299 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
301 if (ace->whotype != NFS4_ACL_WHO_NAMED)
303 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
304 status = nfsd_map_name_to_gid(argp->rqstp,
305 buf, dummy32, &ace->who_gid);
307 status = nfsd_map_name_to_uid(argp->rqstp,
308 buf, dummy32, &ace->who_uid);
314 if (bmval[1] & FATTR4_WORD1_MODE) {
317 READ32(iattr->ia_mode);
318 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
319 iattr->ia_valid |= ATTR_MODE;
321 if (bmval[1] & FATTR4_WORD1_OWNER) {
326 len += (XDR_QUADLEN(dummy32) << 2);
327 READMEM(buf, dummy32);
328 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
330 iattr->ia_valid |= ATTR_UID;
332 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
337 len += (XDR_QUADLEN(dummy32) << 2);
338 READMEM(buf, dummy32);
339 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
341 iattr->ia_valid |= ATTR_GID;
343 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
348 case NFS4_SET_TO_CLIENT_TIME:
349 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
350 all 32 bits of 'nseconds'. */
353 READ64(iattr->ia_atime.tv_sec);
354 READ32(iattr->ia_atime.tv_nsec);
355 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
357 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
359 case NFS4_SET_TO_SERVER_TIME:
360 iattr->ia_valid |= ATTR_ATIME;
366 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
371 case NFS4_SET_TO_CLIENT_TIME:
372 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
373 all 32 bits of 'nseconds'. */
376 READ64(iattr->ia_mtime.tv_sec);
377 READ32(iattr->ia_mtime.tv_nsec);
378 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
380 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
382 case NFS4_SET_TO_SERVER_TIME:
383 iattr->ia_valid |= ATTR_MTIME;
391 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
392 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
395 READ32(dummy32); /* lfs: we don't use it */
398 READ32(dummy32); /* pi: we don't use it either */
403 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
404 return nfserr_badlabel;
405 len += (XDR_QUADLEN(dummy32) << 2);
406 READMEM(buf, dummy32);
407 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
409 return nfserr_jukebox;
410 defer_free(argp, kfree, label->data);
411 memcpy(label->data, buf, dummy32);
415 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
416 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
417 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
418 READ_BUF(expected_len - len);
419 else if (len != expected_len)
425 status = nfserrno(host_err);
430 nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
434 READ_BUF(sizeof(stateid_t));
435 READ32(sid->si_generation);
436 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
442 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
447 READ32(access->ac_req_access);
452 static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
460 /* callback_sec_params4 */
464 cbs->flavor = (u32)(-1);
466 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
468 for (i = 0; i < nr_secflavs; ++i) {
473 /* Nothing to read */
474 if (cbs->flavor == (u32)(-1))
475 cbs->flavor = RPC_AUTH_NULL;
485 SAVEMEM(machine_name, dummy);
496 if (cbs->flavor == (u32)(-1)) {
497 kuid_t kuid = make_kuid(&init_user_ns, uid);
498 kgid_t kgid = make_kgid(&init_user_ns, gid);
499 if (uid_valid(kuid) && gid_valid(kgid)) {
502 cbs->flavor = RPC_AUTH_UNIX;
504 dprintk("RPC_AUTH_UNIX with invalid"
505 "uid or gid ignoring!\n");
510 dprintk("RPC_AUTH_GSS callback secflavor "
515 /* gcbp_handle_from_server */
518 p += XDR_QUADLEN(dummy);
519 /* gcbp_handle_from_client */
525 dprintk("Illegal callback secflavor\n");
532 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
537 READ32(bc->bc_cb_program);
538 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
543 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
547 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
548 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
550 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
551 * could help us figure out we should be using it. */
556 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
561 READ32(close->cl_seqid);
562 return nfsd4_decode_stateid(argp, &close->cl_stateid);
569 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
574 READ64(commit->co_offset);
575 READ32(commit->co_count);
581 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
586 READ32(create->cr_type);
587 switch (create->cr_type) {
590 READ32(create->cr_linklen);
591 READ_BUF(create->cr_linklen);
592 SAVEMEM(create->cr_linkname, create->cr_linklen);
597 READ32(create->cr_specdata1);
598 READ32(create->cr_specdata2);
608 READ32(create->cr_namelen);
609 READ_BUF(create->cr_namelen);
610 SAVEMEM(create->cr_name, create->cr_namelen);
611 if ((status = check_filename(create->cr_name, create->cr_namelen)))
614 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
615 &create->cr_acl, &create->cr_label);
623 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
625 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
629 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
631 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
635 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
640 READ32(link->li_namelen);
641 READ_BUF(link->li_namelen);
642 SAVEMEM(link->li_name, link->li_namelen);
643 if ((status = check_filename(link->li_name, link->li_namelen)))
650 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
655 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
658 READ32(lock->lk_type);
659 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
661 READ32(lock->lk_reclaim);
662 READ64(lock->lk_offset);
663 READ64(lock->lk_length);
664 READ32(lock->lk_is_new);
666 if (lock->lk_is_new) {
668 READ32(lock->lk_new_open_seqid);
669 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
672 READ_BUF(8 + sizeof(clientid_t));
673 READ32(lock->lk_new_lock_seqid);
674 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
675 READ32(lock->lk_new_owner.len);
676 READ_BUF(lock->lk_new_owner.len);
677 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
679 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
683 READ32(lock->lk_old_lock_seqid);
690 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
695 READ32(lockt->lt_type);
696 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
698 READ64(lockt->lt_offset);
699 READ64(lockt->lt_length);
700 COPYMEM(&lockt->lt_clientid, 8);
701 READ32(lockt->lt_owner.len);
702 READ_BUF(lockt->lt_owner.len);
703 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
709 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
714 READ32(locku->lu_type);
715 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
717 READ32(locku->lu_seqid);
718 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
722 READ64(locku->lu_offset);
723 READ64(locku->lu_length);
729 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
734 READ32(lookup->lo_len);
735 READ_BUF(lookup->lo_len);
736 SAVEMEM(lookup->lo_name, lookup->lo_len);
737 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
743 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
750 *share_access = w & NFS4_SHARE_ACCESS_MASK;
751 *deleg_want = w & NFS4_SHARE_WANT_MASK;
753 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
755 switch (w & NFS4_SHARE_ACCESS_MASK) {
756 case NFS4_SHARE_ACCESS_READ:
757 case NFS4_SHARE_ACCESS_WRITE:
758 case NFS4_SHARE_ACCESS_BOTH:
761 return nfserr_bad_xdr;
763 w &= ~NFS4_SHARE_ACCESS_MASK;
766 if (!argp->minorversion)
767 return nfserr_bad_xdr;
768 switch (w & NFS4_SHARE_WANT_MASK) {
769 case NFS4_SHARE_WANT_NO_PREFERENCE:
770 case NFS4_SHARE_WANT_READ_DELEG:
771 case NFS4_SHARE_WANT_WRITE_DELEG:
772 case NFS4_SHARE_WANT_ANY_DELEG:
773 case NFS4_SHARE_WANT_NO_DELEG:
774 case NFS4_SHARE_WANT_CANCEL:
777 return nfserr_bad_xdr;
779 w &= ~NFS4_SHARE_WANT_MASK;
783 if (!deleg_when) /* open_downgrade */
786 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
787 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
788 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
789 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
793 return nfserr_bad_xdr;
796 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
802 /* Note: unlinke access bits, deny bits may be zero. */
803 if (*x & ~NFS4_SHARE_DENY_BOTH)
804 return nfserr_bad_xdr;
807 return nfserr_bad_xdr;
810 static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
817 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
818 return nfserr_bad_xdr;
821 SAVEMEM(o->data, o->len);
824 return nfserr_bad_xdr;
828 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
833 memset(open->op_bmval, 0, sizeof(open->op_bmval));
834 open->op_iattr.ia_valid = 0;
835 open->op_openowner = NULL;
837 open->op_xdr_error = 0;
838 /* seqid, share_access, share_deny, clientid, ownerlen */
840 READ32(open->op_seqid);
841 /* decode, yet ignore deleg_when until supported */
842 status = nfsd4_decode_share_access(argp, &open->op_share_access,
843 &open->op_deleg_want, &dummy);
846 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
849 READ_BUF(sizeof(clientid_t));
850 COPYMEM(&open->op_clientid, sizeof(clientid_t));
851 status = nfsd4_decode_opaque(argp, &open->op_owner);
855 READ32(open->op_create);
856 switch (open->op_create) {
857 case NFS4_OPEN_NOCREATE:
859 case NFS4_OPEN_CREATE:
861 READ32(open->op_createmode);
862 switch (open->op_createmode) {
863 case NFS4_CREATE_UNCHECKED:
864 case NFS4_CREATE_GUARDED:
865 status = nfsd4_decode_fattr(argp, open->op_bmval,
866 &open->op_iattr, &open->op_acl, &open->op_label);
870 case NFS4_CREATE_EXCLUSIVE:
871 READ_BUF(NFS4_VERIFIER_SIZE);
872 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
874 case NFS4_CREATE_EXCLUSIVE4_1:
875 if (argp->minorversion < 1)
877 READ_BUF(NFS4_VERIFIER_SIZE);
878 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
879 status = nfsd4_decode_fattr(argp, open->op_bmval,
880 &open->op_iattr, &open->op_acl, &open->op_label);
894 READ32(open->op_claim_type);
895 switch (open->op_claim_type) {
896 case NFS4_OPEN_CLAIM_NULL:
897 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
899 READ32(open->op_fname.len);
900 READ_BUF(open->op_fname.len);
901 SAVEMEM(open->op_fname.data, open->op_fname.len);
902 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
905 case NFS4_OPEN_CLAIM_PREVIOUS:
907 READ32(open->op_delegate_type);
909 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
910 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
914 READ32(open->op_fname.len);
915 READ_BUF(open->op_fname.len);
916 SAVEMEM(open->op_fname.data, open->op_fname.len);
917 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
920 case NFS4_OPEN_CLAIM_FH:
921 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
922 if (argp->minorversion < 1)
926 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
927 if (argp->minorversion < 1)
929 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
941 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
945 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
949 READ32(open_conf->oc_seqid);
955 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
959 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
963 READ32(open_down->od_seqid);
964 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
965 &open_down->od_deleg_want, NULL);
968 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
975 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
980 READ32(putfh->pf_fhlen);
981 if (putfh->pf_fhlen > NFS4_FHSIZE)
983 READ_BUF(putfh->pf_fhlen);
984 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
990 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
994 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
998 READ64(read->rd_offset);
999 READ32(read->rd_length);
1005 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1010 READ64(readdir->rd_cookie);
1011 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1012 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1013 READ32(readdir->rd_maxcount);
1014 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1021 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1026 READ32(remove->rm_namelen);
1027 READ_BUF(remove->rm_namelen);
1028 SAVEMEM(remove->rm_name, remove->rm_namelen);
1029 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1036 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1041 READ32(rename->rn_snamelen);
1042 READ_BUF(rename->rn_snamelen + 4);
1043 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1044 READ32(rename->rn_tnamelen);
1045 READ_BUF(rename->rn_tnamelen);
1046 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1047 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1049 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1056 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1060 READ_BUF(sizeof(clientid_t));
1061 COPYMEM(clientid, sizeof(clientid_t));
1067 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1068 struct nfsd4_secinfo *secinfo)
1073 READ32(secinfo->si_namelen);
1074 READ_BUF(secinfo->si_namelen);
1075 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1076 status = check_filename(secinfo->si_name, secinfo->si_namelen);
1083 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1084 struct nfsd4_secinfo_no_name *sin)
1089 READ32(sin->sin_style);
1094 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1098 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1101 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1102 &setattr->sa_acl, &setattr->sa_label);
1106 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1110 READ_BUF(NFS4_VERIFIER_SIZE);
1111 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1113 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1115 return nfserr_bad_xdr;
1117 READ32(setclientid->se_callback_prog);
1118 READ32(setclientid->se_callback_netid_len);
1120 READ_BUF(setclientid->se_callback_netid_len + 4);
1121 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1122 READ32(setclientid->se_callback_addr_len);
1124 READ_BUF(setclientid->se_callback_addr_len + 4);
1125 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1126 READ32(setclientid->se_callback_ident);
1132 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1136 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1137 COPYMEM(&scd_c->sc_clientid, 8);
1138 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1143 /* Also used for NVERIFY */
1145 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1149 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1152 /* For convenience's sake, we compare raw xdr'd attributes in
1153 * nfsd4_proc_verify */
1156 READ32(verify->ve_attrlen);
1157 READ_BUF(verify->ve_attrlen);
1158 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1164 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1170 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1174 READ64(write->wr_offset);
1175 READ32(write->wr_stable_how);
1176 if (write->wr_stable_how > 2)
1178 READ32(write->wr_buflen);
1180 /* Sorry .. no magic macros for this.. *
1181 * READ_BUF(write->wr_buflen);
1182 * SAVEMEM(write->wr_buf, write->wr_buflen);
1184 avail = (char*)argp->end - (char*)argp->p;
1185 if (avail + argp->pagelen < write->wr_buflen) {
1186 dprintk("NFSD: xdr error (%s:%d)\n",
1187 __FILE__, __LINE__);
1190 write->wr_head.iov_base = p;
1191 write->wr_head.iov_len = avail;
1192 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
1193 write->wr_pagelist = argp->pagelist;
1195 len = XDR_QUADLEN(write->wr_buflen) << 2;
1201 pages = len >> PAGE_SHIFT;
1202 argp->pagelist += pages;
1203 argp->pagelen -= pages * PAGE_SIZE;
1204 len -= pages * PAGE_SIZE;
1206 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1207 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
1209 argp->p += XDR_QUADLEN(len);
1215 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1220 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1221 READ32(rlockowner->rl_owner.len);
1222 READ_BUF(rlockowner->rl_owner.len);
1223 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1225 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1226 return nfserr_inval;
1231 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1232 struct nfsd4_exchange_id *exid)
1237 READ_BUF(NFS4_VERIFIER_SIZE);
1238 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1240 status = nfsd4_decode_opaque(argp, &exid->clname);
1242 return nfserr_bad_xdr;
1245 READ32(exid->flags);
1247 /* Ignore state_protect4_a */
1249 READ32(exid->spa_how);
1250 switch (exid->spa_how) {
1254 /* spo_must_enforce */
1257 READ_BUF(dummy * 4);
1260 /* spo_must_allow */
1263 READ_BUF(dummy * 4);
1270 READ_BUF(dummy * 4);
1275 READ_BUF(dummy * 4);
1278 /* ssp_hash_algs<> */
1285 p += XDR_QUADLEN(dummy);
1288 /* ssp_encr_algs<> */
1295 p += XDR_QUADLEN(dummy);
1298 /* ssp_window and ssp_num_gss_handles */
1307 /* Ignore Implementation ID */
1308 READ_BUF(4); /* nfs_impl_id4 array length */
1319 p += XDR_QUADLEN(dummy);
1325 p += XDR_QUADLEN(dummy);
1335 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1336 struct nfsd4_create_session *sess)
1342 COPYMEM(&sess->clientid, 8);
1343 READ32(sess->seqid);
1344 READ32(sess->flags);
1346 /* Fore channel attrs */
1348 READ32(dummy); /* headerpadsz is always 0 */
1349 READ32(sess->fore_channel.maxreq_sz);
1350 READ32(sess->fore_channel.maxresp_sz);
1351 READ32(sess->fore_channel.maxresp_cached);
1352 READ32(sess->fore_channel.maxops);
1353 READ32(sess->fore_channel.maxreqs);
1354 READ32(sess->fore_channel.nr_rdma_attrs);
1355 if (sess->fore_channel.nr_rdma_attrs == 1) {
1357 READ32(sess->fore_channel.rdma_attrs);
1358 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1359 dprintk("Too many fore channel attr bitmaps!\n");
1363 /* Back channel attrs */
1365 READ32(dummy); /* headerpadsz is always 0 */
1366 READ32(sess->back_channel.maxreq_sz);
1367 READ32(sess->back_channel.maxresp_sz);
1368 READ32(sess->back_channel.maxresp_cached);
1369 READ32(sess->back_channel.maxops);
1370 READ32(sess->back_channel.maxreqs);
1371 READ32(sess->back_channel.nr_rdma_attrs);
1372 if (sess->back_channel.nr_rdma_attrs == 1) {
1374 READ32(sess->back_channel.rdma_attrs);
1375 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1376 dprintk("Too many back channel attr bitmaps!\n");
1381 READ32(sess->callback_prog);
1382 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1387 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1388 struct nfsd4_destroy_session *destroy_session)
1391 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1392 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1398 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1399 struct nfsd4_free_stateid *free_stateid)
1403 READ_BUF(sizeof(stateid_t));
1404 READ32(free_stateid->fr_stateid.si_generation);
1405 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1411 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1412 struct nfsd4_sequence *seq)
1416 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1417 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1419 READ32(seq->slotid);
1420 READ32(seq->maxslots);
1421 READ32(seq->cachethis);
1427 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1431 struct nfsd4_test_stateid_id *stateid;
1434 test_stateid->ts_num_ids = ntohl(*p++);
1436 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1438 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1439 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1441 status = nfserrno(-ENOMEM);
1445 defer_free(argp, kfree, stateid);
1446 INIT_LIST_HEAD(&stateid->ts_id_list);
1447 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1449 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1458 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1459 status = nfserr_bad_xdr;
1463 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1468 COPYMEM(&dc->clientid, 8);
1473 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1478 READ32(rc->rca_one_fs);
1484 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1490 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1492 return nfserr_notsupp;
1495 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1497 static nfsd4_dec nfsd4_dec_ops[] = {
1498 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1499 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1500 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1501 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1502 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1503 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1504 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1505 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1506 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1507 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1508 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1509 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1510 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1511 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1512 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1513 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1514 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1515 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1516 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1517 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1518 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
1519 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1520 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1521 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1522 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1523 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1524 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1525 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1526 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1527 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1528 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1529 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1530 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1531 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1532 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1533 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1534 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
1537 static nfsd4_dec nfsd41_dec_ops[] = {
1538 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1539 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1540 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1541 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1542 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1543 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1544 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1545 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1546 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1547 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1548 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1549 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1550 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1551 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1552 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1553 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1554 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1555 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1556 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1557 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1558 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1559 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1560 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1561 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1562 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1563 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1564 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1565 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1566 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1567 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1568 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1569 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1570 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1571 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1572 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1573 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1574 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
1576 /* new operations for NFSv4.1 */
1577 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1578 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1579 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1580 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1581 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
1582 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
1583 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1584 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1585 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1586 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1587 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1588 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
1589 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1590 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1591 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
1592 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
1593 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1594 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
1595 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
1598 struct nfsd4_minorversion_ops {
1599 nfsd4_dec *decoders;
1603 static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
1604 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
1605 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1606 [2] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1610 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1613 struct nfsd4_op *op;
1614 struct nfsd4_minorversion_ops *ops;
1615 bool cachethis = false;
1619 READ32(argp->taglen);
1620 READ_BUF(argp->taglen + 8);
1621 SAVEMEM(argp->tag, argp->taglen);
1622 READ32(argp->minorversion);
1623 READ32(argp->opcnt);
1625 if (argp->taglen > NFSD4_MAX_TAGLEN)
1627 if (argp->opcnt > 100)
1630 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1631 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1633 argp->ops = argp->iops;
1634 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1639 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
1642 ops = &nfsd4_minorversion[argp->minorversion];
1643 for (i = 0; i < argp->opcnt; i++) {
1650 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
1651 op->status = ops->decoders[op->opnum](argp, &op->u);
1653 op->opnum = OP_ILLEGAL;
1654 op->status = nfserr_op_illegal;
1662 * We'll try to cache the result in the DRC if any one
1663 * op in the compound wants to be cached:
1665 cachethis |= nfsd4_cache_this_op(op);
1667 /* Sessions make the DRC unnecessary: */
1668 if (argp->minorversion)
1670 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1675 #define WRITE32(n) *p++ = htonl(n)
1676 #define WRITE64(n) do { \
1677 *p++ = htonl((u32)((n) >> 32)); \
1678 *p++ = htonl((u32)(n)); \
1680 #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
1681 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1682 memcpy(p, ptr, nbytes); \
1683 p += XDR_QUADLEN(nbytes); \
1686 static void write32(__be32 **p, u32 n)
1691 static void write64(__be32 **p, u64 n)
1693 write32(p, (n >> 32));
1697 static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1699 if (IS_I_VERSION(inode)) {
1700 write64(p, inode->i_version);
1702 write32(p, stat->ctime.tv_sec);
1703 write32(p, stat->ctime.tv_nsec);
1707 static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1709 write32(p, c->atomic);
1710 if (c->change_supported) {
1711 write64(p, c->before_change);
1712 write64(p, c->after_change);
1714 write32(p, c->before_ctime_sec);
1715 write32(p, c->before_ctime_nsec);
1716 write32(p, c->after_ctime_sec);
1717 write32(p, c->after_ctime_nsec);
1721 #define RESERVE_SPACE(nbytes) do { \
1723 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1725 #define ADJUST_ARGS() resp->p = p
1727 /* Encode as an array of strings the string given with components
1728 * separated @sep, escaped with esc_enter and esc_exit.
1730 static __be32 nfsd4_encode_components_esc(char sep, char *components,
1731 __be32 **pp, int *buflen,
1732 char esc_enter, char esc_exit)
1736 int strlen, count=0;
1737 char *str, *end, *next;
1739 dprintk("nfsd4_encode_components(%s)\n", components);
1740 if ((*buflen -= 4) < 0)
1741 return nfserr_resource;
1742 WRITE32(0); /* We will fill this in with @count later */
1743 end = str = components;
1745 bool found_esc = false;
1747 /* try to parse as esc_start, ..., esc_end, sep */
1748 if (*str == esc_enter) {
1749 for (; *end && (*end != esc_exit); end++)
1750 /* find esc_exit or end of string */;
1752 if (*end && (!*next || *next == sep)) {
1759 for (; *end && (*end != sep); end++)
1760 /* find sep or end of string */;
1764 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1765 return nfserr_resource;
1767 WRITEMEM(str, strlen);
1780 /* Encode as an array of strings the string given with components
1783 static __be32 nfsd4_encode_components(char sep, char *components,
1784 __be32 **pp, int *buflen)
1786 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1790 * encode a location element of a fs_locations structure
1792 static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1793 __be32 **pp, int *buflen)
1798 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1802 status = nfsd4_encode_components('/', location->path, &p, buflen);
1810 * Encode a path in RFC3530 'pathname4' format
1812 static __be32 nfsd4_encode_path(const struct path *root,
1813 const struct path *path, __be32 **pp, int *buflen)
1817 .dentry = path->dentry,
1820 struct dentry **components = NULL;
1821 unsigned int ncomponents = 0;
1822 __be32 err = nfserr_jukebox;
1824 dprintk("nfsd4_encode_components(");
1827 /* First walk the path up to the nfsd root, and store the
1828 * dentries/path components in an array.
1831 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1833 if (cur.dentry == cur.mnt->mnt_root) {
1834 if (follow_up(&cur))
1838 if ((ncomponents & 15) == 0) {
1839 struct dentry **new;
1840 new = krealloc(components,
1841 sizeof(*new) * (ncomponents + 16),
1847 components[ncomponents++] = cur.dentry;
1848 cur.dentry = dget_parent(cur.dentry);
1854 WRITE32(ncomponents);
1856 while (ncomponents) {
1857 struct dentry *dentry = components[ncomponents - 1];
1858 unsigned int len = dentry->d_name.len;
1860 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1864 WRITEMEM(dentry->d_name.name, len);
1865 dprintk("/%s", dentry->d_name.name);
1875 dput(components[--ncomponents]);
1881 static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1882 const struct path *path, __be32 **pp, int *buflen)
1884 struct svc_export *exp_ps;
1887 exp_ps = rqst_find_fsidzero_export(rqstp);
1889 return nfserrno(PTR_ERR(exp_ps));
1890 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1896 * encode a fs_locations structure
1898 static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1899 struct svc_export *exp,
1900 __be32 **pp, int *buflen)
1905 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1907 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
1910 if ((*buflen -= 4) < 0)
1911 return nfserr_resource;
1912 WRITE32(fslocs->locations_count);
1913 for (i=0; i<fslocs->locations_count; i++) {
1914 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1923 static u32 nfs4_file_type(umode_t mode)
1925 switch (mode & S_IFMT) {
1926 case S_IFIFO: return NF4FIFO;
1927 case S_IFCHR: return NF4CHR;
1928 case S_IFDIR: return NF4DIR;
1929 case S_IFBLK: return NF4BLK;
1930 case S_IFLNK: return NF4LNK;
1931 case S_IFREG: return NF4REG;
1932 case S_IFSOCK: return NF4SOCK;
1933 default: return NF4BAD;
1938 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
1939 __be32 **p, int *buflen)
1943 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1944 return nfserr_resource;
1945 if (whotype != NFS4_ACL_WHO_NAMED)
1946 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1947 else if (gid_valid(gid))
1948 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
1950 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
1952 return nfserrno(status);
1953 *p = xdr_encode_opaque(*p, NULL, status);
1954 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1955 BUG_ON(*buflen < 0);
1959 static inline __be32
1960 nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
1962 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1966 static inline __be32
1967 nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
1969 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1973 static inline __be32
1974 nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
1975 __be32 **p, int *buflen)
1977 kuid_t uid = INVALID_UID;
1978 kgid_t gid = INVALID_GID;
1980 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1981 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1986 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
1989 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1990 FATTR4_WORD0_RDATTR_ERROR)
1991 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1993 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
1994 static inline __be32
1995 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
1999 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
2000 return nfserr_resource;
2003 * For now we use a 0 here to indicate the null translation; in
2004 * the future we may place a call to translation code here.
2006 if ((*buflen -= 8) < 0)
2007 return nfserr_resource;
2009 WRITE32(0); /* lfs */
2010 WRITE32(0); /* pi */
2011 p = xdr_encode_opaque(p, context, len);
2012 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2018 static inline __be32
2019 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2023 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
2025 /* As per referral draft: */
2026 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2027 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2028 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2029 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2030 *rdattr_err = NFSERR_MOVED;
2032 return nfserr_moved;
2034 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2035 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2040 static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2042 struct path path = exp->ex_path;
2046 while (follow_up(&path)) {
2047 if (path.dentry != path.mnt->mnt_root)
2050 err = vfs_getattr(&path, stat);
2056 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2059 * countp is the buffer size in _words_
2062 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
2063 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
2064 struct svc_rqst *rqstp, int ignore_crossmnt)
2066 u32 bmval0 = bmval[0];
2067 u32 bmval1 = bmval[1];
2068 u32 bmval2 = bmval[2];
2070 struct svc_fh tempfh;
2071 struct kstatfs statfs;
2072 int buflen = count << 2;
2077 __be32 *p = *buffer;
2081 struct nfs4_acl *acl = NULL;
2082 void *context = NULL;
2084 bool contextsupport = false;
2085 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2086 u32 minorversion = resp->cstate.minorversion;
2087 struct path path = {
2088 .mnt = exp->ex_path.mnt,
2091 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2093 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2094 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2095 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2096 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
2098 if (exp->ex_fslocs.migrated) {
2100 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2105 err = vfs_getattr(&path, &stat);
2108 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2109 FATTR4_WORD0_MAXNAME)) ||
2110 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2111 FATTR4_WORD1_SPACE_TOTAL))) {
2112 err = vfs_statfs(&path, &statfs);
2116 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2117 fh_init(&tempfh, NFS4_FHSIZE);
2118 status = fh_compose(&tempfh, exp, dentry, NULL);
2123 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2124 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
2125 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2126 aclsupport = (err == 0);
2127 if (bmval0 & FATTR4_WORD0_ACL) {
2128 if (err == -EOPNOTSUPP)
2129 bmval0 &= ~FATTR4_WORD0_ACL;
2130 else if (err == -EINVAL) {
2131 status = nfserr_attrnotsupp;
2133 } else if (err != 0)
2138 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2139 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2140 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2141 err = security_inode_getsecctx(dentry->d_inode,
2142 &context, &contextlen);
2143 contextsupport = (err == 0);
2144 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2145 if (err == -EOPNOTSUPP)
2146 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2151 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2154 if ((buflen -= 16) < 0)
2160 } else if (bmval1) {
2161 if ((buflen -= 12) < 0)
2167 if ((buflen -= 8) < 0)
2172 attrlenp = p++; /* to be backfilled later */
2174 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2175 u32 word0 = nfsd_suppattrs0(minorversion);
2176 u32 word1 = nfsd_suppattrs1(minorversion);
2177 u32 word2 = nfsd_suppattrs2(minorversion);
2180 word0 &= ~FATTR4_WORD0_ACL;
2181 if (!contextsupport)
2182 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2184 if ((buflen -= 12) < 0)
2190 if ((buflen -= 16) < 0)
2198 if (bmval0 & FATTR4_WORD0_TYPE) {
2199 if ((buflen -= 4) < 0)
2201 dummy = nfs4_file_type(stat.mode);
2202 if (dummy == NF4BAD)
2203 goto out_serverfault;
2206 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2207 if ((buflen -= 4) < 0)
2209 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2210 WRITE32(NFS4_FH_PERSISTENT);
2212 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
2214 if (bmval0 & FATTR4_WORD0_CHANGE) {
2215 if ((buflen -= 8) < 0)
2217 write_change(&p, &stat, dentry->d_inode);
2219 if (bmval0 & FATTR4_WORD0_SIZE) {
2220 if ((buflen -= 8) < 0)
2224 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2225 if ((buflen -= 4) < 0)
2229 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2230 if ((buflen -= 4) < 0)
2234 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2235 if ((buflen -= 4) < 0)
2239 if (bmval0 & FATTR4_WORD0_FSID) {
2240 if ((buflen -= 16) < 0)
2242 if (exp->ex_fslocs.migrated) {
2243 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2244 WRITE64(NFS4_REFERRAL_FSID_MINOR);
2245 } else switch(fsid_source(fhp)) {
2246 case FSIDSOURCE_FSID:
2247 WRITE64((u64)exp->ex_fsid);
2250 case FSIDSOURCE_DEV:
2252 WRITE32(MAJOR(stat.dev));
2254 WRITE32(MINOR(stat.dev));
2256 case FSIDSOURCE_UUID:
2257 WRITEMEM(exp->ex_uuid, 16);
2261 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2262 if ((buflen -= 4) < 0)
2266 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2267 if ((buflen -= 4) < 0)
2269 WRITE32(nn->nfsd4_lease);
2271 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2272 if ((buflen -= 4) < 0)
2274 WRITE32(rdattr_err);
2276 if (bmval0 & FATTR4_WORD0_ACL) {
2277 struct nfs4_ace *ace;
2280 if ((buflen -= 4) < 0)
2286 if ((buflen -= 4) < 0)
2288 WRITE32(acl->naces);
2290 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2291 if ((buflen -= 4*3) < 0)
2295 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2296 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
2297 if (status == nfserr_resource)
2304 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2305 if ((buflen -= 4) < 0)
2307 WRITE32(aclsupport ?
2308 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2310 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2311 if ((buflen -= 4) < 0)
2315 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2316 if ((buflen -= 4) < 0)
2320 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2321 if ((buflen -= 4) < 0)
2325 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2326 if ((buflen -= 4) < 0)
2330 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2331 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2334 WRITE32(fhp->fh_handle.fh_size);
2335 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2337 if (bmval0 & FATTR4_WORD0_FILEID) {
2338 if ((buflen -= 8) < 0)
2342 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2343 if ((buflen -= 8) < 0)
2345 WRITE64((u64) statfs.f_ffree);
2347 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2348 if ((buflen -= 8) < 0)
2350 WRITE64((u64) statfs.f_ffree);
2352 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2353 if ((buflen -= 8) < 0)
2355 WRITE64((u64) statfs.f_files);
2357 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2358 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2359 if (status == nfserr_resource)
2364 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2365 if ((buflen -= 4) < 0)
2369 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2370 if ((buflen -= 8) < 0)
2374 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2375 if ((buflen -= 4) < 0)
2379 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2380 if ((buflen -= 4) < 0)
2382 WRITE32(statfs.f_namelen);
2384 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2385 if ((buflen -= 8) < 0)
2387 WRITE64((u64) svc_max_payload(rqstp));
2389 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2390 if ((buflen -= 8) < 0)
2392 WRITE64((u64) svc_max_payload(rqstp));
2394 if (bmval1 & FATTR4_WORD1_MODE) {
2395 if ((buflen -= 4) < 0)
2397 WRITE32(stat.mode & S_IALLUGO);
2399 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2400 if ((buflen -= 4) < 0)
2404 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2405 if ((buflen -= 4) < 0)
2407 WRITE32(stat.nlink);
2409 if (bmval1 & FATTR4_WORD1_OWNER) {
2410 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2411 if (status == nfserr_resource)
2416 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2417 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2418 if (status == nfserr_resource)
2423 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2424 if ((buflen -= 8) < 0)
2426 WRITE32((u32) MAJOR(stat.rdev));
2427 WRITE32((u32) MINOR(stat.rdev));
2429 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2430 if ((buflen -= 8) < 0)
2432 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2435 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2436 if ((buflen -= 8) < 0)
2438 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2441 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2442 if ((buflen -= 8) < 0)
2444 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2447 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2448 if ((buflen -= 8) < 0)
2450 dummy64 = (u64)stat.blocks << 9;
2453 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2454 if ((buflen -= 12) < 0)
2456 WRITE64((s64)stat.atime.tv_sec);
2457 WRITE32(stat.atime.tv_nsec);
2459 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2460 if ((buflen -= 12) < 0)
2466 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2467 if ((buflen -= 12) < 0)
2469 WRITE64((s64)stat.ctime.tv_sec);
2470 WRITE32(stat.ctime.tv_nsec);
2472 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2473 if ((buflen -= 12) < 0)
2475 WRITE64((s64)stat.mtime.tv_sec);
2476 WRITE32(stat.mtime.tv_nsec);
2478 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2479 if ((buflen -= 8) < 0)
2482 * Get parent's attributes if not ignoring crossmount
2483 * and this is the root of a cross-mounted filesystem.
2485 if (ignore_crossmnt == 0 &&
2486 dentry == exp->ex_path.mnt->mnt_root)
2487 get_parent_attributes(exp, &stat);
2490 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2491 status = nfsd4_encode_security_label(rqstp, context,
2492 contextlen, &p, &buflen);
2496 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2498 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2499 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2500 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2503 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2508 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2510 security_release_secctx(context, contextlen);
2511 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2517 status = nfserrno(err);
2520 status = nfserr_resource;
2523 status = nfserr_serverfault;
2527 static inline int attributes_need_mount(u32 *bmval)
2529 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2531 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2537 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
2538 const char *name, int namlen, __be32 **p, int buflen)
2540 struct svc_export *exp = cd->rd_fhp->fh_export;
2541 struct dentry *dentry;
2543 int ignore_crossmnt = 0;
2545 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2547 return nfserrno(PTR_ERR(dentry));
2548 if (!dentry->d_inode) {
2550 * nfsd_buffered_readdir drops the i_mutex between
2551 * readdir and calling this callback, leaving a window
2552 * where this directory entry could have gone away.
2555 return nfserr_noent;
2560 * In the case of a mountpoint, the client may be asking for
2561 * attributes that are only properties of the underlying filesystem
2562 * as opposed to the cross-mounted file system. In such a case,
2563 * we will not follow the cross mount and will fill the attribtutes
2564 * directly from the mountpoint dentry.
2566 if (nfsd_mountpoint(dentry, exp)) {
2569 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2570 && !attributes_need_mount(cd->rd_bmval)) {
2571 ignore_crossmnt = 1;
2575 * Why the heck aren't we just using nfsd_lookup??
2576 * Different "."/".." handling? Something else?
2577 * At least, add a comment here to explain....
2579 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2581 nfserr = nfserrno(err);
2584 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2590 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
2591 cd->rd_rqstp, ignore_crossmnt);
2599 nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
2606 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2607 *p++ = htonl(0); /* bmval1 */
2610 *p++ = nfserr; /* no htonl */
2611 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2616 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2617 loff_t offset, u64 ino, unsigned int d_type)
2619 struct readdir_cd *ccd = ccdv;
2620 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2622 __be32 *p = cd->buffer;
2624 __be32 nfserr = nfserr_toosmall;
2626 /* In nfsv4, "." and ".." never make it onto the wire.. */
2627 if (name && isdotent(name, namlen)) {
2628 cd->common.err = nfs_ok;
2633 xdr_encode_hyper(cd->offset, (u64) offset);
2635 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2639 *p++ = xdr_one; /* mark entry present */
2641 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2642 p = xdr_encode_array(p, name, namlen); /* name length & name */
2644 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
2648 case nfserr_resource:
2649 nfserr = nfserr_toosmall;
2655 * If the client requested the RDATTR_ERROR attribute,
2656 * we stuff the error code into this attribute
2657 * and continue. If this attribute was not requested,
2658 * then in accordance with the spec, we fail the
2659 * entire READDIR operation(!)
2661 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2663 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
2665 nfserr = nfserr_toosmall;
2669 cd->buflen -= (p - cd->buffer);
2671 cd->offset = cookiep;
2673 cd->common.err = nfs_ok;
2676 cd->common.err = nfserr;
2681 nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2685 RESERVE_SPACE(sizeof(stateid_t));
2686 WRITE32(sid->si_generation);
2687 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2692 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
2698 WRITE32(access->ac_supported);
2699 WRITE32(access->ac_resp_access);
2705 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2710 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2711 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2713 /* Sorry, we do not yet support RDMA over 4.1: */
2721 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
2724 nfsd4_encode_stateid(resp, &close->cl_stateid);
2731 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
2736 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2737 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
2744 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
2750 write_cinfo(&p, &create->cr_cinfo);
2752 WRITE32(create->cr_bmval[0]);
2753 WRITE32(create->cr_bmval[1]);
2760 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
2762 struct svc_fh *fhp = getattr->ga_fhp;
2768 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2769 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2770 &resp->p, buflen, getattr->ga_bmval,
2776 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
2778 struct svc_fh *fhp = *fhpp;
2783 len = fhp->fh_handle.fh_size;
2784 RESERVE_SPACE(len + 4);
2786 WRITEMEM(&fhp->fh_handle.fh_base, len);
2793 * Including all fields other than the name, a LOCK4denied structure requires
2794 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2797 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2799 struct xdr_netobj *conf = &ld->ld_owner;
2802 RESERVE_SPACE(32 + XDR_LEN(conf->len));
2803 WRITE64(ld->ld_start);
2804 WRITE64(ld->ld_length);
2805 WRITE32(ld->ld_type);
2807 WRITEMEM(&ld->ld_clientid, 8);
2809 WRITEMEM(conf->data, conf->len);
2811 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2812 WRITE64((u64)0); /* clientid */
2813 WRITE32(0); /* length of owner name */
2819 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
2822 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2823 else if (nfserr == nfserr_denied)
2824 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2830 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
2832 if (nfserr == nfserr_denied)
2833 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2838 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
2841 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2848 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
2854 write_cinfo(&p, &link->li_cinfo);
2862 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
2869 nfsd4_encode_stateid(resp, &open->op_stateid);
2871 write_cinfo(&p, &open->op_cinfo);
2872 WRITE32(open->op_rflags);
2874 WRITE32(open->op_bmval[0]);
2875 WRITE32(open->op_bmval[1]);
2876 WRITE32(open->op_delegate_type);
2879 switch (open->op_delegate_type) {
2880 case NFS4_OPEN_DELEGATE_NONE:
2882 case NFS4_OPEN_DELEGATE_READ:
2883 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2885 WRITE32(open->op_recall);
2888 * TODO: ACE's in delegations
2890 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2893 WRITE32(0); /* XXX: is NULL principal ok? */
2896 case NFS4_OPEN_DELEGATE_WRITE:
2897 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2902 * TODO: space_limit's in delegations
2904 WRITE32(NFS4_LIMIT_SIZE);
2909 * TODO: ACE's in delegations
2911 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2914 WRITE32(0); /* XXX: is NULL principal ok? */
2917 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2918 switch (open->op_why_no_deleg) {
2919 case WND4_CONTENTION:
2922 WRITE32(open->op_why_no_deleg);
2923 WRITE32(0); /* deleg signaling not supported yet */
2927 WRITE32(open->op_why_no_deleg);
2934 /* XXX save filehandle here */
2940 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
2943 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
2949 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
2952 nfsd4_encode_stateid(resp, &od->od_stateid);
2958 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
2959 struct nfsd4_read *read)
2964 unsigned long maxcount;
2970 if (resp->xbuf->page_len)
2971 return nfserr_resource;
2973 RESERVE_SPACE(8); /* eof flag and byte count */
2975 maxcount = svc_max_payload(resp->rqstp);
2976 if (maxcount > read->rd_length)
2977 maxcount = read->rd_length;
2982 page = *(resp->rqstp->rq_next_page);
2983 if (!page) { /* ran out of pages */
2987 resp->rqstp->rq_vec[v].iov_base = page_address(page);
2988 resp->rqstp->rq_vec[v].iov_len =
2989 len < PAGE_SIZE ? len : PAGE_SIZE;
2990 resp->rqstp->rq_next_page++;
2996 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
2997 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
3002 eof = (read->rd_offset + maxcount >=
3003 read->rd_fhp->fh_dentry->d_inode->i_size);
3008 resp->xbuf->head[0].iov_len = (char*)p
3009 - (char*)resp->xbuf->head[0].iov_base;
3010 resp->xbuf->page_len = maxcount;
3012 /* Use rest of head for padding and remaining ops: */
3013 resp->xbuf->tail[0].iov_base = p;
3014 resp->xbuf->tail[0].iov_len = 0;
3018 resp->xbuf->tail[0].iov_base += maxcount&3;
3019 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3026 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
3034 if (resp->xbuf->page_len)
3035 return nfserr_resource;
3036 if (!*resp->rqstp->rq_next_page)
3037 return nfserr_resource;
3039 page = page_address(*(resp->rqstp->rq_next_page++));
3041 maxcount = PAGE_SIZE;
3045 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3046 * if they would overflow the buffer. Is this kosher in NFSv4? If
3047 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3048 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3050 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3051 if (nfserr == nfserr_isdir)
3052 return nfserr_inval;
3058 resp->xbuf->head[0].iov_len = (char*)p
3059 - (char*)resp->xbuf->head[0].iov_base;
3060 resp->xbuf->page_len = maxcount;
3062 /* Use rest of head for padding and remaining ops: */
3063 resp->xbuf->tail[0].iov_base = p;
3064 resp->xbuf->tail[0].iov_len = 0;
3068 resp->xbuf->tail[0].iov_base += maxcount&3;
3069 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3076 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3080 __be32 *page, *savep, *tailbase;
3085 if (resp->xbuf->page_len)
3086 return nfserr_resource;
3087 if (!*resp->rqstp->rq_next_page)
3088 return nfserr_resource;
3090 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
3093 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3097 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
3100 maxcount = PAGE_SIZE;
3101 if (maxcount > readdir->rd_maxcount)
3102 maxcount = readdir->rd_maxcount;
3105 * Convert from bytes to words, account for the two words already
3106 * written, make sure to leave two words at the end for the next
3107 * pointer and eof field.
3109 maxcount = (maxcount >> 2) - 4;
3111 nfserr = nfserr_toosmall;
3115 page = page_address(*(resp->rqstp->rq_next_page++));
3116 readdir->common.err = 0;
3117 readdir->buflen = maxcount;
3118 readdir->buffer = page;
3119 readdir->offset = NULL;
3121 offset = readdir->rd_cookie;
3122 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3124 &readdir->common, nfsd4_encode_dirent);
3125 if (nfserr == nfs_ok &&
3126 readdir->common.err == nfserr_toosmall &&
3127 readdir->buffer == page)
3128 nfserr = nfserr_toosmall;
3132 if (readdir->offset)
3133 xdr_encode_hyper(readdir->offset, offset);
3135 p = readdir->buffer;
3136 *p++ = 0; /* no more entries */
3137 *p++ = htonl(readdir->common.err == nfserr_eof);
3138 resp->xbuf->page_len = ((char*)p) -
3139 (char*)page_address(*(resp->rqstp->rq_next_page-1));
3141 /* Use rest of head for padding and remaining ops: */
3142 resp->xbuf->tail[0].iov_base = tailbase;
3143 resp->xbuf->tail[0].iov_len = 0;
3144 resp->p = resp->xbuf->tail[0].iov_base;
3145 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
3155 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3161 write_cinfo(&p, &remove->rm_cinfo);
3168 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3174 write_cinfo(&p, &rename->rn_sinfo);
3175 write_cinfo(&p, &rename->rn_tinfo);
3182 nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3183 __be32 nfserr, struct svc_export *exp)
3185 u32 i, nflavs, supported;
3186 struct exp_flavor_info *flavs;
3187 struct exp_flavor_info def_flavs[2];
3188 __be32 *p, *flavorsp;
3189 static bool report = true;
3193 if (exp->ex_nflavors) {
3194 flavs = exp->ex_flavors;
3195 nflavs = exp->ex_nflavors;
3196 } else { /* Handling of some defaults in absence of real secinfo: */
3198 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3200 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3201 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3202 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3204 flavs[0].pseudoflavor
3205 = svcauth_gss_flavor(exp->ex_client);
3208 flavs[0].pseudoflavor
3209 = exp->ex_client->flavour->flavour;
3215 flavorsp = p++; /* to be backfilled later */
3218 for (i = 0; i < nflavs; i++) {
3219 rpc_authflavor_t pf = flavs[i].pseudoflavor;
3220 struct rpcsec_gss_info info;
3222 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3224 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
3225 WRITE32(RPC_AUTH_GSS);
3226 WRITE32(info.oid.len);
3227 WRITEMEM(info.oid.data, info.oid.len);
3229 WRITE32(info.service);
3231 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3238 pr_warn("NFS: SECINFO: security flavor %u "
3239 "is not supported\n", pf);
3243 if (nflavs != supported)
3245 *flavorsp = htonl(supported);
3254 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3255 struct nfsd4_secinfo *secinfo)
3257 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3261 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3262 struct nfsd4_secinfo_no_name *secinfo)
3264 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3268 * The SETATTR encode routine is special -- it always encodes a bitmap,
3269 * regardless of the error status.
3272 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3285 WRITE32(setattr->sa_bmval[0]);
3286 WRITE32(setattr->sa_bmval[1]);
3287 WRITE32(setattr->sa_bmval[2]);
3294 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3299 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
3300 WRITEMEM(&scd->se_clientid, 8);
3301 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
3304 else if (nfserr == nfserr_clid_inuse) {
3314 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3320 WRITE32(write->wr_bytes_written);
3321 WRITE32(write->wr_how_written);
3322 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
3328 static const u32 nfs4_minimal_spo_must_enforce[2] = {
3329 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3330 1 << (OP_EXCHANGE_ID - 32) |
3331 1 << (OP_CREATE_SESSION - 32) |
3332 1 << (OP_DESTROY_SESSION - 32) |
3333 1 << (OP_DESTROY_CLIENTID - 32)
3337 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
3338 struct nfsd4_exchange_id *exid)
3344 int server_scope_sz;
3345 uint64_t minor_id = 0;
3350 major_id = utsname()->nodename;
3351 major_id_sz = strlen(major_id);
3352 server_scope = utsname()->nodename;
3353 server_scope_sz = strlen(server_scope);
3356 8 /* eir_clientid */ +
3357 4 /* eir_sequenceid */ +
3359 4 /* spr_how (SP4_NONE) */ +
3360 8 /* so_minor_id */ +
3361 4 /* so_major_id.len */ +
3362 (XDR_QUADLEN(major_id_sz) * 4) +
3363 4 /* eir_server_scope.len */ +
3364 (XDR_QUADLEN(server_scope_sz) * 4) +
3365 4 /* eir_server_impl_id.count (0) */);
3367 WRITEMEM(&exid->clientid, 8);
3368 WRITE32(exid->seqid);
3369 WRITE32(exid->flags);
3371 /* state_protect4_r. Currently only support SP4_NONE */
3372 BUG_ON(exid->spa_how != SP4_NONE);
3373 WRITE32(exid->spa_how);
3374 switch (exid->spa_how) {
3378 /* spo_must_enforce bitmap: */
3380 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3381 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3382 /* empty spo_must_allow bitmap: */
3389 /* The server_owner struct */
3390 WRITE64(minor_id); /* Minor id */
3392 WRITE32(major_id_sz);
3393 WRITEMEM(major_id, major_id_sz);
3396 WRITE32(server_scope_sz);
3397 WRITEMEM(server_scope, server_scope_sz);
3399 /* Implementation id */
3400 WRITE32(0); /* zero length nfs_impl_id4 array */
3406 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3407 struct nfsd4_create_session *sess)
3415 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3416 WRITE32(sess->seqid);
3417 WRITE32(sess->flags);
3421 WRITE32(0); /* headerpadsz */
3422 WRITE32(sess->fore_channel.maxreq_sz);
3423 WRITE32(sess->fore_channel.maxresp_sz);
3424 WRITE32(sess->fore_channel.maxresp_cached);
3425 WRITE32(sess->fore_channel.maxops);
3426 WRITE32(sess->fore_channel.maxreqs);
3427 WRITE32(sess->fore_channel.nr_rdma_attrs);
3430 if (sess->fore_channel.nr_rdma_attrs) {
3432 WRITE32(sess->fore_channel.rdma_attrs);
3437 WRITE32(0); /* headerpadsz */
3438 WRITE32(sess->back_channel.maxreq_sz);
3439 WRITE32(sess->back_channel.maxresp_sz);
3440 WRITE32(sess->back_channel.maxresp_cached);
3441 WRITE32(sess->back_channel.maxops);
3442 WRITE32(sess->back_channel.maxreqs);
3443 WRITE32(sess->back_channel.nr_rdma_attrs);
3446 if (sess->back_channel.nr_rdma_attrs) {
3448 WRITE32(sess->back_channel.rdma_attrs);
3455 nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3456 struct nfsd4_destroy_session *destroy_session)
3462 nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3463 struct nfsd4_free_stateid *free_stateid)
3477 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
3478 struct nfsd4_sequence *seq)
3485 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3486 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3487 WRITE32(seq->seqid);
3488 WRITE32(seq->slotid);
3489 /* Note slotid's are numbered from zero: */
3490 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3491 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
3492 WRITE32(seq->status_flags);
3495 resp->cstate.datap = p; /* DRC cache data pointer */
3500 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3501 struct nfsd4_test_stateid *test_stateid)
3503 struct nfsd4_test_stateid_id *stateid, *next;
3506 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
3507 *p++ = htonl(test_stateid->ts_num_ids);
3509 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
3510 *p++ = stateid->ts_id_status;
3518 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3523 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3526 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3527 * since we don't need to filter out obsolete ops as this is
3528 * done in the decoding phase.
3530 static nfsd4_enc nfsd4_enc_ops[] = {
3531 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3532 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3533 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3534 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3535 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3536 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3537 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3538 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3539 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3540 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3541 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3542 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3543 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3544 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3545 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3546 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
3547 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
3548 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3549 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3550 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3551 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3552 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3553 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3554 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3555 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3556 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3557 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3558 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3559 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3560 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3561 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3562 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3563 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3564 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3565 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3566 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3567 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
3569 /* NFSv4.1 operations */
3570 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
3571 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
3572 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3573 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3574 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
3575 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
3576 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3577 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3578 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3579 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3580 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3581 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3582 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
3583 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3584 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
3585 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
3586 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3587 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3588 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
3592 * Calculate the total amount of memory that the compound response has taken
3593 * after encoding the current operation with pad.
3595 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3596 * which was specified at nfsd4_operation, else pad is zero.
3598 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
3600 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3601 * will be at least a page and will therefore hold the xdr_buf head.
3603 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
3605 struct xdr_buf *xb = &resp->rqstp->rq_res;
3606 struct nfsd4_session *session = NULL;
3607 struct nfsd4_slot *slot = resp->cstate.slot;
3608 u32 length, tlen = 0;
3610 if (!nfsd4_has_session(&resp->cstate))
3613 session = resp->cstate.session;
3614 if (session == NULL)
3617 if (xb->page_len == 0) {
3618 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3620 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3621 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3623 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3625 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3626 length, xb->page_len, tlen, pad);
3628 if (length > session->se_fchannel.maxresp_sz)
3629 return nfserr_rep_too_big;
3631 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
3632 length > session->se_fchannel.maxresp_cached)
3633 return nfserr_rep_too_big_to_cache;
3639 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3641 struct nfs4_stateowner *so = resp->cstate.replay_owner;
3647 statp = p++; /* to be backfilled at the end */
3650 if (op->opnum == OP_ILLEGAL)
3652 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3653 !nfsd4_enc_ops[op->opnum]);
3654 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
3655 /* nfsd4_check_drc_limit guarantees enough room for error status */
3657 op->status = nfsd4_check_resp_size(resp, 0);
3659 so->so_replay.rp_status = op->status;
3660 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3661 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3665 * Note: We write the status directly, instead of using WRITE32(),
3666 * since it is already in network byte order.
3668 *statp = op->status;
3672 * Encode the reply stored in the stateowner reply cache
3674 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3675 * previously sent already encoded operation.
3677 * called with nfs4_lock_state() held
3680 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3683 struct nfs4_replay *rp = op->replay;
3689 *p++ = rp->rp_status; /* already xdr'ed */
3692 RESERVE_SPACE(rp->rp_buflen);
3693 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3698 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
3700 return xdr_ressize_check(rqstp, p);
3703 int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
3705 struct svc_rqst *rqstp = rq;
3706 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3708 if (args->ops != args->iops) {
3710 args->ops = args->iops;
3714 while (args->to_free) {
3715 struct tmpbuf *tb = args->to_free;
3716 args->to_free = tb->next;
3717 tb->release(tb->buf);
3724 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
3727 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3728 args->pagelist = rqstp->rq_arg.pages;
3729 args->pagelen = rqstp->rq_arg.page_len;
3731 args->to_free = NULL;
3732 args->ops = args->iops;
3733 args->rqstp = rqstp;
3735 return !nfsd4_decode_compound(args);
3739 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
3742 * All that remains is to write the tag and operation count...
3744 struct nfsd4_compound_state *cs = &resp->cstate;
3747 *p++ = htonl(resp->taglen);
3748 memcpy(p, resp->tag, resp->taglen);
3749 p += XDR_QUADLEN(resp->taglen);
3750 *p++ = htonl(resp->opcnt);
3752 if (rqstp->rq_res.page_len)
3753 iov = &rqstp->rq_res.tail[0];
3755 iov = &rqstp->rq_res.head[0];
3756 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3757 BUG_ON(iov->iov_len > PAGE_SIZE);
3758 if (nfsd4_has_session(cs)) {
3759 if (cs->status != nfserr_replay_cache) {
3760 nfsd4_store_cache_entry(resp);
3761 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3763 /* Renew the clientid on success and on replay */
3764 put_client_renew(cs->session->se_client);
3765 nfsd4_put_session(cs->session);