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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * These functions support the macros and help fiddle mbuf chains for
40 * the nfs op functions. They do things like create the rpc header and
41 * copy data between mbuf chains and uio lists.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
54 #include <sys/socket.h>
56 #include <sys/malloc.h>
57 #include <sys/sysent.h>
58 #include <sys/syscall.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_extern.h>
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
66 #include <nfsserver/nfs.h>
67 #include <nfs/xdr_subs.h>
68 #include <nfs/nfs_common.h>
70 #include <netinet/in.h>
72 enum vtype nv3tov_type[8]= {
73 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
75 nfstype nfsv3_type[9] = {
76 NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON
79 static void *nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how);
87 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
91 * copies mbuf chain to the uio scatter/gather list
94 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
104 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
105 rem = nfsm_rndup(siz)-siz;
107 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
109 left = uiop->uio_iov->iov_len;
110 uiocp = uiop->uio_iov->iov_base;
119 mbufcp = mtod(mp, caddr_t);
122 xfer = (left > len) ? len : left;
125 if (uiop->uio_iov->iov_op != NULL)
126 (*(uiop->uio_iov->iov_op))
127 (mbufcp, uiocp, xfer);
130 if (uiop->uio_segflg == UIO_SYSSPACE)
131 bcopy(mbufcp, uiocp, xfer);
133 copyout(mbufcp, uiocp, xfer);
138 uiop->uio_offset += xfer;
139 uiop->uio_resid -= xfer;
141 if (uiop->uio_iov->iov_len <= siz) {
145 uiop->uio_iov->iov_base =
146 (char *)uiop->uio_iov->iov_base + uiosiz;
147 uiop->uio_iov->iov_len -= uiosiz;
155 error = nfs_adv(mrep, dpos, rem, len);
163 * Help break down an mbuf chain by setting the first siz bytes contiguous
164 * pointed to by returned val.
165 * This is used by the macros nfsm_dissect for tough
166 * cases. (The macros use the vars. dpos and dpos2)
169 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, int how)
171 struct mbuf *mp, *mp2;
178 *mdp = mp = mp->m_next;
182 *dposp = mtod(mp, caddr_t);
187 } else if (mp->m_next == NULL) {
189 } else if (siz > MHLEN) {
190 panic("nfs S too big");
192 MGET(mp2, how, 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)
274 return nfsm_dissect_xx_sub(s, md, dpos, M_TRYWAIT);
278 nfsm_dissect_xx_nonblock(int s, struct mbuf **md, caddr_t *dpos)
280 return nfsm_dissect_xx_sub(s, md, dpos, M_DONTWAIT);
284 nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how)
290 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
296 cp2 = nfsm_disct(md, dpos, s, t1, how);
301 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
305 tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
308 *s = fxdr_unsigned(int32_t, *tl);
315 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
319 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
324 t1 = nfs_adv(md, dpos, s, t1);