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>
51 #define BLOCKIF_SIG 0xb109b109
53 #define BLOCKIF_MAXREQ 32
67 TAILQ_ENTRY(blockif_elem) be_link;
68 struct blockif_req *be_req;
70 enum blockstat be_status;
80 pthread_mutex_t bc_mtx;
81 pthread_cond_t bc_cond;
84 /* Request elements and free/inuse queues */
85 TAILQ_HEAD(, blockif_elem) bc_freeq;
86 TAILQ_HEAD(, blockif_elem) bc_inuseq;
88 struct blockif_elem bc_reqs[BLOCKIF_MAXREQ];
92 blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
95 struct blockif_elem *be;
97 assert(bc->bc_req_count < BLOCKIF_MAXREQ);
99 be = TAILQ_FIRST(&bc->bc_freeq);
101 assert(be->be_status == BST_FREE);
103 TAILQ_REMOVE(&bc->bc_freeq, be, be_link);
104 be->be_status = BST_INUSE;
107 TAILQ_INSERT_TAIL(&bc->bc_inuseq, be, be_link);
115 blockif_dequeue(struct blockif_ctxt *bc, struct blockif_elem *el)
117 struct blockif_elem *be;
119 if (bc->bc_req_count == 0)
122 be = TAILQ_FIRST(&bc->bc_inuseq);
124 assert(be->be_status == BST_INUSE);
127 TAILQ_REMOVE(&bc->bc_inuseq, be, be_link);
128 be->be_status = BST_FREE;
130 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
138 blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be)
140 struct blockif_req *br;
148 if (preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
155 else if (pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
166 (*br->br_callback)(br, err);
170 blockif_thr(void *arg)
172 struct blockif_ctxt *bc;
173 struct blockif_elem req;
178 pthread_mutex_lock(&bc->bc_mtx);
179 while (!blockif_dequeue(bc, &req)) {
180 pthread_mutex_unlock(&bc->bc_mtx);
181 blockif_proc(bc, &req);
182 pthread_mutex_lock(&bc->bc_mtx);
184 pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx);
185 pthread_mutex_unlock(&bc->bc_mtx);
188 * Check ctxt status here to see if exit requested
198 struct blockif_ctxt *
199 blockif_open(const char *optstr, const char *ident)
201 char tname[MAXCOMLEN + 1];
203 struct blockif_ctxt *bc;
206 int extra, fd, i, sectsz;
207 int nocache, sync, ro;
214 * The first element in the optstring is always a pathname.
215 * Optional elements follow
217 nopt = strdup(optstr);
218 for (xopts = strtok(nopt, ",");
220 xopts = strtok(NULL, ",")) {
221 if (!strcmp(xopts, "nocache"))
223 else if (!strcmp(xopts, "sync"))
225 else if (!strcmp(xopts, "ro"))
235 fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra);
237 /* Attempt a r/w fail with a r/o open */
238 fd = open(nopt, O_RDONLY | extra);
243 perror("Could not open backing file");
247 if (fstat(fd, &sbuf) < 0) {
248 perror("Could not stat backing file");
254 * Deal with raw devices
258 if (S_ISCHR(sbuf.st_mode)) {
259 if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
260 ioctl(fd, DIOCGSECTORSIZE, §sz)) {
261 perror("Could not fetch dev blk/sector size");
269 bc = calloc(1, sizeof(struct blockif_ctxt));
275 bc->bc_magic = BLOCKIF_SIG;
279 bc->bc_sectsz = sectsz;
280 pthread_mutex_init(&bc->bc_mtx, NULL);
281 pthread_cond_init(&bc->bc_cond, NULL);
282 TAILQ_INIT(&bc->bc_freeq);
283 TAILQ_INIT(&bc->bc_inuseq);
284 bc->bc_req_count = 0;
285 for (i = 0; i < BLOCKIF_MAXREQ; i++) {
286 bc->bc_reqs[i].be_status = BST_FREE;
287 TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
290 pthread_create(&bc->bc_btid, NULL, blockif_thr, bc);
292 snprintf(tname, sizeof(tname), "blk-%s", ident);
293 pthread_set_name_np(bc->bc_btid, tname);
299 blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
306 pthread_mutex_lock(&bc->bc_mtx);
307 if (bc->bc_req_count < BLOCKIF_MAXREQ) {
309 * Enqueue and inform the block i/o thread
310 * that there is work available
312 blockif_enqueue(bc, breq, op);
313 pthread_cond_signal(&bc->bc_cond);
316 * Callers are not allowed to enqueue more than
317 * the specified blockif queue limit. Return an
318 * error to indicate that the queue length has been
323 pthread_mutex_unlock(&bc->bc_mtx);
329 blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
332 assert(bc->bc_magic == BLOCKIF_SIG);
333 return (blockif_request(bc, breq, BOP_READ));
337 blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
340 assert(bc->bc_magic == BLOCKIF_SIG);
341 return (blockif_request(bc, breq, BOP_WRITE));
345 blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
348 assert(bc->bc_magic == BLOCKIF_SIG);
349 return (blockif_request(bc, breq, BOP_FLUSH));
353 blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
355 struct blockif_elem *be;
357 assert(bc->bc_magic == BLOCKIF_SIG);
359 pthread_mutex_lock(&bc->bc_mtx);
360 TAILQ_FOREACH(be, &bc->bc_inuseq, be_link) {
361 if (be->be_req == breq)
365 pthread_mutex_unlock(&bc->bc_mtx);
369 TAILQ_REMOVE(&bc->bc_inuseq, be, be_link);
370 be->be_status = BST_FREE;
372 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
374 pthread_mutex_unlock(&bc->bc_mtx);
380 blockif_close(struct blockif_ctxt *bc)
387 assert(bc->bc_magic == BLOCKIF_SIG);
390 * Stop the block i/o thread
393 pthread_cond_signal(&bc->bc_cond);
394 pthread_join(bc->bc_btid, &jval);
396 /* XXX Cancel queued i/o's ??? */
409 * Return virtual C/H/S values for a given block. Use the algorithm
410 * outlined in the VHD specification to calculate values.
413 blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
415 off_t sectors; /* total sectors of the block dev */
416 off_t hcyl; /* cylinders times heads */
417 uint16_t secpt; /* sectors per track */
420 assert(bc->bc_magic == BLOCKIF_SIG);
422 sectors = bc->bc_size / bc->bc_sectsz;
424 /* Clamp the size to the largest possible with CHS */
425 if (sectors > 65535UL*16*255)
426 sectors = 65535UL*16*255;
428 if (sectors >= 65536UL*16*63) {
431 hcyl = sectors / secpt;
434 hcyl = sectors / secpt;
435 heads = (hcyl + 1023) / 1024;
440 if (hcyl >= (heads * 1024) || heads > 16) {
443 hcyl = sectors / secpt;
445 if (hcyl >= (heads * 1024)) {
448 hcyl = sectors / secpt;
461 blockif_size(struct blockif_ctxt *bc)
464 assert(bc->bc_magic == BLOCKIF_SIG);
465 return (bc->bc_size);
469 blockif_sectsz(struct blockif_ctxt *bc)
472 assert(bc->bc_magic == BLOCKIF_SIG);
473 return (bc->bc_sectsz);
477 blockif_queuesz(struct blockif_ctxt *bc)
480 assert(bc->bc_magic == BLOCKIF_SIG);
481 return (BLOCKIF_MAXREQ);
485 blockif_is_ro(struct blockif_ctxt *bc)
488 assert(bc->bc_magic == BLOCKIF_SIG);
489 return (bc->bc_rdonly);