dm: block: clean up assert

This patch is to clean up assert for block interface.
'magic' is removed from block structure, as the user should make sure
the block device is created and not closed when access to it.

Tracked-On: #3252
Signed-off-by: Conghui Chen <conghui.chen@intel.com>
Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
This commit is contained in:
Conghui Chen 2019-06-18 19:25:25 +08:00 committed by wenlingz
parent 13228d910f
commit 4145b8af6e

View File

@ -33,7 +33,6 @@
#include <linux/falloc.h> #include <linux/falloc.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <errno.h> #include <errno.h>
#include <assert.h>
#include <err.h> #include <err.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -98,7 +97,6 @@ struct blockif_elem {
}; };
struct blockif_ctxt { struct blockif_ctxt {
int magic;
int fd; int fd;
int isblk; int isblk;
int candiscard; int candiscard;
@ -151,7 +149,6 @@ blockif_flush_cache(struct blockif_ctxt *bc)
int err; int err;
err = 0; err = 0;
assert(bc != NULL);
if (!bc->wce) { if (!bc->wce) {
if (fsync(bc->fd)) if (fsync(bc->fd))
err = errno; err = errno;
@ -168,8 +165,10 @@ blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
int i; int i;
be = TAILQ_FIRST(&bc->freeq); be = TAILQ_FIRST(&bc->freeq);
assert(be != NULL); if (be == NULL || be->status != BST_FREE) {
assert(be->status == BST_FREE); WPRINTF(("%s: failed to get element from freeq\n", __func__));
return 0;
}
TAILQ_REMOVE(&bc->freeq, be, link); TAILQ_REMOVE(&bc->freeq, be, link);
be->req = breq; be->req = breq;
be->op = op; be->op = op;
@ -212,7 +211,6 @@ blockif_dequeue(struct blockif_ctxt *bc, pthread_t t, struct blockif_elem **bep)
TAILQ_FOREACH(be, &bc->pendq, link) { TAILQ_FOREACH(be, &bc->pendq, link) {
if (be->status == BST_PEND) if (be->status == BST_PEND)
break; break;
assert(be->status == BST_BLOCK);
} }
if (be == NULL) if (be == NULL)
return 0; return 0;
@ -721,7 +719,6 @@ blockif_open(const char *optstr, const char *ident)
bc->sub_file_start_lba = 0; bc->sub_file_start_lba = 0;
} }
bc->magic = BLOCKIF_SIG;
bc->fd = fd; bc->fd = fd;
bc->isblk = S_ISBLK(sbuf.st_mode); bc->isblk = S_ISBLK(sbuf.st_mode);
bc->candiscard = candiscard; bc->candiscard = candiscard;
@ -809,28 +806,24 @@ blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
int int
blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq) blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
{ {
assert(bc->magic == BLOCKIF_SIG);
return blockif_request(bc, breq, BOP_READ); return blockif_request(bc, breq, BOP_READ);
} }
int int
blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq) blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
{ {
assert(bc->magic == BLOCKIF_SIG);
return blockif_request(bc, breq, BOP_WRITE); return blockif_request(bc, breq, BOP_WRITE);
} }
int int
blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq) blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
{ {
assert(bc->magic == BLOCKIF_SIG);
return blockif_request(bc, breq, BOP_FLUSH); return blockif_request(bc, breq, BOP_FLUSH);
} }
int int
blockif_discard(struct blockif_ctxt *bc, struct blockif_req *breq) blockif_discard(struct blockif_ctxt *bc, struct blockif_req *breq)
{ {
assert(bc->magic == BLOCKIF_SIG);
return blockif_request(bc, breq, BOP_DISCARD); return blockif_request(bc, breq, BOP_DISCARD);
} }
@ -839,8 +832,6 @@ blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
{ {
struct blockif_elem *be; struct blockif_elem *be;
assert(bc->magic == BLOCKIF_SIG);
pthread_mutex_lock(&bc->mtx); pthread_mutex_lock(&bc->mtx);
/* /*
* Check pending requests. * Check pending requests.
@ -917,7 +908,6 @@ blockif_close(struct blockif_ctxt *bc)
void *jval; void *jval;
int i; int i;
assert(bc->magic == BLOCKIF_SIG);
sub_file_unlock(bc); sub_file_unlock(bc);
/* /*
@ -936,7 +926,6 @@ blockif_close(struct blockif_ctxt *bc)
/* /*
* Release resources * Release resources
*/ */
bc->magic = 0;
close(bc->fd); close(bc->fd);
free(bc); free(bc);
@ -955,8 +944,6 @@ blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
uint16_t secpt; /* sectors per track */ uint16_t secpt; /* sectors per track */
uint8_t heads; uint8_t heads;
assert(bc->magic == BLOCKIF_SIG);
sectors = bc->size / bc->sectsz; sectors = bc->size / bc->sectsz;
/* Clamp the size to the largest possible with CHS */ /* Clamp the size to the largest possible with CHS */
@ -998,21 +985,18 @@ blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
off_t off_t
blockif_size(struct blockif_ctxt *bc) blockif_size(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->size; return bc->size;
} }
int int
blockif_sectsz(struct blockif_ctxt *bc) blockif_sectsz(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->sectsz; return bc->sectsz;
} }
void void
blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off) blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off)
{ {
assert(bc->magic == BLOCKIF_SIG);
*size = bc->psectsz; *size = bc->psectsz;
*off = bc->psectoff; *off = bc->psectoff;
} }
@ -1020,56 +1004,48 @@ blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off)
int int
blockif_queuesz(struct blockif_ctxt *bc) blockif_queuesz(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return (BLOCKIF_MAXREQ - 1); return (BLOCKIF_MAXREQ - 1);
} }
int int
blockif_is_ro(struct blockif_ctxt *bc) blockif_is_ro(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->rdonly; return bc->rdonly;
} }
int int
blockif_candiscard(struct blockif_ctxt *bc) blockif_candiscard(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->candiscard; return bc->candiscard;
} }
int int
blockif_max_discard_sectors(struct blockif_ctxt *bc) blockif_max_discard_sectors(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->max_discard_sectors; return bc->max_discard_sectors;
} }
int int
blockif_max_discard_seg(struct blockif_ctxt *bc) blockif_max_discard_seg(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->max_discard_seg; return bc->max_discard_seg;
} }
int int
blockif_discard_sector_alignment(struct blockif_ctxt *bc) blockif_discard_sector_alignment(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->discard_sector_alignment; return bc->discard_sector_alignment;
} }
uint8_t uint8_t
blockif_get_wce(struct blockif_ctxt *bc) blockif_get_wce(struct blockif_ctxt *bc)
{ {
assert(bc->magic == BLOCKIF_SIG);
return bc->wce; return bc->wce;
} }
void void
blockif_set_wce(struct blockif_ctxt *bc, uint8_t wce) blockif_set_wce(struct blockif_ctxt *bc, uint8_t wce)
{ {
assert(bc->magic == BLOCKIF_SIG);
bc->wce = wce; bc->wce = wce;
} }
@ -1079,7 +1055,6 @@ blockif_flush_all(struct blockif_ctxt *bc)
int err; int err;
err=0; err=0;
assert(bc->magic == BLOCKIF_SIG);
if (fsync(bc->fd)) if (fsync(bc->fd))
err = errno; err = errno;
return err; return err;