Changeset View
Standalone View
usr.sbin/bhyve/block_if.c
- This file was copied to usr.sbin/bhyve/block_backends.c.
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | |||||
#include <pthread.h> | #include <pthread.h> | ||||
#include <pthread_np.h> | #include <pthread_np.h> | ||||
#include <signal.h> | #include <signal.h> | ||||
#include <sysexits.h> | #include <sysexits.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <machine/atomic.h> | #include <machine/atomic.h> | ||||
#include <sys/linker_set.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 | SET_DECLARE(block_backend_set, block_backend_t); | ||||
#define BLOCKIF_NUMTHR 8 | static pthread_once_t locblk_once = PTHREAD_ONCE_INIT; | ||||
#define BLOCKIF_MAXREQ (BLOCKIF_RING_MAX + BLOCKIF_NUMTHR) | |||||
enum blockop { | struct locblk_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 locblk_sig_elem *bse_next; | ||||
}; | }; | ||||
static struct blockif_sig_elem *blockif_bse_head; | static struct locblk_sig_elem *locblk_bse_head; | ||||
static int | static int | ||||
blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq, | locblk_enqueue(struct locblk_ctxt *bc, struct blockif_req *breq, | ||||
enum blockop op) | enum blockop op) | ||||
{ | { | ||||
struct blockif_elem *be, *tbe; | struct locblk_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); | ||||
assert(be->be_status == BST_FREE); | assert(be->be_status == BST_FREE); | ||||
TAILQ_REMOVE(&bc->bc_freeq, be, be_link); | TAILQ_REMOVE(&bc->bc_freeq, be, be_link); | ||||
be->be_req = breq; | be->be_req = breq; | ||||
Show All 24 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) | locblk_dequeue(struct locblk_ctxt *bc, pthread_t t, struct locblk_elem **bep) | ||||
{ | { | ||||
struct blockif_elem *be; | struct locblk_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) | locblk_complete(struct locblk_ctxt *bc, struct locblk_elem *be) | ||||
{ | { | ||||
struct blockif_elem *tbe; | struct locblk_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) | locblk_proc(struct locblk_ctxt *bc, struct locblk_elem *be, uint8_t *buf) | ||||
{ | { | ||||
struct blockif_req *br; | struct blockif_req *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) | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | locblk_proc(struct locblk_ctxt *bc, struct locblk_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) | locblk_thr(void *arg) | ||||
{ | { | ||||
struct blockif_ctxt *bc; | struct locblk_ctxt *bc; | ||||
struct blockif_elem *be; | struct locblk_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 (locblk_dequeue(bc, t, &be)) { | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
blockif_proc(bc, be, buf); | locblk_proc(bc, be, buf); | ||||
pthread_mutex_lock(&bc->bc_mtx); | pthread_mutex_lock(&bc->bc_mtx); | ||||
blockif_complete(bc, be); | locblk_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) | locblk_sigcont_handler(int signal, enum ev_type type, void *arg) | ||||
{ | { | ||||
struct blockif_sig_elem *bse; | struct locblk_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 = locblk_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 *)&locblk_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 int | |||||
locblk_request(struct locblk_ctxt *bc, struct blockif_req *breq, | |||||
enum blockop op) | |||||
{ | |||||
int err = 0; | |||||
pthread_mutex_lock(&bc->bc_mtx); | |||||
if (!TAILQ_EMPTY(&bc->bc_freeq)) { | |||||
/* | |||||
* Enqueue and inform the block i/o thread | |||||
* that there is work available | |||||
*/ | |||||
if (locblk_enqueue(bc, breq, op)) | |||||
pthread_cond_signal(&bc->bc_cond); | |||||
} else { | |||||
/* | |||||
* Callers are not allowed to enqueue more than | |||||
* the specified blockif queue limit. Return an | |||||
* error to indicate that the queue length has been | |||||
* exceeded. | |||||
*/ | |||||
err = E2BIG; | |||||
} | |||||
pthread_mutex_unlock(&bc->bc_mtx); | |||||
return (err); | |||||
} | |||||
/* | |||||
* Routines for access blockdevices on the local system. | |||||
* be it either a file, or a block device | |||||
*/ | |||||
static void | static void | ||||
blockif_init(void) | locblk_init(void) | ||||
{ | { | ||||
mevent_add(SIGCONT, EVF_SIGNAL, blockif_sigcont_handler, NULL); | mevent_add(SIGCONT, EVF_SIGNAL, locblk_sigcont_handler, NULL); | ||||
(void) signal(SIGCONT, SIG_IGN); | (void) signal(SIGCONT, SIG_IGN); | ||||
} | } | ||||
struct blockif_ctxt * | static void | ||||
blockif_open(const char *optstr, const char *ident) | locblk_cleanup(block_backend_t **be) | ||||
{ /* empty block | |||||
* currently no cleanup required. | |||||
*/ | |||||
} | |||||
static block_backend_t locblk_backend; | |||||
static int | |||||
locblk_open(block_backend_t **be, const char *optstr, const char *pci_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; | struct locblk_ctxt *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 | ||||
freqlabs: Indentation looks different than in the rest of the file. | |||||
pthread_once(&blockif_once, blockif_init); | pthread_once(&locblk_once, locblk_init); | ||||
fd = -1; | fd = -1; | ||||
ssopt = 0; | ssopt = 0; | ||||
nocache = 0; | nocache = 0; | ||||
sync = 0; | sync = 0; | ||||
ro = 0; | ro = 0; | ||||
/* | /* | ||||
Show All 22 Lines | #endif | ||||
} | } | ||||
extra = 0; | extra = 0; | ||||
if (nocache) | if (nocache) | ||||
extra |= O_DIRECT; | extra |= O_DIRECT; | ||||
if (sync) | if (sync) | ||||
extra |= O_SYNC; | extra |= O_SYNC; | ||||
/* | |||||
* device and option parsing completed correctly so set the | |||||
* backend return value to this backend. | |||||
* Anything that returns an error below is due to errors in | |||||
* handling of the device. But this is the correct backend. | |||||
*/ | |||||
*be = &locblk_backend; | |||||
fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra); | fd = open(nopt, (ro ? O_RDONLY : O_RDWR) | extra); | ||||
if (fd < 0 && !ro) { | if (fd < 0 && !ro) { | ||||
/* Attempt a r/w fail with a r/o open */ | /* Attempt a r/w fail with a r/o open */ | ||||
fd = open(nopt, O_RDONLY | extra); | fd = open(nopt, O_RDONLY | extra); | ||||
ro = 1; | ro = 1; | ||||
} | } | ||||
if (fd < 0) { | if (fd < 0) { | ||||
warn("Could not open backing file: %s", nopt); | warn("blk-local: Could not open backing file: %s", nopt); | ||||
goto err; | goto err; | ||||
} | } | ||||
if (fstat(fd, &sbuf) < 0) { | if (fstat(fd, &sbuf) < 0) { | ||||
warn("Could not stat backing file %s", nopt); | warn("Could not stat backing file %s", nopt); | ||||
goto err; | goto err; | ||||
} | } | ||||
Done Inline ActionsIf you null out the ':' before moving the pointer forward then you can use strcmp below to ensure we get an exact match for the backend name. *optrest = '\0'; optrest++; freqlabs: If you null out the ':' before moving the pointer forward then you can use strcmp below to… | |||||
#ifndef WITHOUT_CAPSICUM | #ifndef WITHOUT_CAPSICUM | ||||
cap_rights_init(&rights, CAP_FSYNC, CAP_IOCTL, CAP_READ, CAP_SEEK, | cap_rights_init(&rights, CAP_FSYNC, CAP_IOCTL, CAP_READ, CAP_SEEK, | ||||
CAP_WRITE); | CAP_WRITE); | ||||
if (ro) | if (ro) | ||||
cap_rights_clear(&rights, CAP_FSYNC, CAP_WRITE); | cap_rights_clear(&rights, CAP_FSYNC, CAP_WRITE); | ||||
if (caph_rights_limit(fd, &rights) == -1) | if (caph_rights_limit(fd, &rights) == -1) | ||||
errx(EX_OSERR, "Unable to apply rights for sandbox"); | errx(EX_OSERR, "Unable to apply rights for sandbox"); | ||||
#endif | #endif | ||||
/* | /* | ||||
* Deal with raw devices | * Deal with raw devices | ||||
*/ | */ | ||||
size = sbuf.st_size; | size = sbuf.st_size; | ||||
sectsz = DEV_BSIZE; | sectsz = DEV_BSIZE; | ||||
psectsz = psectoff = 0; | psectsz = psectoff = 0; | ||||
candelete = geom = 0; | candelete = geom = 0; | ||||
if (S_ISCHR(sbuf.st_mode)) { | if (S_ISCHR(sbuf.st_mode)) { | ||||
if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 || | if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 || | ||||
ioctl(fd, DIOCGSECTORSIZE, §sz)) { | ioctl(fd, DIOCGSECTORSIZE, §sz)) { | ||||
perror("Could not fetch dev blk/sector size"); | perror("Could not fetch dev blk/sector size"); | ||||
goto err; | goto err; | ||||
Show All 25 Lines | if (ssopt != 0) { | ||||
} | } | ||||
/* | /* | ||||
* Some backend drivers (e.g. cd0, ada0) require that the I/O | * Some backend drivers (e.g. cd0, ada0) require that the I/O | ||||
* size be a multiple of the device's sector size. | * size be a multiple of the device's sector size. | ||||
* | * | ||||
* Validate that the emulated sector size complies with this | * Validate that the emulated sector size complies with this | ||||
* requirement. | * requirement. | ||||
*/ | */ | ||||
if (S_ISCHR(sbuf.st_mode)) { | if (S_ISCHR(sbuf.st_mode)) { | ||||
if (ssopt < sectsz || (ssopt % sectsz) != 0) { | if (ssopt < sectsz || (ssopt % sectsz) != 0) { | ||||
EPRINTLN("Sector size %d incompatible " | EPRINTLN("Sector size %d incompatible " | ||||
"with underlying device sector size %d", | "with underlying device sector size %d", | ||||
ssopt, sectsz); | ssopt, sectsz); | ||||
Done Inline ActionsThis comment is outdated. freqlabs: This comment is outdated. | |||||
goto err; | goto err; | ||||
} | } | ||||
} | } | ||||
sectsz = ssopt; | sectsz = ssopt; | ||||
psectsz = pssopt; | psectsz = pssopt; | ||||
Done Inline ActionsWe can break if open fails. There should be only one backend with the given name. freqlabs: We can `break` if open fails. There should be only one backend with the given name. | |||||
psectoff = 0; | psectoff = 0; | ||||
} | } | ||||
bc = calloc(1, sizeof(struct blockif_ctxt)); | bc = calloc(1, sizeof(struct locblk_ctxt)); | ||||
if (bc == NULL) { | if (bc == NULL) { | ||||
perror("calloc"); | perror("calloc"); | ||||
goto err; | goto err; | ||||
} | } | ||||
/* And update the backend descriptor for this */ | |||||
(*be)->bc = (struct blockif_ctxt*)bc; | |||||
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); | ||||
Done Inline ActionsNote, bb_open may return NULL here and you will be dereferencing NULL trociny: Note, bb_open may return NULL here and you will be dereferencing NULL | |||||
Done Inline Actionsgood call wjw_digiware.nl: good call
| |||||
bc->bc_isgeom = geom; | bc->bc_isgeom = geom; | ||||
bc->bc_candelete = candelete; | bc->bc_candelete = candelete; | ||||
bc->bc_rdonly = ro; | bc->bc_rdonly = ro; | ||||
bc->bc_size = size; | bc->bc_size = size; | ||||
bc->bc_sectsz = sectsz; | bc->bc_sectsz = sectsz; | ||||
bc->bc_psectsz = psectsz; | bc->bc_psectsz = psectsz; | ||||
bc->bc_psectoff = psectoff; | bc->bc_psectoff = psectoff; | ||||
pthread_mutex_init(&bc->bc_mtx, NULL); | pthread_mutex_init(&bc->bc_mtx, NULL); | ||||
pthread_cond_init(&bc->bc_cond, NULL); | pthread_cond_init(&bc->bc_cond, NULL); | ||||
TAILQ_INIT(&bc->bc_freeq); | TAILQ_INIT(&bc->bc_freeq); | ||||
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, locblk_thr, bc); | ||||
snprintf(tname, sizeof(tname), "blk-%s-%d", ident, i); | snprintf(tname, sizeof(tname), "blk-%s-%d", pci_ident, i); | ||||
pthread_set_name_np(bc->bc_btid[i], tname); | pthread_set_name_np(bc->bc_btid[i], tname); | ||||
} | } | ||||
return (bc); | return (0); | ||||
err: | err: | ||||
if (fd >= 0) | if (fd >= 0) | ||||
close(fd); | close(fd); | ||||
free(nopt); | free(nopt); | ||||
return (NULL); | return (-1); | ||||
} | } | ||||
Done Inline ActionsWhat is a point of passing the scheme prefix to the driver? The first step that it would need to do is to trim it. Why not instead always pass to the bb_open optstr without scheme. It is easy to trim it from the optstr that has it -- just increase optstr by the prefix length. trociny: What is a point of passing the scheme prefix to the driver? The first step that it would need… | |||||
Done Inline ActionsYeah, that is the other way around ..... wjw_digiware.nl: Yeah, that is the other way around .....
Makes it easier here, but a bit more code in the `FOR… | |||||
Done Inline ActionsOtherwise you would have "a bit mode code" in every driver to parse the prefix. trociny: Otherwise you would have "a bit mode code" in every driver to parse the prefix. | |||||
static int | |||||
blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq, | |||||
enum blockop op) | |||||
{ | |||||
int err; | |||||
err = 0; | |||||
pthread_mutex_lock(&bc->bc_mtx); | |||||
if (!TAILQ_EMPTY(&bc->bc_freeq)) { | |||||
/* | |||||
* Enqueue and inform the block i/o thread | |||||
* that there is work available | |||||
*/ | |||||
if (blockif_enqueue(bc, breq, op)) | |||||
pthread_cond_signal(&bc->bc_cond); | |||||
} else { | |||||
/* | |||||
* Callers are not allowed to enqueue more than | |||||
* the specified blockif queue limit. Return an | |||||
* error to indicate that the queue length has been | |||||
* exceeded. | |||||
*/ | |||||
err = E2BIG; | |||||
} | |||||
pthread_mutex_unlock(&bc->bc_mtx); | |||||
return (err); | |||||
} | |||||
int | int | ||||
Done Inline ActionsBTW, in my version I just check optstr for a prefix ending with semicolon (e.g. 'rbd:'), and then use this prefix to find the driver. trociny: BTW, in my version I just check `optstr` for a prefix ending with semicolon (e.g. 'rbd:'), and… | |||||
Done Inline ActionsI think it is beter actually to leave this up to the backends themselfs, this creates more flexibility. And would for example allow for multiple /........ backends. Where if the basic local backend refuses, there could be other backends working on this optstr that can handle the request. wjw_digiware.nl: @trociny
I think it is beter actually to leave this up to the backends themselfs, this… | |||||
Done Inline ActionsI can hardly imagine how backends are going to detect if its "their" device, if only they would not rely on some prefix name. And if they would why not to do this in generic way instead of implementing this in every driver? So, bhyve would look for provided disk name, and if it had "foo:" prefix, it would assume that the driver is "foo", and would try to open the device using this driver (if it had been successfully loaded. trociny: I can hardly imagine how backends are going to detect if its "their" device, if only they would… | |||||
Done Inline ActionsI agree with scheme: suggest, ie file://, rbd://, nbd://, etc. with a fallback to the file backend if no scheme is given. Each backend can register its scheme name and we can directly pick the right one with no ambiguity. freqlabs: I agree with `scheme:` suggest, ie `file://`, `rbd://`, `nbd://`, etc. with a fallback to the… | |||||
Done Inline ActionsOops, I meant to remove // from the rest of those before I submit. Anything after the : would be passed to the backend. freqlabs: Oops, I meant to remove `//` from the rest of those before I submit. Anything after the `:`… | |||||
Done Inline ActionsRight, and perhaps keep the /...... for legacy defaulting to local store. wjw_digiware.nl: Right, and perhaps keep the `/......` for legacy defaulting to local store. | |||||
Done Inline ActionsI guess it would be possible to add a scheme descriptor to the backend struct, and only go with that. wjw_digiware.nl: @trociny
I guess it would be possible to add a scheme descriptor to the backend struct, and… | |||||
blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq) | locblk_read(block_backend_t *be, struct blockif_req *breq) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (blockif_request(bc, breq, BOP_READ)); | return (locblk_request(bc, breq, BOP_READ)); | ||||
} | } | ||||
int | int | ||||
blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq) | locblk_write(block_backend_t *be, struct blockif_req *breq) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
Done Inline Actions!= NULL please to avoid confusion about the type. freqlabs: `!= NULL` please to avoid confusion about the type. | |||||
return (blockif_request(bc, breq, BOP_WRITE)); | return (locblk_request(bc, breq, BOP_WRITE)); | ||||
} | } | ||||
int | int | ||||
blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq) | locblk_flush(block_backend_t *be, struct blockif_req *breq) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (blockif_request(bc, breq, BOP_FLUSH)); | return (locblk_request(bc, breq, BOP_FLUSH)); | ||||
} | } | ||||
int | int | ||||
blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq) | locblk_delete(block_backend_t *be, struct blockif_req *breq) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (blockif_request(bc, breq, BOP_DELETE)); | return (locblk_request(bc, breq, BOP_DELETE)); | ||||
} | } | ||||
int | int | ||||
blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq) | locblk_cancel(block_backend_t *be, struct blockif_req *breq) | ||||
{ | { | ||||
struct blockif_elem *be; | struct locblk_elem *belem; | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
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(belem, &bc->bc_pendq, be_link) { | ||||
if (be->be_req == breq) | if (belem->be_req == breq) | ||||
break; | break; | ||||
} | } | ||||
if (be != NULL) { | if (be != NULL) { | ||||
/* | /* | ||||
* Found it. | * Found it. | ||||
*/ | */ | ||||
blockif_complete(bc, be); | locblk_complete(bc, belem); | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Check in-flight requests. | * Check in-flight requests. | ||||
*/ | */ | ||||
TAILQ_FOREACH(be, &bc->bc_busyq, be_link) { | TAILQ_FOREACH(belem, &bc->bc_busyq, be_link) { | ||||
if (be->be_req == breq) | if (belem->be_req == breq) | ||||
break; | break; | ||||
} | } | ||||
if (be == NULL) { | if (belem == NULL) { | ||||
/* | /* | ||||
* Didn't find it. | * Didn't find it. | ||||
*/ | */ | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
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 (belem->be_status == BST_BUSY) { | ||||
struct blockif_sig_elem bse, *old_head; | struct locblk_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 = locblk_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 *)&locblk_bse_head, | ||||
(uintptr_t)old_head, | (uintptr_t)old_head, | ||||
(uintptr_t)&bse)); | (uintptr_t)&bse)); | ||||
pthread_kill(be->be_tid, SIGCONT); | pthread_kill(belem->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 | int | ||||
blockif_close(struct blockif_ctxt *bc) | locblk_close(block_backend_t *be) | ||||
{ | { | ||||
void *jval; | void *jval; | ||||
int i; | int i; | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
/* | /* | ||||
* Stop the block i/o thread | * Stop the block i/o thread | ||||
*/ | */ | ||||
pthread_mutex_lock(&bc->bc_mtx); | pthread_mutex_lock(&bc->bc_mtx); | ||||
bc->bc_closing = 1; | bc->bc_closing = 1; | ||||
pthread_mutex_unlock(&bc->bc_mtx); | pthread_mutex_unlock(&bc->bc_mtx); | ||||
pthread_cond_broadcast(&bc->bc_cond); | pthread_cond_broadcast(&bc->bc_cond); | ||||
for (i = 0; i < BLOCKIF_NUMTHR; i++) | for (i = 0; i < BLOCKIF_NUMTHR; i++) | ||||
pthread_join(bc->bc_btid[i], &jval); | pthread_join(bc->bc_btid[i], &jval); | ||||
/* XXX Cancel queued i/o's ??? */ | /* XXX Cancel queued i/o's ??? */ | ||||
/* | /* | ||||
* Release resources | * Release resources | ||||
*/ | */ | ||||
bc->bc_magic = 0; | bc->bc_magic = 0; | ||||
close(bc->bc_fd); | close(bc->bc_fd); | ||||
free(bc); | free(bc); | ||||
return (0); | return (0); | ||||
Done Inline ActionsThis isn't the only place I see this, but I'll just point it out here: return (bc->be->close(bc)); freqlabs: This isn't the only place I see this, but I'll just point it out here:
Minor style nit… | |||||
Done Inline ActionsI have not fixed this in files I have not touched. wjw_digiware.nl: I have not fixed this in files I have not touched. | |||||
} | } | ||||
/* | /* | ||||
* 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 | void | ||||
blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s) | locblk_chs(block_backend_t *be, 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; | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
sectors = bc->bc_size / bc->bc_sectsz; | sectors = bc->bc_size / bc->bc_sectsz; | ||||
/* Clamp the size to the largest possible with CHS */ | /* Clamp the size to the largest possible with CHS */ | ||||
if (sectors > 65535UL*16*255) | if (sectors > 65535UL*16*255) | ||||
sectors = 65535UL*16*255; | sectors = 65535UL*16*255; | ||||
Show All 25 Lines | locblk_chs(block_backend_t *be, uint16_t *c, uint8_t *h, uint8_t *s) | ||||
*h = heads; | *h = heads; | ||||
*s = secpt; | *s = secpt; | ||||
} | } | ||||
/* | /* | ||||
* Accessors | * Accessors | ||||
*/ | */ | ||||
off_t | off_t | ||||
blockif_size(struct blockif_ctxt *bc) | locblk_size(block_backend_t *be) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_size); | return (bc->bc_size); | ||||
} | } | ||||
int | int | ||||
blockif_sectsz(struct blockif_ctxt *bc) | locblk_sectsz(block_backend_t *be) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_sectsz); | return (bc->bc_sectsz); | ||||
} | } | ||||
void | void | ||||
blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off) | locblk_psectsz(block_backend_t *be, int *size, int *off) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
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 | int | ||||
blockif_queuesz(struct blockif_ctxt *bc) | locblk_queuesz(block_backend_t *be) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (BLOCKIF_MAXREQ - 1); | return (BLOCKIF_MAXREQ - 1); | ||||
} | } | ||||
int | int | ||||
blockif_is_ro(struct blockif_ctxt *bc) | locblk_is_ro(block_backend_t *be) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_rdonly); | return (bc->bc_rdonly); | ||||
} | } | ||||
int | int | ||||
blockif_candelete(struct blockif_ctxt *bc) | locblk_candelete(block_backend_t *be) | ||||
{ | { | ||||
locblk_ctxt_t *bc = (locblk_ctxt_t *)(be->bc); | |||||
assert(bc->bc_magic == BLOCKIF_SIG); | assert(bc->bc_magic == BLOCKIF_SIG); | ||||
return (bc->bc_candelete); | return (bc->bc_candelete); | ||||
} | } | ||||
static | |||||
block_backend_t locblk_backend = { | |||||
.prefix = "blk-local", | |||||
.init = locblk_init, | |||||
.cleanup = locblk_cleanup, | |||||
.open = locblk_open, | |||||
.size = locblk_size, | |||||
.chs = locblk_chs, | |||||
.sectsz = locblk_sectsz, | |||||
.psectsz = locblk_psectsz, | |||||
.queuesz = locblk_queuesz, | |||||
.is_ro = locblk_is_ro, | |||||
.candelete = locblk_candelete, | |||||
.read = locblk_read, | |||||
.write = locblk_write, | |||||
.flush = locblk_flush, | |||||
.delete = locblk_delete, | |||||
.cancel = locblk_cancel, | |||||
.close = locblk_close, | |||||
.priv_size = 0, | |||||
}; | |||||
DATA_SET(block_backend_set, locblk_backend); |
Indentation looks different than in the rest of the file.