2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * These functions support the macros and help fiddle mbuf chains for
44 * the nfs op functions. They do things like create the rpc header and
45 * copy data between mbuf chains and uio lists.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
54 #include <sys/mount.h>
55 #include <sys/vnode.h>
56 #include <sys/namei.h>
58 #include <sys/socket.h>
60 #include <sys/malloc.h>
61 #include <sys/sysent.h>
62 #include <sys/syscall.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfsserver/nfs.h>
71 #include <nfs/xdr_subs.h>
72 #include <nfs/nfs_common.h>
74 #include <netinet/in.h>
76 enum vtype nv3tov_type[8]= {
77 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
79 nfstype nfsv3_type[9] = {
80 NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON
89 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
93 * copies mbuf chain to the uio scatter/gather list
96 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
106 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
107 rem = nfsm_rndup(siz)-siz;
109 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
111 left = uiop->uio_iov->iov_len;
112 uiocp = uiop->uio_iov->iov_base;
121 mbufcp = mtod(mp, caddr_t);
124 xfer = (left > len) ? len : left;
127 if (uiop->uio_iov->iov_op != NULL)
128 (*(uiop->uio_iov->iov_op))
129 (mbufcp, uiocp, xfer);
132 if (uiop->uio_segflg == UIO_SYSSPACE)
133 bcopy(mbufcp, uiocp, xfer);
135 copyout(mbufcp, uiocp, xfer);
140 uiop->uio_offset += xfer;
141 uiop->uio_resid -= xfer;
143 if (uiop->uio_iov->iov_len <= siz) {
147 uiop->uio_iov->iov_base =
148 (char *)uiop->uio_iov->iov_base + uiosiz;
149 uiop->uio_iov->iov_len -= uiosiz;
157 error = nfs_adv(mrep, dpos, rem, len);
165 * Help break down an mbuf chain by setting the first siz bytes contiguous
166 * pointed to by returned val.
167 * This is used by the macros nfsm_dissect for tough
168 * cases. (The macros use the vars. dpos and dpos2)
171 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left)
173 struct mbuf *mp, *mp2;
180 *mdp = mp = mp->m_next;
184 *dposp = mtod(mp, caddr_t);
189 } else if (mp->m_next == NULL) {
191 } else if (siz > MHLEN) {
192 panic("nfs S too big");
194 MGET(mp2, M_TRYWAIT, MT_DATA);
195 mp2->m_next = mp->m_next;
199 ptr = mtod(mp, caddr_t);
201 bcopy(*dposp, ptr, left); /* Copy what was left */
205 /* Loop around copying up the siz2 bytes */
209 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
211 bcopy(mtod(mp2, caddr_t), ptr, xfer);
222 *dposp = mtod(mp2, caddr_t);
228 * Advance the position in the mbuf chain.
231 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
246 *dposp = mtod(m, caddr_t)+offs;
251 nfsm_build_xx(int s, struct mbuf **mb, caddr_t *bpos)
256 if (s > M_TRAILINGSPACE(*mb)) {
257 MGET(mb2, M_TRYWAIT, MT_DATA);
259 panic("build > MLEN");
263 *bpos = mtod(*mb, caddr_t);
272 nfsm_dissect_xx(int s, struct mbuf **md, caddr_t *dpos)
278 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
284 cp2 = nfsm_disct(md, dpos, s, t1);
289 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
293 tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
296 *s = fxdr_unsigned(int32_t, *tl);
303 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
307 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
312 t1 = nfs_adv(md, dpos, s, t1);