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;
282 bc->bc_sectsz = sectsz;
283 pthread_mutex_init(&bc->bc_mtx, NULL);
284 pthread_cond_init(&bc->bc_cond, NULL);
285 TAILQ_INIT(&bc->bc_freeq);
286 TAILQ_INIT(&bc->bc_inuseq);
287 bc->bc_req_count = 0;
288 for (i = 0; i < BLOCKIF_MAXREQ; i++) {
289 bc->bc_reqs[i].be_status = BST_FREE;
290 TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link);
293 pthread_create(&bc->bc_btid, NULL, blockif_thr, bc);
295 snprintf(tname, sizeof(tname), "blk-%s", ident);
296 pthread_set_name_np(bc->bc_btid, tname);
302 blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
309 pthread_mutex_lock(&bc->bc_mtx);
310 if (bc->bc_req_count < BLOCKIF_MAXREQ) {
312 * Enqueue and inform the block i/o thread
313 * that there is work available
315 blockif_enqueue(bc, breq, op);
316 pthread_cond_signal(&bc->bc_cond);
319 * Callers are not allowed to enqueue more than
320 * the specified blockif queue limit. Return an
321 * error to indicate that the queue length has been
326 pthread_mutex_unlock(&bc->bc_mtx);
332 blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
335 assert(bc->bc_magic == BLOCKIF_SIG);
336 return (blockif_request(bc, breq, BOP_READ));
340 blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
343 assert(bc->bc_magic == BLOCKIF_SIG);
344 return (blockif_request(bc, breq, BOP_WRITE));
348 blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
351 assert(bc->bc_magic == BLOCKIF_SIG);
352 return (blockif_request(bc, breq, BOP_FLUSH));
356 blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
359 assert(bc->bc_magic == BLOCKIF_SIG);
360 return (blockif_request(bc, breq, BOP_CANCEL));
364 blockif_close(struct blockif_ctxt *bc)
371 assert(bc->bc_magic == BLOCKIF_SIG);
374 * Stop the block i/o thread
377 pthread_cond_signal(&bc->bc_cond);
378 pthread_join(bc->bc_btid, &jval);
380 /* XXX Cancel queued i/o's ??? */
393 * Return virtual C/H/S values for a given block. Use the algorithm
394 * outlined in the VHD specification to calculate values.
397 blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
399 off_t sectors; /* total sectors of the block dev */
400 off_t hcyl; /* cylinders times heads */
401 uint16_t secpt; /* sectors per track */
404 assert(bc->bc_magic == BLOCKIF_SIG);
406 sectors = bc->bc_size / bc->bc_sectsz;
408 /* Clamp the size to the largest possible with CHS */
409 if (sectors > 65535UL*16*255)
410 sectors = 65535UL*16*255;
412 if (sectors >= 65536UL*16*63) {
415 hcyl = sectors / secpt;
418 hcyl = sectors / secpt;
419 heads = (hcyl + 1023) / 1024;
424 if (hcyl >= (heads * 1024) || heads > 16) {
427 hcyl = sectors / secpt;
429 if (hcyl >= (heads * 1024)) {
432 hcyl = sectors / secpt;
445 blockif_size(struct blockif_ctxt *bc)
448 assert(bc->bc_magic == BLOCKIF_SIG);
449 return (bc->bc_size);
453 blockif_sectsz(struct blockif_ctxt *bc)
456 assert(bc->bc_magic == BLOCKIF_SIG);
457 return (bc->bc_sectsz);
461 blockif_queuesz(struct blockif_ctxt *bc)
464 assert(bc->bc_magic == BLOCKIF_SIG);
465 return (BLOCKIF_MAXREQ);
469 blockif_is_ro(struct blockif_ctxt *bc)
472 assert(bc->bc_magic == BLOCKIF_SIG);
473 return (bc->bc_rdonly);