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
74 TAILQ_ENTRY(blockif_elem) be_link;
75 struct blockif_req *be_req;
77 enum blockstat be_status;
92 pthread_mutex_t bc_mtx;
93 pthread_cond_t bc_cond;
96 /* Request elements and free/pending/busy queues */
97 TAILQ_HEAD(, blockif_elem) bc_freeq;
98 TAILQ_HEAD(, blockif_elem) bc_pendq;
99 TAILQ_HEAD(, blockif_elem) bc_busyq;
101 struct blockif_elem bc_reqs[BLOCKIF_MAXREQ];
104 static pthread_once_t blockif_once = PTHREAD_ONCE_INIT;
106 struct blockif_sig_elem {
107 pthread_mutex_t bse_mtx;
108 pthread_cond_t bse_cond;
110 struct blockif_sig_elem *bse_next;
113 static struct blockif_sig_elem *blockif_bse_head;
116 blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
119 struct blockif_elem *be;
121 assert(bc->bc_req_count < BLOCKIF_MAXREQ);
123 be = TAILQ_FIRST(&bc->bc_freeq);
125 assert(be->be_status == BST_FREE);
127 TAILQ_REMOVE(&bc->bc_freeq, be, be_link);
128 be->be_status = BST_PEND;
131 TAILQ_INSERT_TAIL(&bc->bc_pendq, be, be_link);
139 blockif_dequeue(struct blockif_ctxt *bc, struct blockif_elem **bep)
141 struct blockif_elem *be;
143 if (bc->bc_req_count == 0)
146 be = TAILQ_FIRST(&bc->bc_pendq);
148 assert(be->be_status == BST_PEND);
149 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
150 be->be_status = BST_BUSY;
151 be->be_tid = bc->bc_btid;
152 TAILQ_INSERT_TAIL(&bc->bc_busyq, be, be_link);
160 blockif_complete(struct blockif_ctxt *bc, struct blockif_elem *be)
162 assert(be->be_status == BST_DONE);
164 TAILQ_REMOVE(&bc->bc_busyq, be, be_link);
166 be->be_status = BST_FREE;
168 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
174 blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be)
176 struct blockif_req *br;
185 if (preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
192 else if (pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
198 if (ioctl(bc->bc_fd, DIOCGFLUSH))
200 } else if (fsync(bc->bc_fd))
204 if (!bc->bc_candelete)
206 else if (bc->bc_rdonly)
208 else if (bc->bc_ischr) {
209 arg[0] = br->br_offset;
210 arg[1] = br->br_iov[0].iov_len;
211 if (ioctl(bc->bc_fd, DIOCGDELETE, arg))
221 be->be_status = BST_DONE;
223 (*br->br_callback)(br, err);
227 blockif_thr(void *arg)
229 struct blockif_ctxt *bc;
230 struct blockif_elem *be;
235 pthread_mutex_lock(&bc->bc_mtx);
236 while (!blockif_dequeue(bc, &be)) {
237 pthread_mutex_unlock(&bc->bc_mtx);
238 blockif_proc(bc, be);
239 pthread_mutex_lock(&bc->bc_mtx);
240 blockif_complete(bc, be);
242 pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx);
243 pthread_mutex_unlock(&bc->bc_mtx);
246 * Check ctxt status here to see if exit requested
257 blockif_sigcont_handler(int signal, enum ev_type type, void *arg)
259 struct blockif_sig_elem *bse;
263 * Process the entire list even if not intended for
267 bse = blockif_bse_head;
270 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
272 (uintptr_t)bse->bse_next));
274 pthread_mutex_lock(&bse->bse_mtx);
275 bse->bse_pending = 0;
276 pthread_cond_signal(&bse->bse_cond);
277 pthread_mutex_unlock(&bse->bse_mtx);
284 mevent_add(SIGCONT, EVF_SIGNAL, blockif_sigcont_handler, NULL);
285 (void) signal(SIGCONT, SIG_IGN);
288 struct blockif_ctxt *
289 blockif_open(const char *optstr, const char *ident)
291 char tname[MAXCOMLEN + 1];
293 struct blockif_ctxt *bc;
295 struct diocgattr_arg arg;
296 off_t size, psectsz, psectoff;
297 int extra, fd, i, sectsz;
298 int nocache, sync, ro, candelete;
300 pthread_once(&blockif_once, blockif_init);
307 * The first element in the optstring is always a pathname.
308 * Optional elements follow
310 nopt = strdup(optstr);
311 for (xopts = strtok(nopt, ",");
313 xopts = strtok(NULL, ",")) {
314 if (!strcmp(xopts, "nocache"))
316 else if (!strcmp(xopts, "sync"))
318 else if (!strcmp(xopts, "ro"))
328 fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra);
330 /* Attempt a r/w fail with a r/o open */
331 fd = open(nopt, O_RDONLY | extra);
336 perror("Could not open backing file");
340 if (fstat(fd, &sbuf) < 0) {
341 perror("Could not stat backing file");
347 * Deal with raw devices
351 psectsz = psectoff = 0;
353 if (S_ISCHR(sbuf.st_mode)) {
354 if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
355 ioctl(fd, DIOCGSECTORSIZE, §sz)) {
356 perror("Could not fetch dev blk/sector size");
362 if (ioctl(fd, DIOCGSTRIPESIZE, &psectsz) == 0 && psectsz > 0)
363 ioctl(fd, DIOCGSTRIPEOFFSET, &psectoff);
364 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
365 arg.len = sizeof(arg.value.i);
366 if (ioctl(fd, DIOCGATTR, &arg) == 0)
367 candelete = arg.value.i;
369 psectsz = sbuf.st_blksize;
371 bc = calloc(1, sizeof(struct blockif_ctxt));
377 bc->bc_magic = BLOCKIF_SIG;
379 bc->bc_ischr = S_ISCHR(sbuf.st_mode);
380 bc->bc_candelete = candelete;
383 bc->bc_sectsz = sectsz;
384 bc->bc_psectsz = psectsz;
385 bc->bc_psectoff = psectoff;
386 pthread_mutex_init(&bc->bc_mtx, NULL);
387 pthread_cond_init(&bc->bc_cond, NULL);
388 TAILQ_INIT(&bc->bc_freeq);
389 TAILQ_INIT(&bc->bc_pendq);
390 TAILQ_INIT(&bc->bc_busyq);
391 bc->bc_req_count = 0;
392 for (i = 0; i < BLOCKIF_MAXREQ; i++) {
393 bc->bc_reqs[i].be_status = BST_FREE;
394 TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
397 pthread_create(&bc->bc_btid, NULL, blockif_thr, bc);
399 snprintf(tname, sizeof(tname), "blk-%s", ident);
400 pthread_set_name_np(bc->bc_btid, tname);
406 blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
413 pthread_mutex_lock(&bc->bc_mtx);
414 if (bc->bc_req_count < BLOCKIF_MAXREQ) {
416 * Enqueue and inform the block i/o thread
417 * that there is work available
419 blockif_enqueue(bc, breq, op);
420 pthread_cond_signal(&bc->bc_cond);
423 * Callers are not allowed to enqueue more than
424 * the specified blockif queue limit. Return an
425 * error to indicate that the queue length has been
430 pthread_mutex_unlock(&bc->bc_mtx);
436 blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
439 assert(bc->bc_magic == BLOCKIF_SIG);
440 return (blockif_request(bc, breq, BOP_READ));
444 blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
447 assert(bc->bc_magic == BLOCKIF_SIG);
448 return (blockif_request(bc, breq, BOP_WRITE));
452 blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
455 assert(bc->bc_magic == BLOCKIF_SIG);
456 return (blockif_request(bc, breq, BOP_FLUSH));
460 blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq)
463 assert(bc->bc_magic == BLOCKIF_SIG);
464 return (blockif_request(bc, breq, BOP_DELETE));
468 blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
470 struct blockif_elem *be;
472 assert(bc->bc_magic == BLOCKIF_SIG);
474 pthread_mutex_lock(&bc->bc_mtx);
476 * Check pending requests.
478 TAILQ_FOREACH(be, &bc->bc_pendq, be_link) {
479 if (be->be_req == breq)
486 TAILQ_REMOVE(&bc->bc_pendq, be, be_link);
487 be->be_status = BST_FREE;
489 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
491 pthread_mutex_unlock(&bc->bc_mtx);
497 * Check in-flight requests.
499 TAILQ_FOREACH(be, &bc->bc_busyq, be_link) {
500 if (be->be_req == breq)
507 pthread_mutex_unlock(&bc->bc_mtx);
512 * Interrupt the processing thread to force it return
513 * prematurely via it's normal callback path.
515 while (be->be_status == BST_BUSY) {
516 struct blockif_sig_elem bse, *old_head;
518 pthread_mutex_init(&bse.bse_mtx, NULL);
519 pthread_cond_init(&bse.bse_cond, NULL);
524 old_head = blockif_bse_head;
525 bse.bse_next = old_head;
526 } while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head,
530 pthread_kill(be->be_tid, SIGCONT);
532 pthread_mutex_lock(&bse.bse_mtx);
533 while (bse.bse_pending)
534 pthread_cond_wait(&bse.bse_cond, &bse.bse_mtx);
535 pthread_mutex_unlock(&bse.bse_mtx);
538 pthread_mutex_unlock(&bc->bc_mtx);
541 * The processing thread has been interrupted. Since it's not
542 * clear if the callback has been invoked yet, return EBUSY.
548 blockif_close(struct blockif_ctxt *bc)
555 assert(bc->bc_magic == BLOCKIF_SIG);
558 * Stop the block i/o thread
561 pthread_cond_signal(&bc->bc_cond);
562 pthread_join(bc->bc_btid, &jval);
564 /* XXX Cancel queued i/o's ??? */
577 * Return virtual C/H/S values for a given block. Use the algorithm
578 * outlined in the VHD specification to calculate values.
581 blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
583 off_t sectors; /* total sectors of the block dev */
584 off_t hcyl; /* cylinders times heads */
585 uint16_t secpt; /* sectors per track */
588 assert(bc->bc_magic == BLOCKIF_SIG);
590 sectors = bc->bc_size / bc->bc_sectsz;
592 /* Clamp the size to the largest possible with CHS */
593 if (sectors > 65535UL*16*255)
594 sectors = 65535UL*16*255;
596 if (sectors >= 65536UL*16*63) {
599 hcyl = sectors / secpt;
602 hcyl = sectors / secpt;
603 heads = (hcyl + 1023) / 1024;
608 if (hcyl >= (heads * 1024) || heads > 16) {
611 hcyl = sectors / secpt;
613 if (hcyl >= (heads * 1024)) {
616 hcyl = sectors / secpt;
629 blockif_size(struct blockif_ctxt *bc)
632 assert(bc->bc_magic == BLOCKIF_SIG);
633 return (bc->bc_size);
637 blockif_sectsz(struct blockif_ctxt *bc)
640 assert(bc->bc_magic == BLOCKIF_SIG);
641 return (bc->bc_sectsz);
645 blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off)
648 assert(bc->bc_magic == BLOCKIF_SIG);
649 *size = bc->bc_psectsz;
650 *off = bc->bc_psectoff;
654 blockif_queuesz(struct blockif_ctxt *bc)
657 assert(bc->bc_magic == BLOCKIF_SIG);
658 return (BLOCKIF_MAXREQ - 1);
662 blockif_is_ro(struct blockif_ctxt *bc)
665 assert(bc->bc_magic == BLOCKIF_SIG);
666 return (bc->bc_rdonly);
670 blockif_candelete(struct blockif_ctxt *bc)
673 assert(bc->bc_magic == BLOCKIF_SIG);
674 return (bc->bc_candelete);