]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/nfs/nfs_common.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.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/sysent.h>
58 #include <sys/syscall.h>
59
60 #include <vm/vm.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_extern.h>
63
64 #include <nfs/nfsproto.h>
65 #include <nfsserver/nfs.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/nfs_common.h>
68
69 #include <netinet/in.h>
70
71 enum vtype nv3tov_type[8]= {
72         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
73 };
74 nfstype nfsv3_type[9] = {
75         NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK, NFFIFO, NFNON
76 };
77
78 static void *nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how);
79
80 u_quad_t
81 nfs_curusec(void) 
82 {
83         struct timeval tv;
84
85         getmicrotime(&tv);
86         return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
87 }
88
89 /*
90  * copies mbuf chain to the uio scatter/gather list
91  */
92 int
93 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
94 {
95         char *mbufcp, *uiocp;
96         int xfer, left, len;
97         struct mbuf *mp;
98         long uiosiz, rem;
99         int error = 0;
100
101         mp = *mrep;
102         mbufcp = *dpos;
103         len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
104         rem = nfsm_rndup(siz)-siz;
105         while (siz > 0) {
106                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
107                         return (EFBIG);
108                 left = uiop->uio_iov->iov_len;
109                 uiocp = uiop->uio_iov->iov_base;
110                 if (left > siz)
111                         left = siz;
112                 uiosiz = left;
113                 while (left > 0) {
114                         while (len == 0) {
115                                 mp = mp->m_next;
116                                 if (mp == NULL)
117                                         return (EBADRPC);
118                                 mbufcp = mtod(mp, caddr_t);
119                                 len = mp->m_len;
120                         }
121                         xfer = (left > len) ? len : left;
122 #ifdef notdef
123                         /* Not Yet.. */
124                         if (uiop->uio_iov->iov_op != NULL)
125                                 (*(uiop->uio_iov->iov_op))
126                                 (mbufcp, uiocp, xfer);
127                         else
128 #endif
129                         if (uiop->uio_segflg == UIO_SYSSPACE)
130                                 bcopy(mbufcp, uiocp, xfer);
131                         else
132                                 copyout(mbufcp, uiocp, xfer);
133                         left -= xfer;
134                         len -= xfer;
135                         mbufcp += xfer;
136                         uiocp += xfer;
137                         uiop->uio_offset += xfer;
138                         uiop->uio_resid -= xfer;
139                 }
140                 if (uiop->uio_iov->iov_len <= siz) {
141                         uiop->uio_iovcnt--;
142                         uiop->uio_iov++;
143                 } else {
144                         uiop->uio_iov->iov_base =
145                             (char *)uiop->uio_iov->iov_base + uiosiz;
146                         uiop->uio_iov->iov_len -= uiosiz;
147                 }
148                 siz -= uiosiz;
149         }
150         *dpos = mbufcp;
151         *mrep = mp;
152         if (rem > 0) {
153                 if (len < rem)
154                         error = nfs_adv(mrep, dpos, rem, len);
155                 else
156                         *dpos += rem;
157         }
158         return (error);
159 }
160
161 /*
162  * Help break down an mbuf chain by setting the first siz bytes contiguous
163  * pointed to by returned val.
164  * This is used by the macros nfsm_dissect for tough
165  * cases. (The macros use the vars. dpos and dpos2)
166  */
167 void *
168 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, int how)
169 {
170         struct mbuf *mp, *mp2;
171         int siz2, xfer;
172         caddr_t ptr, npos = NULL;
173         void *ret;
174
175         mp = *mdp;
176         while (left == 0) {
177                 *mdp = mp = mp->m_next;
178                 if (mp == NULL)
179                         return NULL;
180                 left = mp->m_len;
181                 *dposp = mtod(mp, caddr_t);
182         }
183         if (left >= siz) {
184                 ret = *dposp;
185                 *dposp += siz;
186         } else if (mp->m_next == NULL) {
187                 return NULL;
188         } else if (siz > MHLEN) {
189                 panic("nfs S too big");
190         } else {
191                 MGET(mp2, how, MT_DATA);
192                 if (mp2 == NULL)
193                         return NULL;
194                 mp2->m_len = siz;
195                 mp2->m_next = mp->m_next;
196                 mp->m_next = mp2;
197                 mp->m_len -= left;
198                 mp = mp2;
199                 ptr = mtod(mp, caddr_t);
200                 ret = ptr;
201                 bcopy(*dposp, ptr, left);               /* Copy what was left */
202                 siz2 = siz-left;
203                 ptr += left;
204                 mp2 = mp->m_next;
205                 npos = mtod(mp2, caddr_t);
206                 /* Loop around copying up the siz2 bytes */
207                 while (siz2 > 0) {
208                         if (mp2 == NULL)
209                                 return NULL;
210                         xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
211                         if (xfer > 0) {
212                                 bcopy(mtod(mp2, caddr_t), ptr, xfer);
213                                 mp2->m_data += xfer;
214                                 mp2->m_len -= xfer;
215                                 ptr += xfer;
216                                 siz2 -= xfer;
217                         }
218                         if (siz2 > 0) {
219                                 mp2 = mp2->m_next;
220                                 if (mp2 != NULL)
221                                         npos = mtod(mp2, caddr_t);
222                         }
223                 }
224                 *mdp = mp2;
225                 *dposp = mtod(mp2, caddr_t);
226                 if (!nfsm_aligned(*dposp, u_int32_t)) {
227                         bcopy(*dposp, npos, mp2->m_len);
228                         mp2->m_data = npos;
229                         *dposp = npos;
230                 }
231         }
232         return ret;
233 }
234
235 /*
236  * Advance the position in the mbuf chain.
237  */
238 int
239 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
240 {
241         struct mbuf *m;
242         int s;
243
244         m = *mdp;
245         s = left;
246         while (s < offs) {
247                 offs -= s;
248                 m = m->m_next;
249                 if (m == NULL)
250                         return (EBADRPC);
251                 s = m->m_len;
252         }
253         *mdp = m;
254         *dposp = mtod(m, caddr_t)+offs;
255         return (0);
256 }
257
258 void *
259 nfsm_build_xx(int s, struct mbuf **mb, caddr_t *bpos)
260 {
261         struct mbuf *mb2;
262         void *ret;
263
264         if (s > M_TRAILINGSPACE(*mb)) {
265                 MGET(mb2, M_WAIT, MT_DATA);
266                 if (s > MLEN)
267                         panic("build > MLEN");
268                 (*mb)->m_next = mb2;
269                 *mb = mb2;
270                 (*mb)->m_len = 0;
271                 *bpos = mtod(*mb, caddr_t);
272         }
273         ret = *bpos;
274         (*mb)->m_len += s;
275         *bpos += s;
276         return ret;
277 }
278
279 void *
280 nfsm_dissect_xx(int s, struct mbuf **md, caddr_t *dpos)
281 {
282         return nfsm_dissect_xx_sub(s, md, dpos, M_WAIT);
283 }
284
285 void *
286 nfsm_dissect_xx_nonblock(int s, struct mbuf **md, caddr_t *dpos)
287 {
288         return nfsm_dissect_xx_sub(s, md, dpos, M_DONTWAIT);
289 }
290
291 static void *
292 nfsm_dissect_xx_sub(int s, struct mbuf **md, caddr_t *dpos, int how)
293 {
294         int t1;
295         char *cp2;
296         void *ret;
297
298         t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
299         if (t1 >= s) {
300                 ret = *dpos;
301                 *dpos += s;
302                 return ret;
303         }
304         cp2 = nfsm_disct(md, dpos, s, t1, how); 
305         return cp2;
306 }
307
308 int
309 nfsm_strsiz_xx(int *s, int m, struct mbuf **mb, caddr_t *bpos)
310 {
311         u_int32_t *tl;
312
313         tl = nfsm_dissect_xx(NFSX_UNSIGNED, mb, bpos);
314         if (tl == NULL)
315                 return EBADRPC;
316         *s = fxdr_unsigned(int32_t, *tl);
317         if (*s > m)
318                 return EBADRPC;
319         return 0;
320 }
321
322 int
323 nfsm_adv_xx(int s, struct mbuf **md, caddr_t *dpos)
324 {
325         int t1;
326
327         t1 = mtod(*md, caddr_t) + (*md)->m_len - *dpos;
328         if (t1 >= s) {
329                 *dpos += s;
330                 return 0;
331         }
332         t1 = nfs_adv(md, dpos, s, t1);
333         if (t1)
334                 return t1;
335         return 0;
336 }