Disallow writes to filesystems mounted read-only via NULLFS. In this case
[dragonfly.git] / sys / kern / uipc_mbuf2.c
CommitLineData
984263bc 1/* $FreeBSD: src/sys/kern/uipc_mbuf2.c,v 1.2.2.5 2003/01/23 21:06:44 sam Exp $ */
efda3bd0 2/* $DragonFly: src/sys/kern/uipc_mbuf2.c,v 1.12 2006/09/05 00:55:45 dillon Exp $ */
984263bc
MD
3/* $KAME: uipc_mbuf2.c,v 1.31 2001/11/28 11:08:53 itojun Exp $ */
4/* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
5
6/*
7 * Copyright (C) 1999 WIDE Project.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * Copyright (c) 1982, 1986, 1988, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
68 */
69
70/*#define PULLDOWN_DEBUG*/
71
72#include <sys/param.h>
73#include <sys/systm.h>
74#include <sys/kernel.h>
75#include <sys/proc.h>
76#include <sys/malloc.h>
77#include <sys/mbuf.h>
78
984263bc
MD
79MALLOC_DEFINE(M_PACKET_TAGS, "tag", "packet-attached information");
80
81/* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
402ed7e1 82static struct mbuf *m_dup1 (struct mbuf *, int, int, int);
984263bc
MD
83
84/*
85 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
86 * packet chain before "off" is kept untouched.
87 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
88 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
89 *
90 * on error return (NULL return value), original "m" will be freed.
91 *
92 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
93 */
94struct mbuf *
95m_pulldown(m, off, len, offp)
96 struct mbuf *m;
97 int off, len;
98 int *offp;
99{
100 struct mbuf *n, *o;
101 int hlen, tlen, olen;
102 int sharedcluster;
103
104 /* check invalid arguments. */
105 if (m == NULL)
106 panic("m == NULL in m_pulldown()");
107 if (len > MCLBYTES) {
108 m_freem(m);
109 return NULL; /* impossible */
110 }
111
112#ifdef PULLDOWN_DEBUG
113 {
114 struct mbuf *t;
115 printf("before:");
116 for (t = m; t; t = t->m_next)
117 printf(" %d", t->m_len);
118 printf("\n");
119 }
120#endif
121 n = m;
122 while (n != NULL && off > 0) {
123 if (n->m_len > off)
124 break;
125 off -= n->m_len;
126 n = n->m_next;
127 }
128 /* be sure to point non-empty mbuf */
129 while (n != NULL && n->m_len == 0)
130 n = n->m_next;
131 if (!n) {
132 m_freem(m);
133 return NULL; /* mbuf chain too short */
134 }
135
90775e29 136 sharedcluster = (m_sharecount(n) > 1);
984263bc
MD
137
138 /*
139 * the target data is on <n, off>.
140 * if we got enough data on the mbuf "n", we're done.
141 */
142 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
143 goto ok;
144
145 /*
146 * when len <= n->m_len - off and off != 0, it is a special case.
147 * len bytes from <n, off> sits in single mbuf, but the caller does
148 * not like the starting position (off).
149 * chop the current mbuf into two pieces, set off to 0.
150 */
151 if (len <= n->m_len - off) {
74f1caca 152 o = m_dup1(n, off, n->m_len - off, MB_DONTWAIT);
984263bc
MD
153 if (o == NULL) {
154 m_freem(m);
155 return NULL; /* ENOBUFS */
156 }
157 n->m_len = off;
158 o->m_next = n->m_next;
159 n->m_next = o;
160 n = n->m_next;
161 off = 0;
162 goto ok;
163 }
164
165 /*
166 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
167 * and construct contiguous mbuf with m_len == len.
168 * note that hlen + tlen == len, and tlen > 0.
169 */
170 hlen = n->m_len - off;
171 tlen = len - hlen;
172
173 /*
174 * ensure that we have enough trailing data on mbuf chain.
175 * if not, we can do nothing about the chain.
176 */
177 olen = 0;
178 for (o = n->m_next; o != NULL; o = o->m_next)
179 olen += o->m_len;
180 if (hlen + olen < len) {
181 m_freem(m);
182 return NULL; /* mbuf chain too short */
183 }
184
185 /*
186 * easy cases first.
187 * we need to use m_copydata() to get data from <n->m_next, 0>.
188 */
189 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
190 !sharedcluster) {
191 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
192 n->m_len += tlen;
193 m_adj(n->m_next, tlen);
194 goto ok;
195 }
196 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
197 !sharedcluster) {
198 n->m_next->m_data -= hlen;
199 n->m_next->m_len += hlen;
200 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
201 n->m_len -= hlen;
202 n = n->m_next;
203 off = 0;
204 goto ok;
205 }
206
207 /*
208 * now, we need to do the hard way. don't m_copy as there's no room
209 * on both end.
210 */
50503f0f 211 o = m_getl(len, MB_DONTWAIT, m->m_type, 0, NULL);
984263bc
MD
212 if (!o) {
213 m_freem(m);
214 return NULL; /* ENOBUFS */
215 }
216 /* get hlen from <n, off> into <o, 0> */
217 o->m_len = hlen;
218 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
219 n->m_len -= hlen;
220 /* get tlen from <n->m_next, 0> into <o, hlen> */
221 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
222 o->m_len += tlen;
223 m_adj(n->m_next, tlen);
224 o->m_next = n->m_next;
225 n->m_next = o;
226 n = o;
227 off = 0;
228
229ok:
230#ifdef PULLDOWN_DEBUG
231 {
232 struct mbuf *t;
233 printf("after:");
234 for (t = m; t; t = t->m_next)
235 printf("%c%d", t == n ? '*' : ' ', t->m_len);
236 printf(" (off=%d)\n", off);
237 }
238#endif
239 if (offp)
240 *offp = off;
241 return n;
242}
243
244static struct mbuf *
245m_dup1(m, off, len, wait)
246 struct mbuf *m;
247 int off;
248 int len;
249 int wait;
250{
251 struct mbuf *n;
984263bc
MD
252
253 if (len > MCLBYTES)
254 return NULL;
50503f0f 255 n = m_getl(len, wait, m->m_type, m->m_flags, NULL);
984263bc
MD
256 if (!n)
257 return NULL;
50503f0f 258 if (off == 0 && (m->m_flags & M_PKTHDR) && !m_dup_pkthdr(n, m, wait)) {
984263bc
MD
259 m_free(n);
260 return NULL;
261 }
262 m_copydata(m, off, len, mtod(n, caddr_t));
263 return n;
264}
265
266/* Get a packet tag structure along with specified data following. */
267struct m_tag *
268m_tag_alloc(u_int32_t cookie, int type, int len, int wait)
269{
270 struct m_tag *t;
271
272 if (len < 0)
273 return NULL;
efda3bd0 274 t = kmalloc(len + sizeof(struct m_tag), M_PACKET_TAGS,
74f1caca 275 wait != MB_WAIT ? M_NOWAIT : M_WAITOK);
984263bc
MD
276 if (t == NULL)
277 return NULL;
278 t->m_tag_id = type;
279 t->m_tag_len = len;
280 t->m_tag_cookie = cookie;
281 return t;
282}
283
284
285/* Free a packet tag. */
286void
287m_tag_free(struct m_tag *t)
288{
efda3bd0 289 kfree(t, M_PACKET_TAGS);
984263bc
MD
290}
291
292/* Prepend a packet tag. */
293void
294m_tag_prepend(struct mbuf *m, struct m_tag *t)
295{
296 KASSERT(m && t, ("m_tag_prepend: null argument, m %p t %p", m, t));
d031aa80 297 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_prepend: mbuf %p non-header", m));
984263bc
MD
298 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
299}
300
301/* Unlink a packet tag. */
302void
303m_tag_unlink(struct mbuf *m, struct m_tag *t)
304{
305 KASSERT(m && t, ("m_tag_unlink: null argument, m %p t %p", m, t));
d031aa80 306 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_unlink: mbuf %p non-header", m));
984263bc
MD
307 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
308}
309
310/* Unlink and free a packet tag. */
311void
312m_tag_delete(struct mbuf *m, struct m_tag *t)
313{
314 KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t));
d031aa80 315 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_delete: mbuf %p non-header", m));
984263bc
MD
316 m_tag_unlink(m, t);
317 m_tag_free(t);
318}
319
320/* Unlink and free a packet tag chain, starting from given tag. */
321void
7b6f875f 322m_tag_delete_chain(struct mbuf *m)
984263bc
MD
323{
324 struct m_tag *p, *q;
325
326 KASSERT(m, ("m_tag_delete_chain: null mbuf"));
d031aa80 327 KASSERT(m->m_flags & M_PKTHDR, ("m_tag_delete_chain: mbuf %p non-header", m));
7b6f875f 328 p = SLIST_FIRST(&m->m_pkthdr.tags);
984263bc
MD
329 if (p == NULL)
330 return;
331 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
332 m_tag_delete(m, q);
333 m_tag_delete(m, p);
334}
335
336/* Find a tag, starting from a given position. */
337struct m_tag *
338m_tag_locate(struct mbuf *m, u_int32_t cookie, int type, struct m_tag *t)
339{
340 struct m_tag *p;
341
342 KASSERT(m, ("m_tag_find: null mbuf"));
343 if (t == NULL)
344 p = SLIST_FIRST(&m->m_pkthdr.tags);
345 else
346 p = SLIST_NEXT(t, m_tag_link);
347 while (p != NULL) {
348 if (p->m_tag_cookie == cookie && p->m_tag_id == type)
349 return p;
350 p = SLIST_NEXT(p, m_tag_link);
351 }
352 return NULL;
353}
354
355/* Copy a single tag. */
356struct m_tag *
357m_tag_copy(struct m_tag *t, int how)
358{
359 struct m_tag *p;
360
361 KASSERT(t, ("m_tag_copy: null tag"));
362 p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
363 if (p == NULL)
364 return (NULL);
365 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
366 return p;
367}
368
369/*
370 * Copy two tag chains. The destination mbuf (to) loses any attached
371 * tags even if the operation fails. This should not be a problem, as
372 * m_tag_copy_chain() is typically called with a newly-allocated
373 * destination mbuf.
374 */
375int
f15db79e 376m_tag_copy_chain(struct mbuf *to, const struct mbuf *from, int how)
984263bc
MD
377{
378 struct m_tag *p, *t, *tprev = NULL;
379
380 KASSERT(to && from,
381 ("m_tag_copy: null argument, to %p from %p", to, from));
7b6f875f 382 m_tag_delete_chain(to);
984263bc
MD
383 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
384 t = m_tag_copy(p, how);
385 if (t == NULL) {
7b6f875f 386 m_tag_delete_chain(to);
984263bc
MD
387 return 0;
388 }
389 if (tprev == NULL)
390 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
391 else {
392 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
393 tprev = t;
394 }
395 }
396 return 1;
397}
398
399/* Initialize tags on an mbuf. */
400void
401m_tag_init(struct mbuf *m)
402{
403 SLIST_INIT(&m->m_pkthdr.tags);
404}
405
406/* Get first tag in chain. */
407struct m_tag *
408m_tag_first(struct mbuf *m)
409{
410 return SLIST_FIRST(&m->m_pkthdr.tags);
411}
412
413/* Get next tag in chain. */
414struct m_tag *
415m_tag_next(struct mbuf *m, struct m_tag *t)
416{
417 return SLIST_NEXT(t, m_tag_link);
418}