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_NUMTHR 8
58 #define BLOCKIF_MAXREQ (64 + BLOCKIF_NUMTHR)
76 TAILQ_ENTRY(blockif_elem) be_link;
77 struct blockif_req *be_req;
79 enum blockstat be_status;
96 pthread_t bc_btid[BLOCKIF_NUMTHR];
97 pthread_mutex_t bc_mtx;
98 pthread_cond_t bc_cond;
100 /* Request elements and free/pending/busy queues */
101 TAILQ_HEAD(, blockif_elem) bc_freeq;
102 TAILQ_HEAD(, blockif_elem) bc_pendq;
103 TAILQ_HEAD(, blockif_elem) bc_busyq;
104 struct blockif_elem bc_reqs[BLOCKIF_MAXREQ];
107 static pthread_once_t blockif_once = PTHREAD_ONCE_INIT;
109 struct blockif_sig_elem {
110 pthread_mutex_t bse_mtx;
111 pthread_cond_t bse_cond;
113 struct blockif_sig_elem *bse_next;
116 static struct blockif_sig_elem *blockif_bse_head;
119 blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
122 struct blockif_elem *be, *tbe;
126 be = TAILQ_FIRST(&bc->bc_freeq);
128 assert(be->be_status == BST_FREE);
129 TAILQ_REMOVE(&bc->bc_freeq, be, be_link);
136 off = breq->br_offset;
137 for (i = 0; i < breq->br_iovcnt; i++)
138 off += breq->br_iov[i].iov_len;
144 TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) {
145 if (tbe->be_block == breq->br_offset)
149 TAILQ_FOREACH(tbe, &bc->bc_busyq, be_link) {
150 if (tbe->be_block == breq->br_offset)
155 be->be_status = BST_PEND;
157 be->be_status = BST_BLOCK;
158 TAILQ_INSERT_TAIL(&bc->bc_pendq, be, be_link);
159 return (be->be_status == BST_PEND);
163 blockif_dequeue(struct blockif_ctxt *bc, pthread_t t, struct blockif_elem **bep)
165 struct blockif_elem *be;
167 TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
168 if (be->be_status == BST_PEND)
170 assert(be->be_status == BST_BLOCK);
174 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
175 be->be_status = BST_BUSY;
177 TAILQ_INSERT_TAIL(&bc->bc_busyq, be, be_link);
183 blockif_complete(struct blockif_ctxt *bc, struct blockif_elem *be)
185 struct blockif_elem *tbe;
187 if (be->be_status == BST_DONE || be->be_status == BST_BUSY)
188 TAILQ_REMOVE(&bc->bc_busyq, be, be_link);
190 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
191 TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) {
192 if (tbe->be_req->br_offset == be->be_block)
193 tbe->be_status = BST_PEND;
196 be->be_status = BST_FREE;
198 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
202 blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf)
204 struct blockif_req *br;
206 ssize_t clen, len, off, boff, voff;
210 if (br->br_iovcnt <= 1)
216 if ((len = preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
225 while (br->br_resid > 0) {
226 len = MIN(br->br_resid, MAXPHYS);
227 if (pread(bc->bc_fd, buf, len, br->br_offset +
234 clen = MIN(len - boff, br->br_iov[i].iov_len -
236 memcpy(br->br_iov[i].iov_base + voff,
238 if (clen < br->br_iov[i].iov_len - voff)
245 } while (boff < len);
256 if ((len = pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
265 while (br->br_resid > 0) {
266 len = MIN(br->br_resid, MAXPHYS);
269 clen = MIN(len - boff, br->br_iov[i].iov_len -
272 br->br_iov[i].iov_base + voff, clen);
273 if (clen < br->br_iov[i].iov_len - voff)
280 } while (boff < len);
281 if (pwrite(bc->bc_fd, buf, len, br->br_offset +
292 if (ioctl(bc->bc_fd, DIOCGFLUSH))
294 } else if (fsync(bc->bc_fd))
298 if (!bc->bc_candelete)
300 else if (bc->bc_rdonly)
302 else if (bc->bc_ischr) {
303 arg[0] = br->br_offset;
304 arg[1] = br->br_resid;
305 if (ioctl(bc->bc_fd, DIOCGDELETE, arg))
317 be->be_status = BST_DONE;
319 (*br->br_callback)(br, err);
323 blockif_thr(void *arg)
325 struct blockif_ctxt *bc;
326 struct blockif_elem *be;
332 buf = malloc(MAXPHYS);
337 pthread_mutex_lock(&bc->bc_mtx);
339 while (blockif_dequeue(bc, t, &be)) {
340 pthread_mutex_unlock(&bc->bc_mtx);
341 blockif_proc(bc, be, buf);
342 pthread_mutex_lock(&bc->bc_mtx);
343 blockif_complete(bc, be);
345 /* Check ctxt status here to see if exit requested */
348 pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx);
350 pthread_mutex_unlock(&bc->bc_mtx);
359 blockif_sigcont_handler(int signal, enum ev_type type, void *arg)
361 struct blockif_sig_elem *bse;
365 * Process the entire list even if not intended for
369 bse = blockif_bse_head;
372 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
374 (uintptr_t)bse->bse_next));
376 pthread_mutex_lock(&bse->bse_mtx);
377 bse->bse_pending = 0;
378 pthread_cond_signal(&bse->bse_cond);
379 pthread_mutex_unlock(&bse->bse_mtx);
386 mevent_add(SIGCONT, EVF_SIGNAL, blockif_sigcont_handler, NULL);
387 (void) signal(SIGCONT, SIG_IGN);
390 struct blockif_ctxt *
391 blockif_open(const char *optstr, const char *ident)
393 char tname[MAXCOMLEN + 1];
394 char name[MAXPATHLEN];
396 struct blockif_ctxt *bc;
398 struct diocgattr_arg arg;
399 off_t size, psectsz, psectoff;
400 int extra, fd, i, sectsz;
401 int nocache, sync, ro, candelete, geom;
403 pthread_once(&blockif_once, blockif_init);
410 * The first element in the optstring is always a pathname.
411 * Optional elements follow
413 nopt = strdup(optstr);
414 for (xopts = strtok(nopt, ",");
416 xopts = strtok(NULL, ",")) {
417 if (!strcmp(xopts, "nocache"))
419 else if (!strcmp(xopts, "sync"))
421 else if (!strcmp(xopts, "ro"))
431 fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra);
433 /* Attempt a r/w fail with a r/o open */
434 fd = open(nopt, O_RDONLY | extra);
439 perror("Could not open backing file");
443 if (fstat(fd, &sbuf) < 0) {
444 perror("Could not stat backing file");
450 * Deal with raw devices
454 psectsz = psectoff = 0;
455 candelete = geom = 0;
456 if (S_ISCHR(sbuf.st_mode)) {
457 if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
458 ioctl(fd, DIOCGSECTORSIZE, §sz)) {
459 perror("Could not fetch dev blk/sector size");
465 if (ioctl(fd, DIOCGSTRIPESIZE, &psectsz) == 0 && psectsz > 0)
466 ioctl(fd, DIOCGSTRIPEOFFSET, &psectoff);
467 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
468 arg.len = sizeof(arg.value.i);
469 if (ioctl(fd, DIOCGATTR, &arg) == 0)
470 candelete = arg.value.i;
471 if (ioctl(fd, DIOCGPROVIDERNAME, name) == 0)
474 psectsz = sbuf.st_blksize;
476 bc = calloc(1, sizeof(struct blockif_ctxt));
482 bc->bc_magic = BLOCKIF_SIG;
484 bc->bc_ischr = S_ISCHR(sbuf.st_mode);
485 bc->bc_isgeom = geom;
486 bc->bc_candelete = candelete;
489 bc->bc_sectsz = sectsz;
490 bc->bc_psectsz = psectsz;
491 bc->bc_psectoff = psectoff;
492 pthread_mutex_init(&bc->bc_mtx, NULL);
493 pthread_cond_init(&bc->bc_cond, NULL);
494 TAILQ_INIT(&bc->bc_freeq);
495 TAILQ_INIT(&bc->bc_pendq);
496 TAILQ_INIT(&bc->bc_busyq);
497 for (i = 0; i < BLOCKIF_MAXREQ; i++) {
498 bc->bc_reqs[i].be_status = BST_FREE;
499 TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
502 for (i = 0; i < BLOCKIF_NUMTHR; i++) {
503 pthread_create(&bc->bc_btid[i], NULL, blockif_thr, bc);
504 snprintf(tname, sizeof(tname), "blk-%s-%d", ident, i);
505 pthread_set_name_np(bc->bc_btid[i], tname);
512 blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
519 pthread_mutex_lock(&bc->bc_mtx);
520 if (!TAILQ_EMPTY(&bc->bc_freeq)) {
522 * Enqueue and inform the block i/o thread
523 * that there is work available
525 if (blockif_enqueue(bc, breq, op))
526 pthread_cond_signal(&bc->bc_cond);
529 * Callers are not allowed to enqueue more than
530 * the specified blockif queue limit. Return an
531 * error to indicate that the queue length has been
536 pthread_mutex_unlock(&bc->bc_mtx);
542 blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
545 assert(bc->bc_magic == BLOCKIF_SIG);
546 return (blockif_request(bc, breq, BOP_READ));
550 blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
553 assert(bc->bc_magic == BLOCKIF_SIG);
554 return (blockif_request(bc, breq, BOP_WRITE));
558 blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
561 assert(bc->bc_magic == BLOCKIF_SIG);
562 return (blockif_request(bc, breq, BOP_FLUSH));
566 blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq)
569 assert(bc->bc_magic == BLOCKIF_SIG);
570 return (blockif_request(bc, breq, BOP_DELETE));
574 blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
576 struct blockif_elem *be;
578 assert(bc->bc_magic == BLOCKIF_SIG);
580 pthread_mutex_lock(&bc->bc_mtx);
582 * Check pending requests.
584 TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
585 if (be->be_req == breq)
592 blockif_complete(bc, be);
593 pthread_mutex_unlock(&bc->bc_mtx);
599 * Check in-flight requests.
601 TAILQ_FOREACH(be, &bc->bc_busyq, be_link) {
602 if (be->be_req == breq)
609 pthread_mutex_unlock(&bc->bc_mtx);
614 * Interrupt the processing thread to force it return
615 * prematurely via it's normal callback path.
617 while (be->be_status == BST_BUSY) {
618 struct blockif_sig_elem bse, *old_head;
620 pthread_mutex_init(&bse.bse_mtx, NULL);
621 pthread_cond_init(&bse.bse_cond, NULL);
626 old_head = blockif_bse_head;
627 bse.bse_next = old_head;
628 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
632 pthread_kill(be->be_tid, SIGCONT);
634 pthread_mutex_lock(&bse.bse_mtx);
635 while (bse.bse_pending)
636 pthread_cond_wait(&bse.bse_cond, &bse.bse_mtx);
637 pthread_mutex_unlock(&bse.bse_mtx);
640 pthread_mutex_unlock(&bc->bc_mtx);
643 * The processing thread has been interrupted. Since it's not
644 * clear if the callback has been invoked yet, return EBUSY.
650 blockif_close(struct blockif_ctxt *bc)
657 assert(bc->bc_magic == BLOCKIF_SIG);
660 * Stop the block i/o thread
662 pthread_mutex_lock(&bc->bc_mtx);
664 pthread_mutex_unlock(&bc->bc_mtx);
665 pthread_cond_broadcast(&bc->bc_cond);
666 for (i = 0; i < BLOCKIF_NUMTHR; i++)
667 pthread_join(bc->bc_btid[i], &jval);
669 /* XXX Cancel queued i/o's ??? */
682 * Return virtual C/H/S values for a given block. Use the algorithm
683 * outlined in the VHD specification to calculate values.
686 blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
688 off_t sectors; /* total sectors of the block dev */
689 off_t hcyl; /* cylinders times heads */
690 uint16_t secpt; /* sectors per track */
693 assert(bc->bc_magic == BLOCKIF_SIG);
695 sectors = bc->bc_size / bc->bc_sectsz;
697 /* Clamp the size to the largest possible with CHS */
698 if (sectors > 65535UL*16*255)
699 sectors = 65535UL*16*255;
701 if (sectors >= 65536UL*16*63) {
704 hcyl = sectors / secpt;
707 hcyl = sectors / secpt;
708 heads = (hcyl + 1023) / 1024;
713 if (hcyl >= (heads * 1024) || heads > 16) {
716 hcyl = sectors / secpt;
718 if (hcyl >= (heads * 1024)) {
721 hcyl = sectors / secpt;
734 blockif_size(struct blockif_ctxt *bc)
737 assert(bc->bc_magic == BLOCKIF_SIG);
738 return (bc->bc_size);
742 blockif_sectsz(struct blockif_ctxt *bc)
745 assert(bc->bc_magic == BLOCKIF_SIG);
746 return (bc->bc_sectsz);
750 blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off)
753 assert(bc->bc_magic == BLOCKIF_SIG);
754 *size = bc->bc_psectsz;
755 *off = bc->bc_psectoff;
759 blockif_queuesz(struct blockif_ctxt *bc)
762 assert(bc->bc_magic == BLOCKIF_SIG);
763 return (BLOCKIF_MAXREQ - 1);
767 blockif_is_ro(struct blockif_ctxt *bc)
770 assert(bc->bc_magic == BLOCKIF_SIG);
771 return (bc->bc_rdonly);
775 blockif_candelete(struct blockif_ctxt *bc)
778 assert(bc->bc_magic == BLOCKIF_SIG);
779 return (bc->bc_candelete);