2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
65 * FreeBSD-Id: nfs_subs.c,v 1.47 1997/11/07 08:53:24 phk Exp $
69 * These functions support the macros and help fiddle mbuf chains for
70 * the nfs op functions. They do things like create the rpc header and
71 * copy data between mbuf chains and uio lists.
73 #include <sys/param.h>
75 #include <sys/kauth.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/mount_internal.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/kpi_mbuf.h>
81 #include <sys/socket.h>
83 #include <sys/malloc.h>
84 #include <sys/syscall.h>
85 #include <sys/ubc_internal.h>
86 #include <sys/fcntl.h>
88 #include <sys/domain.h>
89 #include <libkern/OSAtomic.h>
90 #include <kern/thread_call.h>
93 #include <sys/vmparam.h>
96 #include <kern/clock.h>
98 #include <nfs/rpcv2.h>
99 #include <nfs/nfsproto.h>
101 #include <nfs/nfsnode.h>
103 #define _NFS_XDR_SUBS_FUNCS_ /* define this to get xdrbuf function definitions */
105 #include <nfs/xdr_subs.h>
106 #include <nfs/nfsm_subs.h>
107 #include <nfs/nfs_gss.h>
108 #include <nfs/nfsmount.h>
109 #include <nfs/nfs_lock.h>
111 #include <miscfs/specfs/specdev.h>
113 #include <netinet/in.h>
114 #include <net/kpi_interface.h>
116 #include <sys/utfconv.h>
121 struct nfsstats nfsstats
;
122 size_t nfs_mbuf_mhlen
= 0, nfs_mbuf_minclsize
= 0;
125 * functions to convert between NFS and VFS types
128 vtonfs_type(enum vtype vtype
, int nfsvers
)
144 if (nfsvers
> NFS_VER2
)
147 if (nfsvers
> NFS_VER2
)
158 nfstov_type(nfstype nvtype
, int nfsvers
)
174 if (nfsvers
> NFS_VER2
)
177 if (nfsvers
> NFS_VER2
)
180 if (nfsvers
> NFS_VER3
)
183 if (nfsvers
> NFS_VER3
)
191 vtonfsv2_mode(enum vtype vtype
, mode_t m
)
201 return vnode_makeimode(vtype
, m
);
203 return vnode_makeimode(VCHR
, m
);
208 return vnode_makeimode(VNON
, m
);
215 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
217 int nfsv3_procid
[NFS_NPROCS
] = {
243 #endif /* NFSSERVER */
246 * and the reverse mapping from generic to Version 2 procedure numbers
248 int nfsv2_procid
[NFS_NPROCS
] = {
276 * initialize NFS's cache of mbuf constants
284 nfs_mbuf_mhlen
= ms
.mhlen
;
285 nfs_mbuf_minclsize
= ms
.minclsize
;
291 * allocate a list of mbufs to hold the given amount of data
294 nfsm_mbuf_get_list(size_t size
, mbuf_t
*mp
, int *mbcnt
)
297 mbuf_t mhead
, mlast
, m
;
301 mhead
= mlast
= NULL
;
305 nfsm_mbuf_get(error
, &m
, (size
- len
));
310 if (mlast
&& ((error
= mbuf_setnext(mlast
, m
)))) {
314 mlen
= mbuf_maxlen(m
);
315 if ((len
+ mlen
) > size
)
317 mbuf_setlen(m
, mlen
);
330 #endif /* NFSSERVER */
333 * nfsm_chain_new_mbuf()
335 * Add a new mbuf to the given chain.
338 nfsm_chain_new_mbuf(struct nfsm_chain
*nmc
, size_t sizehint
)
343 if (nmc
->nmc_flags
& NFSM_CHAIN_FLAG_ADD_CLUSTERS
)
344 sizehint
= nfs_mbuf_minclsize
;
346 /* allocate a new mbuf */
347 nfsm_mbuf_get(error
, &mb
, sizehint
);
351 panic("got NULL mbuf?");
353 /* do we have a current mbuf? */
355 /* first cap off current mbuf */
356 mbuf_setlen(nmc
->nmc_mcur
, nmc
->nmc_ptr
- (caddr_t
)mbuf_data(nmc
->nmc_mcur
));
357 /* then append the new mbuf */
358 error
= mbuf_setnext(nmc
->nmc_mcur
, mb
);
365 /* set up for using the new mbuf */
367 nmc
->nmc_ptr
= mbuf_data(mb
);
368 nmc
->nmc_left
= mbuf_trailingspace(mb
);
374 * nfsm_chain_add_opaque_f()
376 * Add "len" bytes of opaque data pointed to by "buf" to the given chain.
379 nfsm_chain_add_opaque_f(struct nfsm_chain
*nmc
, const u_char
*buf
, uint32_t len
)
381 uint32_t paddedlen
, tlen
;
384 paddedlen
= nfsm_rndup(len
);
387 if (!nmc
->nmc_left
) {
388 error
= nfsm_chain_new_mbuf(nmc
, paddedlen
);
392 tlen
= MIN(nmc
->nmc_left
, paddedlen
);
397 bcopy(buf
, nmc
->nmc_ptr
, tlen
);
399 bzero(nmc
->nmc_ptr
, tlen
);
401 nmc
->nmc_ptr
+= tlen
;
402 nmc
->nmc_left
-= tlen
;
414 * nfsm_chain_add_opaque_nopad_f()
416 * Add "len" bytes of opaque data pointed to by "buf" to the given chain.
420 nfsm_chain_add_opaque_nopad_f(struct nfsm_chain
*nmc
, const u_char
*buf
, uint32_t len
)
426 if (nmc
->nmc_left
<= 0) {
427 error
= nfsm_chain_new_mbuf(nmc
, len
);
431 tlen
= MIN(nmc
->nmc_left
, len
);
432 bcopy(buf
, nmc
->nmc_ptr
, tlen
);
433 nmc
->nmc_ptr
+= tlen
;
434 nmc
->nmc_left
-= tlen
;
442 * nfsm_chain_add_uio()
444 * Add "len" bytes of data from "uio" to the given chain.
447 nfsm_chain_add_uio(struct nfsm_chain
*nmc
, uio_t uio
, uint32_t len
)
449 uint32_t paddedlen
, tlen
;
452 paddedlen
= nfsm_rndup(len
);
455 if (!nmc
->nmc_left
) {
456 error
= nfsm_chain_new_mbuf(nmc
, paddedlen
);
460 tlen
= MIN(nmc
->nmc_left
, paddedlen
);
465 uiomove(nmc
->nmc_ptr
, tlen
, uio
);
467 bzero(nmc
->nmc_ptr
, tlen
);
469 nmc
->nmc_ptr
+= tlen
;
470 nmc
->nmc_left
-= tlen
;
480 * Find the length of the NFS mbuf chain
481 * up to the current encoding/decoding offset.
484 nfsm_chain_offset(struct nfsm_chain
*nmc
)
489 for (mb
= nmc
->nmc_mhead
; mb
; mb
= mbuf_next(mb
)) {
490 if (mb
== nmc
->nmc_mcur
)
491 return (len
+ (nmc
->nmc_ptr
- (caddr_t
) mbuf_data(mb
)));
499 * nfsm_chain_advance()
501 * Advance an nfsm_chain by "len" bytes.
504 nfsm_chain_advance(struct nfsm_chain
*nmc
, uint32_t len
)
509 if (nmc
->nmc_left
>= len
) {
510 nmc
->nmc_left
-= len
;
514 len
-= nmc
->nmc_left
;
515 nmc
->nmc_mcur
= mb
= mbuf_next(nmc
->nmc_mcur
);
518 nmc
->nmc_ptr
= mbuf_data(mb
);
519 nmc
->nmc_left
= mbuf_len(mb
);
526 * nfsm_chain_reverse()
528 * Reverse decode offset in an nfsm_chain by "len" bytes.
531 nfsm_chain_reverse(struct nfsm_chain
*nmc
, uint32_t len
)
533 uint32_t mlen
, new_offset
;
536 mlen
= nmc
->nmc_ptr
- (caddr_t
) mbuf_data(nmc
->nmc_mcur
);
539 nmc
->nmc_left
+= len
;
543 new_offset
= nfsm_chain_offset(nmc
) - len
;
544 nfsm_chain_dissect_init(error
, nmc
, nmc
->nmc_mhead
);
548 return (nfsm_chain_advance(nmc
, new_offset
));
552 * nfsm_chain_get_opaque_pointer_f()
554 * Return a pointer to the next "len" bytes of contiguous data in
555 * the mbuf chain. If the next "len" bytes are not contiguous, we
556 * try to manipulate the mbuf chain so that it is.
558 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
561 nfsm_chain_get_opaque_pointer_f(struct nfsm_chain
*nmc
, uint32_t len
, u_char
**pptr
)
564 uint32_t left
, need
, mblen
, cplen
, padlen
;
568 /* move to next mbuf with data */
569 while (nmc
->nmc_mcur
&& (nmc
->nmc_left
== 0)) {
570 mb
= mbuf_next(nmc
->nmc_mcur
);
574 nmc
->nmc_ptr
= mbuf_data(mb
);
575 nmc
->nmc_left
= mbuf_len(mb
);
577 /* check if we've run out of data */
581 /* do we already have a contiguous buffer? */
582 if (nmc
->nmc_left
>= len
) {
583 /* the returned pointer will be the current pointer */
584 *pptr
= (u_char
*)nmc
->nmc_ptr
;
585 error
= nfsm_chain_advance(nmc
, nfsm_rndup(len
));
589 padlen
= nfsm_rndup(len
) - len
;
591 /* we need (len - left) more bytes */
592 mbcur
= nmc
->nmc_mcur
;
593 left
= nmc
->nmc_left
;
596 if (need
> mbuf_trailingspace(mbcur
)) {
598 * The needed bytes won't fit in the current mbuf so we'll
599 * allocate a new mbuf to hold the contiguous range of data.
601 nfsm_mbuf_get(error
, &mb
, len
);
604 /* double check that this mbuf can hold all the data */
605 if (mbuf_maxlen(mb
) < len
) {
610 /* the returned pointer will be the new mbuf's data pointer */
611 *pptr
= ptr
= mbuf_data(mb
);
613 /* copy "left" bytes to the new mbuf */
614 bcopy(nmc
->nmc_ptr
, ptr
, left
);
616 mbuf_setlen(mb
, left
);
618 /* insert the new mbuf between the current and next mbufs */
619 error
= mbuf_setnext(mb
, mbuf_next(mbcur
));
621 error
= mbuf_setnext(mbcur
, mb
);
627 /* reduce current mbuf's length by "left" */
628 mbuf_setlen(mbcur
, mbuf_len(mbcur
) - left
);
631 * update nmc's state to point at the end of the mbuf
632 * where the needed data will be copied to.
634 nmc
->nmc_mcur
= mbcur
= mb
;
636 nmc
->nmc_ptr
= (caddr_t
)ptr
;
638 /* The rest of the data will fit in this mbuf. */
640 /* the returned pointer will be the current pointer */
641 *pptr
= (u_char
*)nmc
->nmc_ptr
;
644 * update nmc's state to point at the end of the mbuf
645 * where the needed data will be copied to.
647 nmc
->nmc_ptr
+= left
;
652 * move the next "need" bytes into the current
653 * mbuf from the mbufs that follow
656 /* extend current mbuf length */
657 mbuf_setlen(mbcur
, mbuf_len(mbcur
) + need
);
659 /* mb follows mbufs we're copying/compacting data from */
660 mb
= mbuf_next(mbcur
);
663 /* copy as much as we need/can */
665 mblen
= mbuf_len(mb
);
666 cplen
= MIN(mblen
, need
);
668 bcopy(ptr
, nmc
->nmc_ptr
, cplen
);
670 * update the mbuf's pointer and length to reflect that
671 * the data was shifted to an earlier mbuf in the chain
673 error
= mbuf_setdata(mb
, ptr
+ cplen
, mblen
- cplen
);
675 mbuf_setlen(mbcur
, mbuf_len(mbcur
) - need
);
678 /* update pointer/need */
679 nmc
->nmc_ptr
+= cplen
;
682 /* if more needed, go to next mbuf */
687 /* did we run out of data in the mbuf chain? */
689 mbuf_setlen(mbcur
, mbuf_len(mbcur
) - need
);
694 * update nmc's state to point after this contiguous data
696 * "mb" points to the last mbuf we copied data from so we
697 * just set nmc to point at whatever remains in that mbuf.
700 nmc
->nmc_ptr
= mbuf_data(mb
);
701 nmc
->nmc_left
= mbuf_len(mb
);
703 /* move past any padding */
705 error
= nfsm_chain_advance(nmc
, padlen
);
711 * nfsm_chain_get_opaque_f()
713 * Read the next "len" bytes in the chain into "buf".
714 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
717 nfsm_chain_get_opaque_f(struct nfsm_chain
*nmc
, uint32_t len
, u_char
*buf
)
719 uint32_t cplen
, padlen
;
722 padlen
= nfsm_rndup(len
) - len
;
724 /* loop through mbufs copying all the data we need */
725 while (len
&& nmc
->nmc_mcur
) {
726 /* copy as much as we need/can */
727 cplen
= MIN(nmc
->nmc_left
, len
);
729 bcopy(nmc
->nmc_ptr
, buf
, cplen
);
730 nmc
->nmc_ptr
+= cplen
;
731 nmc
->nmc_left
-= cplen
;
735 /* if more needed, go to next mbuf */
737 mbuf_t mb
= mbuf_next(nmc
->nmc_mcur
);
739 nmc
->nmc_ptr
= mb
? mbuf_data(mb
) : NULL
;
740 nmc
->nmc_left
= mb
? mbuf_len(mb
) : 0;
744 /* did we run out of data in the mbuf chain? */
749 nfsm_chain_adv(error
, nmc
, padlen
);
755 * nfsm_chain_get_uio()
757 * Read the next "len" bytes in the chain into the given uio.
758 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
761 nfsm_chain_get_uio(struct nfsm_chain
*nmc
, uint32_t len
, uio_t uio
)
763 uint32_t cplen
, padlen
;
766 padlen
= nfsm_rndup(len
) - len
;
768 /* loop through mbufs copying all the data we need */
769 while (len
&& nmc
->nmc_mcur
) {
770 /* copy as much as we need/can */
771 cplen
= MIN(nmc
->nmc_left
, len
);
773 error
= uiomove(nmc
->nmc_ptr
, cplen
, uio
);
776 nmc
->nmc_ptr
+= cplen
;
777 nmc
->nmc_left
-= cplen
;
780 /* if more needed, go to next mbuf */
782 mbuf_t mb
= mbuf_next(nmc
->nmc_mcur
);
784 nmc
->nmc_ptr
= mb
? mbuf_data(mb
) : NULL
;
785 nmc
->nmc_left
= mb
? mbuf_len(mb
) : 0;
789 /* did we run out of data in the mbuf chain? */
794 nfsm_chain_adv(error
, nmc
, padlen
);
802 nfsm_chain_add_string_nfc(struct nfsm_chain
*nmc
, const uint8_t *s
, uint32_t slen
)
804 uint8_t smallbuf
[64];
805 uint8_t *nfcname
= smallbuf
;
806 size_t buflen
= sizeof(smallbuf
), nfclen
;
809 error
= utf8_normalizestr(s
, slen
, nfcname
, &nfclen
, buflen
, UTF_PRECOMPOSED
|UTF_NO_NULL_TERM
);
810 if (error
== ENAMETOOLONG
) {
812 MALLOC_ZONE(nfcname
, uint8_t *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
814 error
= utf8_normalizestr(s
, slen
, nfcname
, &nfclen
, buflen
, UTF_PRECOMPOSED
|UTF_NO_NULL_TERM
);
817 /* if we got an error, just use the original string */
819 nfsm_chain_add_string(error
, nmc
, s
, slen
);
821 nfsm_chain_add_string(error
, nmc
, nfcname
, nfclen
);
823 if (nfcname
&& (nfcname
!= smallbuf
))
824 FREE_ZONE(nfcname
, MAXPATHLEN
, M_NAMEI
);
829 * Add an NFSv2 "sattr" structure to an mbuf chain
832 nfsm_chain_add_v2sattr_f(struct nfsm_chain
*nmc
, struct vnode_attr
*vap
, uint32_t szrdev
)
836 nfsm_chain_add_32(error
, nmc
, vtonfsv2_mode(vap
->va_type
,
837 (VATTR_IS_ACTIVE(vap
, va_mode
) ? vap
->va_mode
: 0600)));
838 nfsm_chain_add_32(error
, nmc
,
839 VATTR_IS_ACTIVE(vap
, va_uid
) ? vap
->va_uid
: (uint32_t)-1);
840 nfsm_chain_add_32(error
, nmc
,
841 VATTR_IS_ACTIVE(vap
, va_gid
) ? vap
->va_gid
: (uint32_t)-1);
842 nfsm_chain_add_32(error
, nmc
, szrdev
);
843 nfsm_chain_add_v2time(error
, nmc
,
844 VATTR_IS_ACTIVE(vap
, va_access_time
) ?
845 &vap
->va_access_time
: NULL
);
846 nfsm_chain_add_v2time(error
, nmc
,
847 VATTR_IS_ACTIVE(vap
, va_modify_time
) ?
848 &vap
->va_modify_time
: NULL
);
854 * Add an NFSv3 "sattr" structure to an mbuf chain
857 nfsm_chain_add_v3sattr_f(struct nfsm_chain
*nmc
, struct vnode_attr
*vap
)
861 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
862 nfsm_chain_add_32(error
, nmc
, TRUE
);
863 nfsm_chain_add_32(error
, nmc
, vap
->va_mode
);
865 nfsm_chain_add_32(error
, nmc
, FALSE
);
867 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
868 nfsm_chain_add_32(error
, nmc
, TRUE
);
869 nfsm_chain_add_32(error
, nmc
, vap
->va_uid
);
871 nfsm_chain_add_32(error
, nmc
, FALSE
);
873 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
874 nfsm_chain_add_32(error
, nmc
, TRUE
);
875 nfsm_chain_add_32(error
, nmc
, vap
->va_gid
);
877 nfsm_chain_add_32(error
, nmc
, FALSE
);
879 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
880 nfsm_chain_add_32(error
, nmc
, TRUE
);
881 nfsm_chain_add_64(error
, nmc
, vap
->va_data_size
);
883 nfsm_chain_add_32(error
, nmc
, FALSE
);
885 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
886 nfsm_chain_add_32(error
, nmc
, NFS_TIME_SET_TO_SERVER
);
887 nfsm_chain_add_32(error
, nmc
, NFS_TIME_SET_TO_SERVER
);
889 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
890 nfsm_chain_add_32(error
, nmc
, NFS_TIME_SET_TO_CLIENT
);
891 nfsm_chain_add_32(error
, nmc
, vap
->va_access_time
.tv_sec
);
892 nfsm_chain_add_32(error
, nmc
, vap
->va_access_time
.tv_nsec
);
894 nfsm_chain_add_32(error
, nmc
, NFS_TIME_DONT_CHANGE
);
896 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
897 nfsm_chain_add_32(error
, nmc
, NFS_TIME_SET_TO_CLIENT
);
898 nfsm_chain_add_32(error
, nmc
, vap
->va_modify_time
.tv_sec
);
899 nfsm_chain_add_32(error
, nmc
, vap
->va_modify_time
.tv_nsec
);
901 nfsm_chain_add_32(error
, nmc
, NFS_TIME_DONT_CHANGE
);
910 * nfsm_chain_get_fh_attr()
912 * Get the file handle and attributes from an mbuf chain. (NFSv2/v3)
915 nfsm_chain_get_fh_attr(
916 struct nfsm_chain
*nmc
,
922 struct nfs_vattr
*nvap
)
924 int error
= 0, gotfh
, gotattr
;
928 if (nfsvers
== NFS_VER3
) /* check for file handle */
929 nfsm_chain_get_32(error
, nmc
, gotfh
);
930 if (!error
&& gotfh
) /* get file handle */
931 nfsm_chain_get_fh(error
, nmc
, nfsvers
, fhp
);
934 if (nfsvers
== NFS_VER3
) /* check for file attributes */
935 nfsm_chain_get_32(error
, nmc
, gotattr
);
938 if (!gotfh
) /* skip attributes */
939 nfsm_chain_adv(error
, nmc
, NFSX_V3FATTR
);
940 else /* get attributes */
941 error
= nfs_parsefattr(nmc
, nfsvers
, nvap
);
943 /* we need valid attributes in order to call nfs_nget() */
944 if (nfs3_getattr_rpc(NULL
, NFSTOMP(dnp
), fhp
->fh_data
, fhp
->fh_len
, 0, ctx
, nvap
, xidp
)) {
954 * Get and process NFSv3 WCC data from an mbuf chain
957 nfsm_chain_get_wcc_data_f(
958 struct nfsm_chain
*nmc
,
960 struct timespec
*premtime
,
967 nfsm_chain_get_32(error
, nmc
, flag
);
968 if (!error
&& flag
) {
969 nfsm_chain_adv(error
, nmc
, 2 * NFSX_UNSIGNED
);
970 nfsm_chain_get_32(error
, nmc
, premtime
->tv_sec
);
971 nfsm_chain_get_32(error
, nmc
, premtime
->tv_nsec
);
972 nfsm_chain_adv(error
, nmc
, 2 * NFSX_UNSIGNED
);
974 premtime
->tv_sec
= 0;
975 premtime
->tv_nsec
= 0;
977 nfsm_chain_postop_attr_update_flag(error
, nmc
, np
, *newpostattr
, xidp
);
983 * Get the next RPC transaction ID (XID)
986 nfs_get_xid(uint64_t *xidp
)
990 lck_mtx_lock(nfs_request_mutex
);
993 * Derive initial xid from system time.
995 * Note: it's OK if this code inits nfs_xid to 0 (for example,
996 * due to a broken clock) because we immediately increment it
997 * and we guarantee to never use xid 0. So, nfs_xid should only
998 * ever be 0 the first time this function is called.
1001 nfs_xid
= tv
.tv_sec
<< 12;
1003 if (++nfs_xid
== 0) {
1004 /* Skip zero xid if it should ever happen. */
1008 *xidp
= nfs_xid
+ ((uint64_t)nfs_xidwrap
<< 32);
1009 lck_mtx_unlock(nfs_request_mutex
);
1013 * Build the RPC header and fill in the authorization info.
1014 * Returns the head of the mbuf list and the xid.
1024 struct nfsmount
*nmp
= req
->r_nmp
;
1025 int nfsvers
= nmp
->nm_vers
;
1026 int proc
= ((nfsvers
== NFS_VER2
) ? nfsv2_procid
[req
->r_procnum
] : (int)req
->r_procnum
);
1028 return nfsm_rpchead2(nmp
, nmp
->nm_sotype
, NFS_PROG
, nfsvers
, proc
,
1029 req
->r_auth
, req
->r_cred
, req
, mrest
, xidp
, mreqp
);
1033 nfsm_rpchead2(struct nfsmount
*nmp
, int sotype
, int prog
, int vers
, int proc
, int auth_type
,
1034 kauth_cred_t cred
, struct nfsreq
*req
, mbuf_t mrest
, u_int64_t
*xidp
, mbuf_t
*mreqp
)
1037 int error
, i
, grpsiz
, auth_len
= 0, authsiz
, reqlen
;
1039 struct nfsm_chain nmreq
;
1041 /* calculate expected auth length */
1042 switch (auth_type
) {
1048 gid_t grouplist
[NGROUPS
];
1049 int groupcount
= NGROUPS
;
1054 (void)kauth_cred_getgroups(cred
, grouplist
, &groupcount
);
1058 auth_len
= ((((groupcount
- 1) > nmp
->nm_numgrps
) ?
1059 nmp
->nm_numgrps
: (groupcount
- 1)) << 2) +
1068 auth_len
= 5 * NFSX_UNSIGNED
+ 0; // zero context handle for now
1073 authsiz
= nfsm_rndup(auth_len
);
1075 /* allocate the packet */
1076 headlen
= authsiz
+ 10 * NFSX_UNSIGNED
;
1077 if (sotype
== SOCK_STREAM
) /* also include room for any RPC Record Mark */
1078 headlen
+= NFSX_UNSIGNED
;
1079 if (headlen
>= nfs_mbuf_minclsize
) {
1080 error
= mbuf_getpacket(MBUF_WAITOK
, &mreq
);
1082 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mreq
);
1084 if (headlen
< nfs_mbuf_mhlen
)
1085 mbuf_align_32(mreq
, headlen
);
1087 mbuf_align_32(mreq
, 8 * NFSX_UNSIGNED
);
1091 /* unable to allocate packet */
1092 /* XXX should we keep statistics for these errors? */
1097 * If the caller gave us a non-zero XID then use it because
1098 * it may be a higher-level resend with a GSSAPI credential.
1099 * Otherwise, allocate a new one.
1104 /* build the header(s) */
1105 nfsm_chain_init(&nmreq
, mreq
);
1107 /* First, if it's a TCP stream insert space for an RPC record mark */
1108 if (sotype
== SOCK_STREAM
)
1109 nfsm_chain_add_32(error
, &nmreq
, 0);
1111 /* Then the RPC header. */
1112 nfsm_chain_add_32(error
, &nmreq
, (*xidp
& 0xffffffff));
1113 nfsm_chain_add_32(error
, &nmreq
, RPC_CALL
);
1114 nfsm_chain_add_32(error
, &nmreq
, RPC_VER2
);
1115 nfsm_chain_add_32(error
, &nmreq
, prog
);
1116 nfsm_chain_add_32(error
, &nmreq
, vers
);
1117 nfsm_chain_add_32(error
, &nmreq
, proc
);
1120 switch (auth_type
) {
1122 nfsm_chain_add_32(error
, &nmreq
, RPCAUTH_NONE
); /* auth */
1123 nfsm_chain_add_32(error
, &nmreq
, 0); /* length */
1124 nfsm_chain_add_32(error
, &nmreq
, RPCAUTH_NONE
); /* verf */
1125 nfsm_chain_add_32(error
, &nmreq
, 0); /* length */
1126 nfsm_chain_build_done(error
, &nmreq
);
1127 /* Append the args mbufs */
1129 error
= mbuf_setnext(nmreq
.nmc_mcur
, mrest
);
1132 gid_t grouplist
[NGROUPS
];
1135 nfsm_chain_add_32(error
, &nmreq
, RPCAUTH_SYS
);
1136 nfsm_chain_add_32(error
, &nmreq
, authsiz
);
1137 nfsm_chain_add_32(error
, &nmreq
, 0); /* stamp */
1138 nfsm_chain_add_32(error
, &nmreq
, 0); /* zero-length hostname */
1139 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getuid(cred
)); /* UID */
1140 nfsm_chain_add_32(error
, &nmreq
, kauth_cred_getgid(cred
)); /* GID */
1141 grpsiz
= (auth_len
>> 2) - 5;
1142 nfsm_chain_add_32(error
, &nmreq
, grpsiz
);/* additional GIDs */
1143 memset(grouplist
, 0, sizeof(grouplist
));
1144 groupcount
= grpsiz
;
1145 (void)kauth_cred_getgroups(cred
, grouplist
, &groupcount
);
1146 for (i
= 1; i
<= grpsiz
; i
++)
1147 nfsm_chain_add_32(error
, &nmreq
, grouplist
[i
]);
1149 /* And the verifier... */
1150 nfsm_chain_add_32(error
, &nmreq
, RPCAUTH_NONE
); /* flavor */
1151 nfsm_chain_add_32(error
, &nmreq
, 0); /* length */
1152 nfsm_chain_build_done(error
, &nmreq
);
1154 /* Append the args mbufs */
1156 error
= mbuf_setnext(nmreq
.nmc_mcur
, mrest
);
1162 error
= nfs_gss_clnt_cred_put(req
, &nmreq
, mrest
);
1163 if (error
== ENEEDAUTH
) {
1164 gid_t grouplist
[NGROUPS
];
1165 int groupcount
= NGROUPS
;
1167 * Use sec=sys for this user
1170 req
->r_auth
= auth_type
= RPCAUTH_SYS
;
1171 (void)kauth_cred_getgroups(cred
, grouplist
, &groupcount
);
1172 auth_len
= ((((groupcount
- 1) > nmp
->nm_numgrps
) ?
1173 nmp
->nm_numgrps
: (groupcount
- 1)) << 2) +
1175 authsiz
= nfsm_rndup(auth_len
);
1181 /* finish setting up the packet */
1183 error
= mbuf_pkthdr_setrcvif(mreq
, 0);
1190 /* Calculate the size of the request */
1192 for (mb
= nmreq
.nmc_mhead
; mb
; mb
= mbuf_next(mb
))
1193 reqlen
+= mbuf_len(mb
);
1195 mbuf_pkthdr_setlen(mreq
, reqlen
);
1198 * If the request goes on a TCP stream,
1199 * set its size in the RPC record mark.
1200 * The record mark count doesn't include itself
1201 * and the last fragment bit is set.
1203 if (sotype
== SOCK_STREAM
)
1204 nfsm_chain_set_recmark(error
, &nmreq
,
1205 (reqlen
- NFSX_UNSIGNED
) | 0x80000000);
1212 * Parse an NFS file attribute structure out of an mbuf chain.
1215 nfs_parsefattr(struct nfsm_chain
*nmc
, int nfsvers
, struct nfs_vattr
*nvap
)
1226 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TYPE
);
1227 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_MODE
);
1228 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_NUMLINKS
);
1229 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_OWNER
);
1230 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_OWNER_GROUP
);
1231 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_SIZE
);
1232 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_SPACE_USED
);
1233 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_RAWDEV
);
1234 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_FSID
);
1235 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_FILEID
);
1236 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TIME_ACCESS
);
1237 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TIME_MODIFY
);
1238 NFS_BITMAP_SET(nvap
->nva_bitmap
, NFS_FATTR_TIME_METADATA
);
1240 nfsm_chain_get_32(error
, nmc
, vtype
);
1241 nfsm_chain_get_32(error
, nmc
, vmode
);
1244 if (nfsvers
== NFS_VER3
) {
1245 nvap
->nva_type
= nfstov_type(vtype
, nfsvers
);
1248 * The duplicate information returned in fa_type and fa_mode
1249 * is an ambiguity in the NFS version 2 protocol.
1251 * VREG should be taken literally as a regular file. If a
1252 * server intends to return some type information differently
1253 * in the upper bits of the mode field (e.g. for sockets, or
1254 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1255 * leave the examination of the mode bits even in the VREG
1256 * case to avoid breakage for bogus servers, but we make sure
1257 * that there are actually type bits set in the upper part of
1258 * fa_mode (and failing that, trust the va_type field).
1260 * NFSv3 cleared the issue, and requires fa_mode to not
1261 * contain any type information (while also introducing
1262 * sockets and FIFOs for fa_type).
1264 vtype
= nfstov_type(vtype
, nfsvers
);
1265 if ((vtype
== VNON
) || ((vtype
== VREG
) && ((vmode
& S_IFMT
) != 0)))
1266 vtype
= IFTOVT(vmode
);
1267 nvap
->nva_type
= vtype
;
1270 nvap
->nva_mode
= (vmode
& 07777);
1272 nfsm_chain_get_32(error
, nmc
, nvap
->nva_nlink
);
1273 nfsm_chain_get_32(error
, nmc
, nvap
->nva_uid
);
1274 nfsm_chain_get_32(error
, nmc
, nvap
->nva_gid
);
1276 if (nfsvers
== NFS_VER3
) {
1277 nfsm_chain_get_64(error
, nmc
, nvap
->nva_size
);
1278 nfsm_chain_get_64(error
, nmc
, nvap
->nva_bytes
);
1279 nfsm_chain_get_32(error
, nmc
, nvap
->nva_rawdev
.specdata1
);
1280 nfsm_chain_get_32(error
, nmc
, nvap
->nva_rawdev
.specdata2
);
1282 nfsm_chain_get_64(error
, nmc
, nvap
->nva_fsid
.major
);
1283 nvap
->nva_fsid
.minor
= 0;
1284 nfsm_chain_get_64(error
, nmc
, nvap
->nva_fileid
);
1286 nfsm_chain_get_32(error
, nmc
, nvap
->nva_size
);
1287 nfsm_chain_adv(error
, nmc
, NFSX_UNSIGNED
);
1288 nfsm_chain_get_32(error
, nmc
, rdev
);
1290 nvap
->nva_rawdev
.specdata1
= major(rdev
);
1291 nvap
->nva_rawdev
.specdata2
= minor(rdev
);
1292 nfsm_chain_get_32(error
, nmc
, val
); /* blocks */
1294 nvap
->nva_bytes
= val
* NFS_FABLKSIZE
;
1295 nfsm_chain_get_32(error
, nmc
, val
);
1297 nvap
->nva_fsid
.major
= (uint64_t)val
;
1298 nvap
->nva_fsid
.minor
= 0;
1299 nfsm_chain_get_32(error
, nmc
, val
);
1301 nvap
->nva_fileid
= (uint64_t)val
;
1302 /* Really ugly NFSv2 kludge. */
1303 if ((vtype
== VCHR
) && (rdev
== (dev_t
)0xffffffff))
1304 nvap
->nva_type
= VFIFO
;
1306 nfsm_chain_get_time(error
, nmc
, nfsvers
,
1307 nvap
->nva_timesec
[NFSTIME_ACCESS
],
1308 nvap
->nva_timensec
[NFSTIME_ACCESS
]);
1309 nfsm_chain_get_time(error
, nmc
, nfsvers
,
1310 nvap
->nva_timesec
[NFSTIME_MODIFY
],
1311 nvap
->nva_timensec
[NFSTIME_MODIFY
]);
1312 nfsm_chain_get_time(error
, nmc
, nfsvers
,
1313 nvap
->nva_timesec
[NFSTIME_CHANGE
],
1314 nvap
->nva_timensec
[NFSTIME_CHANGE
]);
1320 * Load the attribute cache (that lives in the nfsnode entry) with
1321 * the value pointed to by nvap, unless the file type in the attribute
1322 * cache doesn't match the file type in the nvap, in which case log a
1323 * warning and return ESTALE.
1325 * If the dontshrink flag is set, then it's not safe to call ubc_setsize()
1326 * to shrink the size of the file.
1331 struct nfs_vattr
*nvap
,
1338 struct nfs_vattr
*npnvap
;
1339 int xattr
= np
->n_vattr
.nva_flags
& NFS_FFLAG_IS_ATTR
;
1340 int referral
= np
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER_REFERRAL
;
1341 int aclbit
, monitored
, error
= 0;
1343 struct nfsmount
*nmp
;
1344 uint32_t events
= np
->n_events
;
1346 if (np
->n_hflag
& NHINIT
) {
1351 mp
= vnode_mount(vp
);
1353 monitored
= vp
? vnode_ismonitored(vp
) : 0;
1355 FSDBG_TOP(527, np
, vp
, *xidp
>> 32, *xidp
);
1357 if (!((nmp
= VFSTONFS(mp
)))) {
1358 FSDBG_BOT(527, ENXIO
, 1, 0, *xidp
);
1362 if (*xidp
< np
->n_xid
) {
1364 * We have already updated attributes with a response from
1365 * a later request. The attributes we have here are probably
1366 * stale so we drop them (just return). However, our
1367 * out-of-order receipt could be correct - if the requests were
1368 * processed out of order at the server. Given the uncertainty
1369 * we invalidate our cached attributes. *xidp is zeroed here
1370 * to indicate the attributes were dropped - only getattr
1371 * cares - it needs to retry the rpc.
1373 NATTRINVALIDATE(np
);
1374 FSDBG_BOT(527, 0, np
, np
->n_xid
, *xidp
);
1379 if (vp
&& (nvap
->nva_type
!= vnode_vtype(vp
))) {
1381 * The filehandle has changed type on us. This can be
1382 * caused by either the server not having unique filehandles
1383 * or because another client has removed the previous
1384 * filehandle and a new object (of a different type)
1385 * has been created with the same filehandle.
1387 * We can't simply switch the type on the vnode because
1388 * there may be type-specific fields that need to be
1389 * cleaned up or set up.
1391 * So, what should we do with this vnode?
1393 * About the best we can do is log a warning and return
1394 * an error. ESTALE is about the closest error, but it
1395 * is a little strange that we come up with this error
1396 * internally instead of simply passing it through from
1397 * the server. Hopefully, the vnode will be reclaimed
1398 * soon so the filehandle can be reincarnated as the new
1401 printf("nfs loadattrcache vnode changed type, was %d now %d\n",
1402 vnode_vtype(vp
), nvap
->nva_type
);
1405 events
|= VNODE_EVENT_DELETE
;
1409 npnvap
= &np
->n_vattr
;
1412 * The ACL cache needs special handling because it is not
1413 * always updated. Save current ACL cache state so it can
1414 * be restored after copying the new attributes into place.
1416 aclbit
= NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_ACL
);
1417 acl
= npnvap
->nva_acl
;
1421 * For monitored nodes, check for attribute changes that should generate events.
1423 if (NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_NUMLINKS
) &&
1424 (nvap
->nva_nlink
!= npnvap
->nva_nlink
))
1425 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_LINK
;
1426 if (events
& VNODE_EVENT_PERMS
)
1427 /* no need to do all the checking if it's already set */;
1428 else if (NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_MODE
) &&
1429 (nvap
->nva_mode
!= npnvap
->nva_mode
))
1430 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_PERMS
;
1431 else if (NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_OWNER
) &&
1432 (nvap
->nva_uid
!= npnvap
->nva_uid
))
1433 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_PERMS
;
1434 else if (NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_OWNER_GROUP
) &&
1435 (nvap
->nva_gid
!= npnvap
->nva_gid
))
1436 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_PERMS
;
1437 else if (nmp
->nm_vers
>= NFS_VER4
) {
1438 if (NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_OWNER
) &&
1439 !kauth_guid_equal(&nvap
->nva_uuuid
, &npnvap
->nva_uuuid
))
1440 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_PERMS
;
1441 else if (NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_OWNER_GROUP
) &&
1442 !kauth_guid_equal(&nvap
->nva_guuid
, &npnvap
->nva_guuid
))
1443 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_PERMS
;
1444 else if ((NFS_BITMAP_ISSET(nvap
->nva_bitmap
, NFS_FATTR_ACL
) &&
1445 nvap
->nva_acl
&& npnvap
->nva_acl
&&
1446 ((nvap
->nva_acl
->acl_entrycount
!= npnvap
->nva_acl
->acl_entrycount
) ||
1447 bcmp(nvap
->nva_acl
, npnvap
->nva_acl
, KAUTH_ACL_COPYSIZE(nvap
->nva_acl
)))))
1448 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_PERMS
;
1450 if (((nmp
->nm_vers
>= NFS_VER4
) && (nvap
->nva_change
!= npnvap
->nva_change
)) ||
1451 (NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_TIME_MODIFY
) &&
1452 ((nvap
->nva_timesec
[NFSTIME_MODIFY
] != npnvap
->nva_timesec
[NFSTIME_MODIFY
]) ||
1453 (nvap
->nva_timensec
[NFSTIME_MODIFY
] != npnvap
->nva_timensec
[NFSTIME_MODIFY
]))))
1454 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_WRITE
;
1455 if (!events
&& NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_RAWDEV
) &&
1456 ((nvap
->nva_rawdev
.specdata1
!= npnvap
->nva_rawdev
.specdata1
) ||
1457 (nvap
->nva_rawdev
.specdata2
!= npnvap
->nva_rawdev
.specdata2
)))
1458 events
|= VNODE_EVENT_ATTRIB
;
1459 if (!events
&& NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_FILEID
) &&
1460 (nvap
->nva_fileid
!= npnvap
->nva_fileid
))
1461 events
|= VNODE_EVENT_ATTRIB
;
1462 if (!events
&& NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_ARCHIVE
) &&
1463 ((nvap
->nva_flags
& NFS_FFLAG_ARCHIVED
) != (npnvap
->nva_flags
& NFS_FFLAG_ARCHIVED
)))
1464 events
|= VNODE_EVENT_ATTRIB
;
1465 if (!events
&& NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_HIDDEN
) &&
1466 ((nvap
->nva_flags
& NFS_FFLAG_HIDDEN
) != (npnvap
->nva_flags
& NFS_FFLAG_HIDDEN
)))
1467 events
|= VNODE_EVENT_ATTRIB
;
1468 if (!events
&& NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_TIME_CREATE
) &&
1469 ((nvap
->nva_timesec
[NFSTIME_CREATE
] != npnvap
->nva_timesec
[NFSTIME_CREATE
]) ||
1470 (nvap
->nva_timensec
[NFSTIME_CREATE
] != npnvap
->nva_timensec
[NFSTIME_CREATE
])))
1471 events
|= VNODE_EVENT_ATTRIB
;
1472 if (!events
&& NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_TIME_BACKUP
) &&
1473 ((nvap
->nva_timesec
[NFSTIME_BACKUP
] != npnvap
->nva_timesec
[NFSTIME_BACKUP
]) ||
1474 (nvap
->nva_timensec
[NFSTIME_BACKUP
] != npnvap
->nva_timensec
[NFSTIME_BACKUP
])))
1475 events
|= VNODE_EVENT_ATTRIB
;
1478 /* Copy the attributes to the attribute cache */
1479 bcopy((caddr_t
)nvap
, (caddr_t
)npnvap
, sizeof(*nvap
));
1482 np
->n_attrstamp
= now
.tv_sec
;
1484 /* NFS_FFLAG_IS_ATTR and NFS_FFLAG_TRIGGER_REFERRAL need to be sticky... */
1486 nvap
->nva_flags
|= xattr
;
1488 nvap
->nva_flags
|= referral
;
1490 if (NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_ACL
)) {
1491 /* we're updating the ACL */
1492 if (nvap
->nva_acl
) {
1493 /* make a copy of the acl for the cache */
1494 npnvap
->nva_acl
= kauth_acl_alloc(nvap
->nva_acl
->acl_entrycount
);
1495 if (npnvap
->nva_acl
) {
1496 bcopy(nvap
->nva_acl
, npnvap
->nva_acl
, KAUTH_ACL_COPYSIZE(nvap
->nva_acl
));
1498 /* can't make a copy to cache, invalidate ACL cache */
1499 NFS_BITMAP_CLR(npnvap
->nva_bitmap
, NFS_FATTR_ACL
);
1505 kauth_acl_free(acl
);
1509 if (NFS_BITMAP_ISSET(npnvap
->nva_bitmap
, NFS_FATTR_ACL
)) {
1510 /* update the ACL timestamp */
1511 np
->n_aclstamp
= now
.tv_sec
;
1513 /* we aren't updating the ACL, so restore original values */
1515 NFS_BITMAP_SET(npnvap
->nva_bitmap
, NFS_FATTR_ACL
);
1516 npnvap
->nva_acl
= acl
;
1521 * For NFSv4, if the fsid doesn't match the fsid for the mount, then
1522 * this node is for a different file system on the server. So we mark
1523 * this node as a trigger node that will trigger the mirror mount.
1525 if ((nmp
->nm_vers
>= NFS_VER4
) && (nvap
->nva_type
== VDIR
) &&
1526 ((np
->n_vattr
.nva_fsid
.major
!= nmp
->nm_fsid
.major
) ||
1527 (np
->n_vattr
.nva_fsid
.minor
!= nmp
->nm_fsid
.minor
)))
1528 np
->n_vattr
.nva_flags
|= NFS_FFLAG_TRIGGER
;
1531 if (!vp
|| (nvap
->nva_type
!= VREG
)) {
1532 np
->n_size
= nvap
->nva_size
;
1533 } else if (nvap
->nva_size
!= np
->n_size
) {
1534 FSDBG(527, np
, nvap
->nva_size
, np
->n_size
, (nvap
->nva_type
== VREG
) | (np
->n_flag
& NMODIFIED
? 6 : 4));
1535 if (!UBCINFOEXISTS(vp
) || (dontshrink
&& (nvap
->nva_size
< np
->n_size
))) {
1536 /* asked not to shrink, so stick with current size */
1537 FSDBG(527, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
1538 nvap
->nva_size
= np
->n_size
;
1539 NATTRINVALIDATE(np
);
1540 } else if ((np
->n_flag
& NMODIFIED
) && (nvap
->nva_size
< np
->n_size
)) {
1541 /* if we've modified, stick with larger size */
1542 FSDBG(527, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0002);
1543 nvap
->nva_size
= np
->n_size
;
1544 npnvap
->nva_size
= np
->n_size
;
1547 * n_size is protected by the data lock, so we need to
1548 * defer updating it until it's safe. We save the new size
1549 * and set a flag and it'll get updated the next time we get/drop
1550 * the data lock or the next time we do a getattr.
1552 np
->n_newsize
= nvap
->nva_size
;
1553 SET(np
->n_flag
, NUPDATESIZE
);
1555 events
|= VNODE_EVENT_ATTRIB
| VNODE_EVENT_EXTEND
;
1559 if (np
->n_flag
& NCHG
) {
1560 if (np
->n_flag
& NACC
) {
1561 nvap
->nva_timesec
[NFSTIME_ACCESS
] = np
->n_atim
.tv_sec
;
1562 nvap
->nva_timensec
[NFSTIME_ACCESS
] = np
->n_atim
.tv_nsec
;
1564 if (np
->n_flag
& NUPD
) {
1565 nvap
->nva_timesec
[NFSTIME_MODIFY
] = np
->n_mtim
.tv_sec
;
1566 nvap
->nva_timensec
[NFSTIME_MODIFY
] = np
->n_mtim
.tv_nsec
;
1571 if (monitored
&& events
)
1572 nfs_vnode_notify(np
, events
);
1573 FSDBG_BOT(527, error
, np
, np
->n_size
, *xidp
);
1578 * Calculate the attribute timeout based on
1579 * how recently the file has been modified.
1582 nfs_attrcachetimeout(nfsnode_t np
)
1584 struct nfsmount
*nmp
;
1589 if (!(nmp
= NFSTONMP(np
)))
1592 isdir
= vnode_isdir(NFSTOV(np
));
1594 if ((nmp
->nm_vers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
)) {
1595 /* If we have a delegation, we always use the max timeout. */
1596 timeo
= isdir
? nmp
->nm_acdirmax
: nmp
->nm_acregmax
;
1597 } else if ((np
)->n_flag
& NMODIFIED
) {
1598 /* If we have modifications, we always use the min timeout. */
1599 timeo
= isdir
? nmp
->nm_acdirmin
: nmp
->nm_acregmin
;
1601 /* Otherwise, we base the timeout on how old the file seems. */
1602 /* Note that if the client and server clocks are way out of sync, */
1603 /* timeout will probably get clamped to a min or max value */
1605 timeo
= (now
.tv_sec
- (np
)->n_vattr
.nva_timesec
[NFSTIME_MODIFY
]) / 10;
1607 if (timeo
< nmp
->nm_acdirmin
)
1608 timeo
= nmp
->nm_acdirmin
;
1609 else if (timeo
> nmp
->nm_acdirmax
)
1610 timeo
= nmp
->nm_acdirmax
;
1612 if (timeo
< nmp
->nm_acregmin
)
1613 timeo
= nmp
->nm_acregmin
;
1614 else if (timeo
> nmp
->nm_acregmax
)
1615 timeo
= nmp
->nm_acregmax
;
1623 * Check the attribute cache time stamp.
1624 * If the cache is valid, copy contents to *nvaper and return 0
1625 * otherwise return an error.
1626 * Must be called with the node locked.
1629 nfs_getattrcache(nfsnode_t np
, struct nfs_vattr
*nvaper
, int flags
)
1631 struct nfs_vattr
*nvap
;
1632 struct timeval nowup
;
1635 /* Check if the attributes are valid. */
1636 if (!NATTRVALID(np
) || ((flags
& NGA_ACL
) && !NACLVALID(np
))) {
1637 FSDBG(528, np
, 0, 0xffffff01, ENOENT
);
1638 OSAddAtomic(1, &nfsstats
.attrcache_misses
);
1642 /* Verify the cached attributes haven't timed out. */
1643 timeo
= nfs_attrcachetimeout(np
);
1644 microuptime(&nowup
);
1645 if ((nowup
.tv_sec
- np
->n_attrstamp
) >= timeo
) {
1646 FSDBG(528, np
, 0, 0xffffff02, ENOENT
);
1647 OSAddAtomic(1, &nfsstats
.attrcache_misses
);
1650 if ((flags
& NGA_ACL
) && ((nowup
.tv_sec
- np
->n_aclstamp
) >= timeo
)) {
1651 FSDBG(528, np
, 0, 0xffffff02, ENOENT
);
1652 OSAddAtomic(1, &nfsstats
.attrcache_misses
);
1656 nvap
= &np
->n_vattr
;
1657 FSDBG(528, np
, nvap
->nva_size
, np
->n_size
, 0xcace);
1658 OSAddAtomic(1, &nfsstats
.attrcache_hits
);
1660 if (nvap
->nva_type
!= VREG
) {
1661 np
->n_size
= nvap
->nva_size
;
1662 } else if (nvap
->nva_size
!= np
->n_size
) {
1663 FSDBG(528, np
, nvap
->nva_size
, np
->n_size
, (nvap
->nva_type
== VREG
) | (np
->n_flag
& NMODIFIED
? 6 : 4));
1664 if ((np
->n_flag
& NMODIFIED
) && (nvap
->nva_size
< np
->n_size
)) {
1665 /* if we've modified, stick with larger size */
1666 nvap
->nva_size
= np
->n_size
;
1669 * n_size is protected by the data lock, so we need to
1670 * defer updating it until it's safe. We save the new size
1671 * and set a flag and it'll get updated the next time we get/drop
1672 * the data lock or the next time we do a getattr.
1674 np
->n_newsize
= nvap
->nva_size
;
1675 SET(np
->n_flag
, NUPDATESIZE
);
1679 bcopy((caddr_t
)nvap
, (caddr_t
)nvaper
, sizeof(struct nfs_vattr
));
1680 if (np
->n_flag
& NCHG
) {
1681 if (np
->n_flag
& NACC
) {
1682 nvaper
->nva_timesec
[NFSTIME_ACCESS
] = np
->n_atim
.tv_sec
;
1683 nvaper
->nva_timensec
[NFSTIME_ACCESS
] = np
->n_atim
.tv_nsec
;
1685 if (np
->n_flag
& NUPD
) {
1686 nvaper
->nva_timesec
[NFSTIME_MODIFY
] = np
->n_mtim
.tv_sec
;
1687 nvaper
->nva_timensec
[NFSTIME_MODIFY
] = np
->n_mtim
.tv_nsec
;
1690 if (nvap
->nva_acl
) {
1691 if (flags
& NGA_ACL
) {
1692 nvaper
->nva_acl
= kauth_acl_alloc(nvap
->nva_acl
->acl_entrycount
);
1693 if (!nvaper
->nva_acl
)
1695 bcopy(nvap
->nva_acl
, nvaper
->nva_acl
, KAUTH_ACL_COPYSIZE(nvap
->nva_acl
));
1697 nvaper
->nva_acl
= NULL
;
1704 * When creating file system objects:
1705 * Don't bother setting UID if it's the same as the credential performing the create.
1706 * Don't bother setting GID if it's the same as the directory or credential.
1709 nfs_avoid_needless_id_setting_on_create(nfsnode_t dnp
, struct vnode_attr
*vap
, vfs_context_t ctx
)
1711 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
1712 if (kauth_cred_getuid(vfs_context_ucred(ctx
)) == vap
->va_uid
) {
1713 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
1714 VATTR_CLEAR_ACTIVE(vap
, va_uuuid
);
1717 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
1718 if ((vap
->va_gid
== dnp
->n_vattr
.nva_gid
) ||
1719 (kauth_cred_getgid(vfs_context_ucred(ctx
)) == vap
->va_gid
)) {
1720 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
1721 VATTR_CLEAR_ACTIVE(vap
, va_guuid
);
1727 * Convert a universal address string to a sockaddr structure.
1729 * Universal addresses can be in the following formats:
1731 * d = decimal (IPv4)
1732 * x = hexadecimal (IPv6)
1733 * p = port (decimal)
1738 * x:x:x:x:x:x:x:x.p.p
1739 * x:x:x:x:x:x:d.d.d.d
1740 * x:x:x:x:x:x:d.d.d.d.p.p
1742 * IPv6 strings can also have a series of zeroes elided
1743 * IPv6 strings can also have a %scope suffix at the end (after any port)
1745 * rules & exceptions:
1746 * - value before : is hex
1747 * - value before . is dec
1748 * - once . hit, all values are dec
1749 * - hex+port case means value before first dot is actually hex
1750 * - . is always preceded by digits except if last hex was double-colon
1752 * scan, converting #s to bytes
1753 * first time a . is encountered, scan the rest to count them.
1754 * 2 dots = just port
1755 * 3 dots = just IPv4 no port
1756 * 5 dots = IPv4 and port
1759 #define IS_DIGIT(C) \
1760 (((C) >= '0') && ((C) <= '9'))
1762 #define IS_XDIGIT(C) \
1764 (((C) >= 'A') && ((C) <= 'F')) || \
1765 (((C) >= 'a') && ((C) <= 'f')))
1768 nfs_uaddr2sockaddr(const char *uaddr
, struct sockaddr
*addr
)
1770 const char *p
, *pd
; /* pointers to current character in scan */
1771 const char *pnum
; /* pointer to current number to decode */
1772 const char *pscope
; /* pointer to IPv6 scope ID */
1773 uint8_t a
[18]; /* octet array to store address bytes */
1774 int i
; /* index of next octet to decode */
1775 int dci
; /* index of octet to insert double-colon zeroes */
1776 int dcount
, xdcount
; /* count of digits in current number */
1777 int needmore
; /* set when we know we need more input (e.g. after colon, period) */
1778 int dots
; /* # of dots */
1779 int hex
; /* contains hex values */
1780 unsigned long val
; /* decoded value */
1781 int s
; /* index used for sliding array to insert elided zeroes */
1784 #define DECIMALVALUE 1
1787 if ((dcount <= 0) || (dcount > (((TYPE) == DECIMALVALUE) ? 3 : 4))) \
1789 if (((TYPE) == DECIMALVALUE) && xdcount) \
1791 val = strtoul(pnum, NULL, ((TYPE) == DECIMALVALUE) ? 10 : 16); \
1792 if (((TYPE) == DECIMALVALUE) && (val >= 256)) \
1794 /* check if there is room left in the array */ \
1795 if (i > (int)(sizeof(a) - (((TYPE) == HEXVALUE) ? 2 : 1) - ((dci != -1) ? 2 : 0))) \
1797 if ((TYPE) == HEXVALUE) \
1798 a[i++] = ((val >> 8) & 0xff); \
1799 a[i++] = (val & 0xff); \
1805 i
= dcount
= xdcount
= 0;
1809 if ((*p
== ':') && (*++p
!= ':')) /* if it starts with colon, gotta be a double */
1813 if (IS_XDIGIT(*p
)) {
1819 } else if (*p
== '.') {
1820 /* rest is decimal IPv4 dotted quad and/or port */
1822 /* this is the first, so count them */
1823 for (pd
= p
; *pd
; pd
++) {
1827 } else if (hex
&& (*pd
== '%')) {
1829 } else if ((*pd
< '0') || (*pd
> '9')) {
1833 if ((dots
!= 2) && (dots
!= 3) && (dots
!= 5))
1835 if (hex
&& (dots
== 2)) { /* hex+port */
1836 if (!dcount
&& needmore
)
1838 if (dcount
) /* last hex may be elided zero */
1846 dcount
= xdcount
= 0;
1849 } else if (*p
== ':') {
1853 if (!dcount
) { /* missing number, probably double colon */
1854 if (dci
>= 0) /* can only have one double colon */
1860 dcount
= xdcount
= 0;
1864 } else if (*p
== '%') { /* scope ID delimiter */
1870 } else { /* unexpected character */
1874 if (needmore
&& !dcount
)
1876 if (dcount
) /* decode trailing number */
1877 GET(dots
? DECIMALVALUE
: HEXVALUE
);
1878 if (dci
>= 0) { /* got a double-colon at i, need to insert a range of zeroes */
1879 /* if we got a port, slide to end of array */
1880 /* otherwise, slide to end of address (non-port) values */
1881 int end
= ((dots
== 2) || (dots
== 5)) ? sizeof(a
) : (sizeof(a
) - 2);
1882 if (i
% 2) /* length of zero range must be multiple of 2 */
1884 if (i
>= end
) /* no room? */
1886 /* slide (i-dci) numbers up from index dci */
1887 for (s
=0; s
< (i
- dci
); s
++)
1888 a
[end
-1-s
] = a
[i
-1-s
];
1889 /* zero (end-i) numbers at index dci */
1890 for (s
=0; s
< (end
- i
); s
++)
1895 /* copy out resulting socket address */
1897 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
1898 if ((((dots
== 0) || (dots
== 3)) && (i
!= (sizeof(a
)-2))))
1900 if ((((dots
== 2) || (dots
== 5)) && (i
!= sizeof(a
))))
1902 bzero(sin6
, sizeof(struct sockaddr_in6
));
1903 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
1904 sin6
->sin6_family
= AF_INET6
;
1905 bcopy(a
, &sin6
->sin6_addr
.s6_addr
, sizeof(struct in6_addr
));
1906 if ((dots
== 5) || (dots
== 2))
1907 sin6
->sin6_port
= htons((a
[16] << 8) | a
[17]);
1909 for (p
=pscope
; IS_DIGIT(*p
); p
++)
1911 if (*p
&& !IS_DIGIT(*p
)) { /* name */
1912 ifnet_t interface
= NULL
;
1913 if (ifnet_find_by_name(pscope
, &interface
) == 0)
1914 sin6
->sin6_scope_id
= ifnet_index(interface
);
1916 ifnet_release(interface
);
1917 } else { /* decimal number */
1918 sin6
->sin6_scope_id
= strtoul(pscope
, NULL
, 10);
1920 /* XXX should we also embed scope id for linklocal? */
1923 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
1924 if ((dots
!= 3) && (dots
!= 5))
1926 if ((dots
== 3) && (i
!= 4))
1928 if ((dots
== 5) && (i
!= 6))
1930 bzero(sin
, sizeof(struct sockaddr_in
));
1931 sin
->sin_len
= sizeof(struct sockaddr_in
);
1932 sin
->sin_family
= AF_INET
;
1933 bcopy(a
, &sin
->sin_addr
.s_addr
, sizeof(struct in_addr
));
1935 sin
->sin_port
= htons((a
[4] << 8) | a
[5]);
1941 #endif /* NFSCLIENT */
1944 * Schedule a callout thread to run an NFS timer function
1945 * interval milliseconds in the future.
1948 nfs_interval_timer_start(thread_call_t call
, int interval
)
1952 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
1953 thread_call_enter_delayed(call
, deadline
);
1959 int nfsrv_cmp_secflavs(struct nfs_sec
*, struct nfs_sec
*);
1960 int nfsrv_hang_addrlist(struct nfs_export
*, struct user_nfs_export_args
*);
1961 int nfsrv_free_netopt(struct radix_node
*, void *);
1962 int nfsrv_free_addrlist(struct nfs_export
*, struct user_nfs_export_args
*);
1963 struct nfs_export_options
*nfsrv_export_lookup(struct nfs_export
*, mbuf_t
);
1964 struct nfs_export
*nfsrv_fhtoexport(struct nfs_filehandle
*);
1965 struct nfs_user_stat_node
*nfsrv_get_user_stat_node(struct nfs_active_user_list
*, struct sockaddr
*, uid_t
);
1966 void nfsrv_init_user_list(struct nfs_active_user_list
*);
1967 void nfsrv_free_user_list(struct nfs_active_user_list
*);
1970 * add NFSv3 WCC data to an mbuf chain
1973 nfsm_chain_add_wcc_data_f(
1974 struct nfsrv_descript
*nd
,
1975 struct nfsm_chain
*nmc
,
1977 struct vnode_attr
*prevap
,
1979 struct vnode_attr
*postvap
)
1984 nfsm_chain_add_32(error
, nmc
, FALSE
);
1986 nfsm_chain_add_32(error
, nmc
, TRUE
);
1987 nfsm_chain_add_64(error
, nmc
, prevap
->va_data_size
);
1988 nfsm_chain_add_time(error
, nmc
, NFS_VER3
, &prevap
->va_modify_time
);
1989 nfsm_chain_add_time(error
, nmc
, NFS_VER3
, &prevap
->va_change_time
);
1991 nfsm_chain_add_postop_attr(error
, nd
, nmc
, postattrerr
, postvap
);
1997 * Extract a lookup path from the given mbufs and store it in
1998 * a newly allocated buffer saved in the given nameidata structure.
2001 nfsm_chain_get_path_namei(
2002 struct nfsm_chain
*nmc
,
2004 struct nameidata
*nip
)
2006 struct componentname
*cnp
= &nip
->ni_cnd
;
2010 if (len
> (MAXPATHLEN
- 1))
2011 return (ENAMETOOLONG
);
2014 * Get a buffer for the name to be translated, and copy the
2015 * name into the buffer.
2017 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
2020 cnp
->cn_pnlen
= MAXPATHLEN
;
2021 cnp
->cn_flags
|= HASBUF
;
2023 /* Copy the name from the mbuf list to the string */
2025 nfsm_chain_get_opaque(error
, nmc
, len
, cp
);
2028 cnp
->cn_pnbuf
[len
] = '\0';
2030 /* sanity check the string */
2031 if ((strlen(cp
) != len
) || strchr(cp
, '/'))
2036 FREE_ZONE(cnp
->cn_pnbuf
, MAXPATHLEN
, M_NAMEI
);
2037 cnp
->cn_flags
&= ~HASBUF
;
2039 nip
->ni_pathlen
= len
;
2045 * Set up nameidata for a lookup() call and do it.
2049 struct nfsrv_descript
*nd
,
2051 struct nameidata
*nip
,
2052 struct nfs_filehandle
*nfhp
,
2054 struct nfs_export
**nxp
,
2055 struct nfs_export_options
**nxop
)
2059 struct componentname
*cnp
= &nip
->ni_cnd
;
2066 * Extract and set starting directory.
2068 error
= nfsrv_fhtovp(nfhp
, nd
, &dp
, nxp
, nxop
);
2071 error
= nfsrv_credcheck(nd
, ctx
, *nxp
, *nxop
);
2072 if (error
|| (vnode_vtype(dp
) != VDIR
)) {
2079 nip
->ni_cnd
.cn_context
= ctx
;
2081 if (*nxop
&& ((*nxop
)->nxo_flags
& NX_READONLY
))
2082 cnp
->cn_flags
|= RDONLY
;
2084 cnp
->cn_flags
|= NOCROSSMOUNT
;
2085 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
2086 nip
->ni_usedvp
= nip
->ni_startdir
= dp
;
2089 * And call lookup() to do the real work
2091 cnflags
= nip
->ni_cnd
.cn_flags
; /* store in case we have to restore */
2092 while ((error
= lookup(nip
)) == ERECYCLE
) {
2093 nip
->ni_cnd
.cn_flags
= cnflags
;
2094 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
2095 nip
->ni_usedvp
= nip
->ni_dvp
= nip
->ni_startdir
= dp
;
2100 /* Check for encountering a symbolic link */
2101 if (cnp
->cn_flags
& ISSYMLINK
) {
2103 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
2104 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
2105 unlock_fsnode(nip
->ni_dvp
, NULL
);
2107 #endif /* __LP64__ */
2108 if (cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
))
2109 vnode_put(nip
->ni_dvp
);
2111 vnode_put(nip
->ni_vp
);
2118 tmppn
= cnp
->cn_pnbuf
;
2119 cnp
->cn_pnbuf
= NULL
;
2120 cnp
->cn_flags
&= ~HASBUF
;
2121 FREE_ZONE(tmppn
, cnp
->cn_pnlen
, M_NAMEI
);
2127 * A fiddled version of m_adj() that ensures null fill to a 4-byte
2128 * boundary and only trims off the back end
2131 nfsm_adj(mbuf_t mp
, int len
, int nul
)
2138 * Trim from tail. Scan the mbuf chain,
2139 * calculating its length and finding the last mbuf.
2140 * If the adjustment only affects this mbuf, then just
2141 * adjust and return. Otherwise, rescan and truncate
2142 * after the remaining size.
2149 mnext
= mbuf_next(m
);
2156 mbuf_setlen(m
, mlen
);
2158 cp
= (caddr_t
)mbuf_data(m
) + mlen
- nul
;
2159 for (i
= 0; i
< nul
; i
++)
2168 * Correct length for chain is "count".
2169 * Find the mbuf with last data, adjust its length,
2170 * and toss data from remaining mbufs on chain.
2172 for (m
= mp
; m
; m
= mbuf_next(m
)) {
2174 if (mlen
>= count
) {
2176 mbuf_setlen(m
, count
);
2178 cp
= (caddr_t
)mbuf_data(m
) + mlen
- nul
;
2179 for (i
= 0; i
< nul
; i
++)
2186 for (m
= mbuf_next(m
); m
; m
= mbuf_next(m
))
2191 * Trim the header out of the mbuf list and trim off any trailing
2192 * junk so that the mbuf list has only the write data.
2195 nfsm_chain_trim_data(struct nfsm_chain
*nmc
, int len
, int *mlen
)
2197 int cnt
= 0, dlen
, adjust
;
2205 for (m
= nmc
->nmc_mhead
; m
&& (m
!= nmc
->nmc_mcur
); m
= mbuf_next(m
))
2210 /* trim current mbuf */
2211 data
= mbuf_data(m
);
2213 adjust
= nmc
->nmc_ptr
- data
;
2215 if ((dlen
> 0) && (adjust
> 0)) {
2216 if (mbuf_setdata(m
, nmc
->nmc_ptr
, dlen
))
2219 mbuf_setlen(m
, dlen
);
2221 /* skip next len bytes */
2222 for (; m
&& (cnt
< len
); m
= mbuf_next(m
)) {
2226 /* truncate to end of data */
2227 mbuf_setlen(m
, dlen
- (cnt
- len
));
2228 if (m
== nmc
->nmc_mcur
)
2229 nmc
->nmc_left
-= (cnt
- len
);
2236 /* trim any trailing data */
2237 if (m
== nmc
->nmc_mcur
)
2239 for (; m
; m
= mbuf_next(m
))
2246 nfsm_chain_add_fattr(
2247 struct nfsrv_descript
*nd
,
2248 struct nfsm_chain
*nmc
,
2249 struct vnode_attr
*vap
)
2253 // XXX Should we assert here that all fields are supported?
2255 nfsm_chain_add_32(error
, nmc
, vtonfs_type(vap
->va_type
, nd
->nd_vers
));
2256 if (nd
->nd_vers
== NFS_VER3
) {
2257 nfsm_chain_add_32(error
, nmc
, vap
->va_mode
& 07777);
2259 nfsm_chain_add_32(error
, nmc
, vtonfsv2_mode(vap
->va_type
, vap
->va_mode
));
2261 nfsm_chain_add_32(error
, nmc
, vap
->va_nlink
);
2262 nfsm_chain_add_32(error
, nmc
, vap
->va_uid
);
2263 nfsm_chain_add_32(error
, nmc
, vap
->va_gid
);
2264 if (nd
->nd_vers
== NFS_VER3
) {
2265 nfsm_chain_add_64(error
, nmc
, vap
->va_data_size
);
2266 nfsm_chain_add_64(error
, nmc
, vap
->va_data_alloc
);
2267 nfsm_chain_add_32(error
, nmc
, major(vap
->va_rdev
));
2268 nfsm_chain_add_32(error
, nmc
, minor(vap
->va_rdev
));
2269 nfsm_chain_add_64(error
, nmc
, vap
->va_fsid
);
2270 nfsm_chain_add_64(error
, nmc
, vap
->va_fileid
);
2272 nfsm_chain_add_32(error
, nmc
, vap
->va_data_size
);
2273 nfsm_chain_add_32(error
, nmc
, NFS_FABLKSIZE
);
2274 if (vap
->va_type
== VFIFO
)
2275 nfsm_chain_add_32(error
, nmc
, 0xffffffff);
2277 nfsm_chain_add_32(error
, nmc
, vap
->va_rdev
);
2278 nfsm_chain_add_32(error
, nmc
, vap
->va_data_alloc
/ NFS_FABLKSIZE
);
2279 nfsm_chain_add_32(error
, nmc
, vap
->va_fsid
);
2280 nfsm_chain_add_32(error
, nmc
, vap
->va_fileid
);
2282 nfsm_chain_add_time(error
, nmc
, nd
->nd_vers
, &vap
->va_access_time
);
2283 nfsm_chain_add_time(error
, nmc
, nd
->nd_vers
, &vap
->va_modify_time
);
2284 nfsm_chain_add_time(error
, nmc
, nd
->nd_vers
, &vap
->va_change_time
);
2290 nfsm_chain_get_sattr(
2291 struct nfsrv_descript
*nd
,
2292 struct nfsm_chain
*nmc
,
2293 struct vnode_attr
*vap
)
2298 struct timespec now
;
2300 if (nd
->nd_vers
== NFS_VER2
) {
2302 * There is/was a bug in the Sun client that puts 0xffff in the mode
2303 * field of sattr when it should put in 0xffffffff. The u_short
2304 * doesn't sign extend. So check the low order 2 bytes for 0xffff.
2306 nfsm_chain_get_32(error
, nmc
, val
);
2307 if ((val
& 0xffff) != 0xffff) {
2308 VATTR_SET(vap
, va_mode
, val
& 07777);
2309 /* save the "type" bits for NFSv2 create */
2310 VATTR_SET(vap
, va_type
, IFTOVT(val
));
2311 VATTR_CLEAR_ACTIVE(vap
, va_type
);
2313 nfsm_chain_get_32(error
, nmc
, val
);
2314 if (val
!= (uint32_t)-1)
2315 VATTR_SET(vap
, va_uid
, val
);
2316 nfsm_chain_get_32(error
, nmc
, val
);
2317 if (val
!= (uint32_t)-1)
2318 VATTR_SET(vap
, va_gid
, val
);
2319 /* save the "size" bits for NFSv2 create (even if they appear unset) */
2320 nfsm_chain_get_32(error
, nmc
, val
);
2321 VATTR_SET(vap
, va_data_size
, val
);
2322 if (val
== (uint32_t)-1)
2323 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
2324 nfsm_chain_get_time(error
, nmc
, NFS_VER2
,
2325 vap
->va_access_time
.tv_sec
,
2326 vap
->va_access_time
.tv_nsec
);
2327 if (vap
->va_access_time
.tv_sec
!= -1)
2328 VATTR_SET_ACTIVE(vap
, va_access_time
);
2329 nfsm_chain_get_time(error
, nmc
, NFS_VER2
,
2330 vap
->va_modify_time
.tv_sec
,
2331 vap
->va_modify_time
.tv_nsec
);
2332 if (vap
->va_modify_time
.tv_sec
!= -1)
2333 VATTR_SET_ACTIVE(vap
, va_modify_time
);
2338 nfsm_chain_get_32(error
, nmc
, val
);
2340 nfsm_chain_get_32(error
, nmc
, val
);
2341 VATTR_SET(vap
, va_mode
, val
& 07777);
2343 nfsm_chain_get_32(error
, nmc
, val
);
2345 nfsm_chain_get_32(error
, nmc
, val
);
2346 VATTR_SET(vap
, va_uid
, val
);
2348 nfsm_chain_get_32(error
, nmc
, val
);
2350 nfsm_chain_get_32(error
, nmc
, val
);
2351 VATTR_SET(vap
, va_gid
, val
);
2353 nfsm_chain_get_32(error
, nmc
, val
);
2355 nfsm_chain_get_64(error
, nmc
, val64
);
2356 VATTR_SET(vap
, va_data_size
, val64
);
2359 nfsm_chain_get_32(error
, nmc
, val
);
2361 case NFS_TIME_SET_TO_CLIENT
:
2362 nfsm_chain_get_time(error
, nmc
, nd
->nd_vers
,
2363 vap
->va_access_time
.tv_sec
,
2364 vap
->va_access_time
.tv_nsec
);
2365 VATTR_SET_ACTIVE(vap
, va_access_time
);
2366 vap
->va_vaflags
&= ~VA_UTIMES_NULL
;
2368 case NFS_TIME_SET_TO_SERVER
:
2369 VATTR_SET(vap
, va_access_time
, now
);
2370 vap
->va_vaflags
|= VA_UTIMES_NULL
;
2373 nfsm_chain_get_32(error
, nmc
, val
);
2375 case NFS_TIME_SET_TO_CLIENT
:
2376 nfsm_chain_get_time(error
, nmc
, nd
->nd_vers
,
2377 vap
->va_modify_time
.tv_sec
,
2378 vap
->va_modify_time
.tv_nsec
);
2379 VATTR_SET_ACTIVE(vap
, va_modify_time
);
2380 vap
->va_vaflags
&= ~VA_UTIMES_NULL
;
2382 case NFS_TIME_SET_TO_SERVER
:
2383 VATTR_SET(vap
, va_modify_time
, now
);
2384 if (!VATTR_IS_ACTIVE(vap
, va_access_time
))
2385 vap
->va_vaflags
|= VA_UTIMES_NULL
;
2393 * Compare two security flavor structs
2396 nfsrv_cmp_secflavs(struct nfs_sec
*sf1
, struct nfs_sec
*sf2
)
2400 if (sf1
->count
!= sf2
->count
)
2402 for (i
= 0; i
< sf1
->count
; i
++)
2403 if (sf1
->flavors
[i
] != sf2
->flavors
[i
])
2409 * Build hash lists of net addresses and hang them off the NFS export.
2410 * Called by nfsrv_export() to set up the lists of export addresses.
2413 nfsrv_hang_addrlist(struct nfs_export
*nx
, struct user_nfs_export_args
*unxa
)
2415 struct nfs_export_net_args nxna
;
2416 struct nfs_netopt
*no
, *rn_no
;
2417 struct radix_node_head
*rnh
;
2418 struct radix_node
*rn
;
2419 struct sockaddr
*saddr
, *smask
;
2426 uaddr
= unxa
->nxa_nets
;
2427 for (net
= 0; net
< unxa
->nxa_netcount
; net
++, uaddr
+= sizeof(nxna
)) {
2428 error
= copyin(uaddr
, &nxna
, sizeof(nxna
));
2432 if (nxna
.nxna_flags
& (NX_MAPROOT
|NX_MAPALL
)) {
2433 struct posix_cred temp_pcred
;
2434 bzero(&temp_pcred
, sizeof(temp_pcred
));
2435 temp_pcred
.cr_uid
= nxna
.nxna_cred
.cr_uid
;
2436 temp_pcred
.cr_ngroups
= nxna
.nxna_cred
.cr_ngroups
;
2437 for (i
=0; i
< nxna
.nxna_cred
.cr_ngroups
&& i
< NGROUPS
; i
++)
2438 temp_pcred
.cr_groups
[i
] = nxna
.nxna_cred
.cr_groups
[i
];
2439 cred
= posix_cred_create(&temp_pcred
);
2440 if (!IS_VALID_CRED(cred
))
2446 if (nxna
.nxna_addr
.ss_len
== 0) {
2447 /* No address means this is a default/world export */
2448 if (nx
->nx_flags
& NX_DEFAULTEXPORT
) {
2449 if (IS_VALID_CRED(cred
))
2450 kauth_cred_unref(&cred
);
2453 nx
->nx_flags
|= NX_DEFAULTEXPORT
;
2454 nx
->nx_defopt
.nxo_flags
= nxna
.nxna_flags
;
2455 nx
->nx_defopt
.nxo_cred
= cred
;
2456 bcopy(&nxna
.nxna_sec
, &nx
->nx_defopt
.nxo_sec
, sizeof(struct nfs_sec
));
2461 i
= sizeof(struct nfs_netopt
);
2462 i
+= nxna
.nxna_addr
.ss_len
+ nxna
.nxna_mask
.ss_len
;
2463 MALLOC(no
, struct nfs_netopt
*, i
, M_NETADDR
, M_WAITOK
);
2465 if (IS_VALID_CRED(cred
))
2466 kauth_cred_unref(&cred
);
2469 bzero(no
, sizeof(struct nfs_netopt
));
2470 no
->no_opt
.nxo_flags
= nxna
.nxna_flags
;
2471 no
->no_opt
.nxo_cred
= cred
;
2472 bcopy(&nxna
.nxna_sec
, &no
->no_opt
.nxo_sec
, sizeof(struct nfs_sec
));
2474 saddr
= (struct sockaddr
*)(no
+ 1);
2475 bcopy(&nxna
.nxna_addr
, saddr
, nxna
.nxna_addr
.ss_len
);
2476 if (nxna
.nxna_mask
.ss_len
) {
2477 smask
= (struct sockaddr
*)((caddr_t
)saddr
+ nxna
.nxna_addr
.ss_len
);
2478 bcopy(&nxna
.nxna_mask
, smask
, nxna
.nxna_mask
.ss_len
);
2482 i
= saddr
->sa_family
;
2483 if ((rnh
= nx
->nx_rtable
[i
]) == 0) {
2485 * Seems silly to initialize every AF when most are not
2486 * used, do so on demand here
2488 for (dom
= domains
; dom
; dom
= dom
->dom_next
)
2489 if (dom
->dom_family
== i
&& dom
->dom_rtattach
) {
2490 dom
->dom_rtattach((void **)&nx
->nx_rtable
[i
],
2494 if ((rnh
= nx
->nx_rtable
[i
]) == 0) {
2495 if (IS_VALID_CRED(cred
))
2496 kauth_cred_unref(&cred
);
2497 _FREE(no
, M_NETADDR
);
2501 rn
= (*rnh
->rnh_addaddr
)((caddr_t
)saddr
, (caddr_t
)smask
, rnh
, no
->no_rnodes
);
2504 * One of the reasons that rnh_addaddr may fail is that
2505 * the entry already exists. To check for this case, we
2506 * look up the entry to see if it is there. If so, we
2507 * do not need to make a new entry but do continue.
2509 * XXX should this be rnh_lookup() instead?
2512 rn
= (*rnh
->rnh_matchaddr
)((caddr_t
)saddr
, rnh
);
2513 rn_no
= (struct nfs_netopt
*)rn
;
2514 if (rn
!= 0 && (rn
->rn_flags
& RNF_ROOT
) == 0 &&
2515 (rn_no
->no_opt
.nxo_flags
== nxna
.nxna_flags
) &&
2516 (!nfsrv_cmp_secflavs(&rn_no
->no_opt
.nxo_sec
, &nxna
.nxna_sec
))) {
2517 kauth_cred_t cred2
= rn_no
->no_opt
.nxo_cred
;
2518 if (cred
== cred2
) {
2519 /* creds are same (or both NULL) */
2521 } else if (cred
&& cred2
&& (kauth_cred_getuid(cred
) == kauth_cred_getuid(cred2
))) {
2523 * Now compare the effective and
2524 * supplementary groups...
2526 * Note: This comparison, as written,
2527 * does not correctly indicate that
2528 * the groups are equivalent, since
2529 * other than the first supplementary
2530 * group, which is also the effective
2531 * group, order on the remaining groups
2532 * doesn't matter, and this is an
2535 gid_t groups
[NGROUPS
];
2536 gid_t groups2
[NGROUPS
];
2537 int groupcount
= NGROUPS
;
2538 int group2count
= NGROUPS
;
2540 if (!kauth_cred_getgroups(cred
, groups
, &groupcount
) &&
2541 !kauth_cred_getgroups(cred2
, groups2
, &group2count
) &&
2542 groupcount
== group2count
) {
2543 for (i
=0; i
< group2count
; i
++)
2544 if (groups
[i
] != groups2
[i
])
2546 if (i
>= group2count
|| i
>= NGROUPS
)
2551 if (IS_VALID_CRED(cred
))
2552 kauth_cred_unref(&cred
);
2553 _FREE(no
, M_NETADDR
);
2565 * In order to properly track an export's netopt count, we need to pass
2566 * an additional argument to nfsrv_free_netopt() so that it can decrement
2567 * the export's netopt count.
2569 struct nfsrv_free_netopt_arg
{
2571 struct radix_node_head
*rnh
;
2575 nfsrv_free_netopt(struct radix_node
*rn
, void *w
)
2577 struct nfsrv_free_netopt_arg
*fna
= (struct nfsrv_free_netopt_arg
*)w
;
2578 struct radix_node_head
*rnh
= fna
->rnh
;
2579 uint32_t *cnt
= fna
->cnt
;
2580 struct nfs_netopt
*nno
= (struct nfs_netopt
*)rn
;
2582 (*rnh
->rnh_deladdr
)(rn
->rn_key
, rn
->rn_mask
, rnh
);
2583 if (IS_VALID_CRED(nno
->no_opt
.nxo_cred
))
2584 kauth_cred_unref(&nno
->no_opt
.nxo_cred
);
2585 _FREE((caddr_t
)rn
, M_NETADDR
);
2591 * Free the net address hash lists that are hanging off the mount points.
2594 nfsrv_free_addrlist(struct nfs_export
*nx
, struct user_nfs_export_args
*unxa
)
2596 struct nfs_export_net_args nxna
;
2597 struct radix_node_head
*rnh
;
2598 struct radix_node
*rn
;
2599 struct nfsrv_free_netopt_arg fna
;
2600 struct nfs_netopt
*nno
;
2605 if (!unxa
|| !unxa
->nxa_netcount
) {
2606 /* delete everything */
2607 for (i
= 0; i
<= AF_MAX
; i
++)
2608 if ( (rnh
= nx
->nx_rtable
[i
]) ) {
2610 fna
.cnt
= &nx
->nx_expcnt
;
2611 (*rnh
->rnh_walktree
)(rnh
, nfsrv_free_netopt
, (caddr_t
)&fna
);
2612 _FREE((caddr_t
)rnh
, M_RTABLE
);
2613 nx
->nx_rtable
[i
] = 0;
2618 /* delete only the exports specified */
2619 uaddr
= unxa
->nxa_nets
;
2620 for (net
= 0; net
< unxa
->nxa_netcount
; net
++, uaddr
+= sizeof(nxna
)) {
2621 error
= copyin(uaddr
, &nxna
, sizeof(nxna
));
2625 if (nxna
.nxna_addr
.ss_len
== 0) {
2626 /* No address means this is a default/world export */
2627 if (nx
->nx_flags
& NX_DEFAULTEXPORT
) {
2628 nx
->nx_flags
&= ~NX_DEFAULTEXPORT
;
2629 if (IS_VALID_CRED(nx
->nx_defopt
.nxo_cred
)) {
2630 kauth_cred_unref(&nx
->nx_defopt
.nxo_cred
);
2637 if ((rnh
= nx
->nx_rtable
[nxna
.nxna_addr
.ss_family
]) == 0) {
2638 /* AF not initialized? */
2639 if (!(unxa
->nxa_flags
& NXA_ADD
))
2640 printf("nfsrv_free_addrlist: address not found (0)\n");
2644 rn
= (*rnh
->rnh_lookup
)(&nxna
.nxna_addr
,
2645 nxna
.nxna_mask
.ss_len
? &nxna
.nxna_mask
: NULL
, rnh
);
2646 if (!rn
|| (rn
->rn_flags
& RNF_ROOT
)) {
2647 if (!(unxa
->nxa_flags
& NXA_ADD
))
2648 printf("nfsrv_free_addrlist: address not found (1)\n");
2652 (*rnh
->rnh_deladdr
)(rn
->rn_key
, rn
->rn_mask
, rnh
);
2653 nno
= (struct nfs_netopt
*)rn
;
2654 if (IS_VALID_CRED(nno
->no_opt
.nxo_cred
))
2655 kauth_cred_unref(&nno
->no_opt
.nxo_cred
);
2656 _FREE((caddr_t
)rn
, M_NETADDR
);
2659 if (nx
->nx_expcnt
== ((nx
->nx_flags
& NX_DEFAULTEXPORT
) ? 1 : 0)) {
2660 /* no more entries in rnh, so free it up */
2661 _FREE((caddr_t
)rnh
, M_RTABLE
);
2662 nx
->nx_rtable
[nxna
.nxna_addr
.ss_family
] = 0;
2669 void enablequotas(struct mount
*mp
, vfs_context_t ctx
); // XXX
2672 nfsrv_export(struct user_nfs_export_args
*unxa
, vfs_context_t ctx
)
2676 struct nfs_exportfs
*nxfs
, *nxfs2
, *nxfs3
;
2677 struct nfs_export
*nx
, *nx2
, *nx3
;
2678 struct nfs_filehandle nfh
;
2679 struct nameidata mnd
, xnd
;
2680 vnode_t mvp
= NULL
, xvp
= NULL
;
2682 char path
[MAXPATHLEN
];
2685 if (unxa
->nxa_flags
== NXA_CHECK
) {
2686 /* just check if the path is an NFS-exportable file system */
2687 error
= copyinstr(unxa
->nxa_fspath
, path
, MAXPATHLEN
, &pathlen
);
2690 NDINIT(&mnd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
2691 UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
2692 error
= namei(&mnd
);
2696 mp
= vnode_mount(mvp
);
2697 /* make sure it's the root of a file system */
2698 if (!vnode_isvroot(mvp
))
2700 /* make sure the file system is NFS-exportable */
2702 nfh
.nfh_len
= NFSV3_MAX_FID_SIZE
;
2703 error
= VFS_VPTOFH(mvp
, (int*)&nfh
.nfh_len
, &nfh
.nfh_fid
[0], NULL
);
2705 if (!error
&& (nfh
.nfh_len
> (int)NFSV3_MAX_FID_SIZE
))
2707 if (!error
&& !(mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSREADDIR_EXTENDED
))
2714 /* all other operations: must be super user */
2715 if ((error
= vfs_context_suser(ctx
)))
2718 if (unxa
->nxa_flags
& NXA_DELETE_ALL
) {
2719 /* delete all exports on all file systems */
2720 lck_rw_lock_exclusive(&nfsrv_export_rwlock
);
2721 while ((nxfs
= LIST_FIRST(&nfsrv_exports
))) {
2722 mp
= vfs_getvfs_by_mntonname(nxfs
->nxfs_path
);
2724 vfs_clearflags(mp
, MNT_EXPORTED
);
2728 /* delete all exports on this file system */
2729 while ((nx
= LIST_FIRST(&nxfs
->nxfs_exports
))) {
2730 LIST_REMOVE(nx
, nx_next
);
2731 LIST_REMOVE(nx
, nx_hash
);
2732 /* delete all netopts for this export */
2733 nfsrv_free_addrlist(nx
, NULL
);
2734 nx
->nx_flags
&= ~NX_DEFAULTEXPORT
;
2735 if (IS_VALID_CRED(nx
->nx_defopt
.nxo_cred
)) {
2736 kauth_cred_unref(&nx
->nx_defopt
.nxo_cred
);
2738 /* free active user list for this export */
2739 nfsrv_free_user_list(&nx
->nx_user_list
);
2740 FREE(nx
->nx_path
, M_TEMP
);
2743 LIST_REMOVE(nxfs
, nxfs_next
);
2744 FREE(nxfs
->nxfs_path
, M_TEMP
);
2747 if (nfsrv_export_hashtbl
) {
2748 /* all exports deleted, clean up export hash table */
2749 FREE(nfsrv_export_hashtbl
, M_TEMP
);
2750 nfsrv_export_hashtbl
= NULL
;
2752 lck_rw_done(&nfsrv_export_rwlock
);
2756 error
= copyinstr(unxa
->nxa_fspath
, path
, MAXPATHLEN
, &pathlen
);
2760 lck_rw_lock_exclusive(&nfsrv_export_rwlock
);
2762 /* init export hash table if not already */
2763 if (!nfsrv_export_hashtbl
) {
2764 if (nfsrv_export_hash_size
<= 0)
2765 nfsrv_export_hash_size
= NFSRVEXPHASHSZ
;
2766 nfsrv_export_hashtbl
= hashinit(nfsrv_export_hash_size
, M_TEMP
, &nfsrv_export_hash
);
2769 // first check if we've already got an exportfs with the given ID
2770 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
) {
2771 if (nxfs
->nxfs_id
== unxa
->nxa_fsid
)
2775 /* verify exported FS path matches given path */
2776 if (strncmp(path
, nxfs
->nxfs_path
, MAXPATHLEN
)) {
2780 if ((unxa
->nxa_flags
& (NXA_ADD
|NXA_OFFLINE
)) == NXA_ADD
) {
2781 /* if adding, verify that the mount is still what we expect */
2782 mp
= vfs_getvfs_by_mntonname(nxfs
->nxfs_path
);
2787 /* find exported FS root vnode */
2788 NDINIT(&mnd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
2789 UIO_SYSSPACE
, CAST_USER_ADDR_T(nxfs
->nxfs_path
), ctx
);
2790 error
= namei(&mnd
);
2794 /* make sure it's (still) the root of a file system */
2795 if (!vnode_isvroot(mvp
)) {
2799 /* sanity check: this should be same mount */
2800 if (mp
!= vnode_mount(mvp
)) {
2806 /* no current exported file system with that ID */
2807 if (!(unxa
->nxa_flags
& NXA_ADD
)) {
2812 /* find exported FS root vnode */
2813 NDINIT(&mnd
, LOOKUP
, OP_LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
2814 UIO_SYSSPACE
, CAST_USER_ADDR_T(path
), ctx
);
2815 error
= namei(&mnd
);
2817 if (!(unxa
->nxa_flags
& NXA_OFFLINE
))
2821 /* make sure it's the root of a file system */
2822 if (!vnode_isvroot(mvp
)) {
2823 /* bail if not marked offline */
2824 if (!(unxa
->nxa_flags
& NXA_OFFLINE
)) {
2832 mp
= vnode_mount(mvp
);
2835 /* make sure the file system is NFS-exportable */
2836 nfh
.nfh_len
= NFSV3_MAX_FID_SIZE
;
2837 error
= VFS_VPTOFH(mvp
, (int*)&nfh
.nfh_len
, &nfh
.nfh_fid
[0], NULL
);
2838 if (!error
&& (nfh
.nfh_len
> (int)NFSV3_MAX_FID_SIZE
))
2840 if (!error
&& !(mp
->mnt_vtable
->vfc_vfsflags
& VFC_VFSREADDIR_EXTENDED
))
2847 /* add an exportfs for it */
2848 MALLOC(nxfs
, struct nfs_exportfs
*, sizeof(struct nfs_exportfs
), M_TEMP
, M_WAITOK
);
2853 bzero(nxfs
, sizeof(struct nfs_exportfs
));
2854 nxfs
->nxfs_id
= unxa
->nxa_fsid
;
2855 MALLOC(nxfs
->nxfs_path
, char*, pathlen
, M_TEMP
, M_WAITOK
);
2856 if (!nxfs
->nxfs_path
) {
2861 bcopy(path
, nxfs
->nxfs_path
, pathlen
);
2862 /* insert into list in reverse-sorted order */
2864 LIST_FOREACH(nxfs2
, &nfsrv_exports
, nxfs_next
) {
2865 if (strncmp(nxfs
->nxfs_path
, nxfs2
->nxfs_path
, MAXPATHLEN
) > 0)
2870 LIST_INSERT_BEFORE(nxfs2
, nxfs
, nxfs_next
);
2872 LIST_INSERT_AFTER(nxfs3
, nxfs
, nxfs_next
);
2874 LIST_INSERT_HEAD(&nfsrv_exports
, nxfs
, nxfs_next
);
2876 /* make sure any quotas are enabled before we export the file system */
2878 enablequotas(mp
, ctx
);
2881 if (unxa
->nxa_exppath
) {
2882 error
= copyinstr(unxa
->nxa_exppath
, path
, MAXPATHLEN
, &pathlen
);
2885 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
) {
2886 if (nx
->nx_id
== unxa
->nxa_expid
)
2890 /* verify exported FS path matches given path */
2891 if (strncmp(path
, nx
->nx_path
, MAXPATHLEN
)) {
2896 /* no current export with that ID */
2897 if (!(unxa
->nxa_flags
& NXA_ADD
)) {
2901 /* add an export for it */
2902 MALLOC(nx
, struct nfs_export
*, sizeof(struct nfs_export
), M_TEMP
, M_WAITOK
);
2907 bzero(nx
, sizeof(struct nfs_export
));
2908 nx
->nx_id
= unxa
->nxa_expid
;
2910 microtime(&nx
->nx_exptime
);
2911 MALLOC(nx
->nx_path
, char*, pathlen
, M_TEMP
, M_WAITOK
);
2918 bcopy(path
, nx
->nx_path
, pathlen
);
2919 /* initialize the active user list */
2920 nfsrv_init_user_list(&nx
->nx_user_list
);
2921 /* insert into list in reverse-sorted order */
2923 LIST_FOREACH(nx2
, &nxfs
->nxfs_exports
, nx_next
) {
2924 if (strncmp(nx
->nx_path
, nx2
->nx_path
, MAXPATHLEN
) > 0)
2929 LIST_INSERT_BEFORE(nx2
, nx
, nx_next
);
2931 LIST_INSERT_AFTER(nx3
, nx
, nx_next
);
2933 LIST_INSERT_HEAD(&nxfs
->nxfs_exports
, nx
, nx_next
);
2934 /* insert into hash */
2935 LIST_INSERT_HEAD(NFSRVEXPHASH(nxfs
->nxfs_id
, nx
->nx_id
), nx
, nx_hash
);
2938 * We don't allow/support nested exports. Check if the new entry
2939 * nests with the entries before and after or if there's an
2940 * entry for the file system root and subdirs.
2943 if ((nx3
&& !strncmp(nx3
->nx_path
, nx
->nx_path
, pathlen
- 1) &&
2944 (nx3
->nx_path
[pathlen
-1] == '/')) ||
2945 (nx2
&& !strncmp(nx2
->nx_path
, nx
->nx_path
, strlen(nx2
->nx_path
)) &&
2946 (nx
->nx_path
[strlen(nx2
->nx_path
)] == '/')))
2949 /* check export conflict with fs root export and vice versa */
2950 expisroot
= !nx
->nx_path
[0] ||
2951 ((nx
->nx_path
[0] == '.') && !nx
->nx_path
[1]);
2952 LIST_FOREACH(nx2
, &nxfs
->nxfs_exports
, nx_next
) {
2956 } else if (!nx2
->nx_path
[0])
2958 else if ((nx2
->nx_path
[0] == '.') && !nx2
->nx_path
[1])
2966 * Don't actually return an error because mountd is
2967 * probably about to delete the conflicting export.
2968 * This can happen when a new export momentarily conflicts
2969 * with an old export while the transition is being made.
2970 * Theoretically, mountd could be written to avoid this
2971 * transient situation - but it would greatly increase the
2972 * complexity of mountd for very little overall benefit.
2974 printf("nfsrv_export: warning: nested exports: %s/%s\n",
2975 nxfs
->nxfs_path
, nx
->nx_path
);
2978 nx
->nx_fh
.nfh_xh
.nxh_flags
= NXHF_INVALIDFH
;
2980 /* make sure file handle is set up */
2981 if ((nx
->nx_fh
.nfh_xh
.nxh_version
!= htonl(NFS_FH_VERSION
)) ||
2982 (nx
->nx_fh
.nfh_xh
.nxh_flags
& NXHF_INVALIDFH
)) {
2983 /* try to set up export root file handle */
2984 nx
->nx_fh
.nfh_xh
.nxh_version
= htonl(NFS_FH_VERSION
);
2985 nx
->nx_fh
.nfh_xh
.nxh_fsid
= htonl(nx
->nx_fs
->nxfs_id
);
2986 nx
->nx_fh
.nfh_xh
.nxh_expid
= htonl(nx
->nx_id
);
2987 nx
->nx_fh
.nfh_xh
.nxh_flags
= 0;
2988 nx
->nx_fh
.nfh_xh
.nxh_reserved
= 0;
2989 nx
->nx_fh
.nfh_fhp
= (u_char
*)&nx
->nx_fh
.nfh_xh
;
2990 bzero(&nx
->nx_fh
.nfh_fid
[0], NFSV2_MAX_FID_SIZE
);
2992 /* find export root vnode */
2993 if (!nx
->nx_path
[0] || ((nx
->nx_path
[0] == '.') && !nx
->nx_path
[1])) {
2994 /* exporting file system's root directory */
2998 xnd
.ni_cnd
.cn_nameiop
= LOOKUP
;
3000 xnd
.ni_op
= OP_LOOKUP
;
3002 xnd
.ni_cnd
.cn_flags
= LOCKLEAF
;
3003 xnd
.ni_pathlen
= pathlen
- 1;
3004 xnd
.ni_cnd
.cn_nameptr
= xnd
.ni_cnd
.cn_pnbuf
= path
;
3005 xnd
.ni_startdir
= mvp
;
3006 xnd
.ni_usedvp
= mvp
;
3007 xnd
.ni_cnd
.cn_context
= ctx
;
3008 while ((error
= lookup(&xnd
)) == ERECYCLE
) {
3009 xnd
.ni_cnd
.cn_flags
= LOCKLEAF
;
3010 xnd
.ni_cnd
.cn_nameptr
= xnd
.ni_cnd
.cn_pnbuf
;
3011 xnd
.ni_usedvp
= xnd
.ni_dvp
= xnd
.ni_startdir
= mvp
;
3018 if (vnode_vtype(xvp
) != VDIR
) {
3024 /* grab file handle */
3025 nx
->nx_fh
.nfh_len
= NFSV3_MAX_FID_SIZE
;
3026 error
= VFS_VPTOFH(xvp
, (int*)&nx
->nx_fh
.nfh_len
, &nx
->nx_fh
.nfh_fid
[0], NULL
);
3027 if (!error
&& (nx
->nx_fh
.nfh_len
> (int)NFSV3_MAX_FID_SIZE
)) {
3030 nx
->nx_fh
.nfh_xh
.nxh_fidlen
= nx
->nx_fh
.nfh_len
;
3031 nx
->nx_fh
.nfh_len
+= sizeof(nx
->nx_fh
.nfh_xh
);
3038 nx
->nx_fh
.nfh_xh
.nxh_flags
= NXHF_INVALIDFH
;
3039 nx
->nx_fh
.nfh_xh
.nxh_fidlen
= 0;
3040 nx
->nx_fh
.nfh_len
= sizeof(nx
->nx_fh
.nfh_xh
);
3047 /* perform the export changes */
3048 if (unxa
->nxa_flags
& NXA_DELETE
) {
3050 /* delete all exports on this file system */
3051 while ((nx
= LIST_FIRST(&nxfs
->nxfs_exports
))) {
3052 LIST_REMOVE(nx
, nx_next
);
3053 LIST_REMOVE(nx
, nx_hash
);
3054 /* delete all netopts for this export */
3055 nfsrv_free_addrlist(nx
, NULL
);
3056 nx
->nx_flags
&= ~NX_DEFAULTEXPORT
;
3057 if (IS_VALID_CRED(nx
->nx_defopt
.nxo_cred
)) {
3058 kauth_cred_unref(&nx
->nx_defopt
.nxo_cred
);
3060 /* delete active user list for this export */
3061 nfsrv_free_user_list(&nx
->nx_user_list
);
3062 FREE(nx
->nx_path
, M_TEMP
);
3066 } else if (!unxa
->nxa_netcount
) {
3067 /* delete all netopts for this export */
3068 nfsrv_free_addrlist(nx
, NULL
);
3069 nx
->nx_flags
&= ~NX_DEFAULTEXPORT
;
3070 if (IS_VALID_CRED(nx
->nx_defopt
.nxo_cred
)) {
3071 kauth_cred_unref(&nx
->nx_defopt
.nxo_cred
);
3074 /* delete only the netopts for the given addresses */
3075 error
= nfsrv_free_addrlist(nx
, unxa
);
3080 if (unxa
->nxa_flags
& NXA_ADD
) {
3082 * If going offline set the export time so that when
3083 * coming back on line we will present a new write verifier
3086 if (unxa
->nxa_flags
& NXA_OFFLINE
)
3087 microtime(&nx
->nx_exptime
);
3089 error
= nfsrv_hang_addrlist(nx
, unxa
);
3091 vfs_setflags(mp
, MNT_EXPORTED
);
3095 if (nx
&& !nx
->nx_expcnt
) {
3096 /* export has no export options */
3097 LIST_REMOVE(nx
, nx_next
);
3098 LIST_REMOVE(nx
, nx_hash
);
3099 /* delete active user list for this export */
3100 nfsrv_free_user_list(&nx
->nx_user_list
);
3101 FREE(nx
->nx_path
, M_TEMP
);
3104 if (LIST_EMPTY(&nxfs
->nxfs_exports
)) {
3105 /* exported file system has no more exports */
3106 LIST_REMOVE(nxfs
, nxfs_next
);
3107 FREE(nxfs
->nxfs_path
, M_TEMP
);
3110 vfs_clearflags(mp
, MNT_EXPORTED
);
3121 lck_rw_done(&nfsrv_export_rwlock
);
3125 struct nfs_export_options
*
3126 nfsrv_export_lookup(struct nfs_export
*nx
, mbuf_t nam
)
3128 struct nfs_export_options
*nxo
= NULL
;
3129 struct nfs_netopt
*no
= NULL
;
3130 struct radix_node_head
*rnh
;
3131 struct sockaddr
*saddr
;
3133 /* Lookup in the export list first. */
3135 saddr
= mbuf_data(nam
);
3136 rnh
= nx
->nx_rtable
[saddr
->sa_family
];
3138 no
= (struct nfs_netopt
*)
3139 (*rnh
->rnh_matchaddr
)((caddr_t
)saddr
, rnh
);
3140 if (no
&& no
->no_rnodes
->rn_flags
& RNF_ROOT
)
3146 /* If no address match, use the default if it exists. */
3147 if ((nxo
== NULL
) && (nx
->nx_flags
& NX_DEFAULTEXPORT
))
3148 nxo
= &nx
->nx_defopt
;
3152 /* find an export for the given handle */
3154 nfsrv_fhtoexport(struct nfs_filehandle
*nfhp
)
3156 struct nfs_exphandle
*nxh
= (struct nfs_exphandle
*)nfhp
->nfh_fhp
;
3157 struct nfs_export
*nx
;
3158 uint32_t fsid
, expid
;
3160 if (!nfsrv_export_hashtbl
)
3162 fsid
= ntohl(nxh
->nxh_fsid
);
3163 expid
= ntohl(nxh
->nxh_expid
);
3164 nx
= NFSRVEXPHASH(fsid
, expid
)->lh_first
;
3165 for (; nx
; nx
= LIST_NEXT(nx
, nx_hash
)) {
3166 if (nx
->nx_fs
->nxfs_id
!= fsid
)
3168 if (nx
->nx_id
!= expid
)
3176 * nfsrv_fhtovp() - convert FH to vnode and export info
3180 struct nfs_filehandle
*nfhp
,
3181 struct nfsrv_descript
*nd
,
3183 struct nfs_export
**nxp
,
3184 struct nfs_export_options
**nxop
)
3186 struct nfs_exphandle
*nxh
= (struct nfs_exphandle
*)nfhp
->nfh_fhp
;
3187 struct nfs_export_options
*nxo
;
3202 v
= ntohl(nxh
->nxh_version
);
3203 if (v
!= NFS_FH_VERSION
) {
3204 /* file handle format not supported */
3207 if (nfhp
->nfh_len
> NFSV3_MAX_FH_SIZE
)
3209 if (nfhp
->nfh_len
< (int)sizeof(struct nfs_exphandle
))
3211 v
= ntohs(nxh
->nxh_flags
);
3212 if (v
& NXHF_INVALIDFH
)
3215 *nxp
= nfsrv_fhtoexport(nfhp
);
3219 /* Get the export option structure for this <export, client> tuple. */
3220 *nxop
= nxo
= nfsrv_export_lookup(*nxp
, nam
);
3221 if (nam
&& (*nxop
== NULL
))
3225 /* Validate the security flavor of the request */
3226 for (i
= 0, valid
= 0; i
< nxo
->nxo_sec
.count
; i
++) {
3227 if (nd
->nd_sec
== nxo
->nxo_sec
.flavors
[i
]) {
3234 * RFC 2623 section 2.3.2 recommends no authentication
3235 * requirement for certain NFS procedures used for mounting.
3236 * This allows an unauthenticated superuser on the client
3237 * to do mounts for the benefit of authenticated users.
3239 if (nd
->nd_vers
== NFS_VER2
)
3240 if (nd
->nd_procnum
== NFSV2PROC_GETATTR
||
3241 nd
->nd_procnum
== NFSV2PROC_STATFS
)
3243 if (nd
->nd_vers
== NFS_VER3
)
3244 if (nd
->nd_procnum
== NFSPROC_FSINFO
)
3248 return (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
3252 if (nxo
&& (nxo
->nxo_flags
& NX_OFFLINE
))
3253 return ((nd
== NULL
|| nd
->nd_vers
== NFS_VER2
) ? ESTALE
: NFSERR_TRYLATER
);
3255 /* find mount structure */
3256 mp
= vfs_getvfs_by_mntonname((*nxp
)->nx_fs
->nxfs_path
);
3258 error
= vfs_busy(mp
, LK_NOWAIT
);
3265 * We have an export, but no mount?
3266 * Perhaps the export just hasn't been marked offline yet.
3268 return ((nd
== NULL
|| nd
->nd_vers
== NFS_VER2
) ? ESTALE
: NFSERR_TRYLATER
);
3271 fidp
= nfhp
->nfh_fhp
+ sizeof(*nxh
);
3272 error
= VFS_FHTOVP(mp
, nxh
->nxh_fidlen
, fidp
, vpp
, NULL
);
3276 /* vnode pointer should be good at this point or ... */
3283 * nfsrv_credcheck() - check/map credentials according
3284 * to given export options.
3288 struct nfsrv_descript
*nd
,
3290 __unused
struct nfs_export
*nx
,
3291 struct nfs_export_options
*nxo
)
3293 if (nxo
&& nxo
->nxo_cred
) {
3294 if ((nxo
->nxo_flags
& NX_MAPALL
) ||
3295 ((nxo
->nxo_flags
& NX_MAPROOT
) && !suser(nd
->nd_cr
, NULL
))) {
3296 kauth_cred_ref(nxo
->nxo_cred
);
3297 kauth_cred_unref(&nd
->nd_cr
);
3298 nd
->nd_cr
= nxo
->nxo_cred
;
3301 ctx
->vc_ucred
= nd
->nd_cr
;
3306 * nfsrv_vptofh() - convert vnode to file handle for given export
3308 * If the caller is passing in a vnode for a ".." directory entry,
3309 * they can pass a directory NFS file handle (dnfhp) which will be
3310 * checked against the root export file handle. If it matches, we
3311 * refuse to provide the file handle for the out-of-export directory.
3315 struct nfs_export
*nx
,
3317 struct nfs_filehandle
*dnfhp
,
3320 struct nfs_filehandle
*nfhp
)
3323 uint32_t maxfidsize
;
3325 nfhp
->nfh_fhp
= (u_char
*)&nfhp
->nfh_xh
;
3326 nfhp
->nfh_xh
.nxh_version
= htonl(NFS_FH_VERSION
);
3327 nfhp
->nfh_xh
.nxh_fsid
= htonl(nx
->nx_fs
->nxfs_id
);
3328 nfhp
->nfh_xh
.nxh_expid
= htonl(nx
->nx_id
);
3329 nfhp
->nfh_xh
.nxh_flags
= 0;
3330 nfhp
->nfh_xh
.nxh_reserved
= 0;
3332 if (nfsvers
== NFS_VER2
)
3333 bzero(&nfhp
->nfh_fid
[0], NFSV2_MAX_FID_SIZE
);
3335 /* if directory FH matches export root, return invalid FH */
3336 if (dnfhp
&& nfsrv_fhmatch(dnfhp
, &nx
->nx_fh
)) {
3337 if (nfsvers
== NFS_VER2
)
3338 nfhp
->nfh_len
= NFSX_V2FH
;
3340 nfhp
->nfh_len
= sizeof(nfhp
->nfh_xh
);
3341 nfhp
->nfh_xh
.nxh_fidlen
= 0;
3342 nfhp
->nfh_xh
.nxh_flags
= htons(NXHF_INVALIDFH
);
3346 if (nfsvers
== NFS_VER2
)
3347 maxfidsize
= NFSV2_MAX_FID_SIZE
;
3349 maxfidsize
= NFSV3_MAX_FID_SIZE
;
3350 nfhp
->nfh_len
= maxfidsize
;
3352 error
= VFS_VPTOFH(vp
, (int*)&nfhp
->nfh_len
, &nfhp
->nfh_fid
[0], ctx
);
3355 if (nfhp
->nfh_len
> maxfidsize
)
3357 nfhp
->nfh_xh
.nxh_fidlen
= nfhp
->nfh_len
;
3358 nfhp
->nfh_len
+= sizeof(nfhp
->nfh_xh
);
3359 if ((nfsvers
== NFS_VER2
) && (nfhp
->nfh_len
< NFSX_V2FH
))
3360 nfhp
->nfh_len
= NFSX_V2FH
;
3366 * Compare two file handles to see it they're the same.
3367 * Note that we don't use nfh_len because that may include
3368 * padding in an NFSv2 file handle.
3371 nfsrv_fhmatch(struct nfs_filehandle
*fh1
, struct nfs_filehandle
*fh2
)
3373 struct nfs_exphandle
*nxh1
, *nxh2
;
3376 nxh1
= (struct nfs_exphandle
*)fh1
->nfh_fhp
;
3377 nxh2
= (struct nfs_exphandle
*)fh2
->nfh_fhp
;
3378 len1
= sizeof(fh1
->nfh_xh
) + nxh1
->nxh_fidlen
;
3379 len2
= sizeof(fh2
->nfh_xh
) + nxh2
->nxh_fidlen
;
3382 if (bcmp(nxh1
, nxh2
, len1
))
3388 * Functions for dealing with active user lists
3392 * Search the hash table for a user node with a matching IP address and uid field.
3393 * If found, the node's tm_last timestamp is updated and the node is returned.
3395 * If not found, a new node is allocated (or reclaimed via LRU), initialized, and returned.
3396 * Returns NULL if a new node could not be allcoated.
3398 * The list's user_mutex lock MUST be held.
3400 struct nfs_user_stat_node
*
3401 nfsrv_get_user_stat_node(struct nfs_active_user_list
*list
, struct sockaddr
*saddr
, uid_t uid
)
3403 struct nfs_user_stat_node
*unode
;
3405 struct nfs_user_stat_hashtbl_head
*head
;
3407 /* seach the hash table */
3408 head
= NFS_USER_STAT_HASH(list
->user_hashtbl
, uid
);
3409 LIST_FOREACH(unode
, head
, hash_link
) {
3410 if ((uid
== unode
->uid
) && (nfs_sockaddr_cmp(saddr
, (struct sockaddr
*)&unode
->sock
) == 0)) {
3411 /* found matching node */
3417 /* found node in the hash table, now update lru position */
3418 TAILQ_REMOVE(&list
->user_lru
, unode
, lru_link
);
3419 TAILQ_INSERT_TAIL(&list
->user_lru
, unode
, lru_link
);
3421 /* update time stamp */
3423 unode
->tm_last
= (uint32_t)now
.tv_sec
;
3427 if (list
->node_count
< nfsrv_user_stat_max_nodes
) {
3428 /* Allocate a new node */
3429 MALLOC(unode
, struct nfs_user_stat_node
*, sizeof(struct nfs_user_stat_node
),
3430 M_TEMP
, M_WAITOK
| M_ZERO
);
3435 /* increment node count */
3436 OSAddAtomic(1, &nfsrv_user_stat_node_count
);
3439 /* reuse the oldest node in the lru list */
3440 unode
= TAILQ_FIRST(&list
->user_lru
);
3445 /* Remove the node */
3446 TAILQ_REMOVE(&list
->user_lru
, unode
, lru_link
);
3447 LIST_REMOVE(unode
, hash_link
);
3450 /* Initialize the node */
3452 bcopy(saddr
, &unode
->sock
, saddr
->sa_len
);
3455 unode
->bytes_read
= 0;
3456 unode
->bytes_written
= 0;
3457 unode
->tm_start
= (uint32_t)now
.tv_sec
;
3458 unode
->tm_last
= (uint32_t)now
.tv_sec
;
3460 /* insert the node */
3461 TAILQ_INSERT_TAIL(&list
->user_lru
, unode
, lru_link
);
3462 LIST_INSERT_HEAD(head
, unode
, hash_link
);
3468 nfsrv_update_user_stat(struct nfs_export
*nx
, struct nfsrv_descript
*nd
, uid_t uid
, u_int ops
, u_int rd_bytes
, u_int wr_bytes
)
3470 struct nfs_user_stat_node
*unode
;
3471 struct nfs_active_user_list
*ulist
;
3472 struct sockaddr
*saddr
;
3474 if ((!nfsrv_user_stat_enabled
) || (!nx
) || (!nd
) || (!nd
->nd_nam
))
3477 saddr
= (struct sockaddr
*)mbuf_data(nd
->nd_nam
);
3479 /* check address family before going any further */
3480 if ((saddr
->sa_family
!= AF_INET
) && (saddr
->sa_family
!= AF_INET6
))
3483 ulist
= &nx
->nx_user_list
;
3485 /* lock the active user list */
3486 lck_mtx_lock(&ulist
->user_mutex
);
3488 /* get the user node */
3489 unode
= nfsrv_get_user_stat_node(ulist
, saddr
, uid
);
3492 lck_mtx_unlock(&ulist
->user_mutex
);
3496 /* update counters */
3498 unode
->bytes_read
+= rd_bytes
;
3499 unode
->bytes_written
+= wr_bytes
;
3502 lck_mtx_unlock(&ulist
->user_mutex
);
3505 /* initialize an active user list */
3507 nfsrv_init_user_list(struct nfs_active_user_list
*ulist
)
3511 /* initialize the lru */
3512 TAILQ_INIT(&ulist
->user_lru
);
3514 /* initialize the hash table */
3515 for(i
= 0; i
< NFS_USER_STAT_HASH_SIZE
; i
++)
3516 LIST_INIT(&ulist
->user_hashtbl
[i
]);
3517 ulist
->node_count
= 0;
3519 lck_mtx_init(&ulist
->user_mutex
, nfsrv_active_user_mutex_group
, LCK_ATTR_NULL
);
3522 /* Free all nodes in an active user list */
3524 nfsrv_free_user_list(struct nfs_active_user_list
*ulist
)
3526 struct nfs_user_stat_node
*unode
;
3531 while ((unode
= TAILQ_FIRST(&ulist
->user_lru
))) {
3532 /* Remove node and free */
3533 TAILQ_REMOVE(&ulist
->user_lru
, unode
, lru_link
);
3534 LIST_REMOVE(unode
, hash_link
);
3535 FREE(unode
, M_TEMP
);
3537 /* decrement node count */
3538 OSAddAtomic(-1, &nfsrv_user_stat_node_count
);
3540 ulist
->node_count
= 0;
3542 lck_mtx_destroy(&ulist
->user_mutex
, nfsrv_active_user_mutex_group
);
3545 /* Reclaim old expired user nodes from active user lists. */
3547 nfsrv_active_user_list_reclaim(void)
3549 struct nfs_exportfs
*nxfs
;
3550 struct nfs_export
*nx
;
3551 struct nfs_active_user_list
*ulist
;
3552 struct nfs_user_stat_hashtbl_head oldlist
;
3553 struct nfs_user_stat_node
*unode
, *unode_next
;
3557 LIST_INIT(&oldlist
);
3559 lck_rw_lock_shared(&nfsrv_export_rwlock
);
3561 tstale
= now
.tv_sec
- nfsrv_user_stat_max_idle_sec
;
3562 LIST_FOREACH(nxfs
, &nfsrv_exports
, nxfs_next
) {
3563 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
) {
3564 /* Scan through all user nodes of this export */
3565 ulist
= &nx
->nx_user_list
;
3566 lck_mtx_lock(&ulist
->user_mutex
);
3567 for (unode
= TAILQ_FIRST(&ulist
->user_lru
); unode
; unode
= unode_next
) {
3568 unode_next
= TAILQ_NEXT(unode
, lru_link
);
3570 /* check if this node has expired */
3571 if (unode
->tm_last
>= tstale
)
3574 /* Remove node from the active user list */
3575 TAILQ_REMOVE(&ulist
->user_lru
, unode
, lru_link
);
3576 LIST_REMOVE(unode
, hash_link
);
3578 /* Add node to temp list */
3579 LIST_INSERT_HEAD(&oldlist
, unode
, hash_link
);
3581 /* decrement node count */
3582 OSAddAtomic(-1, &nfsrv_user_stat_node_count
);
3583 ulist
->node_count
--;
3585 /* can unlock this export's list now */
3586 lck_mtx_unlock(&ulist
->user_mutex
);
3589 lck_rw_done(&nfsrv_export_rwlock
);
3591 /* Free expired nodes */
3592 while ((unode
= LIST_FIRST(&oldlist
))) {
3593 LIST_REMOVE(unode
, hash_link
);
3594 FREE(unode
, M_TEMP
);
3599 * Maps errno values to nfs error numbers.
3600 * Use NFSERR_IO as the catch all for ones not specifically defined in
3603 static u_char nfsrv_v2errmap
[] = {
3604 NFSERR_PERM
, NFSERR_NOENT
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3605 NFSERR_NXIO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3606 NFSERR_IO
, NFSERR_IO
, NFSERR_ACCES
, NFSERR_IO
, NFSERR_IO
,
3607 NFSERR_IO
, NFSERR_EXIST
, NFSERR_IO
, NFSERR_NODEV
, NFSERR_NOTDIR
,
3608 NFSERR_ISDIR
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3609 NFSERR_IO
, NFSERR_FBIG
, NFSERR_NOSPC
, NFSERR_IO
, NFSERR_ROFS
,
3610 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3611 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3612 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3613 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3614 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3615 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
3616 NFSERR_IO
, NFSERR_IO
, NFSERR_NAMETOL
, NFSERR_IO
, NFSERR_IO
,
3617 NFSERR_NOTEMPTY
, NFSERR_IO
, NFSERR_IO
, NFSERR_DQUOT
, NFSERR_STALE
,
3621 * Maps errno values to nfs error numbers.
3622 * Although it is not obvious whether or not NFS clients really care if
3623 * a returned error value is in the specified list for the procedure, the
3624 * safest thing to do is filter them appropriately. For Version 2, the
3625 * X/Open XNFS document is the only specification that defines error values
3626 * for each RPC (The RFC simply lists all possible error values for all RPCs),
3627 * so I have decided to not do this for Version 2.
3628 * The first entry is the default error return and the rest are the valid
3629 * errors for that RPC in increasing numeric order.
3631 static short nfsv3err_null
[] = {
3636 static short nfsv3err_getattr
[] = {
3646 static short nfsv3err_setattr
[] = {
3663 static short nfsv3err_lookup
[] = {
3677 static short nfsv3err_access
[] = {
3687 static short nfsv3err_readlink
[] = {
3700 static short nfsv3err_read
[] = {
3713 static short nfsv3err_write
[] = {
3729 static short nfsv3err_create
[] = {
3747 static short nfsv3err_mkdir
[] = {
3765 static short nfsv3err_symlink
[] = {
3783 static short nfsv3err_mknod
[] = {
3802 static short nfsv3err_remove
[] = {
3817 static short nfsv3err_rmdir
[] = {
3836 static short nfsv3err_rename
[] = {
3860 static short nfsv3err_link
[] = {
3881 static short nfsv3err_readdir
[] = {
3895 static short nfsv3err_readdirplus
[] = {
3910 static short nfsv3err_fsstat
[] = {
3920 static short nfsv3err_fsinfo
[] = {
3929 static short nfsv3err_pathconf
[] = {
3938 static short nfsv3err_commit
[] = {
3948 static short *nfsrv_v3errmap
[] = {
3966 nfsv3err_readdirplus
,
3974 * Map errnos to NFS error numbers. For Version 3 also filter out error
3975 * numbers not specified for the associated procedure.
3978 nfsrv_errmap(struct nfsrv_descript
*nd
, int err
)
3980 short *defaulterrp
, *errp
;
3982 if (nd
->nd_vers
== NFS_VER2
) {
3983 if (err
<= (int)sizeof(nfsrv_v2errmap
))
3984 return ((int)nfsrv_v2errmap
[err
- 1]);
3988 if (nd
->nd_procnum
> NFSPROC_COMMIT
)
3989 return (err
& 0xffff);
3990 errp
= defaulterrp
= nfsrv_v3errmap
[nd
->nd_procnum
];
3994 else if (*errp
> err
)
3997 return ((int)*defaulterrp
);
4000 #endif /* NFSSERVER */