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