]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_io.c
Eliminate some thread pointers which do not make sense anymore.
[FreeBSD/FreeBSD.git] / sys / geom / geom_io.c
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD$
36  */
37
38
39 #include <sys/param.h>
40 #ifndef _KERNEL
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <signal.h>
45 #include <err.h>
46 #include <sched.h>
47 #else
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/bio.h>
52 #endif
53
54 #include <sys/errno.h>
55 #include <geom/geom.h>
56 #include <geom/geom_int.h>
57
58 static struct g_bioq g_bio_run_down;
59 static struct g_bioq g_bio_run_up;
60 static struct g_bioq g_bio_idle;
61
62 #include <machine/atomic.h>
63
64 static void
65 g_bioq_lock(struct g_bioq *bq)
66 {
67
68         mtx_lock(&bq->bio_queue_lock);
69 }
70
71 static void
72 g_bioq_unlock(struct g_bioq *bq)
73 {
74
75         mtx_unlock(&bq->bio_queue_lock);
76 }
77
78 #if 0
79 static void
80 g_bioq_destroy(struct g_bioq *bq)
81 {
82
83         mtx_destroy(&bq->bio_queue_lock);
84 }
85 #endif
86
87 static void
88 g_bioq_init(struct g_bioq *bq)
89 {
90
91         TAILQ_INIT(&bq->bio_queue);
92         mtx_init(&bq->bio_queue_lock, "bio queue", MTX_DEF);
93 }
94
95 static struct bio *
96 g_bioq_first(struct g_bioq *bq)
97 {
98         struct bio *bp;
99
100         g_bioq_lock(bq);
101         bp = TAILQ_FIRST(&bq->bio_queue);
102         if (bp != NULL) {
103                 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
104                 bq->bio_queue_length--;
105         }
106         g_bioq_unlock(bq);
107         return (bp);
108 }
109
110 static void
111 g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
112 {
113
114         g_bioq_lock(rq);
115         TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
116         rq->bio_queue_length++;
117         g_bioq_unlock(rq);
118 }
119
120 struct bio *
121 g_new_bio(void)
122 {
123         struct bio *bp;
124
125         bp = g_bioq_first(&g_bio_idle);
126         if (bp == NULL)
127                 bp = g_malloc(sizeof *bp, M_WAITOK | M_ZERO);
128         g_trace(G_T_BIO, "g_new_bio() = %p", bp);
129         return (bp);
130 }
131
132 void
133 g_destroy_bio(struct bio *bp)
134 {
135
136         g_trace(G_T_BIO, "g_destroy_bio(%p)", bp);
137         bzero(bp, sizeof *bp);
138         g_bioq_enqueue_tail(bp, &g_bio_idle);
139 }
140
141 struct bio *
142 g_clone_bio(struct bio *bp)
143 {
144         struct bio *bp2;
145
146         bp2 = g_new_bio();
147         bp2->bio_linkage = bp;
148         bp2->bio_cmd = bp->bio_cmd;
149         bp2->bio_length = bp->bio_length;
150         bp2->bio_offset = bp->bio_offset;
151         bp2->bio_data = bp->bio_data;
152         bp2->bio_attribute = bp->bio_attribute;
153         g_trace(G_T_BIO, "g_clone_bio(%p) = %p", bp, bp2);
154         return(bp2);
155 }
156
157 void
158 g_io_init()
159 {
160
161         g_bioq_init(&g_bio_run_down);
162         g_bioq_init(&g_bio_run_up);
163         g_bioq_init(&g_bio_idle);
164 }
165
166 int
167 g_io_setattr(char *attr, struct g_consumer *cp, int len, void *ptr)
168 {
169         struct bio *bp;
170         int error;
171
172         g_trace(G_T_BIO, "bio_setattr(%s)", attr);
173         do {
174                 bp = g_new_bio();
175                 bp->bio_cmd = BIO_SETATTR;
176                 bp->bio_done = NULL;
177                 bp->bio_attribute = attr;
178                 bp->bio_length = len;
179                 bp->bio_data = ptr;
180                 g_io_request(bp, cp);
181                 while ((bp->bio_flags & BIO_DONE) == 0) {
182                         mtx_lock(&Giant);
183                         tsleep(bp, 0, "setattr", hz / 10);
184                         mtx_unlock(&Giant);
185                 }
186                 error = bp->bio_error;
187                 g_destroy_bio(bp);
188                 if (error == EBUSY)
189                         tsleep(&error, 0, "setattr_busy", hz);
190         } while(error == EBUSY);
191         return (error);
192 }
193
194
195 int
196 g_io_getattr(char *attr, struct g_consumer *cp, int *len, void *ptr)
197 {
198         struct bio *bp;
199         int error;
200
201         g_trace(G_T_BIO, "bio_getattr(%s)", attr);
202         do {
203                 bp = g_new_bio();
204                 bp->bio_cmd = BIO_GETATTR;
205                 bp->bio_done = NULL;
206                 bp->bio_attribute = attr;
207                 bp->bio_length = *len;
208                 bp->bio_data = ptr;
209                 g_io_request(bp, cp);
210                 while ((bp->bio_flags & BIO_DONE) == 0) {
211                         mtx_lock(&Giant);
212                         tsleep(bp, 0, "getattr", hz / 10);
213                         mtx_unlock(&Giant);
214                 }
215                 *len = bp->bio_completed;
216                 error = bp->bio_error;
217                 g_destroy_bio(bp);
218                 if (error == EBUSY)
219                         tsleep(&error, 0, "getattr_busy", hz);
220
221         } while(error == EBUSY);
222         return (error);
223 }
224
225 void
226 g_io_request(struct bio *bp, struct g_consumer *cp)
227 {
228         int error;
229
230         KASSERT(cp != NULL, ("bio_request on thin air"));
231         error = 0;
232         bp->bio_from = cp;
233         bp->bio_to = cp->provider;
234
235         /* begin_stats(&bp->stats); */
236
237         atomic_add_int(&cp->biocount, 1);
238         if (bp->bio_to == NULL)
239                 error = ENXIO;
240         if (!error) {
241                 switch(bp->bio_cmd) {
242                 case BIO_READ:
243                 case BIO_GETATTR:
244                         if (cp->acr == 0)
245                                 error = EPERM;
246                         break;
247                 case BIO_WRITE:
248                         if (cp->acw == 0)
249                                 error = EPERM;
250                         break;
251                 case BIO_SETATTR:
252                 case BIO_DELETE:
253                         if ((cp->acw == 0) || (cp->ace == 0))
254                                 error = EPERM;
255                         break;
256                 default:
257                         error = EPERM;
258                         break;
259                 }
260         }
261         /* if provider is marked for error, don't disturb */
262         if (!error)
263                 error = bp->bio_to->error;
264         if (error) {
265                 bp->bio_error = error;
266                 /* finish_stats(&bp->stats); */
267
268                 g_trace(G_T_BIO,
269                     "bio_request(%p) from %p(%s) to %p(%s) cmd %d error %d\n",
270                     bp, bp->bio_from, bp->bio_from->geom->name,
271                     bp->bio_to, bp->bio_to->name, bp->bio_cmd, bp->bio_error);
272                 g_bioq_enqueue_tail(bp, &g_bio_run_up);
273                 mtx_lock(&Giant);
274                 wakeup(&g_wait_up);
275                 mtx_unlock(&Giant);
276         } else {
277                 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
278                     bp, bp->bio_from, bp->bio_from->geom->name,
279                     bp->bio_to, bp->bio_to->name, bp->bio_cmd);
280                 g_bioq_enqueue_tail(bp, &g_bio_run_down);
281                 mtx_lock(&Giant);
282                 wakeup(&g_wait_down);
283                 mtx_unlock(&Giant);
284         }
285 }
286
287 void
288 g_io_deliver(struct bio *bp)
289 {
290
291         g_trace(G_T_BIO,
292             "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d",
293             bp, bp->bio_from, bp->bio_from->geom->name,
294             bp->bio_to, bp->bio_to->name, bp->bio_cmd, bp->bio_error);
295         /* finish_stats(&bp->stats); */
296
297         g_bioq_enqueue_tail(bp, &g_bio_run_up);
298
299         mtx_lock(&Giant);
300         wakeup(&g_wait_up);
301         mtx_unlock(&Giant);
302 }
303
304 void
305 g_io_schedule_down(struct thread *tp __unused)
306 {
307         struct bio *bp;
308
309         for(;;) {
310                 bp = g_bioq_first(&g_bio_run_down);
311                 if (bp == NULL)
312                         break;
313                 bp->bio_to->geom->start(bp);
314         }
315 }
316
317 void
318 g_io_schedule_up(struct thread *tp __unused)
319 {
320         struct bio *bp;
321         struct g_consumer *cp;
322
323         for(;;) {
324                 bp = g_bioq_first(&g_bio_run_up);
325                 if (bp == NULL)
326                         break;
327
328                 cp = bp->bio_from;
329
330                 bp->bio_flags |= BIO_DONE;
331                 atomic_add_int(&cp->biocount, -1);
332                 if (bp->bio_done != NULL) {
333                         bp->bio_done(bp);
334                 } else {
335                         mtx_lock(&Giant);
336                         wakeup(bp);
337                         mtx_unlock(&Giant);
338                 }
339         }
340 }
341
342 void *
343 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
344 {
345         struct bio *bp;
346         void *ptr;
347         int errorc;
348
349         do {
350                 bp = g_new_bio();
351                 bp->bio_cmd = BIO_READ;
352                 bp->bio_done = NULL;
353                 bp->bio_offset = offset;
354                 bp->bio_length = length;
355                 ptr = g_malloc(length, M_WAITOK);
356                 bp->bio_data = ptr;
357                 g_io_request(bp, cp);
358                 while ((bp->bio_flags & BIO_DONE) == 0) {
359                         mtx_lock(&Giant);
360                         tsleep(bp, 0, "g_read_data", hz / 10);
361                         mtx_unlock(&Giant);
362                 }
363                 errorc = bp->bio_error;
364                 if (error != NULL)
365                         *error = errorc;
366                 g_destroy_bio(bp);
367                 if (errorc) {
368                         g_free(ptr);
369                         ptr = NULL;
370                 }
371                 if (errorc == EBUSY)
372                         tsleep(&errorc, 0, "g_read_data_busy", hz);
373         } while (errorc == EBUSY);
374         return (ptr);
375 }