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");
94 * copies mbuf chain to the uio scatter/gather list
97 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
107 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
108 rem = nfsm_rndup(siz)-siz;
110 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
112 left = uiop->uio_iov->iov_len;
113 uiocp = uiop->uio_iov->iov_base;
122 mbufcp = mtod(mp, caddr_t);
125 xfer = (left > len) ? len : left;
128 if (uiop->uio_iov->iov_op != NULL)
129 (*(uiop->uio_iov->iov_op))
130 (mbufcp, uiocp, xfer);
133 if (uiop->uio_segflg == UIO_SYSSPACE)
134 bcopy(mbufcp, uiocp, xfer);
136 copyout(mbufcp, uiocp, xfer);
141 uiop->uio_offset += xfer;
142 uiop->uio_resid -= xfer;
144 if (uiop->uio_iov->iov_len <= siz) {
148 uiop->uio_iov->iov_base =
149 (char *)uiop->uio_iov->iov_base + uiosiz;
150 uiop->uio_iov->iov_len -= uiosiz;
158 error = nfs_adv(mrep, dpos, rem, len);
166 * Help break down an mbuf chain by setting the first siz bytes contiguous
167 * pointed to by returned val.
168 * This is used by the macros nfsm_dissect for tough
169 * cases. (The macros use the vars. dpos and dpos2)
172 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, int how)
174 struct mbuf *mp, *mp2;
176 caddr_t ptr, npos = NULL;
181 *mdp = mp = mp->m_next;
185 *dposp = mtod(mp, caddr_t);
190 } else if (mp->m_next == NULL) {
192 } else if (siz > MHLEN) {
193 panic("nfs S too big");
195 MGET(mp2, how, MT_DATA);
199 mp2->m_next = mp->m_next;
203 ptr = mtod(mp, caddr_t);
205 bcopy(*dposp, ptr, left); /* Copy what was left */
209 npos = mtod(mp2, caddr_t);
210 /* Loop around copying up the siz2 bytes */
214 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
216 bcopy(mtod(mp2, caddr_t), ptr, xfer);
225 npos = mtod(mp2, caddr_t);
229 *dposp = mtod(mp2, caddr_t);
230 if (!nfsm_aligned(*dposp, u_int32_t)) {
231 bcopy(*dposp, npos, mp2->m_len);
240 * Advance the position in the mbuf chain.
243 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
258 *dposp = mtod(m, caddr_t)+offs;
263 nfsm_build_xx(int s, struct mbuf **mb, caddr_t *bpos)
268 if (s > M_TRAILINGSPACE(*mb)) {
269 MGET(mb2, M_WAIT, MT_DATA);
271 panic("build > MLEN");
275 *bpos = mtod(*mb, caddr_t);
284 nfsm_dissect_xx(int s, struct mbuf **md, caddr_t *dpos)
287 return (nfsm_dissect_xx_sub(s, md, dpos, M_WAIT));
291 nfsm_dissect_xx_nonblock(int s, struct mbuf **md, caddr_t *dpos)
294 return (nfsm_dissect_xx_sub(s, md, dpos, M_DONTWAIT));
298 nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how)
304 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
310 cp2 = nfsm_disct(md, dpos, s, t1, how);
315 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
319 tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
322 *s = fxdr_unsigned(int32_t, *tl);
329 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
333 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
338 t1 = nfs_adv(md, dpos, s, t1);
345 * Check for badly aligned mbuf data and realign by copying the unaligned
346 * portion of the data into a new mbuf chain and freeing the portions of the
347 * old chain that were replaced.
349 * We cannot simply realign the data within the existing mbuf chain because
350 * the underlying buffers may contain other rpc commands and we cannot afford
353 * We would prefer to avoid this situation entirely. The situation does not
354 * occur with NFS/UDP and is supposed to only occassionally occur with TCP.
355 * Use vfs.nfs_common.realign_count and realign_test to check this.
358 nfs_realign(struct mbuf **pm, int how)
364 while ((m = *pm) != NULL) {
365 if (!nfsm_aligned(m->m_len, u_int32_t) ||
366 !nfsm_aligned(mtod(m, intptr_t), u_int32_t)) {
368 * NB: we can't depend on m_pkthdr.len to help us
369 * decide what to do here. May not be worth doing
370 * the m_length calculation as m_copyback will
371 * expand the mbuf chain below as needed.
373 if (m_length(m, NULL) >= MINCLSIZE) {
374 /* NB: m_copyback handles space > MCLBYTES */
375 n = m_getcl(how, MT_DATA, 0);
377 n = m_get(how, MT_DATA);
381 * Align the remainder of the mbuf chain.
386 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
400 static moduledata_t nfs_common_mod = {
406 DECLARE_MODULE(nfs_common, nfs_common_mod, SI_SUB_VFS, SI_ORDER_ANY);
407 MODULE_VERSION(nfs_common, 1);