Changeset View
Standalone View
usr.sbin/bhyve/block_local.c
- This file was copied from usr.sbin/bhyve/block_if.c.
Show All 19 Lines | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
* | * | ||||
* $FreeBSD: head/usr.sbin/bhyve/block_if.c 356523 2020-01-08 22:55:22Z vmaffione $ | * $FreeBSD: head/usr.sbin/bhyve/block_local.c 356523 2020-01-08 22:55:22Z vmaffione $ | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD: head/usr.sbin/bhyve/block_if.c 356523 2020-01-08 22:55:22Z vmaffione $"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#ifndef WITHOUT_CAPSICUM | #ifndef WITHOUT_CAPSICUM | ||||
#include <sys/capsicum.h> | #include <sys/capsicum.h> | ||||
#endif | #endif | ||||
#include <sys/queue.h> | |||||
#include <sys/errno.h> | #include <sys/errno.h> | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <sys/ioctl.h> | #include <sys/ioctl.h> | ||||
#include <sys/disk.h> | #include <sys/disk.h> | ||||
#include <assert.h> | #include <assert.h> | ||||
#ifndef WITHOUT_CAPSICUM | #ifndef WITHOUT_CAPSICUM | ||||
#include <capsicum_helpers.h> | #include <capsicum_helpers.h> | ||||
Show All 11 Lines | |||||
#include <machine/atomic.h> | #include <machine/atomic.h> | ||||
#include "bhyverun.h" | #include "bhyverun.h" | ||||
#include "debug.h" | #include "debug.h" | ||||
#include "mevent.h" | #include "mevent.h" | ||||
#include "block_if.h" | #include "block_if.h" | ||||
#define BLOCKIF_SIG 0xb109b109 | #include <sys/linker_set.h> | ||||
#define BLOCKIF_NUMTHR 8 | static pthread_once_t blocklocal_once = PTHREAD_ONCE_INIT; | ||||
#define BLOCKIF_MAXREQ (BLOCKIF_RING_MAX + BLOCKIF_NUMTHR) | |||||
enum blockop { | struct blocklocal_sig_elem { | ||||
BOP_READ, | |||||
BOP_WRITE, | |||||
BOP_FLUSH, | |||||
BOP_DELETE | |||||
}; | |||||
enum blockstat { | |||||
BST_FREE, | |||||
BST_BLOCK, | |||||
BST_PEND, | |||||
BST_BUSY, | |||||
BST_DONE | |||||
}; | |||||
struct blockif_elem { | |||||
TAILQ_ENTRY(blockif_elem) be_link; | |||||
struct blockif_req *be_req; | |||||
enum blockop be_op; | |||||
enum blockstat be_status; | |||||
pthread_t be_tid; | |||||
off_t be_block; | |||||
}; | |||||
struct blockif_ctxt { | |||||
int bc_magic; | |||||
int bc_fd; | |||||
int bc_ischr; | |||||
int bc_isgeom; | |||||
int bc_candelete; | |||||
int bc_rdonly; | |||||
off_t bc_size; | |||||
int bc_sectsz; | |||||
int bc_psectsz; | |||||
int bc_psectoff; | |||||
int bc_closing; | |||||
pthread_t bc_btid[BLOCKIF_NUMTHR]; | |||||
pthread_mutex_t bc_mtx; | |||||
pthread_cond_t bc_cond; | |||||
/* Request elements and free/pending/busy queues */ | |||||
TAILQ_HEAD(, blockif_elem) bc_freeq; | |||||
TAILQ_HEAD(, blockif_elem) bc_pendq; | |||||
TAILQ_HEAD(, blockif_elem) bc_busyq; | |||||
struct blockif_elem bc_reqs[BLOCKIF_MAXREQ]; | |||||
}; | |||||
static pthread_once_t blockif_once = PTHREAD_ONCE_INIT; | |||||
struct blockif_sig_elem { | |||||
pthread_mutex_t bse_mtx; | pthread_mutex_t bse_mtx; | ||||
pthread_cond_t bse_cond; | pthread_cond_t bse_cond; | ||||
int bse_pending; | int bse_pending; | ||||
struct blockif_sig_elem *bse_next; | struct blocklocal_sig_elem *bse_next; | ||||
}; | }; | ||||
static struct blockif_sig_elem *blockif_bse_head; | static struct blocklocal_sig_elem *blocklocal_bse_head; | ||||
static int | static int | ||||
blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq, | blocklocal_enqueue(blockif_ctxt_t *bc, blockif_req_t *breq, | ||||
enum blockop op) | enum blockop op) | ||||
{ | { | ||||
struct blockif_elem *be, *tbe; | struct blockif_elem *be, *tbe; | ||||
off_t off; | off_t off; | ||||
int i; | int i; | ||||
be = TAILQ_FIRST(&bc->bc_freeq); | be = TAILQ_FIRST(&bc->bc_freeq); | ||||
assert(be != NULL); | assert(be != NULL); | ||||
Show All 27 Lines | if (tbe == NULL) | ||||
be->be_status = BST_PEND; | be->be_status = BST_PEND; | ||||
else | else | ||||
be->be_status = BST_BLOCK; | be->be_status = BST_BLOCK; | ||||
TAILQ_INSERT_TAIL(&bc->bc_pendq, be, be_link); | TAILQ_INSERT_TAIL(&bc->bc_pendq, be, be_link); | ||||
return (be->be_status == BST_PEND); | return (be->be_status == BST_PEND); | ||||
} | } | ||||
static int | static int | ||||
blockif_dequeue(struct blockif_ctxt *bc, pthread_t t, struct blockif_elem **bep) | blocklocal_dequeue(blockif_ctxt_t *bc, pthread_t t, struct blockif_elem **bep) | ||||
{ | { | ||||
struct blockif_elem *be; | struct blockif_elem *be; | ||||
TAILQ_FOREACH(be, &bc->bc_pendq, be_link) { | TAILQ_FOREACH(be, &bc->bc_pendq, be_link) { | ||||
if (be->be_status == BST_PEND) | if (be->be_status == BST_PEND) | ||||
break; | break; | ||||
assert(be->be_status == BST_BLOCK); | assert(be->be_status == BST_BLOCK); | ||||
} | } | ||||
if (be == NULL) | if (be == NULL) | ||||
return (0); | return (0); | ||||
TAILQ_REMOVE(&bc->bc_pendq, be, be_link); | TAILQ_REMOVE(&bc->bc_pendq, be, be_link); | ||||
be->be_status = BST_BUSY; | be->be_status = BST_BUSY; | ||||
be->be_tid = t; | be->be_tid = t; | ||||
TAILQ_INSERT_TAIL(&bc->bc_busyq, be, be_link); | TAILQ_INSERT_TAIL(&bc->bc_busyq, be, be_link); | ||||
*bep = be; | *bep = be; | ||||
return (1); | return (1); | ||||
} | } | ||||
static void | static void | ||||
blockif_complete(struct blockif_ctxt *bc, struct blockif_elem *be) | blocklocal_complete(blockif_ctxt_t *bc, struct blockif_elem *be) | ||||
{ | { | ||||
struct blockif_elem *tbe; | struct blockif_elem *tbe; | ||||
if (be->be_status == BST_DONE || be->be_status == BST_BUSY) | if (be->be_status == BST_DONE || be->be_status == BST_BUSY) | ||||
TAILQ_REMOVE(&bc->bc_busyq, be, be_link); | TAILQ_REMOVE(&bc->bc_busyq, be, be_link); | ||||
else | else | ||||
TAILQ_REMOVE(&bc->bc_pendq, be, be_link); | TAILQ_REMOVE(&bc->bc_pendq, be, be_link); | ||||
TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) { | TAILQ_FOREACH(tbe, &bc->bc_pendq, be_link) { | ||||
if (tbe->be_req->br_offset == be->be_block) | if (tbe->be_req->br_offset == be->be_block) | ||||
tbe->be_status = BST_PEND; | tbe->be_status = BST_PEND; | ||||
} | } | ||||
be->be_tid = 0; | be->be_tid = 0; | ||||
be->be_status = BST_FREE; | be->be_status = BST_FREE; | ||||
be->be_req = NULL; | be->be_req = NULL; | ||||
TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link); | TAILQ_INSERT_TAIL(&bc->bc_freeq, be, be_link); | ||||
} | } | ||||
static void | static void | ||||
blockif_proc(struct blockif_ctxt *bc, struct blockif_elem *be, uint8_t *buf) | blocklocal_proc(blockif_ctxt_t *bc, struct blockif_elem *be, uint8_t *buf) | ||||
{ | { | ||||
struct blockif_req *br; | blockif_req_t *br; | ||||
off_t arg[2]; | off_t arg[2]; | ||||
ssize_t clen, len, off, boff, voff; | ssize_t clen, len, off, boff, voff; | ||||
int i, err; | int i, err; | ||||
br = be->be_req; | br = be->be_req; | ||||
if (br->br_iovcnt <= 1) | if (br->br_iovcnt <= 1) | ||||
buf = NULL; | buf = NULL; | ||||
err = 0; | err = 0; | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | blocklocal_proc(blockif_ctxt_t *bc, struct blockif_elem *be, uint8_t *buf) | ||||
} | } | ||||
be->be_status = BST_DONE; | be->be_status = BST_DONE; | ||||
(*br->br_callback)(br, err); | (*br->br_callback)(br, err); | ||||
} | } | ||||
static void * | static void * | ||||
blockif_thr(void *arg) | blocklocal_thr(void *arg) | ||||
{ | { | ||||
struct blockif_ctxt *bc; | blockif_ctxt_t *bc; | ||||
struct blockif_elem *be; | struct blockif_elem *be; | ||||
pthread_t t; | pthread_t t; | ||||
uint8_t *buf; | uint8_t *buf; | ||||
bc = arg; | bc = arg; | ||||
if (bc->bc_isgeom) | if (bc->bc_isgeom) | ||||
buf = malloc(MAXPHYS); | buf = malloc(MAXPHYS); | ||||
else | else | ||||
buf = NULL; | buf = NULL; | ||||
t = pthread_self(); | t = pthread_self(); | ||||
pthread_mutex_lock(&bc->bc_mtx); | pthread_mutex_lock(&bc->bc_mtx); | ||||
for (;;) { | for (;;) { | ||||
while (blockif_dequeue(bc, t, &be)) { | while (blocklocal_dequeue(bc, t, &be)) { | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
blockif_proc(bc, be, buf); | blocklocal_proc(bc, be, buf); | ||||
pthread_mutex_lock(&bc->bc_mtx); | pthread_mutex_lock(&bc->bc_mtx); | ||||
blockif_complete(bc, be); | blocklocal_complete(bc, be); | ||||
} | } | ||||
/* Check ctxt status here to see if exit requested */ | /* Check ctxt status here to see if exit requested */ | ||||
if (bc->bc_closing) | if (bc->bc_closing) | ||||
break; | break; | ||||
pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx); | pthread_cond_wait(&bc->bc_cond, &bc->bc_mtx); | ||||
} | } | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
if (buf) | if (buf) | ||||
free(buf); | free(buf); | ||||
pthread_exit(NULL); | pthread_exit(NULL); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static void | static void | ||||
blockif_sigcont_handler(int signal, enum ev_type type, void *arg) | blocklocal_sigcont_handler(int signal, enum ev_type type, void *arg) | ||||
{ | { | ||||
struct blockif_sig_elem *bse; | struct blocklocal_sig_elem *bse; | ||||
for (;;) { | for (;;) { | ||||
/* | /* | ||||
* Process the entire list even if not intended for | * Process the entire list even if not intended for | ||||
* this thread. | * this thread. | ||||
*/ | */ | ||||
do { | do { | ||||
bse = blockif_bse_head; | bse = blocklocal_bse_head; | ||||
if (bse == NULL) | if (bse == NULL) | ||||
return; | return; | ||||
} while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head, | } while (!atomic_cmpset_ptr((uintptr_t *)&blocklocal_bse_head, | ||||
(uintptr_t)bse, | (uintptr_t)bse, | ||||
(uintptr_t)bse->bse_next)); | (uintptr_t)bse->bse_next)); | ||||
pthread_mutex_lock(&bse->bse_mtx); | pthread_mutex_lock(&bse->bse_mtx); | ||||
bse->bse_pending = 0; | bse->bse_pending = 0; | ||||
pthread_cond_signal(&bse->bse_cond); | pthread_cond_signal(&bse->bse_cond); | ||||
pthread_mutex_unlock(&bse->bse_mtx); | pthread_mutex_unlock(&bse->bse_mtx); | ||||
} | } | ||||
} | } | ||||
static void | void | ||||
blockif_init(void) | blocklocal_init(void) | ||||
{ | { | ||||
mevent_add(SIGCONT, EVF_SIGNAL, blockif_sigcont_handler, NULL); | mevent_add(SIGCONT, EVF_SIGNAL, blocklocal_sigcont_handler, NULL); | ||||
(void) signal(SIGCONT, SIG_IGN); | (void) signal(SIGCONT, SIG_IGN); | ||||
} | } | ||||
struct blockif_ctxt * | void | ||||
blockif_open(const char *optstr, const char *ident) | blocklocal_cleanup(blockif_ctxt_t *bc) | ||||
{ /* empty block | |||||
* currently no cleanup required. | |||||
*/ | |||||
} | |||||
static blockif_ctxt_t * | |||||
blocklocal_open(const char *optstr, const char *ident) | |||||
{ | { | ||||
char tname[MAXCOMLEN + 1]; | char tname[MAXCOMLEN + 1]; | ||||
char name[MAXPATHLEN]; | char name[MAXPATHLEN]; | ||||
char *nopt, *xopts, *cp; | char *nopt, *xopts, *cp; | ||||
struct blockif_ctxt *bc; | blockif_ctxt_t *bc; | ||||
struct stat sbuf; | struct stat sbuf; | ||||
struct diocgattr_arg arg; | struct diocgattr_arg arg; | ||||
off_t size, psectsz, psectoff; | off_t size, psectsz, psectoff; | ||||
int extra, fd, i, sectsz; | int extra, fd, i, sectsz; | ||||
int nocache, sync, ro, candelete, geom, ssopt, pssopt; | int nocache, sync, ro, candelete, geom, ssopt, pssopt; | ||||
#ifndef WITHOUT_CAPSICUM | #ifndef WITHOUT_CAPSICUM | ||||
cap_rights_t rights; | cap_rights_t rights; | ||||
cap_ioctl_t cmds[] = { DIOCGFLUSH, DIOCGDELETE }; | cap_ioctl_t cmds[] = { DIOCGFLUSH, DIOCGDELETE }; | ||||
#endif | #endif | ||||
pthread_once(&blockif_once, blockif_init); | pthread_once(&blocklocal_once, blocklocal_init); | ||||
fd = -1; | fd = -1; | ||||
ssopt = 0; | ssopt = 0; | ||||
nocache = 0; | nocache = 0; | ||||
sync = 0; | sync = 0; | ||||
ro = 0; | ro = 0; | ||||
/* | /* | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | if (S_ISCHR(sbuf.st_mode)) { | ||||
} | } | ||||
} | } | ||||
sectsz = ssopt; | sectsz = ssopt; | ||||
psectsz = pssopt; | psectsz = pssopt; | ||||
psectoff = 0; | psectoff = 0; | ||||
} | } | ||||
bc = calloc(1, sizeof(struct blockif_ctxt)); | bc = calloc(1, sizeof(blockif_ctxt_t)); | ||||
if (bc == NULL) { | if (bc == NULL) { | ||||
perror("calloc"); | perror("calloc"); | ||||
goto err; | goto err; | ||||
} | } | ||||
bc->bc_magic = BLOCKIF_SIG; | bc->bc_magic = BLOCKIF_SIG; | ||||
bc->bc_fd = fd; | bc->bc_fd = fd; | ||||
bc->bc_ischr = S_ISCHR(sbuf.st_mode); | bc->bc_ischr = S_ISCHR(sbuf.st_mode); | ||||
Show All 10 Lines | #endif | ||||
TAILQ_INIT(&bc->bc_pendq); | TAILQ_INIT(&bc->bc_pendq); | ||||
TAILQ_INIT(&bc->bc_busyq); | TAILQ_INIT(&bc->bc_busyq); | ||||
for (i = 0; i < BLOCKIF_MAXREQ; i++) { | for (i = 0; i < BLOCKIF_MAXREQ; i++) { | ||||
bc->bc_reqs[i].be_status = BST_FREE; | bc->bc_reqs[i].be_status = BST_FREE; | ||||
TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link); | TAILQ_INSERT_HEAD(&bc->bc_freeq, &bc->bc_reqs[i], be_link); | ||||
} | } | ||||
for (i = 0; i < BLOCKIF_NUMTHR; i++) { | for (i = 0; i < BLOCKIF_NUMTHR; i++) { | ||||
pthread_create(&bc->bc_btid[i], NULL, blockif_thr, bc); | pthread_create(&bc->bc_btid[i], NULL, blocklocal_thr, bc); | ||||
snprintf(tname, sizeof(tname), "blk-%s-%d", ident, i); | snprintf(tname, sizeof(tname), "blk-%s-%d", ident, i); | ||||
pthread_set_name_np(bc->bc_btid[i], tname); | pthread_set_name_np(bc->bc_btid[i], tname); | ||||
} | } | ||||
return (bc); | return (bc); | ||||
err: | err: | ||||
if (fd >= 0) | if (fd >= 0) | ||||
close(fd); | close(fd); | ||||
free(nopt); | free(nopt); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static int | static int | ||||
blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq, | blockif_request(blockif_ctxt_t *bc, blockif_req_t *breq, | ||||
enum blockop op) | enum blockop op) | ||||
{ | { | ||||
int err; | int err; | ||||
err = 0; | err = 0; | ||||
pthread_mutex_lock(&bc->bc_mtx); | pthread_mutex_lock(&bc->bc_mtx); | ||||
if (!TAILQ_EMPTY(&bc->bc_freeq)) { | if (!TAILQ_EMPTY(&bc->bc_freeq)) { | ||||
/* | /* | ||||
* Enqueue and inform the block i/o thread | * Enqueue and inform the block i/o thread | ||||
* that there is work available | * that there is work available | ||||
*/ | */ | ||||
if (blockif_enqueue(bc, breq, op)) | if (blocklocal_enqueue(bc, breq, op)) | ||||
pthread_cond_signal(&bc->bc_cond); | pthread_cond_signal(&bc->bc_cond); | ||||
} else { | } else { | ||||
/* | /* | ||||
* Callers are not allowed to enqueue more than | * Callers are not allowed to enqueue more than | ||||
* the specified blockif queue limit. Return an | * the specified blockif queue limit. Return an | ||||
* error to indicate that the queue length has been | * error to indicate that the queue length has been | ||||
* exceeded. | * exceeded. | ||||
*/ | */ | ||||
err = E2BIG; | err = E2BIG; | ||||
} | } | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
return (err); | return (err); | ||||
} | } | ||||
int | static int | ||||
blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq) | blocklocal_read(blockif_ctxt_t *bc, blockif_req_t *breq) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (blockif_request(bc, breq, BOP_READ)); | return (blockif_request(bc, breq, BOP_READ)); | ||||
} | } | ||||
int | static int | ||||
blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq) | blocklocal_write(blockif_ctxt_t *bc, blockif_req_t *breq) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (blockif_request(bc, breq, BOP_WRITE)); | return (blockif_request(bc, breq, BOP_WRITE)); | ||||
} | } | ||||
int | static int | ||||
blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq) | blocklocal_flush(blockif_ctxt_t *bc, blockif_req_t *breq) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (blockif_request(bc, breq, BOP_FLUSH)); | return (blockif_request(bc, breq, BOP_FLUSH)); | ||||
} | } | ||||
int | static int | ||||
blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq) | blocklocal_delete(blockif_ctxt_t *bc, blockif_req_t *breq) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (blockif_request(bc, breq, BOP_DELETE)); | return (blockif_request(bc, breq, BOP_DELETE)); | ||||
} | } | ||||
int | static int | ||||
blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq) | blocklocal_cancel(blockif_ctxt_t *bc, blockif_req_t *breq) | ||||
{ | { | ||||
struct blockif_elem *be; | struct blockif_elem *be; | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
pthread_mutex_lock(&bc->bc_mtx); | pthread_mutex_lock(&bc->bc_mtx); | ||||
/* | /* | ||||
* Check pending requests. | * Check pending requests. | ||||
*/ | */ | ||||
TAILQ_FOREACH(be, &bc->bc_pendq, be_link) { | TAILQ_FOREACH(be, &bc->bc_pendq, be_link) { | ||||
if (be->be_req == breq) | if (be->be_req == breq) | ||||
break; | break; | ||||
} | } | ||||
if (be != NULL) { | if (be != NULL) { | ||||
/* | /* | ||||
* Found it. | * Found it. | ||||
*/ | */ | ||||
blockif_complete(bc, be); | blocklocal_complete(bc, be); | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Check in-flight requests. | * Check in-flight requests. | ||||
*/ | */ | ||||
Show All 9 Lines | if (be == NULL) { | ||||
return (EINVAL); | return (EINVAL); | ||||
} | } | ||||
/* | /* | ||||
* Interrupt the processing thread to force it return | * Interrupt the processing thread to force it return | ||||
* prematurely via it's normal callback path. | * prematurely via it's normal callback path. | ||||
*/ | */ | ||||
while (be->be_status == BST_BUSY) { | while (be->be_status == BST_BUSY) { | ||||
struct blockif_sig_elem bse, *old_head; | struct blocklocal_sig_elem bse, *old_head; | ||||
pthread_mutex_init(&bse.bse_mtx, NULL); | pthread_mutex_init(&bse.bse_mtx, NULL); | ||||
pthread_cond_init(&bse.bse_cond, NULL); | pthread_cond_init(&bse.bse_cond, NULL); | ||||
bse.bse_pending = 1; | bse.bse_pending = 1; | ||||
do { | do { | ||||
old_head = blockif_bse_head; | old_head = blocklocal_bse_head; | ||||
bse.bse_next = old_head; | bse.bse_next = old_head; | ||||
} while (!atomic_cmpset_ptr((uintptr_t *)&blockif_bse_head, | } while (!atomic_cmpset_ptr((uintptr_t *)&blocklocal_bse_head, | ||||
(uintptr_t)old_head, | (uintptr_t)old_head, | ||||
(uintptr_t)&bse)); | (uintptr_t)&bse)); | ||||
pthread_kill(be->be_tid, SIGCONT); | pthread_kill(be->be_tid, SIGCONT); | ||||
pthread_mutex_lock(&bse.bse_mtx); | pthread_mutex_lock(&bse.bse_mtx); | ||||
while (bse.bse_pending) | while (bse.bse_pending) | ||||
pthread_cond_wait(&bse.bse_cond, &bse.bse_mtx); | pthread_cond_wait(&bse.bse_cond, &bse.bse_mtx); | ||||
pthread_mutex_unlock(&bse.bse_mtx); | pthread_mutex_unlock(&bse.bse_mtx); | ||||
} | } | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
/* | /* | ||||
* The processing thread has been interrupted. Since it's not | * The processing thread has been interrupted. Since it's not | ||||
* clear if the callback has been invoked yet, return EBUSY. | * clear if the callback has been invoked yet, return EBUSY. | ||||
*/ | */ | ||||
return (EBUSY); | return (EBUSY); | ||||
} | } | ||||
int | static int | ||||
blockif_close(struct blockif_ctxt *bc) | blocklocal_close(blockif_ctxt_t *bc) | ||||
{ | { | ||||
void *jval; | void *jval; | ||||
int i; | int i; | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
/* | /* | ||||
* Stop the block i/o thread | * Stop the block i/o thread | ||||
Show All 16 Lines | blocklocal_close(blockif_ctxt_t *bc) | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Return virtual C/H/S values for a given block. Use the algorithm | * Return virtual C/H/S values for a given block. Use the algorithm | ||||
* outlined in the VHD specification to calculate values. | * outlined in the VHD specification to calculate values. | ||||
*/ | */ | ||||
void | static void | ||||
blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s) | blocklocal_chs(blockif_ctxt_t *bc, uint16_t *c, uint8_t *h, uint8_t *s) | ||||
{ | { | ||||
off_t sectors; /* total sectors of the block dev */ | off_t sectors; /* total sectors of the block dev */ | ||||
off_t hcyl; /* cylinders times heads */ | off_t hcyl; /* cylinders times heads */ | ||||
uint16_t secpt; /* sectors per track */ | uint16_t secpt; /* sectors per track */ | ||||
uint8_t heads; | uint8_t heads; | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
Show All 30 Lines | blocklocal_chs(blockif_ctxt_t *bc, uint16_t *c, uint8_t *h, uint8_t *s) | ||||
*c = hcyl / heads; | *c = hcyl / heads; | ||||
*h = heads; | *h = heads; | ||||
*s = secpt; | *s = secpt; | ||||
} | } | ||||
/* | /* | ||||
* Accessors | * Accessors | ||||
*/ | */ | ||||
off_t | static off_t | ||||
blockif_size(struct blockif_ctxt *bc) | blocklocal_size(blockif_ctxt_t *bc) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_size); | return (bc->bc_size); | ||||
} | } | ||||
int | static int | ||||
blockif_sectsz(struct blockif_ctxt *bc) | blocklocal_sectsz(blockif_ctxt_t *bc) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_sectsz); | return (bc->bc_sectsz); | ||||
} | } | ||||
void | static void | ||||
blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off) | blocklocal_psectsz(blockif_ctxt_t *bc, int *size, int *off) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
*size = bc->bc_psectsz; | *size = bc->bc_psectsz; | ||||
*off = bc->bc_psectoff; | *off = bc->bc_psectoff; | ||||
} | } | ||||
int | static int | ||||
blockif_queuesz(struct blockif_ctxt *bc) | blocklocal_queuesz(blockif_ctxt_t *bc) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (BLOCKIF_MAXREQ - 1); | return (BLOCKIF_MAXREQ - 1); | ||||
} | } | ||||
int | static int | ||||
blockif_is_ro(struct blockif_ctxt *bc) | blocklocal_is_ro(blockif_ctxt_t *bc) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_rdonly); | return (bc->bc_rdonly); | ||||
} | } | ||||
int | static int | ||||
blockif_candelete(struct blockif_ctxt *bc) | blocklocal_candelete(blockif_ctxt_t *bc) | ||||
{ | { | ||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_candelete); | return (bc->bc_candelete); | ||||
} | } | ||||
block_backend_t blocklocal_backend = { | |||||
.bb_prefix = "blk-local", | |||||
.bb_name = "file", | |||||
freqlabs: These names seem backwards to me. What do you think about this instead?
```
.bb_name… | |||||
Done Inline ActionsThis you will have to fight with @trociny, since he did not like the name scheme. wjw_digiware.nl: This you will have to fight with @trociny, since he did not like the name `scheme`.
Note that… | |||||
Done Inline ActionsMy point actually was why have both bb_name|prefix and bb_scheme? Why don't just have one param: bb_name, so it is both the thread name and scheme name, whatever. It looks for me perfectly well if one have to specify a device like 'blk-local:/path/to/dev_or_file' trociny: My point actually was why have both bb_name|prefix and bb_scheme? Why don't just have one param… | |||||
Done Inline ActionsI had not noticed that bb_threadinfo isn't used by the blockif abstraction. That being the case, just get rid of it. If a backend needs to store some thread info for its own threads, it can keep it in the private data. With that out of the way I don't mind much whether the remaining member is called bb_name or bb_scheme. freqlabs: I had not noticed that bb_threadinfo isn't used by the blockif abstraction. That being the case… | |||||
.bb_init = blocklocal_init, | |||||
.bb_cleanup = blocklocal_cleanup, | |||||
.bb_open = blocklocal_open, | |||||
.bb_size = blocklocal_size, | |||||
.bb_chs = blocklocal_chs, | |||||
.bb_sectsz = blocklocal_sectsz, | |||||
.bb_psectsz = blocklocal_psectsz, | |||||
.bb_queuesz = blocklocal_queuesz, | |||||
.bb_is_ro = blocklocal_is_ro, | |||||
.bb_candelete = blocklocal_candelete, | |||||
.bb_read = blocklocal_read, | |||||
.bb_write = blocklocal_write, | |||||
.bb_flush = blocklocal_flush, | |||||
.bb_delete = blocklocal_delete, | |||||
.bb_cancel = blocklocal_cancel, | |||||
.bb_close = blocklocal_close, | |||||
}; | |||||
DATA_SET(block_backend_set, blocklocal_backend); |
These names seem backwards to me. What do you think about this instead?
Then name would be the thread name and scheme would be the scheme name for matching the backend.