2 * Server-side XDR for NFSv4
4 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * TODO: Neil Brown made the following observation: We currently
36 * initially reserve NFSD_BUFSIZE space on the transmit queue and
37 * never release any of that until the request is complete.
38 * It would be good to calculate a new maximum response size while
39 * decoding the COMPOUND, and call svc_reserve with this number
40 * at the end of nfs4svc_decode_compoundargs.
43 #include <linux/slab.h>
44 #include <linux/namei.h>
45 #include <linux/statfs.h>
46 #include <linux/utsname.h>
47 #include <linux/pagemap.h>
48 #include <linux/sunrpc/svcauth_gss.h>
58 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59 #include <linux/security.h>
63 #define NFSDDBG_FACILITY NFSDDBG_XDR
66 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
67 * directory in order to indicate to the client that a filesystem boundary is present
68 * We use a fixed fsid for a referral
70 #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
71 #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
74 check_filename(char *str, int len)
80 if (isdotent(str, len))
81 return nfserr_badname;
82 for (i = 0; i < len; i++)
84 return nfserr_badname;
96 dprintk("NFSD: xdr error (%s:%d)\n", \
97 __FILE__, __LINE__); \
98 status = nfserr_bad_xdr; \
101 #define READ32(x) (x) = ntohl(*p++)
102 #define READ64(x) do { \
103 (x) = (u64)ntohl(*p++) << 32; \
104 (x) |= ntohl(*p++); \
106 #define READTIME(x) do { \
111 #define READMEM(x,nbytes) do { \
113 p += XDR_QUADLEN(nbytes); \
115 #define SAVEMEM(x,nbytes) do { \
116 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
117 savemem(argp, p, nbytes) : \
119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
123 p += XDR_QUADLEN(nbytes); \
125 #define COPYMEM(x,nbytes) do { \
126 memcpy((x), p, nbytes); \
127 p += XDR_QUADLEN(nbytes); \
130 /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
131 #define READ_BUF(nbytes) do { \
132 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
134 argp->p += XDR_QUADLEN(nbytes); \
135 } else if (!(p = read_buf(argp, nbytes))) { \
136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
142 static void next_decode_page(struct nfsd4_compoundargs *argp)
145 argp->p = page_address(argp->pagelist[0]);
146 if (argp->pagelen < PAGE_SIZE) {
147 argp->end = argp->p + (argp->pagelen>>2);
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
155 static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
160 unsigned int avail = (char *)argp->end - (char *)argp->p;
162 if (avail + argp->pagelen < nbytes)
164 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
166 /* ok, we can do it with the current plus the next page */
167 if (nbytes <= sizeof(argp->tmp))
171 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
177 * The following memcpy is safe because read_buf is always
178 * called with nbytes > avail, and the two cases above both
179 * guarantee p points to at least nbytes bytes.
181 memcpy(p, argp->p, avail);
182 next_decode_page(argp);
183 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
184 argp->p += XDR_QUADLEN(nbytes - avail);
188 static int zero_clientid(clientid_t *clid)
190 return (clid->cl_boot == 0) && (clid->cl_id == 0);
194 defer_free(struct nfsd4_compoundargs *argp,
195 void (*release)(const void *), void *p)
199 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
203 tb->release = release;
204 tb->next = argp->to_free;
209 static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
211 if (p == argp->tmp) {
212 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
216 BUG_ON(p != argp->tmpp);
219 if (defer_free(argp, kfree, p)) {
227 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
241 READ_BUF(bmlen << 2);
253 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
254 struct iattr *iattr, struct nfs4_acl **acl,
255 struct xdr_netobj *label)
257 int expected_len, len = 0;
264 if ((status = nfsd4_decode_bitmap(argp, bmval)))
268 READ32(expected_len);
270 if (bmval[0] & FATTR4_WORD0_SIZE) {
273 READ64(iattr->ia_size);
274 iattr->ia_valid |= ATTR_SIZE;
276 if (bmval[0] & FATTR4_WORD0_ACL) {
278 struct nfs4_ace *ace;
280 READ_BUF(4); len += 4;
283 if (nace > NFS4_ACL_MAX)
284 return nfserr_resource;
286 *acl = nfs4_acl_new(nace);
291 defer_free(argp, kfree, *acl);
293 (*acl)->naces = nace;
294 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
295 READ_BUF(16); len += 16;
298 READ32(ace->access_mask);
301 len += XDR_QUADLEN(dummy32) << 2;
302 READMEM(buf, dummy32);
303 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
305 if (ace->whotype != NFS4_ACL_WHO_NAMED)
307 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
308 status = nfsd_map_name_to_gid(argp->rqstp,
309 buf, dummy32, &ace->who_gid);
311 status = nfsd_map_name_to_uid(argp->rqstp,
312 buf, dummy32, &ace->who_uid);
318 if (bmval[1] & FATTR4_WORD1_MODE) {
321 READ32(iattr->ia_mode);
322 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
323 iattr->ia_valid |= ATTR_MODE;
325 if (bmval[1] & FATTR4_WORD1_OWNER) {
330 len += (XDR_QUADLEN(dummy32) << 2);
331 READMEM(buf, dummy32);
332 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
334 iattr->ia_valid |= ATTR_UID;
336 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
341 len += (XDR_QUADLEN(dummy32) << 2);
342 READMEM(buf, dummy32);
343 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
345 iattr->ia_valid |= ATTR_GID;
347 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
352 case NFS4_SET_TO_CLIENT_TIME:
353 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
354 all 32 bits of 'nseconds'. */
357 READ64(iattr->ia_atime.tv_sec);
358 READ32(iattr->ia_atime.tv_nsec);
359 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
361 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
363 case NFS4_SET_TO_SERVER_TIME:
364 iattr->ia_valid |= ATTR_ATIME;
370 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
375 case NFS4_SET_TO_CLIENT_TIME:
376 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
377 all 32 bits of 'nseconds'. */
380 READ64(iattr->ia_mtime.tv_sec);
381 READ32(iattr->ia_mtime.tv_nsec);
382 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
384 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
386 case NFS4_SET_TO_SERVER_TIME:
387 iattr->ia_valid |= ATTR_MTIME;
395 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
396 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
399 READ32(dummy32); /* lfs: we don't use it */
402 READ32(dummy32); /* pi: we don't use it either */
407 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
408 return nfserr_badlabel;
409 len += (XDR_QUADLEN(dummy32) << 2);
410 READMEM(buf, dummy32);
411 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
413 return nfserr_jukebox;
414 label->len = dummy32;
415 defer_free(argp, kfree, label->data);
416 memcpy(label->data, buf, dummy32);
420 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
421 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
422 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
423 READ_BUF(expected_len - len);
424 else if (len != expected_len)
430 status = nfserrno(host_err);
435 nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
439 READ_BUF(sizeof(stateid_t));
440 READ32(sid->si_generation);
441 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
447 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
452 READ32(access->ac_req_access);
457 static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
465 /* callback_sec_params4 */
469 cbs->flavor = (u32)(-1);
471 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
473 for (i = 0; i < nr_secflavs; ++i) {
478 /* Nothing to read */
479 if (cbs->flavor == (u32)(-1))
480 cbs->flavor = RPC_AUTH_NULL;
490 SAVEMEM(machine_name, dummy);
501 if (cbs->flavor == (u32)(-1)) {
502 kuid_t kuid = make_kuid(&init_user_ns, uid);
503 kgid_t kgid = make_kgid(&init_user_ns, gid);
504 if (uid_valid(kuid) && gid_valid(kgid)) {
507 cbs->flavor = RPC_AUTH_UNIX;
509 dprintk("RPC_AUTH_UNIX with invalid"
510 "uid or gid ignoring!\n");
515 dprintk("RPC_AUTH_GSS callback secflavor "
520 /* gcbp_handle_from_server */
523 p += XDR_QUADLEN(dummy);
524 /* gcbp_handle_from_client */
530 dprintk("Illegal callback secflavor\n");
537 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
542 READ32(bc->bc_cb_program);
543 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
548 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
552 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
553 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
555 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
556 * could help us figure out we should be using it. */
561 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
566 READ32(close->cl_seqid);
567 return nfsd4_decode_stateid(argp, &close->cl_stateid);
574 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
579 READ64(commit->co_offset);
580 READ32(commit->co_count);
586 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
591 READ32(create->cr_type);
592 switch (create->cr_type) {
595 READ32(create->cr_linklen);
596 READ_BUF(create->cr_linklen);
597 SAVEMEM(create->cr_linkname, create->cr_linklen);
602 READ32(create->cr_specdata1);
603 READ32(create->cr_specdata2);
613 READ32(create->cr_namelen);
614 READ_BUF(create->cr_namelen);
615 SAVEMEM(create->cr_name, create->cr_namelen);
616 if ((status = check_filename(create->cr_name, create->cr_namelen)))
619 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
620 &create->cr_acl, &create->cr_label);
628 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
630 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
634 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
636 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
640 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
645 READ32(link->li_namelen);
646 READ_BUF(link->li_namelen);
647 SAVEMEM(link->li_name, link->li_namelen);
648 if ((status = check_filename(link->li_name, link->li_namelen)))
655 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
660 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
663 READ32(lock->lk_type);
664 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
666 READ32(lock->lk_reclaim);
667 READ64(lock->lk_offset);
668 READ64(lock->lk_length);
669 READ32(lock->lk_is_new);
671 if (lock->lk_is_new) {
673 READ32(lock->lk_new_open_seqid);
674 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
677 READ_BUF(8 + sizeof(clientid_t));
678 READ32(lock->lk_new_lock_seqid);
679 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
680 READ32(lock->lk_new_owner.len);
681 READ_BUF(lock->lk_new_owner.len);
682 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
684 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
688 READ32(lock->lk_old_lock_seqid);
695 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
700 READ32(lockt->lt_type);
701 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
703 READ64(lockt->lt_offset);
704 READ64(lockt->lt_length);
705 COPYMEM(&lockt->lt_clientid, 8);
706 READ32(lockt->lt_owner.len);
707 READ_BUF(lockt->lt_owner.len);
708 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
714 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
719 READ32(locku->lu_type);
720 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
722 READ32(locku->lu_seqid);
723 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
727 READ64(locku->lu_offset);
728 READ64(locku->lu_length);
734 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
739 READ32(lookup->lo_len);
740 READ_BUF(lookup->lo_len);
741 SAVEMEM(lookup->lo_name, lookup->lo_len);
742 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
748 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
755 *share_access = w & NFS4_SHARE_ACCESS_MASK;
756 *deleg_want = w & NFS4_SHARE_WANT_MASK;
758 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
760 switch (w & NFS4_SHARE_ACCESS_MASK) {
761 case NFS4_SHARE_ACCESS_READ:
762 case NFS4_SHARE_ACCESS_WRITE:
763 case NFS4_SHARE_ACCESS_BOTH:
766 return nfserr_bad_xdr;
768 w &= ~NFS4_SHARE_ACCESS_MASK;
771 if (!argp->minorversion)
772 return nfserr_bad_xdr;
773 switch (w & NFS4_SHARE_WANT_MASK) {
774 case NFS4_SHARE_WANT_NO_PREFERENCE:
775 case NFS4_SHARE_WANT_READ_DELEG:
776 case NFS4_SHARE_WANT_WRITE_DELEG:
777 case NFS4_SHARE_WANT_ANY_DELEG:
778 case NFS4_SHARE_WANT_NO_DELEG:
779 case NFS4_SHARE_WANT_CANCEL:
782 return nfserr_bad_xdr;
784 w &= ~NFS4_SHARE_WANT_MASK;
788 if (!deleg_when) /* open_downgrade */
791 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
792 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
793 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
794 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
798 return nfserr_bad_xdr;
801 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
807 /* Note: unlinke access bits, deny bits may be zero. */
808 if (*x & ~NFS4_SHARE_DENY_BOTH)
809 return nfserr_bad_xdr;
812 return nfserr_bad_xdr;
815 static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
822 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
823 return nfserr_bad_xdr;
826 SAVEMEM(o->data, o->len);
829 return nfserr_bad_xdr;
833 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
838 memset(open->op_bmval, 0, sizeof(open->op_bmval));
839 open->op_iattr.ia_valid = 0;
840 open->op_openowner = NULL;
842 open->op_xdr_error = 0;
843 /* seqid, share_access, share_deny, clientid, ownerlen */
845 READ32(open->op_seqid);
846 /* decode, yet ignore deleg_when until supported */
847 status = nfsd4_decode_share_access(argp, &open->op_share_access,
848 &open->op_deleg_want, &dummy);
851 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
854 READ_BUF(sizeof(clientid_t));
855 COPYMEM(&open->op_clientid, sizeof(clientid_t));
856 status = nfsd4_decode_opaque(argp, &open->op_owner);
860 READ32(open->op_create);
861 switch (open->op_create) {
862 case NFS4_OPEN_NOCREATE:
864 case NFS4_OPEN_CREATE:
866 READ32(open->op_createmode);
867 switch (open->op_createmode) {
868 case NFS4_CREATE_UNCHECKED:
869 case NFS4_CREATE_GUARDED:
870 status = nfsd4_decode_fattr(argp, open->op_bmval,
871 &open->op_iattr, &open->op_acl, &open->op_label);
875 case NFS4_CREATE_EXCLUSIVE:
876 READ_BUF(NFS4_VERIFIER_SIZE);
877 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
879 case NFS4_CREATE_EXCLUSIVE4_1:
880 if (argp->minorversion < 1)
882 READ_BUF(NFS4_VERIFIER_SIZE);
883 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
884 status = nfsd4_decode_fattr(argp, open->op_bmval,
885 &open->op_iattr, &open->op_acl, &open->op_label);
899 READ32(open->op_claim_type);
900 switch (open->op_claim_type) {
901 case NFS4_OPEN_CLAIM_NULL:
902 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
904 READ32(open->op_fname.len);
905 READ_BUF(open->op_fname.len);
906 SAVEMEM(open->op_fname.data, open->op_fname.len);
907 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
910 case NFS4_OPEN_CLAIM_PREVIOUS:
912 READ32(open->op_delegate_type);
914 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
915 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
919 READ32(open->op_fname.len);
920 READ_BUF(open->op_fname.len);
921 SAVEMEM(open->op_fname.data, open->op_fname.len);
922 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
925 case NFS4_OPEN_CLAIM_FH:
926 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
927 if (argp->minorversion < 1)
931 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
932 if (argp->minorversion < 1)
934 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
946 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
950 if (argp->minorversion >= 1)
951 return nfserr_notsupp;
953 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
957 READ32(open_conf->oc_seqid);
963 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
967 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
971 READ32(open_down->od_seqid);
972 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
973 &open_down->od_deleg_want, NULL);
976 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
983 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
988 READ32(putfh->pf_fhlen);
989 if (putfh->pf_fhlen > NFS4_FHSIZE)
991 READ_BUF(putfh->pf_fhlen);
992 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
998 nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1000 if (argp->minorversion == 0)
1002 return nfserr_notsupp;
1006 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1010 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1014 READ64(read->rd_offset);
1015 READ32(read->rd_length);
1021 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1026 READ64(readdir->rd_cookie);
1027 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1028 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1029 READ32(readdir->rd_maxcount);
1030 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1037 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1042 READ32(remove->rm_namelen);
1043 READ_BUF(remove->rm_namelen);
1044 SAVEMEM(remove->rm_name, remove->rm_namelen);
1045 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1052 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1057 READ32(rename->rn_snamelen);
1058 READ_BUF(rename->rn_snamelen + 4);
1059 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1060 READ32(rename->rn_tnamelen);
1061 READ_BUF(rename->rn_tnamelen);
1062 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1063 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1065 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1072 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1076 if (argp->minorversion >= 1)
1077 return nfserr_notsupp;
1079 READ_BUF(sizeof(clientid_t));
1080 COPYMEM(clientid, sizeof(clientid_t));
1086 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1087 struct nfsd4_secinfo *secinfo)
1092 READ32(secinfo->si_namelen);
1093 READ_BUF(secinfo->si_namelen);
1094 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1095 status = check_filename(secinfo->si_name, secinfo->si_namelen);
1102 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1103 struct nfsd4_secinfo_no_name *sin)
1108 READ32(sin->sin_style);
1113 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1117 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1120 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1121 &setattr->sa_acl, &setattr->sa_label);
1125 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1129 if (argp->minorversion >= 1)
1130 return nfserr_notsupp;
1132 READ_BUF(NFS4_VERIFIER_SIZE);
1133 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1135 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1137 return nfserr_bad_xdr;
1139 READ32(setclientid->se_callback_prog);
1140 READ32(setclientid->se_callback_netid_len);
1142 READ_BUF(setclientid->se_callback_netid_len + 4);
1143 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1144 READ32(setclientid->se_callback_addr_len);
1146 READ_BUF(setclientid->se_callback_addr_len + 4);
1147 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1148 READ32(setclientid->se_callback_ident);
1154 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1158 if (argp->minorversion >= 1)
1159 return nfserr_notsupp;
1161 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1162 COPYMEM(&scd_c->sc_clientid, 8);
1163 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1168 /* Also used for NVERIFY */
1170 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1174 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1177 /* For convenience's sake, we compare raw xdr'd attributes in
1178 * nfsd4_proc_verify */
1181 READ32(verify->ve_attrlen);
1182 READ_BUF(verify->ve_attrlen);
1183 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1189 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1195 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1199 READ64(write->wr_offset);
1200 READ32(write->wr_stable_how);
1201 if (write->wr_stable_how > 2)
1203 READ32(write->wr_buflen);
1205 /* Sorry .. no magic macros for this.. *
1206 * READ_BUF(write->wr_buflen);
1207 * SAVEMEM(write->wr_buf, write->wr_buflen);
1209 avail = (char*)argp->end - (char*)argp->p;
1210 if (avail + argp->pagelen < write->wr_buflen) {
1211 dprintk("NFSD: xdr error (%s:%d)\n",
1212 __FILE__, __LINE__);
1215 write->wr_head.iov_base = p;
1216 write->wr_head.iov_len = avail;
1217 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
1218 write->wr_pagelist = argp->pagelist;
1220 len = XDR_QUADLEN(write->wr_buflen) << 2;
1226 pages = len >> PAGE_SHIFT;
1227 argp->pagelist += pages;
1228 argp->pagelen -= pages * PAGE_SIZE;
1229 len -= pages * PAGE_SIZE;
1231 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1232 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
1234 argp->p += XDR_QUADLEN(len);
1240 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1244 if (argp->minorversion >= 1)
1245 return nfserr_notsupp;
1248 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1249 READ32(rlockowner->rl_owner.len);
1250 READ_BUF(rlockowner->rl_owner.len);
1251 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1253 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1254 return nfserr_inval;
1259 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1260 struct nfsd4_exchange_id *exid)
1265 READ_BUF(NFS4_VERIFIER_SIZE);
1266 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1268 status = nfsd4_decode_opaque(argp, &exid->clname);
1270 return nfserr_bad_xdr;
1273 READ32(exid->flags);
1275 /* Ignore state_protect4_a */
1277 READ32(exid->spa_how);
1278 switch (exid->spa_how) {
1282 /* spo_must_enforce */
1285 READ_BUF(dummy * 4);
1288 /* spo_must_allow */
1291 READ_BUF(dummy * 4);
1298 READ_BUF(dummy * 4);
1303 READ_BUF(dummy * 4);
1306 /* ssp_hash_algs<> */
1313 p += XDR_QUADLEN(dummy);
1316 /* ssp_encr_algs<> */
1323 p += XDR_QUADLEN(dummy);
1326 /* ssp_window and ssp_num_gss_handles */
1335 /* Ignore Implementation ID */
1336 READ_BUF(4); /* nfs_impl_id4 array length */
1347 p += XDR_QUADLEN(dummy);
1353 p += XDR_QUADLEN(dummy);
1363 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1364 struct nfsd4_create_session *sess)
1370 COPYMEM(&sess->clientid, 8);
1371 READ32(sess->seqid);
1372 READ32(sess->flags);
1374 /* Fore channel attrs */
1376 READ32(dummy); /* headerpadsz is always 0 */
1377 READ32(sess->fore_channel.maxreq_sz);
1378 READ32(sess->fore_channel.maxresp_sz);
1379 READ32(sess->fore_channel.maxresp_cached);
1380 READ32(sess->fore_channel.maxops);
1381 READ32(sess->fore_channel.maxreqs);
1382 READ32(sess->fore_channel.nr_rdma_attrs);
1383 if (sess->fore_channel.nr_rdma_attrs == 1) {
1385 READ32(sess->fore_channel.rdma_attrs);
1386 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1387 dprintk("Too many fore channel attr bitmaps!\n");
1391 /* Back channel attrs */
1393 READ32(dummy); /* headerpadsz is always 0 */
1394 READ32(sess->back_channel.maxreq_sz);
1395 READ32(sess->back_channel.maxresp_sz);
1396 READ32(sess->back_channel.maxresp_cached);
1397 READ32(sess->back_channel.maxops);
1398 READ32(sess->back_channel.maxreqs);
1399 READ32(sess->back_channel.nr_rdma_attrs);
1400 if (sess->back_channel.nr_rdma_attrs == 1) {
1402 READ32(sess->back_channel.rdma_attrs);
1403 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1404 dprintk("Too many back channel attr bitmaps!\n");
1409 READ32(sess->callback_prog);
1410 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1415 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1416 struct nfsd4_destroy_session *destroy_session)
1419 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1420 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1426 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1427 struct nfsd4_free_stateid *free_stateid)
1431 READ_BUF(sizeof(stateid_t));
1432 READ32(free_stateid->fr_stateid.si_generation);
1433 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1439 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1440 struct nfsd4_sequence *seq)
1444 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1445 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1447 READ32(seq->slotid);
1448 READ32(seq->maxslots);
1449 READ32(seq->cachethis);
1455 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1459 struct nfsd4_test_stateid_id *stateid;
1462 test_stateid->ts_num_ids = ntohl(*p++);
1464 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1466 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1467 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1469 status = nfserrno(-ENOMEM);
1473 defer_free(argp, kfree, stateid);
1474 INIT_LIST_HEAD(&stateid->ts_id_list);
1475 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1477 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1486 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1487 status = nfserr_bad_xdr;
1491 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1496 COPYMEM(&dc->clientid, 8);
1501 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1506 READ32(rc->rca_one_fs);
1512 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1518 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1520 return nfserr_notsupp;
1523 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1525 static nfsd4_dec nfsd4_dec_ops[] = {
1526 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1527 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1528 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1529 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1530 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1531 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1532 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1533 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1534 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1535 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1536 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1537 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1538 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1539 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1540 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1541 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1542 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1543 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1544 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1545 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1546 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
1547 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1548 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1549 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1550 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1551 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1552 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1553 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1554 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1555 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1556 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1557 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1558 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1559 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1560 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1561 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1562 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
1564 /* new operations for NFSv4.1 */
1565 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1566 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1567 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1568 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1569 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
1570 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
1571 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1572 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1573 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1574 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1575 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1576 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
1577 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1578 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1579 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
1580 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
1581 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1582 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
1583 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
1587 nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1589 if (op->opnum < FIRST_NFS4_OP)
1591 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
1593 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1595 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
1601 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1604 struct nfsd4_op *op;
1605 bool cachethis = false;
1609 READ32(argp->taglen);
1610 READ_BUF(argp->taglen + 8);
1611 SAVEMEM(argp->tag, argp->taglen);
1612 READ32(argp->minorversion);
1613 READ32(argp->opcnt);
1615 if (argp->taglen > NFSD4_MAX_TAGLEN)
1617 if (argp->opcnt > 100)
1620 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1621 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1623 argp->ops = argp->iops;
1624 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1629 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
1632 for (i = 0; i < argp->opcnt; i++) {
1639 if (nfsd4_opnum_in_range(argp, op))
1640 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
1642 op->opnum = OP_ILLEGAL;
1643 op->status = nfserr_op_illegal;
1651 * We'll try to cache the result in the DRC if any one
1652 * op in the compound wants to be cached:
1654 cachethis |= nfsd4_cache_this_op(op);
1656 /* Sessions make the DRC unnecessary: */
1657 if (argp->minorversion)
1659 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1664 #define WRITE32(n) *p++ = htonl(n)
1665 #define WRITE64(n) do { \
1666 *p++ = htonl((u32)((n) >> 32)); \
1667 *p++ = htonl((u32)(n)); \
1669 #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
1670 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1671 memcpy(p, ptr, nbytes); \
1672 p += XDR_QUADLEN(nbytes); \
1675 static void write32(__be32 **p, u32 n)
1680 static void write64(__be32 **p, u64 n)
1682 write32(p, (n >> 32));
1686 static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1688 if (IS_I_VERSION(inode)) {
1689 write64(p, inode->i_version);
1691 write32(p, stat->ctime.tv_sec);
1692 write32(p, stat->ctime.tv_nsec);
1696 static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1698 write32(p, c->atomic);
1699 if (c->change_supported) {
1700 write64(p, c->before_change);
1701 write64(p, c->after_change);
1703 write32(p, c->before_ctime_sec);
1704 write32(p, c->before_ctime_nsec);
1705 write32(p, c->after_ctime_sec);
1706 write32(p, c->after_ctime_nsec);
1710 #define RESERVE_SPACE(nbytes) do { \
1712 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1714 #define ADJUST_ARGS() resp->p = p
1716 /* Encode as an array of strings the string given with components
1717 * separated @sep, escaped with esc_enter and esc_exit.
1719 static __be32 nfsd4_encode_components_esc(char sep, char *components,
1720 __be32 **pp, int *buflen,
1721 char esc_enter, char esc_exit)
1725 int strlen, count=0;
1726 char *str, *end, *next;
1728 dprintk("nfsd4_encode_components(%s)\n", components);
1729 if ((*buflen -= 4) < 0)
1730 return nfserr_resource;
1731 WRITE32(0); /* We will fill this in with @count later */
1732 end = str = components;
1734 bool found_esc = false;
1736 /* try to parse as esc_start, ..., esc_end, sep */
1737 if (*str == esc_enter) {
1738 for (; *end && (*end != esc_exit); end++)
1739 /* find esc_exit or end of string */;
1741 if (*end && (!*next || *next == sep)) {
1748 for (; *end && (*end != sep); end++)
1749 /* find sep or end of string */;
1753 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1754 return nfserr_resource;
1756 WRITEMEM(str, strlen);
1769 /* Encode as an array of strings the string given with components
1772 static __be32 nfsd4_encode_components(char sep, char *components,
1773 __be32 **pp, int *buflen)
1775 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1779 * encode a location element of a fs_locations structure
1781 static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1782 __be32 **pp, int *buflen)
1787 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1791 status = nfsd4_encode_components('/', location->path, &p, buflen);
1799 * Encode a path in RFC3530 'pathname4' format
1801 static __be32 nfsd4_encode_path(const struct path *root,
1802 const struct path *path, __be32 **pp, int *buflen)
1804 struct path cur = *path;
1806 struct dentry **components = NULL;
1807 unsigned int ncomponents = 0;
1808 __be32 err = nfserr_jukebox;
1810 dprintk("nfsd4_encode_components(");
1813 /* First walk the path up to the nfsd root, and store the
1814 * dentries/path components in an array.
1817 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1819 if (cur.dentry == cur.mnt->mnt_root) {
1820 if (follow_up(&cur))
1824 if ((ncomponents & 15) == 0) {
1825 struct dentry **new;
1826 new = krealloc(components,
1827 sizeof(*new) * (ncomponents + 16),
1833 components[ncomponents++] = cur.dentry;
1834 cur.dentry = dget_parent(cur.dentry);
1840 WRITE32(ncomponents);
1842 while (ncomponents) {
1843 struct dentry *dentry = components[ncomponents - 1];
1846 spin_lock(&dentry->d_lock);
1847 len = dentry->d_name.len;
1848 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1850 spin_unlock(&dentry->d_lock);
1854 WRITEMEM(dentry->d_name.name, len);
1855 dprintk("/%s", dentry->d_name.name);
1856 spin_unlock(&dentry->d_lock);
1866 dput(components[--ncomponents]);
1872 static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1873 const struct path *path, __be32 **pp, int *buflen)
1875 struct svc_export *exp_ps;
1878 exp_ps = rqst_find_fsidzero_export(rqstp);
1880 return nfserrno(PTR_ERR(exp_ps));
1881 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1887 * encode a fs_locations structure
1889 static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1890 struct svc_export *exp,
1891 __be32 **pp, int *buflen)
1896 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1898 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
1901 if ((*buflen -= 4) < 0)
1902 return nfserr_resource;
1903 WRITE32(fslocs->locations_count);
1904 for (i=0; i<fslocs->locations_count; i++) {
1905 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1914 static u32 nfs4_file_type(umode_t mode)
1916 switch (mode & S_IFMT) {
1917 case S_IFIFO: return NF4FIFO;
1918 case S_IFCHR: return NF4CHR;
1919 case S_IFDIR: return NF4DIR;
1920 case S_IFBLK: return NF4BLK;
1921 case S_IFLNK: return NF4LNK;
1922 case S_IFREG: return NF4REG;
1923 case S_IFSOCK: return NF4SOCK;
1924 default: return NF4BAD;
1929 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
1930 __be32 **p, int *buflen)
1934 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1935 return nfserr_resource;
1936 if (whotype != NFS4_ACL_WHO_NAMED)
1937 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1938 else if (gid_valid(gid))
1939 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
1941 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
1943 return nfserrno(status);
1944 *p = xdr_encode_opaque(*p, NULL, status);
1945 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1946 BUG_ON(*buflen < 0);
1950 static inline __be32
1951 nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
1953 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1957 static inline __be32
1958 nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
1960 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1964 static inline __be32
1965 nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
1966 __be32 **p, int *buflen)
1968 kuid_t uid = INVALID_UID;
1969 kgid_t gid = INVALID_GID;
1971 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1972 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1977 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
1980 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1981 FATTR4_WORD0_RDATTR_ERROR)
1982 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1984 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
1985 static inline __be32
1986 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
1990 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
1991 return nfserr_resource;
1994 * For now we use a 0 here to indicate the null translation; in
1995 * the future we may place a call to translation code here.
1997 if ((*buflen -= 8) < 0)
1998 return nfserr_resource;
2000 WRITE32(0); /* lfs */
2001 WRITE32(0); /* pi */
2002 p = xdr_encode_opaque(p, context, len);
2003 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2009 static inline __be32
2010 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2014 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
2016 /* As per referral draft: */
2017 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2018 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2019 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2020 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2021 *rdattr_err = NFSERR_MOVED;
2023 return nfserr_moved;
2025 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2026 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2031 static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2033 struct path path = exp->ex_path;
2037 while (follow_up(&path)) {
2038 if (path.dentry != path.mnt->mnt_root)
2041 err = vfs_getattr(&path, stat);
2047 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2050 * countp is the buffer size in _words_
2053 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
2054 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
2055 struct svc_rqst *rqstp, int ignore_crossmnt)
2057 u32 bmval0 = bmval[0];
2058 u32 bmval1 = bmval[1];
2059 u32 bmval2 = bmval[2];
2061 struct svc_fh tempfh;
2062 struct kstatfs statfs;
2063 int buflen = count << 2;
2068 __be32 *p = *buffer;
2072 struct nfs4_acl *acl = NULL;
2073 void *context = NULL;
2075 bool contextsupport = false;
2076 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2077 u32 minorversion = resp->cstate.minorversion;
2078 struct path path = {
2079 .mnt = exp->ex_path.mnt,
2082 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2084 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2085 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2086 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2087 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
2089 if (exp->ex_fslocs.migrated) {
2091 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2096 err = vfs_getattr(&path, &stat);
2099 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2100 FATTR4_WORD0_MAXNAME)) ||
2101 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2102 FATTR4_WORD1_SPACE_TOTAL))) {
2103 err = vfs_statfs(&path, &statfs);
2107 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2108 fh_init(&tempfh, NFS4_FHSIZE);
2109 status = fh_compose(&tempfh, exp, dentry, NULL);
2114 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2115 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
2116 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2117 aclsupport = (err == 0);
2118 if (bmval0 & FATTR4_WORD0_ACL) {
2119 if (err == -EOPNOTSUPP)
2120 bmval0 &= ~FATTR4_WORD0_ACL;
2121 else if (err == -EINVAL) {
2122 status = nfserr_attrnotsupp;
2124 } else if (err != 0)
2129 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2130 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2131 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2132 err = security_inode_getsecctx(dentry->d_inode,
2133 &context, &contextlen);
2134 contextsupport = (err == 0);
2135 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2136 if (err == -EOPNOTSUPP)
2137 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2142 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2145 if ((buflen -= 16) < 0)
2151 } else if (bmval1) {
2152 if ((buflen -= 12) < 0)
2158 if ((buflen -= 8) < 0)
2163 attrlenp = p++; /* to be backfilled later */
2165 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2166 u32 word0 = nfsd_suppattrs0(minorversion);
2167 u32 word1 = nfsd_suppattrs1(minorversion);
2168 u32 word2 = nfsd_suppattrs2(minorversion);
2171 word0 &= ~FATTR4_WORD0_ACL;
2172 if (!contextsupport)
2173 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2175 if ((buflen -= 12) < 0)
2181 if ((buflen -= 16) < 0)
2189 if (bmval0 & FATTR4_WORD0_TYPE) {
2190 if ((buflen -= 4) < 0)
2192 dummy = nfs4_file_type(stat.mode);
2193 if (dummy == NF4BAD)
2194 goto out_serverfault;
2197 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2198 if ((buflen -= 4) < 0)
2200 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2201 WRITE32(NFS4_FH_PERSISTENT);
2203 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
2205 if (bmval0 & FATTR4_WORD0_CHANGE) {
2206 if ((buflen -= 8) < 0)
2208 write_change(&p, &stat, dentry->d_inode);
2210 if (bmval0 & FATTR4_WORD0_SIZE) {
2211 if ((buflen -= 8) < 0)
2215 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2216 if ((buflen -= 4) < 0)
2220 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2221 if ((buflen -= 4) < 0)
2225 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2226 if ((buflen -= 4) < 0)
2230 if (bmval0 & FATTR4_WORD0_FSID) {
2231 if ((buflen -= 16) < 0)
2233 if (exp->ex_fslocs.migrated) {
2234 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2235 WRITE64(NFS4_REFERRAL_FSID_MINOR);
2236 } else switch(fsid_source(fhp)) {
2237 case FSIDSOURCE_FSID:
2238 WRITE64((u64)exp->ex_fsid);
2241 case FSIDSOURCE_DEV:
2243 WRITE32(MAJOR(stat.dev));
2245 WRITE32(MINOR(stat.dev));
2247 case FSIDSOURCE_UUID:
2248 WRITEMEM(exp->ex_uuid, 16);
2252 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2253 if ((buflen -= 4) < 0)
2257 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2258 if ((buflen -= 4) < 0)
2260 WRITE32(nn->nfsd4_lease);
2262 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2263 if ((buflen -= 4) < 0)
2265 WRITE32(rdattr_err);
2267 if (bmval0 & FATTR4_WORD0_ACL) {
2268 struct nfs4_ace *ace;
2271 if ((buflen -= 4) < 0)
2277 if ((buflen -= 4) < 0)
2279 WRITE32(acl->naces);
2281 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2282 if ((buflen -= 4*3) < 0)
2286 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2287 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
2288 if (status == nfserr_resource)
2295 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2296 if ((buflen -= 4) < 0)
2298 WRITE32(aclsupport ?
2299 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2301 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2302 if ((buflen -= 4) < 0)
2306 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2307 if ((buflen -= 4) < 0)
2311 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2312 if ((buflen -= 4) < 0)
2316 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2317 if ((buflen -= 4) < 0)
2321 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2322 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2325 WRITE32(fhp->fh_handle.fh_size);
2326 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2328 if (bmval0 & FATTR4_WORD0_FILEID) {
2329 if ((buflen -= 8) < 0)
2333 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2334 if ((buflen -= 8) < 0)
2336 WRITE64((u64) statfs.f_ffree);
2338 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2339 if ((buflen -= 8) < 0)
2341 WRITE64((u64) statfs.f_ffree);
2343 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2344 if ((buflen -= 8) < 0)
2346 WRITE64((u64) statfs.f_files);
2348 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2349 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2350 if (status == nfserr_resource)
2355 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2356 if ((buflen -= 4) < 0)
2360 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2361 if ((buflen -= 8) < 0)
2365 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2366 if ((buflen -= 4) < 0)
2370 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2371 if ((buflen -= 4) < 0)
2373 WRITE32(statfs.f_namelen);
2375 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2376 if ((buflen -= 8) < 0)
2378 WRITE64((u64) svc_max_payload(rqstp));
2380 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2381 if ((buflen -= 8) < 0)
2383 WRITE64((u64) svc_max_payload(rqstp));
2385 if (bmval1 & FATTR4_WORD1_MODE) {
2386 if ((buflen -= 4) < 0)
2388 WRITE32(stat.mode & S_IALLUGO);
2390 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2391 if ((buflen -= 4) < 0)
2395 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2396 if ((buflen -= 4) < 0)
2398 WRITE32(stat.nlink);
2400 if (bmval1 & FATTR4_WORD1_OWNER) {
2401 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2402 if (status == nfserr_resource)
2407 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2408 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2409 if (status == nfserr_resource)
2414 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2415 if ((buflen -= 8) < 0)
2417 WRITE32((u32) MAJOR(stat.rdev));
2418 WRITE32((u32) MINOR(stat.rdev));
2420 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2421 if ((buflen -= 8) < 0)
2423 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2426 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2427 if ((buflen -= 8) < 0)
2429 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2432 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2433 if ((buflen -= 8) < 0)
2435 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2438 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2439 if ((buflen -= 8) < 0)
2441 dummy64 = (u64)stat.blocks << 9;
2444 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2445 if ((buflen -= 12) < 0)
2447 WRITE64((s64)stat.atime.tv_sec);
2448 WRITE32(stat.atime.tv_nsec);
2450 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2451 if ((buflen -= 12) < 0)
2457 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2458 if ((buflen -= 12) < 0)
2460 WRITE64((s64)stat.ctime.tv_sec);
2461 WRITE32(stat.ctime.tv_nsec);
2463 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2464 if ((buflen -= 12) < 0)
2466 WRITE64((s64)stat.mtime.tv_sec);
2467 WRITE32(stat.mtime.tv_nsec);
2469 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2470 if ((buflen -= 8) < 0)
2473 * Get parent's attributes if not ignoring crossmount
2474 * and this is the root of a cross-mounted filesystem.
2476 if (ignore_crossmnt == 0 &&
2477 dentry == exp->ex_path.mnt->mnt_root)
2478 get_parent_attributes(exp, &stat);
2481 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2482 status = nfsd4_encode_security_label(rqstp, context,
2483 contextlen, &p, &buflen);
2487 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2489 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2490 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2491 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2494 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2499 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2501 security_release_secctx(context, contextlen);
2502 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2508 status = nfserrno(err);
2511 status = nfserr_resource;
2514 status = nfserr_serverfault;
2518 static inline int attributes_need_mount(u32 *bmval)
2520 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2522 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2528 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
2529 const char *name, int namlen, __be32 **p, int buflen)
2531 struct svc_export *exp = cd->rd_fhp->fh_export;
2532 struct dentry *dentry;
2534 int ignore_crossmnt = 0;
2536 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2538 return nfserrno(PTR_ERR(dentry));
2539 if (!dentry->d_inode) {
2541 * nfsd_buffered_readdir drops the i_mutex between
2542 * readdir and calling this callback, leaving a window
2543 * where this directory entry could have gone away.
2546 return nfserr_noent;
2551 * In the case of a mountpoint, the client may be asking for
2552 * attributes that are only properties of the underlying filesystem
2553 * as opposed to the cross-mounted file system. In such a case,
2554 * we will not follow the cross mount and will fill the attribtutes
2555 * directly from the mountpoint dentry.
2557 if (nfsd_mountpoint(dentry, exp)) {
2560 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2561 && !attributes_need_mount(cd->rd_bmval)) {
2562 ignore_crossmnt = 1;
2566 * Why the heck aren't we just using nfsd_lookup??
2567 * Different "."/".." handling? Something else?
2568 * At least, add a comment here to explain....
2570 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2572 nfserr = nfserrno(err);
2575 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2581 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
2582 cd->rd_rqstp, ignore_crossmnt);
2590 nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
2597 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2598 *p++ = htonl(0); /* bmval1 */
2601 *p++ = nfserr; /* no htonl */
2602 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2607 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2608 loff_t offset, u64 ino, unsigned int d_type)
2610 struct readdir_cd *ccd = ccdv;
2611 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2613 __be32 *p = cd->buffer;
2615 __be32 nfserr = nfserr_toosmall;
2617 /* In nfsv4, "." and ".." never make it onto the wire.. */
2618 if (name && isdotent(name, namlen)) {
2619 cd->common.err = nfs_ok;
2624 xdr_encode_hyper(cd->offset, (u64) offset);
2626 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2630 *p++ = xdr_one; /* mark entry present */
2632 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2633 p = xdr_encode_array(p, name, namlen); /* name length & name */
2635 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
2639 case nfserr_resource:
2640 nfserr = nfserr_toosmall;
2646 * If the client requested the RDATTR_ERROR attribute,
2647 * we stuff the error code into this attribute
2648 * and continue. If this attribute was not requested,
2649 * then in accordance with the spec, we fail the
2650 * entire READDIR operation(!)
2652 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2654 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
2656 nfserr = nfserr_toosmall;
2660 cd->buflen -= (p - cd->buffer);
2662 cd->offset = cookiep;
2664 cd->common.err = nfs_ok;
2667 cd->common.err = nfserr;
2672 nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2676 RESERVE_SPACE(sizeof(stateid_t));
2677 WRITE32(sid->si_generation);
2678 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2683 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
2689 WRITE32(access->ac_supported);
2690 WRITE32(access->ac_resp_access);
2696 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2701 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2702 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2704 /* Sorry, we do not yet support RDMA over 4.1: */
2712 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
2715 nfsd4_encode_stateid(resp, &close->cl_stateid);
2722 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
2727 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2728 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
2735 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
2741 write_cinfo(&p, &create->cr_cinfo);
2743 WRITE32(create->cr_bmval[0]);
2744 WRITE32(create->cr_bmval[1]);
2751 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
2753 struct svc_fh *fhp = getattr->ga_fhp;
2759 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2760 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2761 &resp->p, buflen, getattr->ga_bmval,
2767 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
2769 struct svc_fh *fhp = *fhpp;
2774 len = fhp->fh_handle.fh_size;
2775 RESERVE_SPACE(len + 4);
2777 WRITEMEM(&fhp->fh_handle.fh_base, len);
2784 * Including all fields other than the name, a LOCK4denied structure requires
2785 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2788 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2790 struct xdr_netobj *conf = &ld->ld_owner;
2793 RESERVE_SPACE(32 + XDR_LEN(conf->len));
2794 WRITE64(ld->ld_start);
2795 WRITE64(ld->ld_length);
2796 WRITE32(ld->ld_type);
2798 WRITEMEM(&ld->ld_clientid, 8);
2800 WRITEMEM(conf->data, conf->len);
2802 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2803 WRITE64((u64)0); /* clientid */
2804 WRITE32(0); /* length of owner name */
2810 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
2813 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2814 else if (nfserr == nfserr_denied)
2815 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2821 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
2823 if (nfserr == nfserr_denied)
2824 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2829 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
2832 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2839 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
2845 write_cinfo(&p, &link->li_cinfo);
2853 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
2860 nfsd4_encode_stateid(resp, &open->op_stateid);
2862 write_cinfo(&p, &open->op_cinfo);
2863 WRITE32(open->op_rflags);
2865 WRITE32(open->op_bmval[0]);
2866 WRITE32(open->op_bmval[1]);
2867 WRITE32(open->op_delegate_type);
2870 switch (open->op_delegate_type) {
2871 case NFS4_OPEN_DELEGATE_NONE:
2873 case NFS4_OPEN_DELEGATE_READ:
2874 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2876 WRITE32(open->op_recall);
2879 * TODO: ACE's in delegations
2881 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2884 WRITE32(0); /* XXX: is NULL principal ok? */
2887 case NFS4_OPEN_DELEGATE_WRITE:
2888 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2893 * TODO: space_limit's in delegations
2895 WRITE32(NFS4_LIMIT_SIZE);
2900 * TODO: ACE's in delegations
2902 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2905 WRITE32(0); /* XXX: is NULL principal ok? */
2908 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2909 switch (open->op_why_no_deleg) {
2910 case WND4_CONTENTION:
2913 WRITE32(open->op_why_no_deleg);
2914 WRITE32(0); /* deleg signaling not supported yet */
2918 WRITE32(open->op_why_no_deleg);
2925 /* XXX save filehandle here */
2931 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
2934 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
2940 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
2943 nfsd4_encode_stateid(resp, &od->od_stateid);
2949 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
2950 struct nfsd4_read *read)
2955 unsigned long maxcount;
2961 if (resp->xbuf->page_len)
2962 return nfserr_resource;
2964 RESERVE_SPACE(8); /* eof flag and byte count */
2966 maxcount = svc_max_payload(resp->rqstp);
2967 if (maxcount > read->rd_length)
2968 maxcount = read->rd_length;
2973 page = *(resp->rqstp->rq_next_page);
2974 if (!page) { /* ran out of pages */
2978 resp->rqstp->rq_vec[v].iov_base = page_address(page);
2979 resp->rqstp->rq_vec[v].iov_len =
2980 len < PAGE_SIZE ? len : PAGE_SIZE;
2981 resp->rqstp->rq_next_page++;
2987 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
2988 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
2993 eof = (read->rd_offset + maxcount >=
2994 read->rd_fhp->fh_dentry->d_inode->i_size);
2999 resp->xbuf->head[0].iov_len = (char*)p
3000 - (char*)resp->xbuf->head[0].iov_base;
3001 resp->xbuf->page_len = maxcount;
3003 /* Use rest of head for padding and remaining ops: */
3004 resp->xbuf->tail[0].iov_base = p;
3005 resp->xbuf->tail[0].iov_len = 0;
3009 resp->xbuf->tail[0].iov_base += maxcount&3;
3010 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3017 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
3025 if (resp->xbuf->page_len)
3026 return nfserr_resource;
3027 if (!*resp->rqstp->rq_next_page)
3028 return nfserr_resource;
3030 page = page_address(*(resp->rqstp->rq_next_page++));
3032 maxcount = PAGE_SIZE;
3036 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3037 * if they would overflow the buffer. Is this kosher in NFSv4? If
3038 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3039 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3041 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3042 if (nfserr == nfserr_isdir)
3043 return nfserr_inval;
3049 resp->xbuf->head[0].iov_len = (char*)p
3050 - (char*)resp->xbuf->head[0].iov_base;
3051 resp->xbuf->page_len = maxcount;
3053 /* Use rest of head for padding and remaining ops: */
3054 resp->xbuf->tail[0].iov_base = p;
3055 resp->xbuf->tail[0].iov_len = 0;
3059 resp->xbuf->tail[0].iov_base += maxcount&3;
3060 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3067 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3071 __be32 *page, *savep, *tailbase;
3076 if (resp->xbuf->page_len)
3077 return nfserr_resource;
3078 if (!*resp->rqstp->rq_next_page)
3079 return nfserr_resource;
3081 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
3084 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3088 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
3091 maxcount = PAGE_SIZE;
3092 if (maxcount > readdir->rd_maxcount)
3093 maxcount = readdir->rd_maxcount;
3096 * Convert from bytes to words, account for the two words already
3097 * written, make sure to leave two words at the end for the next
3098 * pointer and eof field.
3100 maxcount = (maxcount >> 2) - 4;
3102 nfserr = nfserr_toosmall;
3106 page = page_address(*(resp->rqstp->rq_next_page++));
3107 readdir->common.err = 0;
3108 readdir->buflen = maxcount;
3109 readdir->buffer = page;
3110 readdir->offset = NULL;
3112 offset = readdir->rd_cookie;
3113 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3115 &readdir->common, nfsd4_encode_dirent);
3116 if (nfserr == nfs_ok &&
3117 readdir->common.err == nfserr_toosmall &&
3118 readdir->buffer == page)
3119 nfserr = nfserr_toosmall;
3123 if (readdir->offset)
3124 xdr_encode_hyper(readdir->offset, offset);
3126 p = readdir->buffer;
3127 *p++ = 0; /* no more entries */
3128 *p++ = htonl(readdir->common.err == nfserr_eof);
3129 resp->xbuf->page_len = ((char*)p) -
3130 (char*)page_address(*(resp->rqstp->rq_next_page-1));
3132 /* Use rest of head for padding and remaining ops: */
3133 resp->xbuf->tail[0].iov_base = tailbase;
3134 resp->xbuf->tail[0].iov_len = 0;
3135 resp->p = resp->xbuf->tail[0].iov_base;
3136 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
3146 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3152 write_cinfo(&p, &remove->rm_cinfo);
3159 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3165 write_cinfo(&p, &rename->rn_sinfo);
3166 write_cinfo(&p, &rename->rn_tinfo);
3173 nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3174 __be32 nfserr, struct svc_export *exp)
3176 u32 i, nflavs, supported;
3177 struct exp_flavor_info *flavs;
3178 struct exp_flavor_info def_flavs[2];
3179 __be32 *p, *flavorsp;
3180 static bool report = true;
3184 if (exp->ex_nflavors) {
3185 flavs = exp->ex_flavors;
3186 nflavs = exp->ex_nflavors;
3187 } else { /* Handling of some defaults in absence of real secinfo: */
3189 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3191 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3192 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3193 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3195 flavs[0].pseudoflavor
3196 = svcauth_gss_flavor(exp->ex_client);
3199 flavs[0].pseudoflavor
3200 = exp->ex_client->flavour->flavour;
3206 flavorsp = p++; /* to be backfilled later */
3209 for (i = 0; i < nflavs; i++) {
3210 rpc_authflavor_t pf = flavs[i].pseudoflavor;
3211 struct rpcsec_gss_info info;
3213 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3215 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
3216 WRITE32(RPC_AUTH_GSS);
3217 WRITE32(info.oid.len);
3218 WRITEMEM(info.oid.data, info.oid.len);
3220 WRITE32(info.service);
3222 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3229 pr_warn("NFS: SECINFO: security flavor %u "
3230 "is not supported\n", pf);
3234 if (nflavs != supported)
3236 *flavorsp = htonl(supported);
3245 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3246 struct nfsd4_secinfo *secinfo)
3248 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3252 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3253 struct nfsd4_secinfo_no_name *secinfo)
3255 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3259 * The SETATTR encode routine is special -- it always encodes a bitmap,
3260 * regardless of the error status.
3263 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3276 WRITE32(setattr->sa_bmval[0]);
3277 WRITE32(setattr->sa_bmval[1]);
3278 WRITE32(setattr->sa_bmval[2]);
3285 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3290 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
3291 WRITEMEM(&scd->se_clientid, 8);
3292 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
3295 else if (nfserr == nfserr_clid_inuse) {
3305 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3311 WRITE32(write->wr_bytes_written);
3312 WRITE32(write->wr_how_written);
3313 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
3319 static const u32 nfs4_minimal_spo_must_enforce[2] = {
3320 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3321 1 << (OP_EXCHANGE_ID - 32) |
3322 1 << (OP_CREATE_SESSION - 32) |
3323 1 << (OP_DESTROY_SESSION - 32) |
3324 1 << (OP_DESTROY_CLIENTID - 32)
3328 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
3329 struct nfsd4_exchange_id *exid)
3335 int server_scope_sz;
3336 uint64_t minor_id = 0;
3341 major_id = utsname()->nodename;
3342 major_id_sz = strlen(major_id);
3343 server_scope = utsname()->nodename;
3344 server_scope_sz = strlen(server_scope);
3347 8 /* eir_clientid */ +
3348 4 /* eir_sequenceid */ +
3351 8 /* spo_must_enforce, spo_must_allow */ +
3352 8 /* so_minor_id */ +
3353 4 /* so_major_id.len */ +
3354 (XDR_QUADLEN(major_id_sz) * 4) +
3355 4 /* eir_server_scope.len */ +
3356 (XDR_QUADLEN(server_scope_sz) * 4) +
3357 4 /* eir_server_impl_id.count (0) */);
3359 WRITEMEM(&exid->clientid, 8);
3360 WRITE32(exid->seqid);
3361 WRITE32(exid->flags);
3363 WRITE32(exid->spa_how);
3364 switch (exid->spa_how) {
3368 /* spo_must_enforce bitmap: */
3370 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3371 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3372 /* empty spo_must_allow bitmap: */
3379 /* The server_owner struct */
3380 WRITE64(minor_id); /* Minor id */
3382 WRITE32(major_id_sz);
3383 WRITEMEM(major_id, major_id_sz);
3386 WRITE32(server_scope_sz);
3387 WRITEMEM(server_scope, server_scope_sz);
3389 /* Implementation id */
3390 WRITE32(0); /* zero length nfs_impl_id4 array */
3396 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3397 struct nfsd4_create_session *sess)
3405 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3406 WRITE32(sess->seqid);
3407 WRITE32(sess->flags);
3411 WRITE32(0); /* headerpadsz */
3412 WRITE32(sess->fore_channel.maxreq_sz);
3413 WRITE32(sess->fore_channel.maxresp_sz);
3414 WRITE32(sess->fore_channel.maxresp_cached);
3415 WRITE32(sess->fore_channel.maxops);
3416 WRITE32(sess->fore_channel.maxreqs);
3417 WRITE32(sess->fore_channel.nr_rdma_attrs);
3420 if (sess->fore_channel.nr_rdma_attrs) {
3422 WRITE32(sess->fore_channel.rdma_attrs);
3427 WRITE32(0); /* headerpadsz */
3428 WRITE32(sess->back_channel.maxreq_sz);
3429 WRITE32(sess->back_channel.maxresp_sz);
3430 WRITE32(sess->back_channel.maxresp_cached);
3431 WRITE32(sess->back_channel.maxops);
3432 WRITE32(sess->back_channel.maxreqs);
3433 WRITE32(sess->back_channel.nr_rdma_attrs);
3436 if (sess->back_channel.nr_rdma_attrs) {
3438 WRITE32(sess->back_channel.rdma_attrs);
3445 nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3446 struct nfsd4_destroy_session *destroy_session)
3452 nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3453 struct nfsd4_free_stateid *free_stateid)
3467 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
3468 struct nfsd4_sequence *seq)
3475 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3476 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3477 WRITE32(seq->seqid);
3478 WRITE32(seq->slotid);
3479 /* Note slotid's are numbered from zero: */
3480 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3481 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
3482 WRITE32(seq->status_flags);
3485 resp->cstate.datap = p; /* DRC cache data pointer */
3490 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3491 struct nfsd4_test_stateid *test_stateid)
3493 struct nfsd4_test_stateid_id *stateid, *next;
3496 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
3497 *p++ = htonl(test_stateid->ts_num_ids);
3499 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
3500 *p++ = stateid->ts_id_status;
3508 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3513 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3516 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3517 * since we don't need to filter out obsolete ops as this is
3518 * done in the decoding phase.
3520 static nfsd4_enc nfsd4_enc_ops[] = {
3521 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3522 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3523 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3524 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3525 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3526 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3527 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3528 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3529 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3530 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3531 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3532 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3533 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3534 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3535 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3536 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
3537 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
3538 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3539 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3540 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3541 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3542 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3543 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3544 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3545 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3546 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3547 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3548 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3549 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3550 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3551 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3552 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3553 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3554 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3555 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3556 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3557 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
3559 /* NFSv4.1 operations */
3560 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
3561 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
3562 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3563 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3564 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
3565 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
3566 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3567 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3568 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3569 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3570 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3571 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3572 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
3573 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3574 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
3575 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
3576 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3577 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3578 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
3582 * Calculate the total amount of memory that the compound response has taken
3583 * after encoding the current operation with pad.
3585 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3586 * which was specified at nfsd4_operation, else pad is zero.
3588 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
3590 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3591 * will be at least a page and will therefore hold the xdr_buf head.
3593 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
3595 struct xdr_buf *xb = &resp->rqstp->rq_res;
3596 struct nfsd4_session *session = NULL;
3597 struct nfsd4_slot *slot = resp->cstate.slot;
3598 u32 length, tlen = 0;
3600 if (!nfsd4_has_session(&resp->cstate))
3603 session = resp->cstate.session;
3604 if (session == NULL)
3607 if (xb->page_len == 0) {
3608 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3610 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3611 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3613 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3615 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3616 length, xb->page_len, tlen, pad);
3618 if (length > session->se_fchannel.maxresp_sz)
3619 return nfserr_rep_too_big;
3621 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
3622 length > session->se_fchannel.maxresp_cached)
3623 return nfserr_rep_too_big_to_cache;
3629 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3631 struct nfs4_stateowner *so = resp->cstate.replay_owner;
3637 statp = p++; /* to be backfilled at the end */
3640 if (op->opnum == OP_ILLEGAL)
3642 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3643 !nfsd4_enc_ops[op->opnum]);
3644 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
3645 /* nfsd4_check_drc_limit guarantees enough room for error status */
3647 op->status = nfsd4_check_resp_size(resp, 0);
3649 so->so_replay.rp_status = op->status;
3650 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3651 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3655 * Note: We write the status directly, instead of using WRITE32(),
3656 * since it is already in network byte order.
3658 *statp = op->status;
3662 * Encode the reply stored in the stateowner reply cache
3664 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3665 * previously sent already encoded operation.
3667 * called with nfs4_lock_state() held
3670 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3673 struct nfs4_replay *rp = op->replay;
3679 *p++ = rp->rp_status; /* already xdr'ed */
3682 RESERVE_SPACE(rp->rp_buflen);
3683 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3688 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
3690 return xdr_ressize_check(rqstp, p);
3693 int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
3695 struct svc_rqst *rqstp = rq;
3696 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3698 if (args->ops != args->iops) {
3700 args->ops = args->iops;
3704 while (args->to_free) {
3705 struct tmpbuf *tb = args->to_free;
3706 args->to_free = tb->next;
3707 tb->release(tb->buf);
3714 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
3717 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3718 args->pagelist = rqstp->rq_arg.pages;
3719 args->pagelen = rqstp->rq_arg.page_len;
3721 args->to_free = NULL;
3722 args->ops = args->iops;
3723 args->rqstp = rqstp;
3725 return !nfsd4_decode_compound(args);
3729 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
3732 * All that remains is to write the tag and operation count...
3734 struct nfsd4_compound_state *cs = &resp->cstate;
3737 *p++ = htonl(resp->taglen);
3738 memcpy(p, resp->tag, resp->taglen);
3739 p += XDR_QUADLEN(resp->taglen);
3740 *p++ = htonl(resp->opcnt);
3742 if (rqstp->rq_res.page_len)
3743 iov = &rqstp->rq_res.tail[0];
3745 iov = &rqstp->rq_res.head[0];
3746 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3747 BUG_ON(iov->iov_len > PAGE_SIZE);
3748 if (nfsd4_has_session(cs)) {
3749 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3750 struct nfs4_client *clp = cs->session->se_client;
3751 if (cs->status != nfserr_replay_cache) {
3752 nfsd4_store_cache_entry(resp);
3753 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3755 /* Renew the clientid on success and on replay */
3756 spin_lock(&nn->client_lock);
3757 nfsd4_put_session(cs->session);
3758 spin_unlock(&nn->client_lock);
3759 put_client_renew(clp);