2 * Copyright (c) 2013 Peter Grehan <grehan@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/errno.h>
36 #include <sys/ioctl.h>
45 #include <pthread_np.h>
49 #include <machine/atomic.h>
55 #define BLOCKIF_SIG 0xb109b109
57 #define BLOCKIF_MAXREQ 33
73 TAILQ_ENTRY(blockif_elem) be_link;
74 struct blockif_req *be_req;
76 enum blockstat be_status;
87 pthread_mutex_t bc_mtx;
88 pthread_cond_t bc_cond;
91 /* Request elements and free/pending/busy queues */
92 TAILQ_HEAD(, blockif_elem) bc_freeq;
93 TAILQ_HEAD(, blockif_elem) bc_pendq;
94 TAILQ_HEAD(, blockif_elem) bc_busyq;
96 struct blockif_elem bc_reqs[BLOCKIF_MAXREQ];
99 static pthread_once_t blockif_once = PTHREAD_ONCE_INIT;
101 struct blockif_sig_elem {
102 pthread_mutex_t bse_mtx;
103 pthread_cond_t bse_cond;
105 struct blockif_sig_elem *bse_next;
108 static struct blockif_sig_elem *blockif_bse_head;
111 blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
114 struct blockif_elem *be;
116 assert(bc->bc_req_count < BLOCKIF_MAXREQ);
118 be = TAILQ_FIRST(&bc->bc_freeq);
120 assert(be->be_status == BST_FREE);
122 TAILQ_REMOVE(&bc->bc_freeq, be, be_link);
123 be->be_status = BST_PEND;
126 TAILQ_INSERT_TAIL(&bc->bc_pendq, be, be_link);
134 blockif_dequeue(struct blockif_ctxt *bc, struct blockif_elem **bep)
136 struct blockif_elem *be;
138 if (bc->bc_req_count == 0)
141 be = TAILQ_FIRST(&bc->bc_pendq);
143 assert(be->be_status == BST_PEND);
144 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
145 be->be_status = BST_BUSY;
146 be->be_tid = bc->bc_btid;
147 TAILQ_INSERT_TAIL(&bc->bc_busyq, be, be_link);
155 blockif_complete(struct blockif_ctxt *bc, struct blockif_elem *be)
157 assert(be->be_status == BST_DONE);
159 TAILQ_REMOVE(&bc->bc_busyq, be, be_link);
161 be->be_status = BST_FREE;
163 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
169 blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be)
171 struct blockif_req *br;
179 if (preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
186 else if (pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
197 be->be_status = BST_DONE;
199 (*br->br_callback)(br, err);
203 blockif_thr(void *arg)
205 struct blockif_ctxt *bc;
206 struct blockif_elem *be;
211 pthread_mutex_lock(&bc->bc_mtx);
212 while (!blockif_dequeue(bc, &be)) {
213 pthread_mutex_unlock(&bc->bc_mtx);
214 blockif_proc(bc, be);
215 pthread_mutex_lock(&bc->bc_mtx);
216 blockif_complete(bc, be);
218 pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx);
219 pthread_mutex_unlock(&bc->bc_mtx);
222 * Check ctxt status here to see if exit requested
233 blockif_sigcont_handler(int signal, enum ev_type type, void *arg)
235 struct blockif_sig_elem *bse;
239 * Process the entire list even if not intended for
243 bse = blockif_bse_head;
246 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
248 (uintptr_t)bse->bse_next));
250 pthread_mutex_lock(&bse->bse_mtx);
251 bse->bse_pending = 0;
252 pthread_cond_signal(&bse->bse_cond);
253 pthread_mutex_unlock(&bse->bse_mtx);
260 mevent_add(SIGCONT, EVF_SIGNAL, blockif_sigcont_handler, NULL);
261 (void) signal(SIGCONT, SIG_IGN);
264 struct blockif_ctxt *
265 blockif_open(const char *optstr, const char *ident)
267 char tname[MAXCOMLEN + 1];
269 struct blockif_ctxt *bc;
272 int extra, fd, i, sectsz;
273 int nocache, sync, ro;
275 pthread_once(&blockif_once, blockif_init);
282 * The first element in the optstring is always a pathname.
283 * Optional elements follow
285 nopt = strdup(optstr);
286 for (xopts = strtok(nopt, ",");
288 xopts = strtok(NULL, ",")) {
289 if (!strcmp(xopts, "nocache"))
291 else if (!strcmp(xopts, "sync"))
293 else if (!strcmp(xopts, "ro"))
303 fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra);
305 /* Attempt a r/w fail with a r/o open */
306 fd = open(nopt, O_RDONLY | extra);
311 perror("Could not open backing file");
315 if (fstat(fd, &sbuf) < 0) {
316 perror("Could not stat backing file");
322 * Deal with raw devices
326 if (S_ISCHR(sbuf.st_mode)) {
327 if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
328 ioctl(fd, DIOCGSECTORSIZE, §sz)) {
329 perror("Could not fetch dev blk/sector size");
337 bc = calloc(1, sizeof(struct blockif_ctxt));
343 bc->bc_magic = BLOCKIF_SIG;
347 bc->bc_sectsz = sectsz;
348 pthread_mutex_init(&bc->bc_mtx, NULL);
349 pthread_cond_init(&bc->bc_cond, NULL);
350 TAILQ_INIT(&bc->bc_freeq);
351 TAILQ_INIT(&bc->bc_pendq);
352 TAILQ_INIT(&bc->bc_busyq);
353 bc->bc_req_count = 0;
354 for (i = 0; i < BLOCKIF_MAXREQ; i++) {
355 bc->bc_reqs[i].be_status = BST_FREE;
356 TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
359 pthread_create(&bc->bc_btid, NULL, blockif_thr, bc);
361 snprintf(tname, sizeof(tname), "blk-%s", ident);
362 pthread_set_name_np(bc->bc_btid, tname);
368 blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
375 pthread_mutex_lock(&bc->bc_mtx);
376 if (bc->bc_req_count < BLOCKIF_MAXREQ) {
378 * Enqueue and inform the block i/o thread
379 * that there is work available
381 blockif_enqueue(bc, breq, op);
382 pthread_cond_signal(&bc->bc_cond);
385 * Callers are not allowed to enqueue more than
386 * the specified blockif queue limit. Return an
387 * error to indicate that the queue length has been
392 pthread_mutex_unlock(&bc->bc_mtx);
398 blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
401 assert(bc->bc_magic == BLOCKIF_SIG);
402 return (blockif_request(bc, breq, BOP_READ));
406 blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
409 assert(bc->bc_magic == BLOCKIF_SIG);
410 return (blockif_request(bc, breq, BOP_WRITE));
414 blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
417 assert(bc->bc_magic == BLOCKIF_SIG);
418 return (blockif_request(bc, breq, BOP_FLUSH));
422 blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
424 struct blockif_elem *be;
426 assert(bc->bc_magic == BLOCKIF_SIG);
428 pthread_mutex_lock(&bc->bc_mtx);
430 * Check pending requests.
432 TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
433 if (be->be_req == breq)
440 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
441 be->be_status = BST_FREE;
443 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
445 pthread_mutex_unlock(&bc->bc_mtx);
451 * Check in-flight requests.
453 TAILQ_FOREACH(be, &bc->bc_busyq, be_link) {
454 if (be->be_req == breq)
461 pthread_mutex_unlock(&bc->bc_mtx);
466 * Interrupt the processing thread to force it return
467 * prematurely via it's normal callback path.
469 while (be->be_status == BST_BUSY) {
470 struct blockif_sig_elem bse, *old_head;
472 pthread_mutex_init(&bse.bse_mtx, NULL);
473 pthread_cond_init(&bse.bse_cond, NULL);
478 old_head = blockif_bse_head;
479 bse.bse_next = old_head;
480 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
484 pthread_kill(be->be_tid, SIGCONT);
486 pthread_mutex_lock(&bse.bse_mtx);
487 while (bse.bse_pending)
488 pthread_cond_wait(&bse.bse_cond, &bse.bse_mtx);
489 pthread_mutex_unlock(&bse.bse_mtx);
492 pthread_mutex_unlock(&bc->bc_mtx);
495 * The processing thread has been interrupted. Since it's not
496 * clear if the callback has been invoked yet, return EBUSY.
502 blockif_close(struct blockif_ctxt *bc)
509 assert(bc->bc_magic == BLOCKIF_SIG);
512 * Stop the block i/o thread
515 pthread_cond_signal(&bc->bc_cond);
516 pthread_join(bc->bc_btid, &jval);
518 /* XXX Cancel queued i/o's ??? */
531 * Return virtual C/H/S values for a given block. Use the algorithm
532 * outlined in the VHD specification to calculate values.
535 blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
537 off_t sectors; /* total sectors of the block dev */
538 off_t hcyl; /* cylinders times heads */
539 uint16_t secpt; /* sectors per track */
542 assert(bc->bc_magic == BLOCKIF_SIG);
544 sectors = bc->bc_size / bc->bc_sectsz;
546 /* Clamp the size to the largest possible with CHS */
547 if (sectors > 65535UL*16*255)
548 sectors = 65535UL*16*255;
550 if (sectors >= 65536UL*16*63) {
553 hcyl = sectors / secpt;
556 hcyl = sectors / secpt;
557 heads = (hcyl + 1023) / 1024;
562 if (hcyl >= (heads * 1024) || heads > 16) {
565 hcyl = sectors / secpt;
567 if (hcyl >= (heads * 1024)) {
570 hcyl = sectors / secpt;
583 blockif_size(struct blockif_ctxt *bc)
586 assert(bc->bc_magic == BLOCKIF_SIG);
587 return (bc->bc_size);
591 blockif_sectsz(struct blockif_ctxt *bc)
594 assert(bc->bc_magic == BLOCKIF_SIG);
595 return (bc->bc_sectsz);
599 blockif_queuesz(struct blockif_ctxt *bc)
602 assert(bc->bc_magic == BLOCKIF_SIG);
603 return (BLOCKIF_MAXREQ - 1);
607 blockif_is_ro(struct blockif_ctxt *bc)
610 assert(bc->bc_magic == BLOCKIF_SIG);
611 return (bc->bc_rdonly);