]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/nfs/nfs_common.c
- Factor out the code shared between NFS client and server into its own
[FreeBSD/stable/8.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, int how);
81
82 SYSCTL_NODE(_vfs, OID_AUTO, nfs_common, CTLFLAG_RD, 0, "NFS common support");
83
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");
87
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");
91
92 u_quad_t
93 nfs_curusec(void) 
94 {
95         struct timeval tv;
96
97         getmicrotime(&tv);
98         return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
99 }
100
101 /*
102  * copies mbuf chain to the uio scatter/gather list
103  */
104 int
105 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
106 {
107         char *mbufcp, *uiocp;
108         int xfer, left, len;
109         struct mbuf *mp;
110         long uiosiz, rem;
111         int error = 0;
112
113         mp = *mrep;
114         mbufcp = *dpos;
115         len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
116         rem = nfsm_rndup(siz)-siz;
117         while (siz > 0) {
118                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
119                         return (EFBIG);
120                 left = uiop->uio_iov->iov_len;
121                 uiocp = uiop->uio_iov->iov_base;
122                 if (left > siz)
123                         left = siz;
124                 uiosiz = left;
125                 while (left > 0) {
126                         while (len == 0) {
127                                 mp = mp->m_next;
128                                 if (mp == NULL)
129                                         return (EBADRPC);
130                                 mbufcp = mtod(mp, caddr_t);
131                                 len = mp->m_len;
132                         }
133                         xfer = (left > len) ? len : left;
134 #ifdef notdef
135                         /* Not Yet.. */
136                         if (uiop->uio_iov->iov_op != NULL)
137                                 (*(uiop->uio_iov->iov_op))
138                                 (mbufcp, uiocp, xfer);
139                         else
140 #endif
141                         if (uiop->uio_segflg == UIO_SYSSPACE)
142                                 bcopy(mbufcp, uiocp, xfer);
143                         else
144                                 copyout(mbufcp, uiocp, xfer);
145                         left -= xfer;
146                         len -= xfer;
147                         mbufcp += xfer;
148                         uiocp += xfer;
149                         uiop->uio_offset += xfer;
150                         uiop->uio_resid -= xfer;
151                 }
152                 if (uiop->uio_iov->iov_len <= siz) {
153                         uiop->uio_iovcnt--;
154                         uiop->uio_iov++;
155                 } else {
156                         uiop->uio_iov->iov_base =
157                             (char *)uiop->uio_iov->iov_base + uiosiz;
158                         uiop->uio_iov->iov_len -= uiosiz;
159                 }
160                 siz -= uiosiz;
161         }
162         *dpos = mbufcp;
163         *mrep = mp;
164         if (rem > 0) {
165                 if (len < rem)
166                         error = nfs_adv(mrep, dpos, rem, len);
167                 else
168                         *dpos += rem;
169         }
170         return (error);
171 }
172
173 /*
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)
178  */
179 void *
180 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, int how)
181 {
182         struct mbuf *mp, *mp2;
183         int siz2, xfer;
184         caddr_t ptr, npos = NULL;
185         void *ret;
186
187         mp = *mdp;
188         while (left == 0) {
189                 *mdp = mp = mp->m_next;
190                 if (mp == NULL)
191                         return NULL;
192                 left = mp->m_len;
193                 *dposp = mtod(mp, caddr_t);
194         }
195         if (left >= siz) {
196                 ret = *dposp;
197                 *dposp += siz;
198         } else if (mp->m_next == NULL) {
199                 return NULL;
200         } else if (siz > MHLEN) {
201                 panic("nfs S too big");
202         } else {
203                 MGET(mp2, how, MT_DATA);
204                 if (mp2 == NULL)
205                         return NULL;
206                 mp2->m_len = siz;
207                 mp2->m_next = mp->m_next;
208                 mp->m_next = mp2;
209                 mp->m_len -= left;
210                 mp = mp2;
211                 ptr = mtod(mp, caddr_t);
212                 ret = ptr;
213                 bcopy(*dposp, ptr, left);               /* Copy what was left */
214                 siz2 = siz-left;
215                 ptr += left;
216                 mp2 = mp->m_next;
217                 npos = mtod(mp2, caddr_t);
218                 /* Loop around copying up the siz2 bytes */
219                 while (siz2 > 0) {
220                         if (mp2 == NULL)
221                                 return NULL;
222                         xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
223                         if (xfer > 0) {
224                                 bcopy(mtod(mp2, caddr_t), ptr, xfer);
225                                 mp2->m_data += xfer;
226                                 mp2->m_len -= xfer;
227                                 ptr += xfer;
228                                 siz2 -= xfer;
229                         }
230                         if (siz2 > 0) {
231                                 mp2 = mp2->m_next;
232                                 if (mp2 != NULL)
233                                         npos = mtod(mp2, caddr_t);
234                         }
235                 }
236                 *mdp = mp2;
237                 *dposp = mtod(mp2, caddr_t);
238                 if (!nfsm_aligned(*dposp, u_int32_t)) {
239                         bcopy(*dposp, npos, mp2->m_len);
240                         mp2->m_data = npos;
241                         *dposp = npos;
242                 }
243         }
244         return ret;
245 }
246
247 /*
248  * Advance the position in the mbuf chain.
249  */
250 int
251 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
252 {
253         struct mbuf *m;
254         int s;
255
256         m = *mdp;
257         s = left;
258         while (s < offs) {
259                 offs -= s;
260                 m = m->m_next;
261                 if (m == NULL)
262                         return (EBADRPC);
263                 s = m->m_len;
264         }
265         *mdp = m;
266         *dposp = mtod(m, caddr_t)+offs;
267         return (0);
268 }
269
270 void *
271 nfsm_build_xx(int s, struct mbuf **mb, caddr_t *bpos)
272 {
273         struct mbuf *mb2;
274         void *ret;
275
276         if (s > M_TRAILINGSPACE(*mb)) {
277                 MGET(mb2, M_WAIT, MT_DATA);
278                 if (s > MLEN)
279                         panic("build > MLEN");
280                 (*mb)->m_next = mb2;
281                 *mb = mb2;
282                 (*mb)->m_len = 0;
283                 *bpos = mtod(*mb, caddr_t);
284         }
285         ret = *bpos;
286         (*mb)->m_len += s;
287         *bpos += s;
288         return ret;
289 }
290
291 void *
292 nfsm_dissect_xx(int s, struct mbuf **md, caddr_t *dpos)
293 {
294         return nfsm_dissect_xx_sub(s, md, dpos, M_WAIT);
295 }
296
297 void *
298 nfsm_dissect_xx_nonblock(int s, struct mbuf **md, caddr_t *dpos)
299 {
300         return nfsm_dissect_xx_sub(s, md, dpos, M_DONTWAIT);
301 }
302
303 static void *
304 nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how)
305 {
306         int t1;
307         char *cp2;
308         void *ret;
309
310         t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
311         if (t1 >= s) {
312                 ret = *dpos;
313                 *dpos += s;
314                 return ret;
315         }
316         cp2 = nfsm_disct(md, dpos, s, t1, how); 
317         return cp2;
318 }
319
320 int
321 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
322 {
323         u_int32_t *tl;
324
325         tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
326         if (tl == NULL)
327                 return EBADRPC;
328         *s = fxdr_unsigned(int32_t, *tl);
329         if (*s > m)
330                 return EBADRPC;
331         return 0;
332 }
333
334 int
335 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
336 {
337         int t1;
338
339         t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
340         if (t1 >= s) {
341                 *dpos += s;
342                 return 0;
343         }
344         t1 = nfs_adv(md, dpos, s, t1);
345         if (t1)
346                 return t1;
347         return 0;
348 }
349
350 /*
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.
354  *
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
357  * to overwrite them.
358  *
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.
362  */
363 int
364 nfs_realign(struct mbuf **pm, int how)
365 {
366         struct mbuf *m, *n;
367         int off;
368
369         ++nfs_realign_test;
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)) {
373                         /*
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.
378                          */
379                         if (m_length(m, NULL) >= MINCLSIZE) {
380                                 /* NB: m_copyback handles space > MCLBYTES */
381                                 n = m_getcl(how, MT_DATA, 0);
382                         } else
383                                 n = m_get(how, MT_DATA);
384                         if (n == NULL)
385                                 return (ENOMEM);
386                         /*
387                          * Align the remainder of the mbuf chain.
388                          */
389                         n->m_len = 0;
390                         off = 0;
391                         while (m != NULL) {
392                                 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
393                                 off += m->m_len;
394                                 m = m->m_next;
395                         }
396                         m_freem(*pm);
397                         *pm = n;
398                         ++nfs_realign_count;
399                         break;
400                 }
401                 pm = &m->m_next;
402         }
403         return (0);
404 }
405
406 static moduledata_t nfs_common_mod = {
407         "nfs_common",
408         NULL,
409         NULL
410 };
411
412 DECLARE_MODULE(nfs_common, nfs_common_mod, SI_SUB_VFS, SI_ORDER_ANY);
413 MODULE_VERSION(nfs_common, 1);