Skip to content

Commit 4145b8a

Browse files
conghuic23wenlingz
authored andcommitted
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>
1 parent 13228d9 commit 4145b8a

File tree

1 file changed

+4
-29
lines changed

1 file changed

+4
-29
lines changed

devicemodel/hw/block_if.c

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <linux/falloc.h>
3434
#include <linux/fs.h>
3535
#include <errno.h>
36-
#include <assert.h>
3736
#include <err.h>
3837
#include <fcntl.h>
3938
#include <stdio.h>
@@ -98,7 +97,6 @@ struct blockif_elem {
9897
};
9998

10099
struct blockif_ctxt {
101-
int magic;
102100
int fd;
103101
int isblk;
104102
int candiscard;
@@ -151,7 +149,6 @@ blockif_flush_cache(struct blockif_ctxt *bc)
151149
int err;
152150

153151
err = 0;
154-
assert(bc != NULL);
155152
if (!bc->wce) {
156153
if (fsync(bc->fd))
157154
err = errno;
@@ -168,8 +165,10 @@ blockif_enqueue(struct blockif_ctxt *bc, struct blockif_req *breq,
168165
int i;
169166

170167
be = TAILQ_FIRST(&bc->freeq);
171-
assert(be != NULL);
172-
assert(be->status == BST_FREE);
168+
if (be == NULL || be->status != BST_FREE) {
169+
WPRINTF(("%s: failed to get element from freeq\n", __func__));
170+
return 0;
171+
}
173172
TAILQ_REMOVE(&bc->freeq, be, link);
174173
be->req = breq;
175174
be->op = op;
@@ -212,7 +211,6 @@ blockif_dequeue(struct blockif_ctxt *bc, pthread_t t, struct blockif_elem **bep)
212211
TAILQ_FOREACH(be, &bc->pendq, link) {
213212
if (be->status == BST_PEND)
214213
break;
215-
assert(be->status == BST_BLOCK);
216214
}
217215
if (be == NULL)
218216
return 0;
@@ -721,7 +719,6 @@ blockif_open(const char *optstr, const char *ident)
721719
bc->sub_file_start_lba = 0;
722720
}
723721

724-
bc->magic = BLOCKIF_SIG;
725722
bc->fd = fd;
726723
bc->isblk = S_ISBLK(sbuf.st_mode);
727724
bc->candiscard = candiscard;
@@ -809,28 +806,24 @@ blockif_request(struct blockif_ctxt *bc, struct blockif_req *breq,
809806
int
810807
blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq)
811808
{
812-
assert(bc->magic == BLOCKIF_SIG);
813809
return blockif_request(bc, breq, BOP_READ);
814810
}
815811

816812
int
817813
blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq)
818814
{
819-
assert(bc->magic == BLOCKIF_SIG);
820815
return blockif_request(bc, breq, BOP_WRITE);
821816
}
822817

823818
int
824819
blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq)
825820
{
826-
assert(bc->magic == BLOCKIF_SIG);
827821
return blockif_request(bc, breq, BOP_FLUSH);
828822
}
829823

830824
int
831825
blockif_discard(struct blockif_ctxt *bc, struct blockif_req *breq)
832826
{
833-
assert(bc->magic == BLOCKIF_SIG);
834827
return blockif_request(bc, breq, BOP_DISCARD);
835828
}
836829

@@ -839,8 +832,6 @@ blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq)
839832
{
840833
struct blockif_elem *be;
841834

842-
assert(bc->magic == BLOCKIF_SIG);
843-
844835
pthread_mutex_lock(&bc->mtx);
845836
/*
846837
* Check pending requests.
@@ -917,7 +908,6 @@ blockif_close(struct blockif_ctxt *bc)
917908
void *jval;
918909
int i;
919910

920-
assert(bc->magic == BLOCKIF_SIG);
921911
sub_file_unlock(bc);
922912

923913
/*
@@ -936,7 +926,6 @@ blockif_close(struct blockif_ctxt *bc)
936926
/*
937927
* Release resources
938928
*/
939-
bc->magic = 0;
940929
close(bc->fd);
941930
free(bc);
942931

@@ -955,8 +944,6 @@ blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
955944
uint16_t secpt; /* sectors per track */
956945
uint8_t heads;
957946

958-
assert(bc->magic == BLOCKIF_SIG);
959-
960947
sectors = bc->size / bc->sectsz;
961948

962949
/* Clamp the size to the largest possible with CHS */
@@ -998,78 +985,67 @@ blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, uint8_t *s)
998985
off_t
999986
blockif_size(struct blockif_ctxt *bc)
1000987
{
1001-
assert(bc->magic == BLOCKIF_SIG);
1002988
return bc->size;
1003989
}
1004990

1005991
int
1006992
blockif_sectsz(struct blockif_ctxt *bc)
1007993
{
1008-
assert(bc->magic == BLOCKIF_SIG);
1009994
return bc->sectsz;
1010995
}
1011996

1012997
void
1013998
blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off)
1014999
{
1015-
assert(bc->magic == BLOCKIF_SIG);
10161000
*size = bc->psectsz;
10171001
*off = bc->psectoff;
10181002
}
10191003

10201004
int
10211005
blockif_queuesz(struct blockif_ctxt *bc)
10221006
{
1023-
assert(bc->magic == BLOCKIF_SIG);
10241007
return (BLOCKIF_MAXREQ - 1);
10251008
}
10261009

10271010
int
10281011
blockif_is_ro(struct blockif_ctxt *bc)
10291012
{
1030-
assert(bc->magic == BLOCKIF_SIG);
10311013
return bc->rdonly;
10321014
}
10331015

10341016
int
10351017
blockif_candiscard(struct blockif_ctxt *bc)
10361018
{
1037-
assert(bc->magic == BLOCKIF_SIG);
10381019
return bc->candiscard;
10391020
}
10401021

10411022
int
10421023
blockif_max_discard_sectors(struct blockif_ctxt *bc)
10431024
{
1044-
assert(bc->magic == BLOCKIF_SIG);
10451025
return bc->max_discard_sectors;
10461026
}
10471027

10481028
int
10491029
blockif_max_discard_seg(struct blockif_ctxt *bc)
10501030
{
1051-
assert(bc->magic == BLOCKIF_SIG);
10521031
return bc->max_discard_seg;
10531032
}
10541033

10551034
int
10561035
blockif_discard_sector_alignment(struct blockif_ctxt *bc)
10571036
{
1058-
assert(bc->magic == BLOCKIF_SIG);
10591037
return bc->discard_sector_alignment;
10601038
}
10611039

10621040
uint8_t
10631041
blockif_get_wce(struct blockif_ctxt *bc)
10641042
{
1065-
assert(bc->magic == BLOCKIF_SIG);
10661043
return bc->wce;
10671044
}
10681045

10691046
void
10701047
blockif_set_wce(struct blockif_ctxt *bc, uint8_t wce)
10711048
{
1072-
assert(bc->magic == BLOCKIF_SIG);
10731049
bc->wce = wce;
10741050
}
10751051

@@ -1079,7 +1055,6 @@ blockif_flush_all(struct blockif_ctxt *bc)
10791055
int err;
10801056

10811057
err=0;
1082-
assert(bc->magic == BLOCKIF_SIG);
10831058
if (fsync(bc->fd))
10841059
err = errno;
10851060
return err;

0 commit comments

Comments
 (0)