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/module.h>
58 #include <sys/sysent.h>
59 #include <sys/syscall.h>
60 #include <sys/sysctl.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
66 #include <nfs/nfsproto.h>
67 #include <nfsserver/nfs.h>
68 #include <nfs/xdr_subs.h>
69 #include <nfs/nfs_common.h>
71 #include <netinet/in.h>
73 enum vtype nv3tov_type[8]= {
74 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
76 nfstype nfsv3_type[9] = {
77 NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON
80 static void *nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos,
83 SYSCTL_NODE(_vfs, OID_AUTO, nfs_common, CTLFLAG_RD, 0, "NFS common support");
85 static int nfs_realign_test;
86 SYSCTL_INT(_vfs_nfs_common, OID_AUTO, realign_test, CTLFLAG_RD,
87 &nfs_realign_test, 0, "Number of realign tests done");
89 static int nfs_realign_count;
90 SYSCTL_INT(_vfs_nfs_common, OID_AUTO, realign_count, CTLFLAG_RD,
91 &nfs_realign_count, 0, "Number of mbuf realignments done");
99 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
103 * copies mbuf chain to the uio scatter/gather list
106 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
108 char *mbufcp, *uiocp;
116 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
117 rem = nfsm_rndup(siz)-siz;
119 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
121 left = uiop->uio_iov->iov_len;
122 uiocp = uiop->uio_iov->iov_base;
131 mbufcp = mtod(mp, caddr_t);
134 xfer = (left > len) ? len : left;
137 if (uiop->uio_iov->iov_op != NULL)
138 (*(uiop->uio_iov->iov_op))
139 (mbufcp, uiocp, xfer);
142 if (uiop->uio_segflg == UIO_SYSSPACE)
143 bcopy(mbufcp, uiocp, xfer);
145 copyout(mbufcp, uiocp, xfer);
150 uiop->uio_offset += xfer;
151 uiop->uio_resid -= xfer;
153 if (uiop->uio_iov->iov_len <= siz) {
157 uiop->uio_iov->iov_base =
158 (char *)uiop->uio_iov->iov_base + uiosiz;
159 uiop->uio_iov->iov_len -= uiosiz;
167 error = nfs_adv(mrep, dpos, rem, len);
175 * Help break down an mbuf chain by setting the first siz bytes contiguous
176 * pointed to by returned val.
177 * This is used by the macros nfsm_dissect for tough
178 * cases. (The macros use the vars. dpos and dpos2)
181 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, int how)
183 struct mbuf *mp, *mp2;
185 caddr_t ptr, npos = NULL;
190 *mdp = mp = mp->m_next;
194 *dposp = mtod(mp, caddr_t);
199 } else if (mp->m_next == NULL) {
201 } else if (siz > MHLEN) {
202 panic("nfs S too big");
204 MGET(mp2, how, MT_DATA);
208 mp2->m_next = mp->m_next;
212 ptr = mtod(mp, caddr_t);
214 bcopy(*dposp, ptr, left); /* Copy what was left */
218 npos = mtod(mp2, caddr_t);
219 /* Loop around copying up the siz2 bytes */
223 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
225 bcopy(mtod(mp2, caddr_t), ptr, xfer);
234 npos = mtod(mp2, caddr_t);
238 *dposp = mtod(mp2, caddr_t);
239 if (!nfsm_aligned(*dposp, u_int32_t)) {
240 bcopy(*dposp, npos, mp2->m_len);
249 * Advance the position in the mbuf chain.
252 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
267 *dposp = mtod(m, caddr_t)+offs;
272 nfsm_build_xx(int s, struct mbuf **mb, caddr_t *bpos)
277 if (s > M_TRAILINGSPACE(*mb)) {
278 MGET(mb2, M_WAIT, MT_DATA);
280 panic("build > MLEN");
284 *bpos = mtod(*mb, caddr_t);
293 nfsm_dissect_xx(int s, struct mbuf **md, caddr_t *dpos)
296 return (nfsm_dissect_xx_sub(s, md, dpos, M_WAIT));
300 nfsm_dissect_xx_nonblock(int s, struct mbuf **md, caddr_t *dpos)
303 return (nfsm_dissect_xx_sub(s, md, dpos, M_DONTWAIT));
307 nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how)
313 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
319 cp2 = nfsm_disct(md, dpos, s, t1, how);
324 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
328 tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
331 *s = fxdr_unsigned(int32_t, *tl);
338 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
342 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
347 t1 = nfs_adv(md, dpos, s, t1);
354 * Check for badly aligned mbuf data and realign by copying the unaligned
355 * portion of the data into a new mbuf chain and freeing the portions of the
356 * old chain that were replaced.
358 * We cannot simply realign the data within the existing mbuf chain because
359 * the underlying buffers may contain other rpc commands and we cannot afford
362 * We would prefer to avoid this situation entirely. The situation does not
363 * occur with NFS/UDP and is supposed to only occassionally occur with TCP.
364 * Use vfs.nfs_common.realign_count and realign_test to check this.
367 nfs_realign(struct mbuf **pm, int how)
373 while ((m = *pm) != NULL) {
374 if (!nfsm_aligned(m->m_len, u_int32_t) ||
375 !nfsm_aligned(mtod(m, intptr_t), u_int32_t)) {
377 * NB: we can't depend on m_pkthdr.len to help us
378 * decide what to do here. May not be worth doing
379 * the m_length calculation as m_copyback will
380 * expand the mbuf chain below as needed.
382 if (m_length(m, NULL) >= MINCLSIZE) {
383 /* NB: m_copyback handles space > MCLBYTES */
384 n = m_getcl(how, MT_DATA, 0);
386 n = m_get(how, MT_DATA);
390 * Align the remainder of the mbuf chain.
395 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
409 static moduledata_t nfs_common_mod = {
415 DECLARE_MODULE(nfs_common, nfs_common_mod, SI_SUB_VFS, SI_ORDER_ANY);
416 MODULE_VERSION(nfs_common, 1);