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
68 TAILQ_ENTRY(blockif_elem) be_link;
69 struct blockif_req *be_req;
71 enum blockstat be_status;
81 pthread_mutex_t bc_mtx;
82 pthread_cond_t bc_cond;
85 /* Request elements and free/inuse queues */
86 TAILQ_HEAD(, blockif_elem) bc_freeq;
87 TAILQ_HEAD(, blockif_elem) bc_inuseq;
89 struct blockif_elem bc_reqs[BLOCKIF_MAXREQ];
93 blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
96 struct blockif_elem *be;
98 assert(bc->bc_req_count < BLOCKIF_MAXREQ);
100 be = TAILQ_FIRST(&bc->bc_freeq);
102 assert(be->be_status == BST_FREE);
104 TAILQ_REMOVE(&bc->bc_freeq, be, be_link);
105 be->be_status = BST_INUSE;
108 TAILQ_INSERT_TAIL(&bc->bc_inuseq, be, be_link);
116 blockif_dequeue(struct blockif_ctxt *bc, struct blockif_elem *el)
118 struct blockif_elem *be;
120 if (bc->bc_req_count == 0)
123 be = TAILQ_FIRST(&bc->bc_inuseq);
125 assert(be->be_status == BST_INUSE);
128 TAILQ_REMOVE(&bc->bc_inuseq, be, be_link);
129 be->be_status = BST_FREE;
131 TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link);
139 blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be)
141 struct blockif_req *br;
149 if (preadv(bc->bc_fd, br->br_iov, br->br_iovcnt,
156 else if (pwritev(bc->bc_fd, br->br_iov, br->br_iovcnt,
170 (*br->br_callback)(br, err);
174 blockif_thr(void *arg)
176 struct blockif_ctxt *bc;
177 struct blockif_elem req;
182 pthread_mutex_lock(&bc->bc_mtx);
183 while (!blockif_dequeue(bc, &req)) {
184 pthread_mutex_unlock(&bc->bc_mtx);
185 blockif_proc(bc, &req);
186 pthread_mutex_lock(&bc->bc_mtx);
188 pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx);
189 pthread_mutex_unlock(&bc->bc_mtx);
192 * Check ctxt status here to see if exit requested
202 struct blockif_ctxt *
203 blockif_open(const char *optstr, const char *ident)
205 char tname[MAXCOMLEN + 1];
207 struct blockif_ctxt *bc;
210 int extra, fd, i, sectsz;
211 int nocache, sync, ro;
218 * The first element in the optstring is always a pathname.
219 * Optional elements follow
221 nopt = strdup(optstr);
222 for (xopts = strtok(nopt, ",");
224 xopts = strtok(NULL, ",")) {
225 if (!strcmp(xopts, "nocache"))
227 else if (!strcmp(xopts, "sync"))
229 else if (!strcmp(xopts, "ro"))
239 fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra);
241 /* Attempt a r/w fail with a r/o open */
242 fd = open(nopt, O_RDONLY | extra);
247 perror("Could not open backing file");
251 if (fstat(fd, &sbuf) < 0) {
252 perror("Could not stat backing file");
258 * Deal with raw devices
262 if (S_ISCHR(sbuf.st_mode)) {
263 if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
264 ioctl(fd, DIOCGSECTORSIZE, §sz)) {
265 perror("Could not fetch dev blk/sector size");
273 bc = calloc(1, sizeof(struct blockif_ctxt));
279 bc->bc_magic = BLOCKIF_SIG;
283 bc->bc_sectsz = sectsz;
284 pthread_mutex_init(&bc->bc_mtx, NULL);
285 pthread_cond_init(&bc->bc_cond, NULL);
286 TAILQ_INIT(&bc->bc_freeq);
287 TAILQ_INIT(&bc->bc_inuseq);
288 bc->bc_req_count = 0;
289 for (i = 0; i < BLOCKIF_MAXREQ; i++) {
290 bc->bc_reqs[i].be_status = BST_FREE;
291 TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
294 pthread_create(&bc->bc_btid, NULL, blockif_thr, bc);
296 snprintf(tname, sizeof(tname), "blk-%s", ident);
297 pthread_set_name_np(bc->bc_btid, tname);
303 blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
310 pthread_mutex_lock(&bc->bc_mtx);
311 if (bc->bc_req_count < BLOCKIF_MAXREQ) {
313 * Enqueue and inform the block i/o thread
314 * that there is work available
316 blockif_enqueue(bc, breq, op);
317 pthread_cond_signal(&bc->bc_cond);
320 * Callers are not allowed to enqueue more than
321 * the specified blockif queue limit. Return an
322 * error to indicate that the queue length has been
327 pthread_mutex_unlock(&bc->bc_mtx);
333 blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
336 assert(bc->bc_magic == BLOCKIF_SIG);
337 return (blockif_request(bc, breq, BOP_READ));
341 blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
344 assert(bc->bc_magic == BLOCKIF_SIG);
345 return (blockif_request(bc, breq, BOP_WRITE));
349 blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
352 assert(bc->bc_magic == BLOCKIF_SIG);
353 return (blockif_request(bc, breq, BOP_FLUSH));
357 blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
360 assert(bc->bc_magic == BLOCKIF_SIG);
361 return (blockif_request(bc, breq, BOP_CANCEL));
365 blockif_close(struct blockif_ctxt *bc)
372 assert(bc->bc_magic == BLOCKIF_SIG);
375 * Stop the block i/o thread
378 pthread_cond_signal(&bc->bc_cond);
379 pthread_join(bc->bc_btid, &jval);
381 /* XXX Cancel queued i/o's ??? */
394 * Return virtual C/H/S values for a given block. Use the algorithm
395 * outlined in the VHD specification to calculate values.
398 blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
400 off_t sectors; /* total sectors of the block dev */
401 off_t hcyl; /* cylinders times heads */
402 uint16_t secpt; /* sectors per track */
405 assert(bc->bc_magic == BLOCKIF_SIG);
407 sectors = bc->bc_size / bc->bc_sectsz;
409 /* Clamp the size to the largest possible with CHS */
410 if (sectors > 65535UL*16*255)
411 sectors = 65535UL*16*255;
413 if (sectors >= 65536UL*16*63) {
416 hcyl = sectors / secpt;
419 hcyl = sectors / secpt;
420 heads = (hcyl + 1023) / 1024;
425 if (hcyl >= (heads * 1024) || heads > 16) {
428 hcyl = sectors / secpt;
430 if (hcyl >= (heads * 1024)) {
433 hcyl = sectors / secpt;
446 blockif_size(struct blockif_ctxt *bc)
449 assert(bc->bc_magic == BLOCKIF_SIG);
450 return (bc->bc_size);
454 blockif_sectsz(struct blockif_ctxt *bc)
457 assert(bc->bc_magic == BLOCKIF_SIG);
458 return (bc->bc_sectsz);
462 blockif_queuesz(struct blockif_ctxt *bc)
465 assert(bc->bc_magic == BLOCKIF_SIG);
466 return (BLOCKIF_MAXREQ);
470 blockif_is_ro(struct blockif_ctxt *bc)
473 assert(bc->bc_magic == BLOCKIF_SIG);
474 return (bc->bc_rdonly);