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, int how);
82 SYSCTL_NODE(_vfs, OID_AUTO, nfs_common, CTLFLAG_RD, 0, "NFS common support");
84 static int nfs_realign_test;
85 SYSCTL_INT(_vfs_nfs_common, OID_AUTO, realign_test, CTLFLAG_RD,
86 &nfs_realign_test, 0, "Number of realign tests done");
88 static int nfs_realign_count;
89 SYSCTL_INT(_vfs_nfs_common, OID_AUTO, realign_count, CTLFLAG_RD,
90 &nfs_realign_count, 0, "Number of mbuf realignments done");
98 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
102 * copies mbuf chain to the uio scatter/gather list
105 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
107 char *mbufcp, *uiocp;
115 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
116 rem = nfsm_rndup(siz)-siz;
118 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
120 left = uiop->uio_iov->iov_len;
121 uiocp = uiop->uio_iov->iov_base;
130 mbufcp = mtod(mp, caddr_t);
133 xfer = (left > len) ? len : left;
136 if (uiop->uio_iov->iov_op != NULL)
137 (*(uiop->uio_iov->iov_op))
138 (mbufcp, uiocp, xfer);
141 if (uiop->uio_segflg == UIO_SYSSPACE)
142 bcopy(mbufcp, uiocp, xfer);
144 copyout(mbufcp, uiocp, xfer);
149 uiop->uio_offset += xfer;
150 uiop->uio_resid -= xfer;
152 if (uiop->uio_iov->iov_len <= siz) {
156 uiop->uio_iov->iov_base =
157 (char *)uiop->uio_iov->iov_base + uiosiz;
158 uiop->uio_iov->iov_len -= uiosiz;
166 error = nfs_adv(mrep, dpos, rem, len);
174 * Help break down an mbuf chain by setting the first siz bytes contiguous
175 * pointed to by returned val.
176 * This is used by the macros nfsm_dissect for tough
177 * cases. (The macros use the vars. dpos and dpos2)
180 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, int how)
182 struct mbuf *mp, *mp2;
184 caddr_t ptr, npos = NULL;
189 *mdp = mp = mp->m_next;
193 *dposp = mtod(mp, caddr_t);
198 } else if (mp->m_next == NULL) {
200 } else if (siz > MHLEN) {
201 panic("nfs S too big");
203 MGET(mp2, how, MT_DATA);
207 mp2->m_next = mp->m_next;
211 ptr = mtod(mp, caddr_t);
213 bcopy(*dposp, ptr, left); /* Copy what was left */
217 npos = mtod(mp2, caddr_t);
218 /* Loop around copying up the siz2 bytes */
222 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
224 bcopy(mtod(mp2, caddr_t), ptr, xfer);
233 npos = mtod(mp2, caddr_t);
237 *dposp = mtod(mp2, caddr_t);
238 if (!nfsm_aligned(*dposp, u_int32_t)) {
239 bcopy(*dposp, npos, mp2->m_len);
248 * Advance the position in the mbuf chain.
251 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
266 *dposp = mtod(m, caddr_t)+offs;
271 nfsm_build_xx(int s, struct mbuf **mb, caddr_t *bpos)
276 if (s > M_TRAILINGSPACE(*mb)) {
277 MGET(mb2, M_WAIT, MT_DATA);
279 panic("build > MLEN");
283 *bpos = mtod(*mb, caddr_t);
292 nfsm_dissect_xx(int s, struct mbuf **md, caddr_t *dpos)
294 return nfsm_dissect_xx_sub(s, md, dpos, M_WAIT);
298 nfsm_dissect_xx_nonblock(int s, struct mbuf **md, caddr_t *dpos)
300 return nfsm_dissect_xx_sub(s, md, dpos, M_DONTWAIT);
304 nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how)
310 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
316 cp2 = nfsm_disct(md, dpos, s, t1, how);
321 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
325 tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
328 *s = fxdr_unsigned(int32_t, *tl);
335 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
339 t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
344 t1 = nfs_adv(md, dpos, s, t1);
351 * Check for badly aligned mbuf data and realign by copying the unaligned
352 * portion of the data into a new mbuf chain and freeing the portions of the
353 * old chain that were replaced.
355 * We cannot simply realign the data within the existing mbuf chain because
356 * the underlying buffers may contain other rpc commands and we cannot afford
359 * We would prefer to avoid this situation entirely. The situation does not
360 * occur with NFS/UDP and is supposed to only occassionally occur with TCP.
361 * Use vfs.nfs.realign_count and realign_test to check this.
364 nfs_realign(struct mbuf **pm, int how)
370 while ((m = *pm) != NULL) {
371 if (!nfsm_aligned(m->m_len, u_int32_t) ||
372 !nfsm_aligned(mtod(m, intptr_t), u_int32_t)) {
374 * NB: we can't depend on m_pkthdr.len to help us
375 * decide what to do here. May not be worth doing
376 * the m_length calculation as m_copyback will
377 * expand the mbuf chain below as needed.
379 if (m_length(m, NULL) >= MINCLSIZE) {
380 /* NB: m_copyback handles space > MCLBYTES */
381 n = m_getcl(how, MT_DATA, 0);
383 n = m_get(how, MT_DATA);
387 * Align the remainder of the mbuf chain.
392 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
406 static moduledata_t nfs_common_mod = {
412 DECLARE_MODULE(nfs_common, nfs_common_mod, SI_SUB_VFS, SI_ORDER_ANY);
413 MODULE_VERSION(nfs_common, 1);