]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/nfs/nfs_common.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / nfs / nfs_common.c
1 /*-
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  *      @(#)nfs_subs.c  8.8 (Berkeley) 5/22/95
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
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.
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/stat.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>
61
62 #include <vm/vm.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
65
66 #include <nfs/nfsproto.h>
67 #include <nfsserver/nfs.h>
68 #include <nfs/xdr_subs.h>
69 #include <nfs/nfs_common.h>
70
71 #include <netinet/in.h>
72
73 enum vtype nv3tov_type[8]= {
74         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
75 };
76 nfstype nfsv3_type[9] = {
77         NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON
78 };
79
80 static void *nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos,
81     int how);
82
83 SYSCTL_NODE(_vfs, OID_AUTO, nfs_common, CTLFLAG_RD, 0, "NFS common support");
84
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");
88
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");
92
93 u_quad_t
94 nfs_curusec(void)
95 {
96         struct timeval tv;
97
98         getmicrotime(&tv);
99         return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
100 }
101
102 /*
103  * copies mbuf chain to the uio scatter/gather list
104  */
105 int
106 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
107 {
108         char *mbufcp, *uiocp;
109         int xfer, left, len;
110         struct mbuf *mp;
111         long uiosiz, rem;
112         int error = 0;
113
114         mp = *mrep;
115         mbufcp = *dpos;
116         len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
117         rem = nfsm_rndup(siz)-siz;
118         while (siz > 0) {
119                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
120                         return (EFBIG);
121                 left = uiop->uio_iov->iov_len;
122                 uiocp = uiop->uio_iov->iov_base;
123                 if (left > siz)
124                         left = siz;
125                 uiosiz = left;
126                 while (left > 0) {
127                         while (len == 0) {
128                                 mp = mp->m_next;
129                                 if (mp == NULL)
130                                         return (EBADRPC);
131                                 mbufcp = mtod(mp, caddr_t);
132                                 len = mp->m_len;
133                         }
134                         xfer = (left > len) ? len : left;
135 #ifdef notdef
136                         /* Not Yet.. */
137                         if (uiop->uio_iov->iov_op != NULL)
138                                 (*(uiop->uio_iov->iov_op))
139                                 (mbufcp, uiocp, xfer);
140                         else
141 #endif
142                         if (uiop->uio_segflg == UIO_SYSSPACE)
143                                 bcopy(mbufcp, uiocp, xfer);
144                         else
145                                 copyout(mbufcp, uiocp, xfer);
146                         left -= xfer;
147                         len -= xfer;
148                         mbufcp += xfer;
149                         uiocp += xfer;
150                         uiop->uio_offset += xfer;
151                         uiop->uio_resid -= xfer;
152                 }
153                 if (uiop->uio_iov->iov_len <= siz) {
154                         uiop->uio_iovcnt--;
155                         uiop->uio_iov++;
156                 } else {
157                         uiop->uio_iov->iov_base =
158                             (char *)uiop->uio_iov->iov_base + uiosiz;
159                         uiop->uio_iov->iov_len -= uiosiz;
160                 }
161                 siz -= uiosiz;
162         }
163         *dpos = mbufcp;
164         *mrep = mp;
165         if (rem > 0) {
166                 if (len < rem)
167                         error = nfs_adv(mrep, dpos, rem, len);
168                 else
169                         *dpos += rem;
170         }
171         return (error);
172 }
173
174 /*
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)
179  */
180 void *
181 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, int how)
182 {
183         struct mbuf *mp, *mp2;
184         int siz2, xfer;
185         caddr_t ptr, npos = NULL;
186         void *ret;
187
188         mp = *mdp;
189         while (left == 0) {
190                 *mdp = mp = mp->m_next;
191                 if (mp == NULL)
192                         return (NULL);
193                 left = mp->m_len;
194                 *dposp = mtod(mp, caddr_t);
195         }
196         if (left >= siz) {
197                 ret = *dposp;
198                 *dposp += siz;
199         } else if (mp->m_next == NULL) {
200                 return (NULL);
201         } else if (siz > MHLEN) {
202                 panic("nfs S too big");
203         } else {
204                 MGET(mp2, how, MT_DATA);
205                 if (mp2 == NULL)
206                         return (NULL);
207                 mp2->m_len = siz;
208                 mp2->m_next = mp->m_next;
209                 mp->m_next = mp2;
210                 mp->m_len -= left;
211                 mp = mp2;
212                 ptr = mtod(mp, caddr_t);
213                 ret = ptr;
214                 bcopy(*dposp, ptr, left);               /* Copy what was left */
215                 siz2 = siz-left;
216                 ptr += left;
217                 mp2 = mp->m_next;
218                 npos = mtod(mp2, caddr_t);
219                 /* Loop around copying up the siz2 bytes */
220                 while (siz2 > 0) {
221                         if (mp2 == NULL)
222                                 return (NULL);
223                         xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
224                         if (xfer > 0) {
225                                 bcopy(mtod(mp2, caddr_t), ptr, xfer);
226                                 mp2->m_data += xfer;
227                                 mp2->m_len -= xfer;
228                                 ptr += xfer;
229                                 siz2 -= xfer;
230                         }
231                         if (siz2 > 0) {
232                                 mp2 = mp2->m_next;
233                                 if (mp2 != NULL)
234                                         npos = mtod(mp2, caddr_t);
235                         }
236                 }
237                 *mdp = mp2;
238                 *dposp = mtod(mp2, caddr_t);
239                 if (!nfsm_aligned(*dposp, u_int32_t)) {
240                         bcopy(*dposp, npos, mp2->m_len);
241                         mp2->m_data = npos;
242                         *dposp = npos;
243                 }
244         }
245         return (ret);
246 }
247
248 /*
249  * Advance the position in the mbuf chain.
250  */
251 int
252 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
253 {
254         struct mbuf *m;
255         int s;
256
257         m = *mdp;
258         s = left;
259         while (s < offs) {
260                 offs -= s;
261                 m = m->m_next;
262                 if (m == NULL)
263                         return (EBADRPC);
264                 s = m->m_len;
265         }
266         *mdp = m;
267         *dposp = mtod(m, caddr_t)+offs;
268         return (0);
269 }
270
271 void *
272 nfsm_build_xx(int s, struct mbuf **mb, caddr_t *bpos)
273 {
274         struct mbuf *mb2;
275         void *ret;
276
277         if (s > M_TRAILINGSPACE(*mb)) {
278                 MGET(mb2, M_WAIT, MT_DATA);
279                 if (s > MLEN)
280                         panic("build > MLEN");
281                 (*mb)->m_next = mb2;
282                 *mb = mb2;
283                 (*mb)->m_len = 0;
284                 *bpos = mtod(*mb, caddr_t);
285         }
286         ret = *bpos;
287         (*mb)->m_len += s;
288         *bpos += s;
289         return (ret);
290 }
291
292 void *
293 nfsm_dissect_xx(int s, struct mbuf **md, caddr_t *dpos)
294 {
295
296         return (nfsm_dissect_xx_sub(s, md, dpos, M_WAIT));
297 }
298
299 void *
300 nfsm_dissect_xx_nonblock(int s, struct mbuf **md, caddr_t *dpos)
301 {
302
303         return (nfsm_dissect_xx_sub(s, md, dpos, M_DONTWAIT));
304 }
305
306 static void *
307 nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how)
308 {
309         int t1;
310         char *cp2;
311         void *ret;
312
313         t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
314         if (t1 >= s) {
315                 ret = *dpos;
316                 *dpos += s;
317                 return (ret);
318         }
319         cp2 = nfsm_disct(md, dpos, s, t1, how);
320         return (cp2);
321 }
322
323 int
324 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
325 {
326         u_int32_t *tl;
327
328         tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
329         if (tl == NULL)
330                 return (EBADRPC);
331         *s = fxdr_unsigned(int32_t, *tl);
332         if (*s > m)
333                 return (EBADRPC);
334         return (0);
335 }
336
337 int
338 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
339 {
340         int t1;
341
342         t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
343         if (t1 >= s) {
344                 *dpos += s;
345                 return (0);
346         }
347         t1 = nfs_adv(md, dpos, s, t1);
348         if (t1)
349                 return (t1);
350         return (0);
351 }
352
353 /*
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.
357  *
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
360  * to overwrite them.
361  *
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.realign_count and realign_test to check this.
365  */
366 int
367 nfs_realign(struct mbuf **pm, int how)
368 {
369         struct mbuf *m, *n;
370         int off;
371
372         ++nfs_realign_test;
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)) {
376                         /*
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.
381                          */
382                         if (m_length(m, NULL) >= MINCLSIZE) {
383                                 /* NB: m_copyback handles space > MCLBYTES */
384                                 n = m_getcl(how, MT_DATA, 0);
385                         } else
386                                 n = m_get(how, MT_DATA);
387                         if (n == NULL)
388                                 return (ENOMEM);
389                         /*
390                          * Align the remainder of the mbuf chain.
391                          */
392                         n->m_len = 0;
393                         off = 0;
394                         while (m != NULL) {
395                                 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
396                                 off += m->m_len;
397                                 m = m->m_next;
398                         }
399                         m_freem(*pm);
400                         *pm = n;
401                         ++nfs_realign_count;
402                         break;
403                 }
404                 pm = &m->m_next;
405         }
406         return (0);
407 }
408
409 static moduledata_t nfs_common_mod = {
410         "nfs_common",
411         NULL,
412         NULL
413 };
414
415 DECLARE_MODULE(nfs_common, nfs_common_mod, SI_SUB_VFS, SI_ORDER_ANY);
416 MODULE_VERSION(nfs_common, 1);