4 * Server-side XDR for NFSv4
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * TODO: Neil Brown made the following observation: We currently
38 * initially reserve NFSD_BUFSIZE space on the transmit queue and
39 * never release any of that until the request is complete.
40 * It would be good to calculate a new maximum response size while
41 * decoding the COMPOUND, and call svc_reserve with this number
42 * at the end of nfs4svc_decode_compoundargs.
45 #include <linux/param.h>
46 #include <linux/smp.h>
47 #include <linux/smp_lock.h>
49 #include <linux/namei.h>
50 #include <linux/vfs.h>
51 #include <linux/sunrpc/xdr.h>
52 #include <linux/sunrpc/svc.h>
53 #include <linux/sunrpc/clnt.h>
54 #include <linux/nfsd/nfsd.h>
55 #include <linux/nfsd/state.h>
56 #include <linux/nfsd/xdr4.h>
57 #include <linux/nfsd_idmap.h>
58 #include <linux/vserver/xid.h>
60 #define NFSDDBG_FACILITY NFSDDBG_XDR
62 static const char utf8_byte_len[256] = {
63 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
64 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
65 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
66 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
67 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
68 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
69 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
70 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4,5,5,5,5,6,6,0,0
74 is_legal_utf8_sequence(unsigned char *source, int length)
79 if (length==1) return 1;
81 /* Check for overlong sequence, and check second byte */
84 case 0xE0: /* 3 bytes */
85 if ( c < 0xA0 ) return 0;
87 case 0xF0: /* 4 bytes */
88 if ( c < 0x90 ) return 0;
90 case 0xF8: /* 5 bytes */
91 if ( c < 0xC8 ) return 0;
93 case 0xFC: /* 6 bytes */
94 if ( c < 0x84 ) return 0;
97 if ( (c & 0xC0) != 0x80) return 0;
100 /* Check that trailing bytes look like 10xxxxxx */
101 for (ptr = source++ + length - 1; ptr>source; ptr--)
102 if ( ((*ptr) & 0xC0) != 0x80 ) return 0;
106 /* This does some screening on disallowed unicode characters. It is NOT
110 is_allowed_utf8_char(unsigned char *source, int length)
112 /* We assume length and source point to a valid utf8 sequence */
115 /* Disallow F0000 and up (in utf8, F3B08080) */
116 if (*source > 0xF3 ) return 0;
120 if (c >= 0xB0) return 0;
122 /* Disallow D800-F8FF (in utf8, EDA080-EFA3BF */
124 if (c >= 0xA0) return 0;
130 if (c <= 0xA3) return 0;
131 /* Disallow FFF9-FFFF (EFBFB9-EFBFBF) */
133 /* Don't need to check <=0xBF, since valid utf8 */
134 if ( *(source+2) >= 0xB9) return 0;
140 /* This routine should really check to see that the proper stringprep
141 * mappings have been applied. Instead, we do a simple screen of some
142 * of the more obvious illegal values by calling is_allowed_utf8_char.
143 * This will allow many illegal strings through, but if a client behaves,
144 * it will get full functionality. The other option (apart from full
145 * stringprep checking) is to limit everything to an easily handled subset,
146 * such as 7-bit ascii.
148 * Note - currently calling routines ignore return value except as boolean.
151 check_utf8(char *str, int len)
153 unsigned char *chunk, *sourceend;
157 sourceend = str + len;
159 while (chunk < sourceend) {
160 chunklen = utf8_byte_len[*chunk];
163 if (chunk + chunklen > sourceend)
165 if (!is_legal_utf8_sequence(chunk, chunklen))
167 if (!is_allowed_utf8_char(chunk, chunklen))
169 if ( (chunklen==1) && (!*chunk) )
170 return nfserr_inval; /* Disallow embedded nulls */
178 check_filename(char *str, int len, int err)
184 if (isdotent(str, len))
186 for (i = 0; i < len; i++)
189 return check_utf8(str, len);
193 * START OF "GENERIC" DECODE ROUTINES.
194 * These may look a little ugly since they are imported from a "generic"
195 * set of XDR encode/decode routines which are intended to be shared by
196 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
198 * If the pain of reading these is too great, it should be a straightforward
199 * task to translate them into Linux-specific versions which are more
200 * consistent with the style used in NFSv2/v3...
202 #define DECODE_HEAD \
205 #define DECODE_TAIL \
210 printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
211 status = nfserr_bad_xdr; \
214 #define READ32(x) (x) = ntohl(*p++)
215 #define READ64(x) do { \
216 (x) = (u64)ntohl(*p++) << 32; \
217 (x) |= ntohl(*p++); \
219 #define READTIME(x) do { \
224 #define READMEM(x,nbytes) do { \
226 p += XDR_QUADLEN(nbytes); \
228 #define SAVEMEM(x,nbytes) do { \
229 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
230 savemem(argp, p, nbytes) : \
232 printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
235 p += XDR_QUADLEN(nbytes); \
237 #define COPYMEM(x,nbytes) do { \
238 memcpy((x), p, nbytes); \
239 p += XDR_QUADLEN(nbytes); \
242 /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
243 #define READ_BUF(nbytes) do { \
244 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
246 argp->p += XDR_QUADLEN(nbytes); \
247 } else if (!(p = read_buf(argp, nbytes))) { \
248 printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
253 u32 *read_buf(struct nfsd4_compoundargs *argp, int nbytes)
255 /* We want more bytes than seem to be available.
256 * Maybe we need a new page, maybe we have just run out
258 int avail = (char*)argp->end - (char*)argp->p;
260 if (avail + argp->pagelen < nbytes)
262 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
264 /* ok, we can do it with the current plus the next page */
265 if (nbytes <= sizeof(argp->tmp))
270 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
275 memcpy(p, argp->p, avail);
276 /* step to next page */
277 argp->p = page_address(argp->pagelist[0]);
279 if (argp->pagelen < PAGE_SIZE) {
280 argp->end = p + (argp->pagelen>>2);
283 argp->end = p + (PAGE_SIZE>>2);
284 argp->pagelen -= PAGE_SIZE;
286 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
287 argp->p += XDR_QUADLEN(nbytes - avail);
291 char *savemem(struct nfsd4_compoundargs *argp, u32 *p, int nbytes)
294 if (p == argp->tmp) {
295 p = kmalloc(nbytes, GFP_KERNEL);
297 memcpy(p, argp->tmp, nbytes);
303 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
309 tb->next = argp->to_free;
316 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
329 READ_BUF(bmlen << 2);
339 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, struct iattr *iattr)
341 int expected_len, len = 0;
347 if ((status = nfsd4_decode_bitmap(argp, bmval)))
351 * According to spec, unsupported attributes return ERR_NOTSUPP;
352 * read-only attributes return ERR_INVAL.
354 if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
355 return nfserr_attrnotsupp;
356 if ((bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0) || (bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1))
360 READ32(expected_len);
362 if (bmval[0] & FATTR4_WORD0_SIZE) {
365 READ64(iattr->ia_size);
366 iattr->ia_valid |= ATTR_SIZE;
368 if (bmval[1] & FATTR4_WORD1_MODE) {
371 READ32(iattr->ia_mode);
372 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
373 iattr->ia_valid |= ATTR_MODE;
375 if (bmval[1] & FATTR4_WORD1_OWNER) {
380 len += (XDR_QUADLEN(dummy32) << 2);
381 READMEM(buf, dummy32);
382 if (check_utf8(buf, dummy32))
384 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
386 iattr->ia_valid |= ATTR_UID;
388 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
393 len += (XDR_QUADLEN(dummy32) << 2);
394 READMEM(buf, dummy32);
395 if (check_utf8(buf, dummy32))
397 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
399 iattr->ia_valid |= ATTR_GID;
401 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
406 case NFS4_SET_TO_CLIENT_TIME:
407 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
408 all 32 bits of 'nseconds'. */
414 READ32(iattr->ia_atime.tv_sec);
415 READ32(iattr->ia_atime.tv_nsec);
416 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
418 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
420 case NFS4_SET_TO_SERVER_TIME:
421 iattr->ia_valid |= ATTR_ATIME;
427 if (bmval[1] & FATTR4_WORD1_TIME_METADATA) {
428 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
429 all 32 bits of 'nseconds'. */
435 READ32(iattr->ia_ctime.tv_sec);
436 READ32(iattr->ia_ctime.tv_nsec);
437 if (iattr->ia_ctime.tv_nsec >= (u32)1000000000)
439 iattr->ia_valid |= ATTR_CTIME;
441 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
446 case NFS4_SET_TO_CLIENT_TIME:
447 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
448 all 32 bits of 'nseconds'. */
454 READ32(iattr->ia_mtime.tv_sec);
455 READ32(iattr->ia_mtime.tv_nsec);
456 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
458 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
460 case NFS4_SET_TO_SERVER_TIME:
461 iattr->ia_valid |= ATTR_MTIME;
467 if (len != expected_len)
473 status = nfserrno(status);
478 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
483 READ32(access->ac_req_access);
488 #define NFS4_STATE_NOT_LOCKED ((void *)-1)
491 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
495 close->cl_stateowner = NFS4_STATE_NOT_LOCKED;
496 READ_BUF(4 + sizeof(stateid_t));
497 READ32(close->cl_seqid);
498 READ32(close->cl_stateid.si_generation);
499 COPYMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
506 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
511 READ64(commit->co_offset);
512 READ32(commit->co_count);
518 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
523 READ32(create->cr_type);
524 switch (create->cr_type) {
527 READ32(create->cr_linklen);
528 READ_BUF(create->cr_linklen);
529 SAVEMEM(create->cr_linkname, create->cr_linklen);
530 if (check_utf8(create->cr_linkname, create->cr_linklen))
536 READ32(create->cr_specdata1);
537 READ32(create->cr_specdata2);
547 READ32(create->cr_namelen);
548 READ_BUF(create->cr_namelen);
549 SAVEMEM(create->cr_name, create->cr_namelen);
550 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
553 if ((status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr)))
560 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
562 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
566 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
571 READ32(link->li_namelen);
572 READ_BUF(link->li_namelen);
573 SAVEMEM(link->li_name, link->li_namelen);
574 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
581 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
585 lock->lk_stateowner = NFS4_STATE_NOT_LOCKED;
587 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
590 READ32(lock->lk_type);
591 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
593 READ32(lock->lk_reclaim);
594 READ64(lock->lk_offset);
595 READ64(lock->lk_length);
596 READ32(lock->lk_is_new);
598 if (lock->lk_is_new) {
600 READ32(lock->lk_new_open_seqid);
601 READ32(lock->lk_new_open_stateid.si_generation);
603 COPYMEM(&lock->lk_new_open_stateid.si_opaque, sizeof(stateid_opaque_t));
604 READ32(lock->lk_new_lock_seqid);
605 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
606 READ32(lock->lk_new_owner.len);
607 READ_BUF(lock->lk_new_owner.len);
608 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
611 READ32(lock->lk_old_lock_stateid.si_generation);
612 COPYMEM(&lock->lk_old_lock_stateid.si_opaque, sizeof(stateid_opaque_t));
613 READ32(lock->lk_old_lock_seqid);
620 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
625 READ32(lockt->lt_type);
626 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
628 READ64(lockt->lt_offset);
629 READ64(lockt->lt_length);
630 COPYMEM(&lockt->lt_clientid, 8);
631 READ32(lockt->lt_owner.len);
632 READ_BUF(lockt->lt_owner.len);
633 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
639 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
643 locku->lu_stateowner = NFS4_STATE_NOT_LOCKED;
644 READ_BUF(24 + sizeof(stateid_t));
645 READ32(locku->lu_type);
646 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
648 READ32(locku->lu_seqid);
649 READ32(locku->lu_stateid.si_generation);
650 COPYMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
651 READ64(locku->lu_offset);
652 READ64(locku->lu_length);
658 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
663 READ32(lookup->lo_len);
664 READ_BUF(lookup->lo_len);
665 SAVEMEM(lookup->lo_name, lookup->lo_len);
666 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
673 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
677 memset(open->op_bmval, 0, sizeof(open->op_bmval));
678 open->op_iattr.ia_valid = 0;
679 open->op_stateowner = NFS4_STATE_NOT_LOCKED;
681 /* seqid, share_access, share_deny, clientid, ownerlen */
682 READ_BUF(16 + sizeof(clientid_t));
683 READ32(open->op_seqid);
684 READ32(open->op_share_access);
685 READ32(open->op_share_deny);
686 COPYMEM(&open->op_clientid, sizeof(clientid_t));
687 READ32(open->op_owner.len);
689 /* owner, open_flag */
690 READ_BUF(open->op_owner.len + 4);
691 SAVEMEM(open->op_owner.data, open->op_owner.len);
692 READ32(open->op_create);
693 switch (open->op_create) {
694 case NFS4_OPEN_NOCREATE:
696 case NFS4_OPEN_CREATE:
698 READ32(open->op_createmode);
699 switch (open->op_createmode) {
700 case NFS4_CREATE_UNCHECKED:
701 case NFS4_CREATE_GUARDED:
702 if ((status = nfsd4_decode_fattr(argp, open->op_bmval, &open->op_iattr)))
705 case NFS4_CREATE_EXCLUSIVE:
707 COPYMEM(open->op_verf.data, 8);
719 READ32(open->op_claim_type);
720 switch (open->op_claim_type) {
721 case NFS4_OPEN_CLAIM_NULL:
722 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
724 READ32(open->op_fname.len);
725 READ_BUF(open->op_fname.len);
726 SAVEMEM(open->op_fname.data, open->op_fname.len);
727 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
730 case NFS4_OPEN_CLAIM_PREVIOUS:
732 READ32(open->op_delegate_type);
734 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
735 READ_BUF(sizeof(delegation_stateid_t) + 4);
736 COPYMEM(&open->op_delegate_stateid, sizeof(delegation_stateid_t));
737 READ32(open->op_fname.len);
738 READ_BUF(open->op_fname.len);
739 SAVEMEM(open->op_fname.data, open->op_fname.len);
740 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
751 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
755 open_conf->oc_stateowner = NFS4_STATE_NOT_LOCKED;
756 READ_BUF(4 + sizeof(stateid_t));
757 READ32(open_conf->oc_req_stateid.si_generation);
758 COPYMEM(&open_conf->oc_req_stateid.si_opaque, sizeof(stateid_opaque_t));
759 READ32(open_conf->oc_seqid);
765 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
769 open_down->od_stateowner = NFS4_STATE_NOT_LOCKED;
770 READ_BUF(4 + sizeof(stateid_t));
771 READ32(open_down->od_stateid.si_generation);
772 COPYMEM(&open_down->od_stateid.si_opaque, sizeof(stateid_opaque_t));
773 READ32(open_down->od_seqid);
774 READ32(open_down->od_share_access);
775 READ32(open_down->od_share_deny);
781 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
786 READ32(putfh->pf_fhlen);
787 if (putfh->pf_fhlen > NFS4_FHSIZE)
789 READ_BUF(putfh->pf_fhlen);
790 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
796 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
800 READ_BUF(sizeof(stateid_t) + 12);
801 READ32(read->rd_stateid.si_generation);
802 COPYMEM(&read->rd_stateid.si_opaque, sizeof(stateid_opaque_t));
803 READ64(read->rd_offset);
804 READ32(read->rd_length);
810 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
815 READ64(readdir->rd_cookie);
816 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
817 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
818 READ32(readdir->rd_maxcount);
819 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
826 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
831 READ32(remove->rm_namelen);
832 READ_BUF(remove->rm_namelen);
833 SAVEMEM(remove->rm_name, remove->rm_namelen);
834 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
841 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
846 READ32(rename->rn_snamelen);
847 READ_BUF(rename->rn_snamelen + 4);
848 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
849 READ32(rename->rn_tnamelen);
850 READ_BUF(rename->rn_tnamelen);
851 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
852 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
854 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
861 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
865 READ_BUF(sizeof(clientid_t));
866 COPYMEM(clientid, sizeof(clientid_t));
872 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
876 READ_BUF(sizeof(stateid_t));
877 READ32(setattr->sa_stateid.si_generation);
878 COPYMEM(&setattr->sa_stateid.si_opaque, sizeof(stateid_opaque_t));
879 if ((status = nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr)))
886 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
891 COPYMEM(setclientid->se_verf.data, 8);
892 READ32(setclientid->se_namelen);
894 READ_BUF(setclientid->se_namelen + 8);
895 SAVEMEM(setclientid->se_name, setclientid->se_namelen);
896 READ32(setclientid->se_callback_prog);
897 READ32(setclientid->se_callback_netid_len);
899 READ_BUF(setclientid->se_callback_netid_len + 4);
900 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
901 READ32(setclientid->se_callback_addr_len);
903 READ_BUF(setclientid->se_callback_addr_len + 4);
904 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
905 READ32(setclientid->se_callback_ident);
911 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
915 READ_BUF(8 + sizeof(nfs4_verifier));
916 COPYMEM(&scd_c->sc_clientid, 8);
917 COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
922 /* Also used for NVERIFY */
924 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
927 struct nfsd4_compoundargs save = {
930 .rqstp = argp->rqstp,
933 struct iattr ve_iattr; /* request */
934 struct nfs4_acl *ve_acl; /* request */
938 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
941 /* For convenience's sake, we compare raw xdr'd attributes in
942 * nfsd4_proc_verify; however we still decode here just to return
943 * correct error in case of bad xdr. */
945 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
946 if (status == nfserr_inval) {
947 status = nfserrno(status);
952 READ32(verify->ve_attrlen);
953 READ_BUF(verify->ve_attrlen);
954 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
960 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
967 READ_BUF(sizeof(stateid_opaque_t) + 20);
968 READ32(write->wr_stateid.si_generation);
969 COPYMEM(&write->wr_stateid.si_opaque, sizeof(stateid_opaque_t));
970 READ64(write->wr_offset);
971 READ32(write->wr_stable_how);
972 if (write->wr_stable_how > 2)
974 READ32(write->wr_buflen);
976 /* Sorry .. no magic macros for this.. *
977 * READ_BUF(write->wr_buflen);
978 * SAVEMEM(write->wr_buf, write->wr_buflen);
980 avail = (char*)argp->end - (char*)argp->p;
981 if (avail + argp->pagelen < write->wr_buflen) {
982 printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__);
985 write->wr_vec[0].iov_base = p;
986 write->wr_vec[0].iov_len = avail;
988 len = write->wr_buflen;
989 while (len > write->wr_vec[v].iov_len) {
990 len -= write->wr_vec[v].iov_len;
992 write->wr_vec[v].iov_base = page_address(argp->pagelist[0]);
994 if (argp->pagelen >= PAGE_SIZE) {
995 write->wr_vec[v].iov_len = PAGE_SIZE;
996 argp->pagelen -= PAGE_SIZE;
998 write->wr_vec[v].iov_len = argp->pagelen;
999 argp->pagelen -= len;
1002 argp->end = (u32*) (write->wr_vec[v].iov_base + write->wr_vec[v].iov_len);
1003 argp->p = (u32*) (write->wr_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
1004 write->wr_vec[v].iov_len = len;
1005 write->wr_vlen = v+1;
1011 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1016 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1017 READ32(rlockowner->rl_owner.len);
1018 READ_BUF(rlockowner->rl_owner.len);
1019 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1025 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1028 struct nfsd4_op *op;
1032 * XXX: According to spec, we should check the tag
1033 * for UTF-8 compliance. I'm postponing this for
1034 * now because it seems that some clients do use
1038 READ32(argp->taglen);
1039 READ_BUF(argp->taglen + 8);
1040 SAVEMEM(argp->tag, argp->taglen);
1041 READ32(argp->minorversion);
1042 READ32(argp->opcnt);
1044 if (argp->taglen > NFSD4_MAX_TAGLEN)
1046 if (argp->opcnt > 100)
1049 if (argp->opcnt > sizeof(argp->iops)/sizeof(argp->iops[0])) {
1050 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1052 argp->ops = argp->iops;
1053 printk(KERN_INFO "nfsd: couldn't allocate room for COMPOUND\n");
1058 for (i = 0; i < argp->opcnt; i++) {
1063 * We can't use READ_BUF() here because we need to handle
1064 * a missing opcode as an OP_WRITE + 1. So we need to check
1065 * to see if we're truly at the end of our buffer or if there
1066 * is another page we need to flip to.
1069 if (argp->p == argp->end) {
1070 if (argp->pagelen < 4) {
1071 /* There isn't an opcode still on the wire */
1072 op->opnum = OP_WRITE + 1;
1073 op->status = nfserr_bad_xdr;
1079 * False alarm. We just hit a page boundary, but there
1080 * is still data available. Move pointer across page
1081 * boundary. *snip from READ_BUF*
1083 argp->p = page_address(argp->pagelist[0]);
1085 if (argp->pagelen < PAGE_SIZE) {
1086 argp->end = p + (argp->pagelen>>2);
1089 argp->end = p + (PAGE_SIZE>>2);
1090 argp->pagelen -= PAGE_SIZE;
1093 op->opnum = ntohl(*argp->p++);
1095 switch (op->opnum) {
1096 case 2: /* Reserved operation */
1097 op->opnum = OP_ILLEGAL;
1098 if (argp->minorversion == 0)
1099 op->status = nfserr_op_illegal;
1101 op->status = nfserr_minor_vers_mismatch;
1104 op->status = nfsd4_decode_access(argp, &op->u.access);
1107 op->status = nfsd4_decode_close(argp, &op->u.close);
1110 op->status = nfsd4_decode_commit(argp, &op->u.commit);
1113 op->status = nfsd4_decode_create(argp, &op->u.create);
1116 op->status = nfsd4_decode_getattr(argp, &op->u.getattr);
1119 op->status = nfs_ok;
1122 op->status = nfsd4_decode_link(argp, &op->u.link);
1125 op->status = nfsd4_decode_lock(argp, &op->u.lock);
1128 op->status = nfsd4_decode_lockt(argp, &op->u.lockt);
1131 op->status = nfsd4_decode_locku(argp, &op->u.locku);
1134 op->status = nfsd4_decode_lookup(argp, &op->u.lookup);
1137 op->status = nfs_ok;
1140 op->status = nfsd4_decode_verify(argp, &op->u.nverify);
1143 op->status = nfsd4_decode_open(argp, &op->u.open);
1145 case OP_OPEN_CONFIRM:
1146 op->status = nfsd4_decode_open_confirm(argp, &op->u.open_confirm);
1148 case OP_OPEN_DOWNGRADE:
1149 op->status = nfsd4_decode_open_downgrade(argp, &op->u.open_downgrade);
1152 op->status = nfsd4_decode_putfh(argp, &op->u.putfh);
1155 op->status = nfs_ok;
1158 op->status = nfsd4_decode_read(argp, &op->u.read);
1161 op->status = nfsd4_decode_readdir(argp, &op->u.readdir);
1164 op->status = nfs_ok;
1167 op->status = nfsd4_decode_remove(argp, &op->u.remove);
1170 op->status = nfsd4_decode_rename(argp, &op->u.rename);
1173 op->status = nfs_ok;
1176 op->status = nfsd4_decode_renew(argp, &op->u.renew);
1179 op->status = nfs_ok;
1182 op->status = nfsd4_decode_setattr(argp, &op->u.setattr);
1184 case OP_SETCLIENTID:
1185 op->status = nfsd4_decode_setclientid(argp, &op->u.setclientid);
1187 case OP_SETCLIENTID_CONFIRM:
1188 op->status = nfsd4_decode_setclientid_confirm(argp, &op->u.setclientid_confirm);
1191 op->status = nfsd4_decode_verify(argp, &op->u.verify);
1194 op->status = nfsd4_decode_write(argp, &op->u.write);
1196 case OP_RELEASE_LOCKOWNER:
1197 op->status = nfsd4_decode_release_lockowner(argp, &op->u.release_lockowner);
1200 op->opnum = OP_ILLEGAL;
1201 op->status = nfserr_op_illegal;
1214 * END OF "GENERIC" DECODE ROUTINES.
1218 * START OF "GENERIC" ENCODE ROUTINES.
1219 * These may look a little ugly since they are imported from a "generic"
1220 * set of XDR encode/decode routines which are intended to be shared by
1221 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
1223 * If the pain of reading these is too great, it should be a straightforward
1224 * task to translate them into Linux-specific versions which are more
1225 * consistent with the style used in NFSv2/v3...
1227 #define ENCODE_HEAD u32 *p
1229 #define WRITE32(n) *p++ = htonl(n)
1230 #define WRITE64(n) do { \
1231 *p++ = htonl((u32)((n) >> 32)); \
1232 *p++ = htonl((u32)(n)); \
1234 #define WRITEMEM(ptr,nbytes) do { \
1235 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1236 memcpy(p, ptr, nbytes); \
1237 p += XDR_QUADLEN(nbytes); \
1239 #define WRITECINFO(c) do { \
1240 *p++ = htonl(c.atomic); \
1241 *p++ = htonl(c.before_ctime_sec); \
1242 *p++ = htonl(c.before_ctime_nsec); \
1243 *p++ = htonl(c.after_ctime_sec); \
1244 *p++ = htonl(c.after_ctime_nsec); \
1247 #define RESERVE_SPACE(nbytes) do { \
1249 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1251 #define ADJUST_ARGS() resp->p = p
1254 * Header routine to setup seqid operation replay cache
1256 #define ENCODE_SEQID_OP_HEAD \
1263 * Routine for encoding the result of a
1264 * "seqid-mutating" NFSv4 operation. This is
1265 * where seqids are incremented, and the
1266 * replay cache is filled.
1269 #define ENCODE_SEQID_OP_TAIL(stateowner) do { \
1270 if (seqid_mutating_err(nfserr) && stateowner \
1271 && (stateowner != NFS4_STATE_NOT_LOCKED)) { \
1272 if (stateowner->so_confirmed) \
1273 stateowner->so_seqid++; \
1274 stateowner->so_replay.rp_status = nfserr; \
1275 stateowner->so_replay.rp_buflen = \
1276 (((char *)(resp)->p - (char *)save)); \
1277 memcpy(stateowner->so_replay.rp_buf, save, \
1278 stateowner->so_replay.rp_buflen); \
1280 if (stateowner != NFS4_STATE_NOT_LOCKED) \
1281 nfs4_unlock_state(); \
1285 static u32 nfs4_ftypes[16] = {
1286 NF4BAD, NF4FIFO, NF4CHR, NF4BAD,
1287 NF4DIR, NF4BAD, NF4BLK, NF4BAD,
1288 NF4REG, NF4BAD, NF4LNK, NF4BAD,
1289 NF4SOCK, NF4BAD, NF4LNK, NF4BAD,
1295 return 3 - ((l - 1) & 3); /* smallest i>=0 such that (l+i)%4 = 0 */
1299 nfsd4_encode_name(struct svc_rqst *rqstp, int group, uid_t id,
1300 u32 **p, int *buflen)
1305 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1306 return nfserr_resource;
1308 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1310 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1312 return nfserrno(status);
1313 len = (unsigned)status;
1314 *(*p)++ = htonl(len);
1315 memset((u8 *)*p + len, 0, xdr_padding(len));
1316 *p += XDR_QUADLEN(len);
1317 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
1318 BUG_ON(*buflen < 0);
1323 nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, u32 **p, int *buflen)
1325 return nfsd4_encode_name(rqstp, uid, 0, p, buflen);
1329 nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, u32 **p, int *buflen)
1331 return nfsd4_encode_name(rqstp, gid, 1, p, buflen);
1336 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1339 * @countp is the buffer size in _words_; upon successful return this becomes
1340 * replaced with the number of words written.
1343 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
1344 struct dentry *dentry, u32 *buffer, int *countp, u32 *bmval,
1345 struct svc_rqst *rqstp)
1347 u32 bmval0 = bmval[0];
1348 u32 bmval1 = bmval[1];
1350 struct svc_fh tempfh;
1351 struct kstatfs statfs;
1352 int buflen = *countp << 2;
1359 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
1360 BUG_ON(bmval0 & ~NFSD_SUPPORTED_ATTRS_WORD0);
1361 BUG_ON(bmval1 & ~NFSD_SUPPORTED_ATTRS_WORD1);
1363 status = vfs_getattr(exp->ex_mnt, dentry, &stat);
1366 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL)) ||
1367 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
1368 FATTR4_WORD1_SPACE_TOTAL))) {
1369 status = vfs_statfs(dentry->d_inode->i_sb, &statfs);
1373 if ((bmval0 & FATTR4_WORD0_FILEHANDLE) && !fhp) {
1374 fh_init(&tempfh, NFS4_FHSIZE);
1375 status = fh_compose(&tempfh, exp, dentry, NULL);
1380 if ((buflen -= 16) < 0)
1386 attrlenp = p++; /* to be backfilled later */
1388 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
1389 if ((buflen -= 12) < 0)
1392 WRITE32(NFSD_SUPPORTED_ATTRS_WORD0);
1393 WRITE32(NFSD_SUPPORTED_ATTRS_WORD1);
1395 if (bmval0 & FATTR4_WORD0_TYPE) {
1396 if ((buflen -= 4) < 0)
1398 dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
1399 if (dummy == NF4BAD)
1400 goto out_serverfault;
1403 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
1404 if ((buflen -= 4) < 0)
1406 WRITE32( NFS4_FH_NOEXPIRE_WITH_OPEN | NFS4_FH_VOL_RENAME );
1408 if (bmval0 & FATTR4_WORD0_CHANGE) {
1410 * Note: This _must_ be consistent with the scheme for writing
1411 * change_info, so any changes made here must be reflected there
1412 * as well. (See xdr4.h:set_change_info() and the WRITECINFO()
1415 if ((buflen -= 8) < 0)
1417 WRITE32(stat.ctime.tv_sec);
1418 WRITE32(stat.ctime.tv_nsec);
1420 if (bmval0 & FATTR4_WORD0_SIZE) {
1421 if ((buflen -= 8) < 0)
1425 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
1426 if ((buflen -= 4) < 0)
1430 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
1431 if ((buflen -= 4) < 0)
1435 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
1436 if ((buflen -= 4) < 0)
1440 if (bmval0 & FATTR4_WORD0_FSID) {
1441 if ((buflen -= 16) < 0)
1444 WRITE32(MAJOR(stat.dev));
1446 WRITE32(MINOR(stat.dev));
1448 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
1449 if ((buflen -= 4) < 0)
1453 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
1454 if ((buflen -= 4) < 0)
1456 WRITE32(NFSD_LEASE_TIME);
1458 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
1459 if ((buflen -= 4) < 0)
1463 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
1464 if ((buflen -= 4) < 0)
1468 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
1469 if ((buflen -= 4) < 0)
1473 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
1474 if ((buflen -= 4) < 0)
1478 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
1479 if ((buflen -= 4) < 0)
1483 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
1484 if ((buflen -= 4) < 0)
1488 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
1489 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
1492 WRITE32(fhp->fh_handle.fh_size);
1493 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
1495 if (bmval0 & FATTR4_WORD0_FILEID) {
1496 if ((buflen -= 8) < 0)
1498 WRITE64((u64) stat.ino);
1500 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
1501 if ((buflen -= 8) < 0)
1503 WRITE64((u64) statfs.f_ffree);
1505 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
1506 if ((buflen -= 8) < 0)
1508 WRITE64((u64) statfs.f_ffree);
1510 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
1511 if ((buflen -= 8) < 0)
1513 WRITE64((u64) statfs.f_files);
1515 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
1516 if ((buflen -= 4) < 0)
1520 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
1521 if ((buflen -= 8) < 0)
1525 if (bmval0 & FATTR4_WORD0_MAXLINK) {
1526 if ((buflen -= 4) < 0)
1530 if (bmval0 & FATTR4_WORD0_MAXNAME) {
1531 if ((buflen -= 4) < 0)
1535 if (bmval0 & FATTR4_WORD0_MAXREAD) {
1536 if ((buflen -= 8) < 0)
1538 WRITE64((u64) NFSSVC_MAXBLKSIZE);
1540 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
1541 if ((buflen -= 8) < 0)
1543 WRITE64((u64) NFSSVC_MAXBLKSIZE);
1545 if (bmval1 & FATTR4_WORD1_MODE) {
1546 if ((buflen -= 4) < 0)
1548 WRITE32(stat.mode & S_IALLUGO);
1550 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
1551 if ((buflen -= 4) < 0)
1555 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
1556 if ((buflen -= 4) < 0)
1558 WRITE32(stat.nlink);
1560 if (bmval1 & FATTR4_WORD1_OWNER) {
1561 status = nfsd4_encode_user(rqstp,
1562 XIDINO_UID(stat.uid, stat.xid), &p, &buflen);
1563 if (status == nfserr_resource)
1568 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
1569 status = nfsd4_encode_group(rqstp,
1570 XIDINO_GID(stat.gid, stat.xid), &p, &buflen);
1571 if (status == nfserr_resource)
1576 if (bmval1 & FATTR4_WORD1_RAWDEV) {
1577 if ((buflen -= 8) < 0)
1579 WRITE32((u32) MAJOR(stat.rdev));
1580 WRITE32((u32) MINOR(stat.rdev));
1582 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
1583 if ((buflen -= 8) < 0)
1585 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
1588 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
1589 if ((buflen -= 8) < 0)
1591 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
1594 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
1595 if ((buflen -= 8) < 0)
1597 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
1600 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
1601 if ((buflen -= 8) < 0)
1603 dummy64 = (u64)stat.blocks << 9;
1606 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
1607 if ((buflen -= 12) < 0)
1610 WRITE32(stat.atime.tv_sec);
1611 WRITE32(stat.atime.tv_nsec);
1613 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
1614 if ((buflen -= 12) < 0)
1620 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
1621 if ((buflen -= 12) < 0)
1624 WRITE32(stat.ctime.tv_sec);
1625 WRITE32(stat.ctime.tv_nsec);
1627 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
1628 if ((buflen -= 12) < 0)
1631 WRITE32(stat.mtime.tv_sec);
1632 WRITE32(stat.mtime.tv_nsec);
1634 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
1635 struct dentry *mnt_pnt, *mnt_root;
1637 if ((buflen -= 8) < 0)
1639 mnt_root = exp->ex_mnt->mnt_root;
1640 if (mnt_root->d_inode == dentry->d_inode) {
1641 mnt_pnt = exp->ex_mnt->mnt_mountpoint;
1642 WRITE64((u64) mnt_pnt->d_inode->i_ino);
1644 WRITE64((u64) stat.ino);
1646 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
1647 *countp = p - buffer;
1655 status = nfserrno(status);
1659 status = nfserr_resource;
1662 status = nfserr_serverfault;
1667 nfsd4_encode_dirent(struct readdir_cd *ccd, const char *name, int namlen,
1668 loff_t offset, ino_t ino, unsigned int d_type)
1670 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
1672 u32 *p = cd->buffer;
1674 struct dentry *dentry;
1675 struct svc_export *exp = cd->rd_fhp->fh_export;
1679 /* In nfsv4, "." and ".." never make it onto the wire.. */
1680 if (name && isdotent(name, namlen)) {
1681 cd->common.err = nfs_ok;
1686 xdr_encode_hyper(cd->offset, (u64) offset);
1688 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
1692 *p++ = xdr_one; /* mark entry present */
1693 cd->offset = p; /* remember pointer */
1694 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
1695 p = xdr_encode_array(p, name, namlen); /* name length & name */
1698 * Now we come to the ugly part: writing the fattr for this entry.
1700 bmval0 = cd->rd_bmval[0];
1701 bmval1 = cd->rd_bmval[1];
1702 if ((bmval0 & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_FILEID)) || bmval1) {
1704 * "Heavyweight" case: we have no choice except to
1705 * call nfsd4_encode_fattr().
1707 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
1708 if (IS_ERR(dentry)) {
1709 nfserr = nfserrno(PTR_ERR(dentry));
1714 if (d_mountpoint(dentry)) {
1715 if ((nfserr = nfsd_cross_mnt(cd->rd_rqstp, &dentry,
1718 * -EAGAIN is the only error returned from
1719 * nfsd_cross_mnt() and it indicates that an
1720 * up-call has been initiated to fill in the export
1721 * options on exp. When the answer comes back,
1722 * this call will be retried.
1726 nfserr = nfserr_dropit;
1732 nfserr = nfsd4_encode_fattr(NULL, exp,
1733 dentry, p, &buflen, cd->rd_bmval,
1741 if (nfserr == nfserr_resource)
1746 * If we get here, we experienced a miscellaneous
1747 * failure while writing the attributes. If the
1748 * client requested the RDATTR_ERROR attribute,
1749 * we stuff the error code into this attribute
1750 * and continue. If this attribute was not requested,
1751 * then in accordance with the spec, we fail the
1752 * entire READDIR operation(!)
1754 if (!(bmval0 & FATTR4_WORD0_RDATTR_ERROR)) {
1755 cd->common.err = nfserr;
1759 bmval0 = FATTR4_WORD0_RDATTR_ERROR;
1761 /* falling through here will do the right thing... */
1765 * In the common "lightweight" case, we avoid
1766 * the overhead of nfsd4_encode_fattr() by assembling
1767 * a small fattr by hand.
1772 *p++ = htonl(bmval0);
1773 *p++ = htonl(bmval1);
1776 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR)
1777 *p++ = nfserr; /* no htonl */
1778 if (bmval0 & FATTR4_WORD0_FILEID)
1779 p = xdr_encode_hyper(p, (u64)ino);
1780 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
1783 cd->buflen -= (p - cd->buffer);
1785 cd->common.err = nfs_ok;
1789 cd->common.err = nfserr_toosmall;
1794 nfsd4_encode_access(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_access *access)
1800 WRITE32(access->ac_supported);
1801 WRITE32(access->ac_resp_access);
1807 nfsd4_encode_close(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_close *close)
1809 ENCODE_SEQID_OP_HEAD;
1812 RESERVE_SPACE(sizeof(stateid_t));
1813 WRITE32(close->cl_stateid.si_generation);
1814 WRITEMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
1817 ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
1822 nfsd4_encode_commit(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_commit *commit)
1828 WRITEMEM(commit->co_verf.data, 8);
1834 nfsd4_encode_create(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_create *create)
1840 WRITECINFO(create->cr_cinfo);
1842 WRITE32(create->cr_bmval[0]);
1843 WRITE32(create->cr_bmval[1]);
1849 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_getattr *getattr)
1851 struct svc_fh *fhp = getattr->ga_fhp;
1857 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
1858 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
1859 resp->p, &buflen, getattr->ga_bmval,
1868 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, int nfserr, struct svc_fh *fhp)
1874 len = fhp->fh_handle.fh_size;
1875 RESERVE_SPACE(len + 4);
1877 WRITEMEM(&fhp->fh_handle.fh_base, len);
1883 * Including all fields other than the name, a LOCK4denied structure requires
1884 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
1887 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
1891 RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop->so_owner.len));
1892 WRITE64(ld->ld_start);
1893 WRITE64(ld->ld_length);
1894 WRITE32(ld->ld_type);
1895 WRITEMEM(&ld->ld_sop->so_client->cl_clientid, 8);
1896 WRITE32(ld->ld_sop->so_owner.len);
1897 WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
1902 nfsd4_encode_lock(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lock *lock)
1905 ENCODE_SEQID_OP_HEAD;
1908 RESERVE_SPACE(4 + sizeof(stateid_t));
1909 WRITE32(lock->lk_resp_stateid.si_generation);
1910 WRITEMEM(&lock->lk_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
1912 } else if (nfserr == nfserr_denied)
1913 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
1915 ENCODE_SEQID_OP_TAIL(lock->lk_stateowner);
1919 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lockt *lockt)
1921 if (nfserr == nfserr_denied)
1922 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
1926 nfsd4_encode_locku(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_locku *locku)
1928 ENCODE_SEQID_OP_HEAD;
1931 RESERVE_SPACE(sizeof(stateid_t));
1932 WRITE32(locku->lu_stateid.si_generation);
1933 WRITEMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
1937 ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
1942 nfsd4_encode_link(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_link *link)
1948 WRITECINFO(link->li_cinfo);
1955 nfsd4_encode_open(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open *open)
1957 ENCODE_SEQID_OP_HEAD;
1962 RESERVE_SPACE(36 + sizeof(stateid_t));
1963 WRITE32(open->op_stateid.si_generation);
1964 WRITEMEM(&open->op_stateid.si_opaque, sizeof(stateid_opaque_t));
1965 WRITECINFO(open->op_cinfo);
1966 WRITE32(open->op_rflags);
1968 WRITE32(open->op_bmval[0]);
1969 WRITE32(open->op_bmval[1]);
1970 WRITE32(open->op_delegate_type);
1973 switch (open->op_delegate_type) {
1974 case NFS4_OPEN_DELEGATE_NONE:
1976 case NFS4_OPEN_DELEGATE_READ:
1977 RESERVE_SPACE(20 + sizeof(delegation_stateid_t));
1978 WRITEMEM(&open->op_delegate_stateid, sizeof(delegation_stateid_t));
1982 * TODO: ACE's in delegations
1984 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
1987 WRITE32(0); /* XXX: is NULL principal ok? */
1990 case NFS4_OPEN_DELEGATE_WRITE:
1991 RESERVE_SPACE(32 + sizeof(delegation_stateid_t));
1992 WRITEMEM(&open->op_delegate_stateid, sizeof(delegation_stateid_t));
1996 * TODO: space_limit's in delegations
1998 WRITE32(NFS4_LIMIT_SIZE);
2003 * TODO: ACE's in delegations
2005 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2008 WRITE32(0); /* XXX: is NULL principal ok? */
2014 /* XXX save filehandle here */
2016 ENCODE_SEQID_OP_TAIL(open->op_stateowner);
2020 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_confirm *oc)
2022 ENCODE_SEQID_OP_HEAD;
2025 RESERVE_SPACE(sizeof(stateid_t));
2026 WRITE32(oc->oc_resp_stateid.si_generation);
2027 WRITEMEM(&oc->oc_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
2031 ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
2035 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_downgrade *od)
2037 ENCODE_SEQID_OP_HEAD;
2040 RESERVE_SPACE(sizeof(stateid_t));
2041 WRITE32(od->od_stateid.si_generation);
2042 WRITEMEM(&od->od_stateid.si_opaque, sizeof(stateid_opaque_t));
2046 ENCODE_SEQID_OP_TAIL(od->od_stateowner);
2050 nfsd4_encode_read(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_read *read)
2054 unsigned long maxcount;
2060 if (resp->xbuf->page_len)
2061 return nfserr_resource;
2063 RESERVE_SPACE(8); /* eof flag and byte count */
2065 maxcount = NFSSVC_MAXBLKSIZE;
2066 if (maxcount > read->rd_length)
2067 maxcount = read->rd_length;
2072 pn = resp->rqstp->rq_resused;
2073 svc_take_page(resp->rqstp);
2074 read->rd_iov[v].iov_base = page_address(resp->rqstp->rq_respages[pn]);
2075 read->rd_iov[v].iov_len = len < PAGE_SIZE ? len : PAGE_SIZE;
2081 nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp,
2083 read->rd_iov, read->rd_vlen,
2085 if (nfserr == nfserr_symlink)
2086 nfserr = nfserr_inval;
2089 eof = (read->rd_offset + maxcount >= read->rd_fhp->fh_dentry->d_inode->i_size);
2094 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
2096 resp->xbuf->page_len = maxcount;
2098 /* read zero bytes -> don't set up tail */
2102 /* set up page for remaining responses */
2103 svc_take_page(resp->rqstp);
2104 resp->xbuf->tail[0].iov_base =
2105 page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2106 resp->rqstp->rq_restailpage = resp->rqstp->rq_resused-1;
2107 resp->xbuf->tail[0].iov_len = 0;
2108 resp->p = resp->xbuf->tail[0].iov_base;
2109 resp->end = resp->p + PAGE_SIZE/4;
2113 resp->xbuf->tail[0].iov_base += maxcount&3;
2114 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
2120 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readlink *readlink)
2128 if (resp->xbuf->page_len)
2129 return nfserr_resource;
2131 svc_take_page(resp->rqstp);
2132 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2134 maxcount = PAGE_SIZE;
2138 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2139 * if they would overflow the buffer. Is this kosher in NFSv4? If
2140 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2141 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2143 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2144 if (nfserr == nfserr_isdir)
2145 return nfserr_inval;
2151 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
2153 svc_take_page(resp->rqstp);
2154 resp->xbuf->tail[0].iov_base =
2155 page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2156 resp->rqstp->rq_restailpage = resp->rqstp->rq_resused-1;
2157 resp->xbuf->tail[0].iov_len = 0;
2158 resp->p = resp->xbuf->tail[0].iov_base;
2159 resp->end = resp->p + PAGE_SIZE/4;
2161 resp->xbuf->page_len = maxcount;
2164 resp->xbuf->tail[0].iov_base += maxcount&3;
2165 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
2171 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readdir *readdir)
2180 if (resp->xbuf->page_len)
2181 return nfserr_resource;
2183 RESERVE_SPACE(8); /* verifier */
2186 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
2190 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
2192 maxcount = PAGE_SIZE;
2193 if (maxcount > readdir->rd_maxcount)
2194 maxcount = readdir->rd_maxcount;
2197 * Convert from bytes to words, account for the two words already
2198 * written, make sure to leave two words at the end for the next
2199 * pointer and eof field.
2201 maxcount = (maxcount >> 2) - 4;
2203 nfserr = nfserr_toosmall;
2207 svc_take_page(resp->rqstp);
2208 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2209 readdir->common.err = 0;
2210 readdir->buflen = maxcount;
2211 readdir->buffer = page;
2212 readdir->offset = NULL;
2214 offset = readdir->rd_cookie;
2215 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
2217 &readdir->common, nfsd4_encode_dirent);
2218 if (nfserr == nfs_ok &&
2219 readdir->common.err == nfserr_toosmall &&
2220 readdir->buffer == page)
2221 nfserr = nfserr_toosmall;
2222 if (nfserr == nfserr_symlink)
2223 nfserr = nfserr_notdir;
2227 if (readdir->offset)
2228 xdr_encode_hyper(readdir->offset, offset);
2230 p = readdir->buffer;
2231 *p++ = 0; /* no more entries */
2232 *p++ = htonl(readdir->common.err == nfserr_eof);
2233 resp->xbuf->page_len = ((char*)p) - (char*)page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2235 /* allocate a page for the tail */
2236 svc_take_page(resp->rqstp);
2237 resp->xbuf->tail[0].iov_base =
2238 page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2239 resp->rqstp->rq_restailpage = resp->rqstp->rq_resused-1;
2240 resp->xbuf->tail[0].iov_len = 0;
2241 resp->p = resp->xbuf->tail[0].iov_base;
2242 resp->end = resp->p + PAGE_SIZE/4;
2252 nfsd4_encode_remove(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_remove *remove)
2258 WRITECINFO(remove->rm_cinfo);
2264 nfsd4_encode_rename(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_rename *rename)
2270 WRITECINFO(rename->rn_sinfo);
2271 WRITECINFO(rename->rn_tinfo);
2277 * The SETATTR encode routine is special -- it always encodes a bitmap,
2278 * regardless of the error status.
2281 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setattr *setattr)
2293 WRITE32(setattr->sa_bmval[0]);
2294 WRITE32(setattr->sa_bmval[1]);
2300 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setclientid *scd)
2305 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
2306 WRITEMEM(&scd->se_clientid, 8);
2307 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
2310 else if (nfserr == nfserr_clid_inuse) {
2319 nfsd4_encode_write(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_write *write)
2325 WRITE32(write->wr_bytes_written);
2326 WRITE32(write->wr_how_written);
2327 WRITEMEM(write->wr_verifier.data, 8);
2333 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
2340 statp = p++; /* to be backfilled at the end */
2343 switch (op->opnum) {
2345 nfsd4_encode_access(resp, op->status, &op->u.access);
2348 nfsd4_encode_close(resp, op->status, &op->u.close);
2351 nfsd4_encode_commit(resp, op->status, &op->u.commit);
2354 nfsd4_encode_create(resp, op->status, &op->u.create);
2357 op->status = nfsd4_encode_getattr(resp, op->status, &op->u.getattr);
2360 nfsd4_encode_getfh(resp, op->status, op->u.getfh);
2363 nfsd4_encode_link(resp, op->status, &op->u.link);
2366 nfsd4_encode_lock(resp, op->status, &op->u.lock);
2369 nfsd4_encode_lockt(resp, op->status, &op->u.lockt);
2372 nfsd4_encode_locku(resp, op->status, &op->u.locku);
2381 nfsd4_encode_open(resp, op->status, &op->u.open);
2383 case OP_OPEN_CONFIRM:
2384 nfsd4_encode_open_confirm(resp, op->status, &op->u.open_confirm);
2386 case OP_OPEN_DOWNGRADE:
2387 nfsd4_encode_open_downgrade(resp, op->status, &op->u.open_downgrade);
2394 op->status = nfsd4_encode_read(resp, op->status, &op->u.read);
2397 op->status = nfsd4_encode_readdir(resp, op->status, &op->u.readdir);
2400 op->status = nfsd4_encode_readlink(resp, op->status, &op->u.readlink);
2403 nfsd4_encode_remove(resp, op->status, &op->u.remove);
2406 nfsd4_encode_rename(resp, op->status, &op->u.rename);
2415 nfsd4_encode_setattr(resp, op->status, &op->u.setattr);
2417 case OP_SETCLIENTID:
2418 nfsd4_encode_setclientid(resp, op->status, &op->u.setclientid);
2420 case OP_SETCLIENTID_CONFIRM:
2425 nfsd4_encode_write(resp, op->status, &op->u.write);
2427 case OP_RELEASE_LOCKOWNER:
2434 * Note: We write the status directly, instead of using WRITE32(),
2435 * since it is already in network byte order.
2437 *statp = op->status;
2441 * Encode the reply stored in the stateowner reply cache
2443 * XDR note: do not encode rp->rp_buflen: the buffer contains the
2444 * previously sent already encoded operation.
2446 * called with nfs4_lock_state() held
2449 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
2452 struct nfs4_replay *rp = op->replay;
2458 *p++ = rp->rp_status; /* already xdr'ed */
2461 RESERVE_SPACE(rp->rp_buflen);
2462 WRITEMEM(rp->rp_buf, rp->rp_buflen);
2464 nfs4_unlock_state();
2468 * END OF "GENERIC" ENCODE ROUTINES.
2472 nfs4svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
2474 return xdr_ressize_check(rqstp, p);
2478 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundargs *args)
2483 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
2484 args->pagelist = rqstp->rq_arg.pages;
2485 args->pagelen = rqstp->rq_arg.page_len;
2487 args->to_free = NULL;
2488 args->ops = args->iops;
2489 args->rqstp = rqstp;
2491 status = nfsd4_decode_compound(args);
2493 if (args->ops != args->iops) {
2495 args->ops = args->iops;
2501 while (args->to_free) {
2502 struct tmpbuf *tb = args->to_free;
2503 args->to_free = tb->next;
2512 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundres *resp)
2515 * All that remains is to write the tag and operation count...
2519 *p++ = htonl(resp->taglen);
2520 memcpy(p, resp->tag, resp->taglen);
2521 p += XDR_QUADLEN(resp->taglen);
2522 *p++ = htonl(resp->opcnt);
2524 if (rqstp->rq_res.page_len)
2525 iov = &rqstp->rq_res.tail[0];
2527 iov = &rqstp->rq_res.head[0];
2528 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
2529 BUG_ON(iov->iov_len > PAGE_SIZE);