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
58 #define BLOCKIF_NUMTHR 8
76 TAILQ_ENTRY(blockif_elem) be_link;
77 struct blockif_req *be_req;
79 enum blockstat be_status;
95 pthread_t bc_btid[BLOCKIF_NUMTHR];
96 pthread_mutex_t bc_mtx;
97 pthread_cond_t bc_cond;
99 /* Request elements and free/pending/busy queues */
100 TAILQ_HEAD(, blockif_elem) bc_freeq;
101 TAILQ_HEAD(, blockif_elem) bc_pendq;
102 TAILQ_HEAD(, blockif_elem) bc_busyq;
103 struct blockif_elem bc_reqs[BLOCKIF_MAXREQ];
106 static pthread_once_t blockif_once = PTHREAD_ONCE_INIT;
108 struct blockif_sig_elem {
109 pthread_mutex_t bse_mtx;
110 pthread_cond_t bse_cond;
112 struct blockif_sig_elem *bse_next;
115 static struct blockif_sig_elem *blockif_bse_head;
118 blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
121 struct blockif_elem *be, *tbe;
125 be = TAILQ_FIRST(&bc->bc_freeq);
127 assert(be->be_status == BST_FREE);
128 TAILQ_REMOVE(&bc->bc_freeq, be, be_link);
135 off = breq->br_offset;
136 for (i = 0; i < breq->br_iovcnt; i++)
137 off += breq->br_iov[i].iov_len;
143 TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) {
144 if (tbe->be_block == breq->br_offset)
148 TAILQ_FOREACH(tbe, &bc->bc_busyq, be_link) {
149 if (tbe->be_block == breq->br_offset)
154 be->be_status = BST_PEND;
156 be->be_status = BST_BLOCK;
157 TAILQ_INSERT_TAIL(&bc->bc_pendq, be, be_link);
158 return (be->be_status == BST_PEND);
162 blockif_dequeue(struct blockif_ctxt *bc, pthread_t t, struct blockif_elem **bep)
164 struct blockif_elem *be;
166 TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
167 if (be->be_status == BST_PEND)
169 assert(be->be_status == BST_BLOCK);
173 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
174 be->be_status = BST_BUSY;
176 TAILQ_INSERT_TAIL(&bc->bc_busyq, be, be_link);
182 blockif_complete(struct blockif_ctxt *bc, struct blockif_elem *be)
184 struct blockif_elem *tbe;
186 if (be->be_status == BST_DONE || be->be_status == BST_BUSY)
187 TAILQ_REMOVE(&bc->bc_busyq, be, be_link);
189 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
190 TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) {
191 if (tbe->be_req->br_offset == be->be_block)
192 tbe->be_status = BST_PEND;
195 be->be_status = BST_FREE;
197 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
201 blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be)
203 struct blockif_req *br;
212 if (preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
219 else if (pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
225 if (ioctl(bc->bc_fd, DIOCGFLUSH))
227 } else if (fsync(bc->bc_fd))
231 if (!bc->bc_candelete)
233 else if (bc->bc_rdonly)
235 else if (bc->bc_ischr) {
236 arg[0] = br->br_offset;
237 arg[1] = br->br_iov[0].iov_len;
238 if (ioctl(bc->bc_fd, DIOCGDELETE, arg))
248 be->be_status = BST_DONE;
250 (*br->br_callback)(br, err);
254 blockif_thr(void *arg)
256 struct blockif_ctxt *bc;
257 struct blockif_elem *be;
263 pthread_mutex_lock(&bc->bc_mtx);
265 while (blockif_dequeue(bc, t, &be)) {
266 pthread_mutex_unlock(&bc->bc_mtx);
267 blockif_proc(bc, be);
268 pthread_mutex_lock(&bc->bc_mtx);
269 blockif_complete(bc, be);
271 /* Check ctxt status here to see if exit requested */
274 pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx);
276 pthread_mutex_unlock(&bc->bc_mtx);
283 blockif_sigcont_handler(int signal, enum ev_type type, void *arg)
285 struct blockif_sig_elem *bse;
289 * Process the entire list even if not intended for
293 bse = blockif_bse_head;
296 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
298 (uintptr_t)bse->bse_next));
300 pthread_mutex_lock(&bse->bse_mtx);
301 bse->bse_pending = 0;
302 pthread_cond_signal(&bse->bse_cond);
303 pthread_mutex_unlock(&bse->bse_mtx);
310 mevent_add(SIGCONT, EVF_SIGNAL, blockif_sigcont_handler, NULL);
311 (void) signal(SIGCONT, SIG_IGN);
314 struct blockif_ctxt *
315 blockif_open(const char *optstr, const char *ident)
317 char tname[MAXCOMLEN + 1];
319 struct blockif_ctxt *bc;
321 struct diocgattr_arg arg;
322 off_t size, psectsz, psectoff;
323 int extra, fd, i, sectsz;
324 int nocache, sync, ro, candelete;
326 pthread_once(&blockif_once, blockif_init);
333 * The first element in the optstring is always a pathname.
334 * Optional elements follow
336 nopt = strdup(optstr);
337 for (xopts = strtok(nopt, ",");
339 xopts = strtok(NULL, ",")) {
340 if (!strcmp(xopts, "nocache"))
342 else if (!strcmp(xopts, "sync"))
344 else if (!strcmp(xopts, "ro"))
354 fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra);
356 /* Attempt a r/w fail with a r/o open */
357 fd = open(nopt, O_RDONLY | extra);
362 perror("Could not open backing file");
366 if (fstat(fd, &sbuf) < 0) {
367 perror("Could not stat backing file");
373 * Deal with raw devices
377 psectsz = psectoff = 0;
379 if (S_ISCHR(sbuf.st_mode)) {
380 if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
381 ioctl(fd, DIOCGSECTORSIZE, §sz)) {
382 perror("Could not fetch dev blk/sector size");
388 if (ioctl(fd, DIOCGSTRIPESIZE, &psectsz) == 0 && psectsz > 0)
389 ioctl(fd, DIOCGSTRIPEOFFSET, &psectoff);
390 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
391 arg.len = sizeof(arg.value.i);
392 if (ioctl(fd, DIOCGATTR, &arg) == 0)
393 candelete = arg.value.i;
395 psectsz = sbuf.st_blksize;
397 bc = calloc(1, sizeof(struct blockif_ctxt));
403 bc->bc_magic = BLOCKIF_SIG;
405 bc->bc_ischr = S_ISCHR(sbuf.st_mode);
406 bc->bc_candelete = candelete;
409 bc->bc_sectsz = sectsz;
410 bc->bc_psectsz = psectsz;
411 bc->bc_psectoff = psectoff;
412 pthread_mutex_init(&bc->bc_mtx, NULL);
413 pthread_cond_init(&bc->bc_cond, NULL);
414 TAILQ_INIT(&bc->bc_freeq);
415 TAILQ_INIT(&bc->bc_pendq);
416 TAILQ_INIT(&bc->bc_busyq);
417 for (i = 0; i < BLOCKIF_MAXREQ; i++) {
418 bc->bc_reqs[i].be_status = BST_FREE;
419 TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
422 for (i = 0; i < BLOCKIF_NUMTHR; i++) {
423 pthread_create(&bc->bc_btid[i], NULL, blockif_thr, bc);
424 snprintf(tname, sizeof(tname), "blk-%s-%d", ident, i);
425 pthread_set_name_np(bc->bc_btid[i], tname);
432 blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
439 pthread_mutex_lock(&bc->bc_mtx);
440 if (!TAILQ_EMPTY(&bc->bc_freeq)) {
442 * Enqueue and inform the block i/o thread
443 * that there is work available
445 if (blockif_enqueue(bc, breq, op))
446 pthread_cond_signal(&bc->bc_cond);
449 * Callers are not allowed to enqueue more than
450 * the specified blockif queue limit. Return an
451 * error to indicate that the queue length has been
456 pthread_mutex_unlock(&bc->bc_mtx);
462 blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
465 assert(bc->bc_magic == BLOCKIF_SIG);
466 return (blockif_request(bc, breq, BOP_READ));
470 blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
473 assert(bc->bc_magic == BLOCKIF_SIG);
474 return (blockif_request(bc, breq, BOP_WRITE));
478 blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
481 assert(bc->bc_magic == BLOCKIF_SIG);
482 return (blockif_request(bc, breq, BOP_FLUSH));
486 blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq)
489 assert(bc->bc_magic == BLOCKIF_SIG);
490 return (blockif_request(bc, breq, BOP_DELETE));
494 blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
496 struct blockif_elem *be;
498 assert(bc->bc_magic == BLOCKIF_SIG);
500 pthread_mutex_lock(&bc->bc_mtx);
502 * Check pending requests.
504 TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
505 if (be->be_req == breq)
512 blockif_complete(bc, be);
513 pthread_mutex_unlock(&bc->bc_mtx);
519 * Check in-flight requests.
521 TAILQ_FOREACH(be, &bc->bc_busyq, be_link) {
522 if (be->be_req == breq)
529 pthread_mutex_unlock(&bc->bc_mtx);
534 * Interrupt the processing thread to force it return
535 * prematurely via it's normal callback path.
537 while (be->be_status == BST_BUSY) {
538 struct blockif_sig_elem bse, *old_head;
540 pthread_mutex_init(&bse.bse_mtx, NULL);
541 pthread_cond_init(&bse.bse_cond, NULL);
546 old_head = blockif_bse_head;
547 bse.bse_next = old_head;
548 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
552 pthread_kill(be->be_tid, SIGCONT);
554 pthread_mutex_lock(&bse.bse_mtx);
555 while (bse.bse_pending)
556 pthread_cond_wait(&bse.bse_cond, &bse.bse_mtx);
557 pthread_mutex_unlock(&bse.bse_mtx);
560 pthread_mutex_unlock(&bc->bc_mtx);
563 * The processing thread has been interrupted. Since it's not
564 * clear if the callback has been invoked yet, return EBUSY.
570 blockif_close(struct blockif_ctxt *bc)
577 assert(bc->bc_magic == BLOCKIF_SIG);
580 * Stop the block i/o thread
583 pthread_cond_broadcast(&bc->bc_cond);
584 for (i = 0; i < BLOCKIF_NUMTHR; i++)
585 pthread_join(bc->bc_btid[i], &jval);
587 /* XXX Cancel queued i/o's ??? */
600 * Return virtual C/H/S values for a given block. Use the algorithm
601 * outlined in the VHD specification to calculate values.
604 blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
606 off_t sectors; /* total sectors of the block dev */
607 off_t hcyl; /* cylinders times heads */
608 uint16_t secpt; /* sectors per track */
611 assert(bc->bc_magic == BLOCKIF_SIG);
613 sectors = bc->bc_size / bc->bc_sectsz;
615 /* Clamp the size to the largest possible with CHS */
616 if (sectors > 65535UL*16*255)
617 sectors = 65535UL*16*255;
619 if (sectors >= 65536UL*16*63) {
622 hcyl = sectors / secpt;
625 hcyl = sectors / secpt;
626 heads = (hcyl + 1023) / 1024;
631 if (hcyl >= (heads * 1024) || heads > 16) {
634 hcyl = sectors / secpt;
636 if (hcyl >= (heads * 1024)) {
639 hcyl = sectors / secpt;
652 blockif_size(struct blockif_ctxt *bc)
655 assert(bc->bc_magic == BLOCKIF_SIG);
656 return (bc->bc_size);
660 blockif_sectsz(struct blockif_ctxt *bc)
663 assert(bc->bc_magic == BLOCKIF_SIG);
664 return (bc->bc_sectsz);
668 blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off)
671 assert(bc->bc_magic == BLOCKIF_SIG);
672 *size = bc->bc_psectsz;
673 *off = bc->bc_psectoff;
677 blockif_queuesz(struct blockif_ctxt *bc)
680 assert(bc->bc_magic == BLOCKIF_SIG);
681 return (BLOCKIF_MAXREQ - 1);
685 blockif_is_ro(struct blockif_ctxt *bc)
688 assert(bc->bc_magic == BLOCKIF_SIG);
689 return (bc->bc_rdonly);
693 blockif_candelete(struct blockif_ctxt *bc)
696 assert(bc->bc_magic == BLOCKIF_SIG);
697 return (bc->bc_candelete);