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 */
170 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 */
463 cbs->flavor = (u32)(-1);
464 for (i = 0; i < nr_secflavs; ++i) {
469 /* Nothing to read */
470 if (cbs->flavor == (u32)(-1))
471 cbs->flavor = RPC_AUTH_NULL;
481 SAVEMEM(machine_name, dummy);
492 if (cbs->flavor == (u32)(-1)) {
493 kuid_t kuid = make_kuid(&init_user_ns, uid);
494 kgid_t kgid = make_kgid(&init_user_ns, gid);
495 if (uid_valid(kuid) && gid_valid(kgid)) {
498 cbs->flavor = RPC_AUTH_UNIX;
500 dprintk("RPC_AUTH_UNIX with invalid"
501 "uid or gid ignoring!\n");
506 dprintk("RPC_AUTH_GSS callback secflavor "
511 /* gcbp_handle_from_server */
514 p += XDR_QUADLEN(dummy);
515 /* gcbp_handle_from_client */
521 dprintk("Illegal callback secflavor\n");
528 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
533 READ32(bc->bc_cb_program);
534 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
539 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
543 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
544 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
546 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
547 * could help us figure out we should be using it. */
552 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
557 READ32(close->cl_seqid);
558 return nfsd4_decode_stateid(argp, &close->cl_stateid);
565 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
570 READ64(commit->co_offset);
571 READ32(commit->co_count);
577 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
582 READ32(create->cr_type);
583 switch (create->cr_type) {
586 READ32(create->cr_linklen);
587 READ_BUF(create->cr_linklen);
588 SAVEMEM(create->cr_linkname, create->cr_linklen);
593 READ32(create->cr_specdata1);
594 READ32(create->cr_specdata2);
604 READ32(create->cr_namelen);
605 READ_BUF(create->cr_namelen);
606 SAVEMEM(create->cr_name, create->cr_namelen);
607 if ((status = check_filename(create->cr_name, create->cr_namelen)))
610 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
611 &create->cr_acl, &create->cr_label);
619 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
621 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
625 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
627 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
631 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
636 READ32(link->li_namelen);
637 READ_BUF(link->li_namelen);
638 SAVEMEM(link->li_name, link->li_namelen);
639 if ((status = check_filename(link->li_name, link->li_namelen)))
646 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
651 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
654 READ32(lock->lk_type);
655 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
657 READ32(lock->lk_reclaim);
658 READ64(lock->lk_offset);
659 READ64(lock->lk_length);
660 READ32(lock->lk_is_new);
662 if (lock->lk_is_new) {
664 READ32(lock->lk_new_open_seqid);
665 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
668 READ_BUF(8 + sizeof(clientid_t));
669 READ32(lock->lk_new_lock_seqid);
670 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
671 READ32(lock->lk_new_owner.len);
672 READ_BUF(lock->lk_new_owner.len);
673 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
675 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
679 READ32(lock->lk_old_lock_seqid);
686 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
691 READ32(lockt->lt_type);
692 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
694 READ64(lockt->lt_offset);
695 READ64(lockt->lt_length);
696 COPYMEM(&lockt->lt_clientid, 8);
697 READ32(lockt->lt_owner.len);
698 READ_BUF(lockt->lt_owner.len);
699 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
705 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
710 READ32(locku->lu_type);
711 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
713 READ32(locku->lu_seqid);
714 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
718 READ64(locku->lu_offset);
719 READ64(locku->lu_length);
725 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
730 READ32(lookup->lo_len);
731 READ_BUF(lookup->lo_len);
732 SAVEMEM(lookup->lo_name, lookup->lo_len);
733 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
739 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
746 *share_access = w & NFS4_SHARE_ACCESS_MASK;
747 *deleg_want = w & NFS4_SHARE_WANT_MASK;
749 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
751 switch (w & NFS4_SHARE_ACCESS_MASK) {
752 case NFS4_SHARE_ACCESS_READ:
753 case NFS4_SHARE_ACCESS_WRITE:
754 case NFS4_SHARE_ACCESS_BOTH:
757 return nfserr_bad_xdr;
759 w &= ~NFS4_SHARE_ACCESS_MASK;
762 if (!argp->minorversion)
763 return nfserr_bad_xdr;
764 switch (w & NFS4_SHARE_WANT_MASK) {
765 case NFS4_SHARE_WANT_NO_PREFERENCE:
766 case NFS4_SHARE_WANT_READ_DELEG:
767 case NFS4_SHARE_WANT_WRITE_DELEG:
768 case NFS4_SHARE_WANT_ANY_DELEG:
769 case NFS4_SHARE_WANT_NO_DELEG:
770 case NFS4_SHARE_WANT_CANCEL:
773 return nfserr_bad_xdr;
775 w &= ~NFS4_SHARE_WANT_MASK;
779 if (!deleg_when) /* open_downgrade */
782 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
783 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
784 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
785 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
789 return nfserr_bad_xdr;
792 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
798 /* Note: unlinke access bits, deny bits may be zero. */
799 if (*x & ~NFS4_SHARE_DENY_BOTH)
800 return nfserr_bad_xdr;
803 return nfserr_bad_xdr;
806 static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
813 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
814 return nfserr_bad_xdr;
817 SAVEMEM(o->data, o->len);
820 return nfserr_bad_xdr;
824 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
829 memset(open->op_bmval, 0, sizeof(open->op_bmval));
830 open->op_iattr.ia_valid = 0;
831 open->op_openowner = NULL;
833 open->op_xdr_error = 0;
834 /* seqid, share_access, share_deny, clientid, ownerlen */
836 READ32(open->op_seqid);
837 /* decode, yet ignore deleg_when until supported */
838 status = nfsd4_decode_share_access(argp, &open->op_share_access,
839 &open->op_deleg_want, &dummy);
842 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
845 READ_BUF(sizeof(clientid_t));
846 COPYMEM(&open->op_clientid, sizeof(clientid_t));
847 status = nfsd4_decode_opaque(argp, &open->op_owner);
851 READ32(open->op_create);
852 switch (open->op_create) {
853 case NFS4_OPEN_NOCREATE:
855 case NFS4_OPEN_CREATE:
857 READ32(open->op_createmode);
858 switch (open->op_createmode) {
859 case NFS4_CREATE_UNCHECKED:
860 case NFS4_CREATE_GUARDED:
861 status = nfsd4_decode_fattr(argp, open->op_bmval,
862 &open->op_iattr, &open->op_acl, &open->op_label);
866 case NFS4_CREATE_EXCLUSIVE:
867 READ_BUF(NFS4_VERIFIER_SIZE);
868 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
870 case NFS4_CREATE_EXCLUSIVE4_1:
871 if (argp->minorversion < 1)
873 READ_BUF(NFS4_VERIFIER_SIZE);
874 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
875 status = nfsd4_decode_fattr(argp, open->op_bmval,
876 &open->op_iattr, &open->op_acl, &open->op_label);
890 READ32(open->op_claim_type);
891 switch (open->op_claim_type) {
892 case NFS4_OPEN_CLAIM_NULL:
893 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
895 READ32(open->op_fname.len);
896 READ_BUF(open->op_fname.len);
897 SAVEMEM(open->op_fname.data, open->op_fname.len);
898 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
901 case NFS4_OPEN_CLAIM_PREVIOUS:
903 READ32(open->op_delegate_type);
905 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
906 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
910 READ32(open->op_fname.len);
911 READ_BUF(open->op_fname.len);
912 SAVEMEM(open->op_fname.data, open->op_fname.len);
913 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
916 case NFS4_OPEN_CLAIM_FH:
917 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
918 if (argp->minorversion < 1)
922 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
923 if (argp->minorversion < 1)
925 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
937 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
941 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
945 READ32(open_conf->oc_seqid);
951 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
955 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
959 READ32(open_down->od_seqid);
960 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
961 &open_down->od_deleg_want, NULL);
964 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
971 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
976 READ32(putfh->pf_fhlen);
977 if (putfh->pf_fhlen > NFS4_FHSIZE)
979 READ_BUF(putfh->pf_fhlen);
980 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
986 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
990 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
994 READ64(read->rd_offset);
995 READ32(read->rd_length);
1001 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1006 READ64(readdir->rd_cookie);
1007 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1008 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1009 READ32(readdir->rd_maxcount);
1010 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1017 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1022 READ32(remove->rm_namelen);
1023 READ_BUF(remove->rm_namelen);
1024 SAVEMEM(remove->rm_name, remove->rm_namelen);
1025 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1032 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1037 READ32(rename->rn_snamelen);
1038 READ_BUF(rename->rn_snamelen + 4);
1039 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1040 READ32(rename->rn_tnamelen);
1041 READ_BUF(rename->rn_tnamelen);
1042 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1043 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1045 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1052 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1056 READ_BUF(sizeof(clientid_t));
1057 COPYMEM(clientid, sizeof(clientid_t));
1063 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1064 struct nfsd4_secinfo *secinfo)
1069 READ32(secinfo->si_namelen);
1070 READ_BUF(secinfo->si_namelen);
1071 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1072 status = check_filename(secinfo->si_name, secinfo->si_namelen);
1079 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1080 struct nfsd4_secinfo_no_name *sin)
1085 READ32(sin->sin_style);
1090 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1094 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1097 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1098 &setattr->sa_acl, &setattr->sa_label);
1102 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1106 READ_BUF(NFS4_VERIFIER_SIZE);
1107 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1109 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1111 return nfserr_bad_xdr;
1113 READ32(setclientid->se_callback_prog);
1114 READ32(setclientid->se_callback_netid_len);
1116 READ_BUF(setclientid->se_callback_netid_len + 4);
1117 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1118 READ32(setclientid->se_callback_addr_len);
1120 READ_BUF(setclientid->se_callback_addr_len + 4);
1121 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1122 READ32(setclientid->se_callback_ident);
1128 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1132 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1133 COPYMEM(&scd_c->sc_clientid, 8);
1134 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1139 /* Also used for NVERIFY */
1141 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1145 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1148 /* For convenience's sake, we compare raw xdr'd attributes in
1149 * nfsd4_proc_verify */
1152 READ32(verify->ve_attrlen);
1153 READ_BUF(verify->ve_attrlen);
1154 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1160 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1166 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1170 READ64(write->wr_offset);
1171 READ32(write->wr_stable_how);
1172 if (write->wr_stable_how > 2)
1174 READ32(write->wr_buflen);
1176 /* Sorry .. no magic macros for this.. *
1177 * READ_BUF(write->wr_buflen);
1178 * SAVEMEM(write->wr_buf, write->wr_buflen);
1180 avail = (char*)argp->end - (char*)argp->p;
1181 if (avail + argp->pagelen < write->wr_buflen) {
1182 dprintk("NFSD: xdr error (%s:%d)\n",
1183 __FILE__, __LINE__);
1186 write->wr_head.iov_base = p;
1187 write->wr_head.iov_len = avail;
1188 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
1189 write->wr_pagelist = argp->pagelist;
1191 len = XDR_QUADLEN(write->wr_buflen) << 2;
1197 pages = len >> PAGE_SHIFT;
1198 argp->pagelist += pages;
1199 argp->pagelen -= pages * PAGE_SIZE;
1200 len -= pages * PAGE_SIZE;
1202 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1203 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
1205 argp->p += XDR_QUADLEN(len);
1211 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1216 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1217 READ32(rlockowner->rl_owner.len);
1218 READ_BUF(rlockowner->rl_owner.len);
1219 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1221 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1222 return nfserr_inval;
1227 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1228 struct nfsd4_exchange_id *exid)
1233 READ_BUF(NFS4_VERIFIER_SIZE);
1234 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1236 status = nfsd4_decode_opaque(argp, &exid->clname);
1238 return nfserr_bad_xdr;
1241 READ32(exid->flags);
1243 /* Ignore state_protect4_a */
1245 READ32(exid->spa_how);
1246 switch (exid->spa_how) {
1250 /* spo_must_enforce */
1253 READ_BUF(dummy * 4);
1256 /* spo_must_allow */
1259 READ_BUF(dummy * 4);
1266 READ_BUF(dummy * 4);
1271 READ_BUF(dummy * 4);
1274 /* ssp_hash_algs<> */
1281 p += XDR_QUADLEN(dummy);
1284 /* ssp_encr_algs<> */
1291 p += XDR_QUADLEN(dummy);
1294 /* ssp_window and ssp_num_gss_handles */
1303 /* Ignore Implementation ID */
1304 READ_BUF(4); /* nfs_impl_id4 array length */
1315 p += XDR_QUADLEN(dummy);
1321 p += XDR_QUADLEN(dummy);
1331 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1332 struct nfsd4_create_session *sess)
1338 COPYMEM(&sess->clientid, 8);
1339 READ32(sess->seqid);
1340 READ32(sess->flags);
1342 /* Fore channel attrs */
1344 READ32(dummy); /* headerpadsz is always 0 */
1345 READ32(sess->fore_channel.maxreq_sz);
1346 READ32(sess->fore_channel.maxresp_sz);
1347 READ32(sess->fore_channel.maxresp_cached);
1348 READ32(sess->fore_channel.maxops);
1349 READ32(sess->fore_channel.maxreqs);
1350 READ32(sess->fore_channel.nr_rdma_attrs);
1351 if (sess->fore_channel.nr_rdma_attrs == 1) {
1353 READ32(sess->fore_channel.rdma_attrs);
1354 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1355 dprintk("Too many fore channel attr bitmaps!\n");
1359 /* Back channel attrs */
1361 READ32(dummy); /* headerpadsz is always 0 */
1362 READ32(sess->back_channel.maxreq_sz);
1363 READ32(sess->back_channel.maxresp_sz);
1364 READ32(sess->back_channel.maxresp_cached);
1365 READ32(sess->back_channel.maxops);
1366 READ32(sess->back_channel.maxreqs);
1367 READ32(sess->back_channel.nr_rdma_attrs);
1368 if (sess->back_channel.nr_rdma_attrs == 1) {
1370 READ32(sess->back_channel.rdma_attrs);
1371 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1372 dprintk("Too many back channel attr bitmaps!\n");
1377 READ32(sess->callback_prog);
1378 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1383 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1384 struct nfsd4_destroy_session *destroy_session)
1387 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1388 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1394 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1395 struct nfsd4_free_stateid *free_stateid)
1399 READ_BUF(sizeof(stateid_t));
1400 READ32(free_stateid->fr_stateid.si_generation);
1401 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1407 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1408 struct nfsd4_sequence *seq)
1412 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1413 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1415 READ32(seq->slotid);
1416 READ32(seq->maxslots);
1417 READ32(seq->cachethis);
1423 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1427 struct nfsd4_test_stateid_id *stateid;
1430 test_stateid->ts_num_ids = ntohl(*p++);
1432 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1434 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1435 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1437 status = nfserrno(-ENOMEM);
1441 defer_free(argp, kfree, stateid);
1442 INIT_LIST_HEAD(&stateid->ts_id_list);
1443 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1445 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1454 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1455 status = nfserr_bad_xdr;
1459 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1464 COPYMEM(&dc->clientid, 8);
1469 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1474 READ32(rc->rca_one_fs);
1480 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1486 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1488 return nfserr_notsupp;
1491 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1493 static nfsd4_dec nfsd4_dec_ops[] = {
1494 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1495 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1496 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1497 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1498 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1499 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1500 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1501 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1502 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1503 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1504 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1505 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1506 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1507 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1508 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1509 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1510 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1511 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1512 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1513 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1514 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
1515 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1516 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1517 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1518 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1519 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1520 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1521 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1522 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1523 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1524 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1525 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1526 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1527 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1528 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1529 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1530 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
1533 static nfsd4_dec nfsd41_dec_ops[] = {
1534 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1535 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1536 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1537 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1538 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1539 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1540 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1541 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1542 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1543 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1544 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1545 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1546 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1547 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1548 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1549 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1550 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1551 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1552 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1553 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1554 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1555 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1556 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1557 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1558 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1559 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1560 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1561 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1562 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1563 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1564 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1565 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1566 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1567 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1568 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1569 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1570 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
1572 /* new operations for NFSv4.1 */
1573 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1574 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1575 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1576 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1577 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
1578 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
1579 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1580 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1581 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1582 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1583 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1584 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
1585 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1586 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1587 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
1588 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
1589 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1590 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
1591 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
1594 struct nfsd4_minorversion_ops {
1595 nfsd4_dec *decoders;
1599 static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
1600 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
1601 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1602 [2] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1606 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1609 struct nfsd4_op *op;
1610 struct nfsd4_minorversion_ops *ops;
1611 bool cachethis = false;
1615 READ32(argp->taglen);
1616 READ_BUF(argp->taglen + 8);
1617 SAVEMEM(argp->tag, argp->taglen);
1618 READ32(argp->minorversion);
1619 READ32(argp->opcnt);
1621 if (argp->taglen > NFSD4_MAX_TAGLEN)
1623 if (argp->opcnt > 100)
1626 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1627 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1629 argp->ops = argp->iops;
1630 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1635 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
1638 ops = &nfsd4_minorversion[argp->minorversion];
1639 for (i = 0; i < argp->opcnt; i++) {
1646 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
1647 op->status = ops->decoders[op->opnum](argp, &op->u);
1649 op->opnum = OP_ILLEGAL;
1650 op->status = nfserr_op_illegal;
1658 * We'll try to cache the result in the DRC if any one
1659 * op in the compound wants to be cached:
1661 cachethis |= nfsd4_cache_this_op(op);
1663 /* Sessions make the DRC unnecessary: */
1664 if (argp->minorversion)
1666 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1671 #define WRITE32(n) *p++ = htonl(n)
1672 #define WRITE64(n) do { \
1673 *p++ = htonl((u32)((n) >> 32)); \
1674 *p++ = htonl((u32)(n)); \
1676 #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
1677 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1678 memcpy(p, ptr, nbytes); \
1679 p += XDR_QUADLEN(nbytes); \
1682 static void write32(__be32 **p, u32 n)
1687 static void write64(__be32 **p, u64 n)
1689 write32(p, (n >> 32));
1693 static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1695 if (IS_I_VERSION(inode)) {
1696 write64(p, inode->i_version);
1698 write32(p, stat->ctime.tv_sec);
1699 write32(p, stat->ctime.tv_nsec);
1703 static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1705 write32(p, c->atomic);
1706 if (c->change_supported) {
1707 write64(p, c->before_change);
1708 write64(p, c->after_change);
1710 write32(p, c->before_ctime_sec);
1711 write32(p, c->before_ctime_nsec);
1712 write32(p, c->after_ctime_sec);
1713 write32(p, c->after_ctime_nsec);
1717 #define RESERVE_SPACE(nbytes) do { \
1719 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1721 #define ADJUST_ARGS() resp->p = p
1723 /* Encode as an array of strings the string given with components
1724 * separated @sep, escaped with esc_enter and esc_exit.
1726 static __be32 nfsd4_encode_components_esc(char sep, char *components,
1727 __be32 **pp, int *buflen,
1728 char esc_enter, char esc_exit)
1732 int strlen, count=0;
1733 char *str, *end, *next;
1735 dprintk("nfsd4_encode_components(%s)\n", components);
1736 if ((*buflen -= 4) < 0)
1737 return nfserr_resource;
1738 WRITE32(0); /* We will fill this in with @count later */
1739 end = str = components;
1741 bool found_esc = false;
1743 /* try to parse as esc_start, ..., esc_end, sep */
1744 if (*str == esc_enter) {
1745 for (; *end && (*end != esc_exit); end++)
1746 /* find esc_exit or end of string */;
1748 if (*end && (!*next || *next == sep)) {
1755 for (; *end && (*end != sep); end++)
1756 /* find sep or end of string */;
1760 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1761 return nfserr_resource;
1763 WRITEMEM(str, strlen);
1776 /* Encode as an array of strings the string given with components
1779 static __be32 nfsd4_encode_components(char sep, char *components,
1780 __be32 **pp, int *buflen)
1782 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1786 * encode a location element of a fs_locations structure
1788 static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1789 __be32 **pp, int *buflen)
1794 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1798 status = nfsd4_encode_components('/', location->path, &p, buflen);
1806 * Encode a path in RFC3530 'pathname4' format
1808 static __be32 nfsd4_encode_path(const struct path *root,
1809 const struct path *path, __be32 **pp, int *buflen)
1813 .dentry = path->dentry,
1816 struct dentry **components = NULL;
1817 unsigned int ncomponents = 0;
1818 __be32 err = nfserr_jukebox;
1820 dprintk("nfsd4_encode_components(");
1823 /* First walk the path up to the nfsd root, and store the
1824 * dentries/path components in an array.
1827 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1829 if (cur.dentry == cur.mnt->mnt_root) {
1830 if (follow_up(&cur))
1834 if ((ncomponents & 15) == 0) {
1835 struct dentry **new;
1836 new = krealloc(components,
1837 sizeof(*new) * (ncomponents + 16),
1843 components[ncomponents++] = cur.dentry;
1844 cur.dentry = dget_parent(cur.dentry);
1850 WRITE32(ncomponents);
1852 while (ncomponents) {
1853 struct dentry *dentry = components[ncomponents - 1];
1854 unsigned int len = dentry->d_name.len;
1856 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1860 WRITEMEM(dentry->d_name.name, len);
1861 dprintk("/%s", dentry->d_name.name);
1871 dput(components[--ncomponents]);
1877 static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1878 const struct path *path, __be32 **pp, int *buflen)
1880 struct svc_export *exp_ps;
1883 exp_ps = rqst_find_fsidzero_export(rqstp);
1885 return nfserrno(PTR_ERR(exp_ps));
1886 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1892 * encode a fs_locations structure
1894 static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1895 struct svc_export *exp,
1896 __be32 **pp, int *buflen)
1901 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1903 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
1906 if ((*buflen -= 4) < 0)
1907 return nfserr_resource;
1908 WRITE32(fslocs->locations_count);
1909 for (i=0; i<fslocs->locations_count; i++) {
1910 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1919 static u32 nfs4_file_type(umode_t mode)
1921 switch (mode & S_IFMT) {
1922 case S_IFIFO: return NF4FIFO;
1923 case S_IFCHR: return NF4CHR;
1924 case S_IFDIR: return NF4DIR;
1925 case S_IFBLK: return NF4BLK;
1926 case S_IFLNK: return NF4LNK;
1927 case S_IFREG: return NF4REG;
1928 case S_IFSOCK: return NF4SOCK;
1929 default: return NF4BAD;
1934 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
1935 __be32 **p, int *buflen)
1939 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1940 return nfserr_resource;
1941 if (whotype != NFS4_ACL_WHO_NAMED)
1942 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1943 else if (gid_valid(gid))
1944 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
1946 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
1948 return nfserrno(status);
1949 *p = xdr_encode_opaque(*p, NULL, status);
1950 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1951 BUG_ON(*buflen < 0);
1955 static inline __be32
1956 nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
1958 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1962 static inline __be32
1963 nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
1965 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1969 static inline __be32
1970 nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
1971 __be32 **p, int *buflen)
1973 kuid_t uid = INVALID_UID;
1974 kgid_t gid = INVALID_GID;
1976 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1977 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1982 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
1985 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1986 FATTR4_WORD0_RDATTR_ERROR)
1987 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1989 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
1990 static inline __be32
1991 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
1995 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
1996 return nfserr_resource;
1999 * For now we use a 0 here to indicate the null translation; in
2000 * the future we may place a call to translation code here.
2002 if ((*buflen -= 8) < 0)
2003 return nfserr_resource;
2005 WRITE32(0); /* lfs */
2006 WRITE32(0); /* pi */
2007 p = xdr_encode_opaque(p, context, len);
2008 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2014 static inline __be32
2015 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2019 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
2021 /* As per referral draft: */
2022 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2023 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2024 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2025 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2026 *rdattr_err = NFSERR_MOVED;
2028 return nfserr_moved;
2030 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2031 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2036 static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2038 struct path path = exp->ex_path;
2042 while (follow_up(&path)) {
2043 if (path.dentry != path.mnt->mnt_root)
2046 err = vfs_getattr(&path, stat);
2052 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2055 * countp is the buffer size in _words_
2058 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
2059 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
2060 struct svc_rqst *rqstp, int ignore_crossmnt)
2062 u32 bmval0 = bmval[0];
2063 u32 bmval1 = bmval[1];
2064 u32 bmval2 = bmval[2];
2066 struct svc_fh tempfh;
2067 struct kstatfs statfs;
2068 int buflen = count << 2;
2073 __be32 *p = *buffer;
2077 struct nfs4_acl *acl = NULL;
2078 void *context = NULL;
2080 bool contextsupport = false;
2081 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2082 u32 minorversion = resp->cstate.minorversion;
2083 struct path path = {
2084 .mnt = exp->ex_path.mnt,
2087 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2089 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2090 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2091 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2092 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
2094 if (exp->ex_fslocs.migrated) {
2096 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2101 err = vfs_getattr(&path, &stat);
2104 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2105 FATTR4_WORD0_MAXNAME)) ||
2106 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2107 FATTR4_WORD1_SPACE_TOTAL))) {
2108 err = vfs_statfs(&path, &statfs);
2112 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2113 fh_init(&tempfh, NFS4_FHSIZE);
2114 status = fh_compose(&tempfh, exp, dentry, NULL);
2119 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2120 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
2121 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2122 aclsupport = (err == 0);
2123 if (bmval0 & FATTR4_WORD0_ACL) {
2124 if (err == -EOPNOTSUPP)
2125 bmval0 &= ~FATTR4_WORD0_ACL;
2126 else if (err == -EINVAL) {
2127 status = nfserr_attrnotsupp;
2129 } else if (err != 0)
2134 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2135 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2136 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2137 err = security_inode_getsecctx(dentry->d_inode,
2138 &context, &contextlen);
2139 contextsupport = (err == 0);
2140 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2141 if (err == -EOPNOTSUPP)
2142 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2147 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2150 if ((buflen -= 16) < 0)
2156 } else if (bmval1) {
2157 if ((buflen -= 12) < 0)
2163 if ((buflen -= 8) < 0)
2168 attrlenp = p++; /* to be backfilled later */
2170 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2171 u32 word0 = nfsd_suppattrs0(minorversion);
2172 u32 word1 = nfsd_suppattrs1(minorversion);
2173 u32 word2 = nfsd_suppattrs2(minorversion);
2176 word0 &= ~FATTR4_WORD0_ACL;
2177 if (!contextsupport)
2178 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2180 if ((buflen -= 12) < 0)
2186 if ((buflen -= 16) < 0)
2194 if (bmval0 & FATTR4_WORD0_TYPE) {
2195 if ((buflen -= 4) < 0)
2197 dummy = nfs4_file_type(stat.mode);
2198 if (dummy == NF4BAD)
2199 goto out_serverfault;
2202 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2203 if ((buflen -= 4) < 0)
2205 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2206 WRITE32(NFS4_FH_PERSISTENT);
2208 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
2210 if (bmval0 & FATTR4_WORD0_CHANGE) {
2211 if ((buflen -= 8) < 0)
2213 write_change(&p, &stat, dentry->d_inode);
2215 if (bmval0 & FATTR4_WORD0_SIZE) {
2216 if ((buflen -= 8) < 0)
2220 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2221 if ((buflen -= 4) < 0)
2225 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2226 if ((buflen -= 4) < 0)
2230 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2231 if ((buflen -= 4) < 0)
2235 if (bmval0 & FATTR4_WORD0_FSID) {
2236 if ((buflen -= 16) < 0)
2238 if (exp->ex_fslocs.migrated) {
2239 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2240 WRITE64(NFS4_REFERRAL_FSID_MINOR);
2241 } else switch(fsid_source(fhp)) {
2242 case FSIDSOURCE_FSID:
2243 WRITE64((u64)exp->ex_fsid);
2246 case FSIDSOURCE_DEV:
2248 WRITE32(MAJOR(stat.dev));
2250 WRITE32(MINOR(stat.dev));
2252 case FSIDSOURCE_UUID:
2253 WRITEMEM(exp->ex_uuid, 16);
2257 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2258 if ((buflen -= 4) < 0)
2262 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2263 if ((buflen -= 4) < 0)
2265 WRITE32(nn->nfsd4_lease);
2267 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2268 if ((buflen -= 4) < 0)
2270 WRITE32(rdattr_err);
2272 if (bmval0 & FATTR4_WORD0_ACL) {
2273 struct nfs4_ace *ace;
2276 if ((buflen -= 4) < 0)
2282 if ((buflen -= 4) < 0)
2284 WRITE32(acl->naces);
2286 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2287 if ((buflen -= 4*3) < 0)
2291 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2292 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
2293 if (status == nfserr_resource)
2300 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2301 if ((buflen -= 4) < 0)
2303 WRITE32(aclsupport ?
2304 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2306 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2307 if ((buflen -= 4) < 0)
2311 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2312 if ((buflen -= 4) < 0)
2316 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2317 if ((buflen -= 4) < 0)
2321 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2322 if ((buflen -= 4) < 0)
2326 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2327 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2330 WRITE32(fhp->fh_handle.fh_size);
2331 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2333 if (bmval0 & FATTR4_WORD0_FILEID) {
2334 if ((buflen -= 8) < 0)
2338 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2339 if ((buflen -= 8) < 0)
2341 WRITE64((u64) statfs.f_ffree);
2343 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2344 if ((buflen -= 8) < 0)
2346 WRITE64((u64) statfs.f_ffree);
2348 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2349 if ((buflen -= 8) < 0)
2351 WRITE64((u64) statfs.f_files);
2353 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2354 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2355 if (status == nfserr_resource)
2360 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2361 if ((buflen -= 4) < 0)
2365 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2366 if ((buflen -= 8) < 0)
2370 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2371 if ((buflen -= 4) < 0)
2375 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2376 if ((buflen -= 4) < 0)
2378 WRITE32(statfs.f_namelen);
2380 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2381 if ((buflen -= 8) < 0)
2383 WRITE64((u64) svc_max_payload(rqstp));
2385 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2386 if ((buflen -= 8) < 0)
2388 WRITE64((u64) svc_max_payload(rqstp));
2390 if (bmval1 & FATTR4_WORD1_MODE) {
2391 if ((buflen -= 4) < 0)
2393 WRITE32(stat.mode & S_IALLUGO);
2395 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2396 if ((buflen -= 4) < 0)
2400 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2401 if ((buflen -= 4) < 0)
2403 WRITE32(stat.nlink);
2405 if (bmval1 & FATTR4_WORD1_OWNER) {
2406 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2407 if (status == nfserr_resource)
2412 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2413 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2414 if (status == nfserr_resource)
2419 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2420 if ((buflen -= 8) < 0)
2422 WRITE32((u32) MAJOR(stat.rdev));
2423 WRITE32((u32) MINOR(stat.rdev));
2425 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2426 if ((buflen -= 8) < 0)
2428 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2431 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2432 if ((buflen -= 8) < 0)
2434 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2437 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2438 if ((buflen -= 8) < 0)
2440 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2443 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2444 if ((buflen -= 8) < 0)
2446 dummy64 = (u64)stat.blocks << 9;
2449 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2450 if ((buflen -= 12) < 0)
2452 WRITE64((s64)stat.atime.tv_sec);
2453 WRITE32(stat.atime.tv_nsec);
2455 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2456 if ((buflen -= 12) < 0)
2462 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2463 if ((buflen -= 12) < 0)
2465 WRITE64((s64)stat.ctime.tv_sec);
2466 WRITE32(stat.ctime.tv_nsec);
2468 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2469 if ((buflen -= 12) < 0)
2471 WRITE64((s64)stat.mtime.tv_sec);
2472 WRITE32(stat.mtime.tv_nsec);
2474 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2475 if ((buflen -= 8) < 0)
2478 * Get parent's attributes if not ignoring crossmount
2479 * and this is the root of a cross-mounted filesystem.
2481 if (ignore_crossmnt == 0 &&
2482 dentry == exp->ex_path.mnt->mnt_root)
2483 get_parent_attributes(exp, &stat);
2486 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2487 status = nfsd4_encode_security_label(rqstp, context,
2488 contextlen, &p, &buflen);
2492 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2494 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2495 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2496 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2499 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2504 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2506 security_release_secctx(context, contextlen);
2507 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2513 status = nfserrno(err);
2516 status = nfserr_resource;
2519 status = nfserr_serverfault;
2523 static inline int attributes_need_mount(u32 *bmval)
2525 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2527 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2533 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
2534 const char *name, int namlen, __be32 **p, int buflen)
2536 struct svc_export *exp = cd->rd_fhp->fh_export;
2537 struct dentry *dentry;
2539 int ignore_crossmnt = 0;
2541 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2543 return nfserrno(PTR_ERR(dentry));
2544 if (!dentry->d_inode) {
2546 * nfsd_buffered_readdir drops the i_mutex between
2547 * readdir and calling this callback, leaving a window
2548 * where this directory entry could have gone away.
2551 return nfserr_noent;
2556 * In the case of a mountpoint, the client may be asking for
2557 * attributes that are only properties of the underlying filesystem
2558 * as opposed to the cross-mounted file system. In such a case,
2559 * we will not follow the cross mount and will fill the attribtutes
2560 * directly from the mountpoint dentry.
2562 if (nfsd_mountpoint(dentry, exp)) {
2565 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2566 && !attributes_need_mount(cd->rd_bmval)) {
2567 ignore_crossmnt = 1;
2571 * Why the heck aren't we just using nfsd_lookup??
2572 * Different "."/".." handling? Something else?
2573 * At least, add a comment here to explain....
2575 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2577 nfserr = nfserrno(err);
2580 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2586 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
2587 cd->rd_rqstp, ignore_crossmnt);
2595 nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
2602 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2603 *p++ = htonl(0); /* bmval1 */
2606 *p++ = nfserr; /* no htonl */
2607 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2612 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2613 loff_t offset, u64 ino, unsigned int d_type)
2615 struct readdir_cd *ccd = ccdv;
2616 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2618 __be32 *p = cd->buffer;
2620 __be32 nfserr = nfserr_toosmall;
2622 /* In nfsv4, "." and ".." never make it onto the wire.. */
2623 if (name && isdotent(name, namlen)) {
2624 cd->common.err = nfs_ok;
2629 xdr_encode_hyper(cd->offset, (u64) offset);
2631 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2635 *p++ = xdr_one; /* mark entry present */
2637 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2638 p = xdr_encode_array(p, name, namlen); /* name length & name */
2640 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
2644 case nfserr_resource:
2645 nfserr = nfserr_toosmall;
2651 * If the client requested the RDATTR_ERROR attribute,
2652 * we stuff the error code into this attribute
2653 * and continue. If this attribute was not requested,
2654 * then in accordance with the spec, we fail the
2655 * entire READDIR operation(!)
2657 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2659 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
2661 nfserr = nfserr_toosmall;
2665 cd->buflen -= (p - cd->buffer);
2667 cd->offset = cookiep;
2669 cd->common.err = nfs_ok;
2672 cd->common.err = nfserr;
2677 nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2681 RESERVE_SPACE(sizeof(stateid_t));
2682 WRITE32(sid->si_generation);
2683 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2688 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
2694 WRITE32(access->ac_supported);
2695 WRITE32(access->ac_resp_access);
2701 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2706 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2707 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2709 /* Sorry, we do not yet support RDMA over 4.1: */
2717 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
2720 nfsd4_encode_stateid(resp, &close->cl_stateid);
2727 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
2732 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2733 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
2740 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
2746 write_cinfo(&p, &create->cr_cinfo);
2748 WRITE32(create->cr_bmval[0]);
2749 WRITE32(create->cr_bmval[1]);
2756 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
2758 struct svc_fh *fhp = getattr->ga_fhp;
2764 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2765 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2766 &resp->p, buflen, getattr->ga_bmval,
2772 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
2774 struct svc_fh *fhp = *fhpp;
2779 len = fhp->fh_handle.fh_size;
2780 RESERVE_SPACE(len + 4);
2782 WRITEMEM(&fhp->fh_handle.fh_base, len);
2789 * Including all fields other than the name, a LOCK4denied structure requires
2790 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2793 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2795 struct xdr_netobj *conf = &ld->ld_owner;
2798 RESERVE_SPACE(32 + XDR_LEN(conf->len));
2799 WRITE64(ld->ld_start);
2800 WRITE64(ld->ld_length);
2801 WRITE32(ld->ld_type);
2803 WRITEMEM(&ld->ld_clientid, 8);
2805 WRITEMEM(conf->data, conf->len);
2807 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2808 WRITE64((u64)0); /* clientid */
2809 WRITE32(0); /* length of owner name */
2815 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
2818 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2819 else if (nfserr == nfserr_denied)
2820 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2826 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
2828 if (nfserr == nfserr_denied)
2829 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2834 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
2837 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2844 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
2850 write_cinfo(&p, &link->li_cinfo);
2858 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
2865 nfsd4_encode_stateid(resp, &open->op_stateid);
2867 write_cinfo(&p, &open->op_cinfo);
2868 WRITE32(open->op_rflags);
2870 WRITE32(open->op_bmval[0]);
2871 WRITE32(open->op_bmval[1]);
2872 WRITE32(open->op_delegate_type);
2875 switch (open->op_delegate_type) {
2876 case NFS4_OPEN_DELEGATE_NONE:
2878 case NFS4_OPEN_DELEGATE_READ:
2879 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2881 WRITE32(open->op_recall);
2884 * TODO: ACE's in delegations
2886 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2889 WRITE32(0); /* XXX: is NULL principal ok? */
2892 case NFS4_OPEN_DELEGATE_WRITE:
2893 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2898 * TODO: space_limit's in delegations
2900 WRITE32(NFS4_LIMIT_SIZE);
2905 * TODO: ACE's in delegations
2907 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2910 WRITE32(0); /* XXX: is NULL principal ok? */
2913 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2914 switch (open->op_why_no_deleg) {
2915 case WND4_CONTENTION:
2918 WRITE32(open->op_why_no_deleg);
2919 WRITE32(0); /* deleg signaling not supported yet */
2923 WRITE32(open->op_why_no_deleg);
2930 /* XXX save filehandle here */
2936 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
2939 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
2945 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
2948 nfsd4_encode_stateid(resp, &od->od_stateid);
2954 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
2955 struct nfsd4_read *read)
2960 unsigned long maxcount;
2966 if (resp->xbuf->page_len)
2967 return nfserr_resource;
2969 RESERVE_SPACE(8); /* eof flag and byte count */
2971 maxcount = svc_max_payload(resp->rqstp);
2972 if (maxcount > read->rd_length)
2973 maxcount = read->rd_length;
2978 page = *(resp->rqstp->rq_next_page);
2979 if (!page) { /* ran out of pages */
2983 resp->rqstp->rq_vec[v].iov_base = page_address(page);
2984 resp->rqstp->rq_vec[v].iov_len =
2985 len < PAGE_SIZE ? len : PAGE_SIZE;
2986 resp->rqstp->rq_next_page++;
2992 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
2993 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
2998 eof = (read->rd_offset + maxcount >=
2999 read->rd_fhp->fh_dentry->d_inode->i_size);
3004 resp->xbuf->head[0].iov_len = (char*)p
3005 - (char*)resp->xbuf->head[0].iov_base;
3006 resp->xbuf->page_len = maxcount;
3008 /* Use rest of head for padding and remaining ops: */
3009 resp->xbuf->tail[0].iov_base = p;
3010 resp->xbuf->tail[0].iov_len = 0;
3014 resp->xbuf->tail[0].iov_base += maxcount&3;
3015 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3022 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
3030 if (resp->xbuf->page_len)
3031 return nfserr_resource;
3032 if (!*resp->rqstp->rq_next_page)
3033 return nfserr_resource;
3035 page = page_address(*(resp->rqstp->rq_next_page++));
3037 maxcount = PAGE_SIZE;
3041 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3042 * if they would overflow the buffer. Is this kosher in NFSv4? If
3043 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3044 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3046 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3047 if (nfserr == nfserr_isdir)
3048 return nfserr_inval;
3054 resp->xbuf->head[0].iov_len = (char*)p
3055 - (char*)resp->xbuf->head[0].iov_base;
3056 resp->xbuf->page_len = maxcount;
3058 /* Use rest of head for padding and remaining ops: */
3059 resp->xbuf->tail[0].iov_base = p;
3060 resp->xbuf->tail[0].iov_len = 0;
3064 resp->xbuf->tail[0].iov_base += maxcount&3;
3065 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3072 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3076 __be32 *page, *savep, *tailbase;
3081 if (resp->xbuf->page_len)
3082 return nfserr_resource;
3083 if (!*resp->rqstp->rq_next_page)
3084 return nfserr_resource;
3086 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
3089 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3093 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
3096 maxcount = PAGE_SIZE;
3097 if (maxcount > readdir->rd_maxcount)
3098 maxcount = readdir->rd_maxcount;
3101 * Convert from bytes to words, account for the two words already
3102 * written, make sure to leave two words at the end for the next
3103 * pointer and eof field.
3105 maxcount = (maxcount >> 2) - 4;
3107 nfserr = nfserr_toosmall;
3111 page = page_address(*(resp->rqstp->rq_next_page++));
3112 readdir->common.err = 0;
3113 readdir->buflen = maxcount;
3114 readdir->buffer = page;
3115 readdir->offset = NULL;
3117 offset = readdir->rd_cookie;
3118 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3120 &readdir->common, nfsd4_encode_dirent);
3121 if (nfserr == nfs_ok &&
3122 readdir->common.err == nfserr_toosmall &&
3123 readdir->buffer == page)
3124 nfserr = nfserr_toosmall;
3128 if (readdir->offset)
3129 xdr_encode_hyper(readdir->offset, offset);
3131 p = readdir->buffer;
3132 *p++ = 0; /* no more entries */
3133 *p++ = htonl(readdir->common.err == nfserr_eof);
3134 resp->xbuf->page_len = ((char*)p) -
3135 (char*)page_address(*(resp->rqstp->rq_next_page-1));
3137 /* Use rest of head for padding and remaining ops: */
3138 resp->xbuf->tail[0].iov_base = tailbase;
3139 resp->xbuf->tail[0].iov_len = 0;
3140 resp->p = resp->xbuf->tail[0].iov_base;
3141 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
3151 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3157 write_cinfo(&p, &remove->rm_cinfo);
3164 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3170 write_cinfo(&p, &rename->rn_sinfo);
3171 write_cinfo(&p, &rename->rn_tinfo);
3178 nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3179 __be32 nfserr, struct svc_export *exp)
3181 u32 i, nflavs, supported;
3182 struct exp_flavor_info *flavs;
3183 struct exp_flavor_info def_flavs[2];
3184 __be32 *p, *flavorsp;
3185 static bool report = true;
3189 if (exp->ex_nflavors) {
3190 flavs = exp->ex_flavors;
3191 nflavs = exp->ex_nflavors;
3192 } else { /* Handling of some defaults in absence of real secinfo: */
3194 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3196 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3197 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3198 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3200 flavs[0].pseudoflavor
3201 = svcauth_gss_flavor(exp->ex_client);
3204 flavs[0].pseudoflavor
3205 = exp->ex_client->flavour->flavour;
3211 flavorsp = p++; /* to be backfilled later */
3214 for (i = 0; i < nflavs; i++) {
3215 rpc_authflavor_t pf = flavs[i].pseudoflavor;
3216 struct rpcsec_gss_info info;
3218 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3220 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
3221 WRITE32(RPC_AUTH_GSS);
3222 WRITE32(info.oid.len);
3223 WRITEMEM(info.oid.data, info.oid.len);
3225 WRITE32(info.service);
3227 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3234 pr_warn("NFS: SECINFO: security flavor %u "
3235 "is not supported\n", pf);
3239 if (nflavs != supported)
3241 *flavorsp = htonl(supported);
3250 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3251 struct nfsd4_secinfo *secinfo)
3253 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3257 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3258 struct nfsd4_secinfo_no_name *secinfo)
3260 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3264 * The SETATTR encode routine is special -- it always encodes a bitmap,
3265 * regardless of the error status.
3268 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3281 WRITE32(setattr->sa_bmval[0]);
3282 WRITE32(setattr->sa_bmval[1]);
3283 WRITE32(setattr->sa_bmval[2]);
3290 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3295 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
3296 WRITEMEM(&scd->se_clientid, 8);
3297 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
3300 else if (nfserr == nfserr_clid_inuse) {
3310 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3316 WRITE32(write->wr_bytes_written);
3317 WRITE32(write->wr_how_written);
3318 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
3324 static const u32 nfs4_minimal_spo_must_enforce[2] = {
3325 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3326 1 << (OP_EXCHANGE_ID - 32) |
3327 1 << (OP_CREATE_SESSION - 32) |
3328 1 << (OP_DESTROY_SESSION - 32) |
3329 1 << (OP_DESTROY_CLIENTID - 32)
3333 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
3334 struct nfsd4_exchange_id *exid)
3340 int server_scope_sz;
3341 uint64_t minor_id = 0;
3346 major_id = utsname()->nodename;
3347 major_id_sz = strlen(major_id);
3348 server_scope = utsname()->nodename;
3349 server_scope_sz = strlen(server_scope);
3352 8 /* eir_clientid */ +
3353 4 /* eir_sequenceid */ +
3355 4 /* spr_how (SP4_NONE) */ +
3356 8 /* so_minor_id */ +
3357 4 /* so_major_id.len */ +
3358 (XDR_QUADLEN(major_id_sz) * 4) +
3359 4 /* eir_server_scope.len */ +
3360 (XDR_QUADLEN(server_scope_sz) * 4) +
3361 4 /* eir_server_impl_id.count (0) */);
3363 WRITEMEM(&exid->clientid, 8);
3364 WRITE32(exid->seqid);
3365 WRITE32(exid->flags);
3367 /* state_protect4_r. Currently only support SP4_NONE */
3368 BUG_ON(exid->spa_how != SP4_NONE);
3369 WRITE32(exid->spa_how);
3370 switch (exid->spa_how) {
3374 /* spo_must_enforce bitmap: */
3376 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3377 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3378 /* empty spo_must_allow bitmap: */
3385 /* The server_owner struct */
3386 WRITE64(minor_id); /* Minor id */
3388 WRITE32(major_id_sz);
3389 WRITEMEM(major_id, major_id_sz);
3392 WRITE32(server_scope_sz);
3393 WRITEMEM(server_scope, server_scope_sz);
3395 /* Implementation id */
3396 WRITE32(0); /* zero length nfs_impl_id4 array */
3402 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3403 struct nfsd4_create_session *sess)
3411 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3412 WRITE32(sess->seqid);
3413 WRITE32(sess->flags);
3417 WRITE32(0); /* headerpadsz */
3418 WRITE32(sess->fore_channel.maxreq_sz);
3419 WRITE32(sess->fore_channel.maxresp_sz);
3420 WRITE32(sess->fore_channel.maxresp_cached);
3421 WRITE32(sess->fore_channel.maxops);
3422 WRITE32(sess->fore_channel.maxreqs);
3423 WRITE32(sess->fore_channel.nr_rdma_attrs);
3426 if (sess->fore_channel.nr_rdma_attrs) {
3428 WRITE32(sess->fore_channel.rdma_attrs);
3433 WRITE32(0); /* headerpadsz */
3434 WRITE32(sess->back_channel.maxreq_sz);
3435 WRITE32(sess->back_channel.maxresp_sz);
3436 WRITE32(sess->back_channel.maxresp_cached);
3437 WRITE32(sess->back_channel.maxops);
3438 WRITE32(sess->back_channel.maxreqs);
3439 WRITE32(sess->back_channel.nr_rdma_attrs);
3442 if (sess->back_channel.nr_rdma_attrs) {
3444 WRITE32(sess->back_channel.rdma_attrs);
3451 nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3452 struct nfsd4_destroy_session *destroy_session)
3458 nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3459 struct nfsd4_free_stateid *free_stateid)
3473 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
3474 struct nfsd4_sequence *seq)
3481 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3482 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3483 WRITE32(seq->seqid);
3484 WRITE32(seq->slotid);
3485 /* Note slotid's are numbered from zero: */
3486 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3487 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
3488 WRITE32(seq->status_flags);
3491 resp->cstate.datap = p; /* DRC cache data pointer */
3496 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3497 struct nfsd4_test_stateid *test_stateid)
3499 struct nfsd4_test_stateid_id *stateid, *next;
3502 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
3503 *p++ = htonl(test_stateid->ts_num_ids);
3505 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
3506 *p++ = stateid->ts_id_status;
3514 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3519 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3522 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3523 * since we don't need to filter out obsolete ops as this is
3524 * done in the decoding phase.
3526 static nfsd4_enc nfsd4_enc_ops[] = {
3527 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3528 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3529 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3530 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3531 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3532 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3533 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3534 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3535 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3536 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3537 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3538 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3539 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3540 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3541 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3542 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
3543 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
3544 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3545 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3546 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3547 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3548 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3549 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3550 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3551 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3552 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3553 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3554 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3555 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3556 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3557 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3558 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3559 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3560 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3561 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3562 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3563 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
3565 /* NFSv4.1 operations */
3566 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
3567 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
3568 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3569 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3570 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
3571 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
3572 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3573 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3574 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3575 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3576 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3577 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3578 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
3579 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3580 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
3581 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
3582 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3583 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3584 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
3588 * Calculate the total amount of memory that the compound response has taken
3589 * after encoding the current operation with pad.
3591 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3592 * which was specified at nfsd4_operation, else pad is zero.
3594 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
3596 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3597 * will be at least a page and will therefore hold the xdr_buf head.
3599 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
3601 struct xdr_buf *xb = &resp->rqstp->rq_res;
3602 struct nfsd4_session *session = NULL;
3603 struct nfsd4_slot *slot = resp->cstate.slot;
3604 u32 length, tlen = 0;
3606 if (!nfsd4_has_session(&resp->cstate))
3609 session = resp->cstate.session;
3610 if (session == NULL)
3613 if (xb->page_len == 0) {
3614 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3616 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3617 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3619 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3621 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3622 length, xb->page_len, tlen, pad);
3624 if (length > session->se_fchannel.maxresp_sz)
3625 return nfserr_rep_too_big;
3627 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
3628 length > session->se_fchannel.maxresp_cached)
3629 return nfserr_rep_too_big_to_cache;
3635 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3637 struct nfs4_stateowner *so = resp->cstate.replay_owner;
3643 statp = p++; /* to be backfilled at the end */
3646 if (op->opnum == OP_ILLEGAL)
3648 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3649 !nfsd4_enc_ops[op->opnum]);
3650 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
3651 /* nfsd4_check_drc_limit guarantees enough room for error status */
3653 op->status = nfsd4_check_resp_size(resp, 0);
3655 so->so_replay.rp_status = op->status;
3656 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3657 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3661 * Note: We write the status directly, instead of using WRITE32(),
3662 * since it is already in network byte order.
3664 *statp = op->status;
3668 * Encode the reply stored in the stateowner reply cache
3670 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3671 * previously sent already encoded operation.
3673 * called with nfs4_lock_state() held
3676 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3679 struct nfs4_replay *rp = op->replay;
3685 *p++ = rp->rp_status; /* already xdr'ed */
3688 RESERVE_SPACE(rp->rp_buflen);
3689 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3694 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
3696 return xdr_ressize_check(rqstp, p);
3699 int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
3701 struct svc_rqst *rqstp = rq;
3702 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3704 if (args->ops != args->iops) {
3706 args->ops = args->iops;
3710 while (args->to_free) {
3711 struct tmpbuf *tb = args->to_free;
3712 args->to_free = tb->next;
3713 tb->release(tb->buf);
3720 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
3723 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3724 args->pagelist = rqstp->rq_arg.pages;
3725 args->pagelen = rqstp->rq_arg.page_len;
3727 args->to_free = NULL;
3728 args->ops = args->iops;
3729 args->rqstp = rqstp;
3731 return !nfsd4_decode_compound(args);
3735 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
3738 * All that remains is to write the tag and operation count...
3740 struct nfsd4_compound_state *cs = &resp->cstate;
3743 *p++ = htonl(resp->taglen);
3744 memcpy(p, resp->tag, resp->taglen);
3745 p += XDR_QUADLEN(resp->taglen);
3746 *p++ = htonl(resp->opcnt);
3748 if (rqstp->rq_res.page_len)
3749 iov = &rqstp->rq_res.tail[0];
3751 iov = &rqstp->rq_res.head[0];
3752 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3753 BUG_ON(iov->iov_len > PAGE_SIZE);
3754 if (nfsd4_has_session(cs)) {
3755 if (cs->status != nfserr_replay_cache) {
3756 nfsd4_store_cache_entry(resp);
3757 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3759 /* Renew the clientid on success and on replay */
3760 put_client_renew(cs->session->se_client);
3761 nfsd4_put_session(cs->session);