Browse Source

Add lock/unlock functions to blockdev_iface

Since one blockdev_iface could be shared by multi block devices
lock/unlock functions is needed. Locks are not needed in single
partition mode.
pull/11/head
gkostka 9 years ago
parent
commit
c8e804846c
  1. 17
      blockdev/blockdev.c
  2. 2
      blockdev/linux/ext4_filedev.c
  3. 6
      blockdev/windows/io_raw.c
  4. 59
      lwext4/ext4_blockdev.c
  5. 16
      lwext4/ext4_blockdev.h

17
blockdev/blockdev.c

@ -39,10 +39,13 @@ static int blockdev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id
static int blockdev_bwrite(struct ext4_blockdev *bdev, const void *buf, static int blockdev_bwrite(struct ext4_blockdev *bdev, const void *buf,
uint64_t blk_id, uint32_t blk_cnt); uint64_t blk_id, uint32_t blk_cnt);
static int blockdev_close(struct ext4_blockdev *bdev); static int blockdev_close(struct ext4_blockdev *bdev);
static int blockdev_lock(struct ext4_blockdev *bdev);
static int blockdev_unlock(struct ext4_blockdev *bdev);
/******************************************************************************/ /******************************************************************************/
EXT4_BLOCKDEV_STATIC_INSTANCE(blockdev, 512, 0, blockdev_open, EXT4_BLOCKDEV_STATIC_INSTANCE(blockdev, 512, 0, blockdev_open,
blockdev_bread, blockdev_bwrite, blockdev_close); blockdev_bread, blockdev_bwrite, blockdev_close,
blockdev_lock, blockdev_unlock);
/******************************************************************************/ /******************************************************************************/
static int blockdev_open(struct ext4_blockdev *bdev) static int blockdev_open(struct ext4_blockdev *bdev)
@ -75,6 +78,18 @@ static int blockdev_close(struct ext4_blockdev *bdev)
return EIO; return EIO;
} }
static int blockdev_lock(struct ext4_blockdev *bdev)
{
/*blockdev_lock: skeleton*/
return EIO;
}
static int blockdev_unlock(struct ext4_blockdev *bdev)
{
/*blockdev_unlock: skeleton*/
return EIO;
}
/******************************************************************************/ /******************************************************************************/
struct ext4_blockdev *ext4_blockdev_get(void) struct ext4_blockdev *ext4_blockdev_get(void)
{ {

2
blockdev/linux/ext4_filedev.c

@ -58,7 +58,7 @@ static int filedev_close(struct ext4_blockdev *bdev);
/******************************************************************************/ /******************************************************************************/
EXT4_BLOCKDEV_STATIC_INSTANCE(_filedev, EXT4_FILEDEV_BSIZE, 0, filedev_open, EXT4_BLOCKDEV_STATIC_INSTANCE(_filedev, EXT4_FILEDEV_BSIZE, 0, filedev_open,
filedev_bread, filedev_bwrite, filedev_close); filedev_bread, filedev_bwrite, filedev_close, 0, 0);
/******************************************************************************/ /******************************************************************************/
static int filedev_open(struct ext4_blockdev *bdev) static int filedev_open(struct ext4_blockdev *bdev)

6
blockdev/windows/io_raw.c

@ -56,7 +56,7 @@ static int io_raw_close(struct ext4_blockdev *bdev);
/******************************************************************************/ /******************************************************************************/
EXT4_BLOCKDEV_STATIC_INSTANCE(_filedev, EXT4_IORAW_BSIZE, 0, io_raw_open, EXT4_BLOCKDEV_STATIC_INSTANCE(_filedev, EXT4_IORAW_BSIZE, 0, io_raw_open,
io_raw_bread, io_raw_bwrite, io_raw_close); io_raw_bread, io_raw_bwrite, io_raw_close, 0, 0);
/******************************************************************************/ /******************************************************************************/
static int io_raw_open(struct ext4_blockdev *bdev) static int io_raw_open(struct ext4_blockdev *bdev)
@ -90,8 +90,8 @@ static int io_raw_open(struct ext4_blockdev *bdev)
disk_size = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder * disk_size = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector; (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
_filedev.ph_bsize = pdg.BytesPerSector; _filedev.bdif->ph_bsize = pdg.BytesPerSector;
_filedev.ph_bcnt = disk_size / pdg.BytesPerSector; _filedev.bdif->ph_bcnt = disk_size / pdg.BytesPerSector;
return EOK; return EOK;
} }

59
lwext4/ext4_blockdev.c

@ -42,6 +42,42 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
static void ext4_bdif_lock(struct ext4_blockdev *bdev)
{
if (!bdev->bdif->lock)
return;
int r = bdev->bdif->lock(bdev);
ext4_assert(r == EOK);
}
static void ext4_bdif_unlock(struct ext4_blockdev *bdev)
{
if (!bdev->bdif->unlock)
return;
int r = bdev->bdif->unlock(bdev);
ext4_assert(r == EOK);
}
static int ext4_bdif_bread(struct ext4_blockdev *bdev, void *buf,
uint64_t blk_id, uint32_t blk_cnt)
{
ext4_bdif_lock(bdev);
int r = bdev->bdif->bread(bdev, buf, blk_id, blk_cnt);
ext4_bdif_unlock(bdev);
return r;
}
static int ext4_bdif_bwrite(struct ext4_blockdev *bdev, const void *buf,
uint64_t blk_id, uint32_t blk_cnt)
{
ext4_bdif_lock(bdev);
int r = bdev->bdif->bwrite(bdev, buf, blk_id, blk_cnt);
ext4_bdif_unlock(bdev);
return r;
}
int ext4_block_init(struct ext4_blockdev *bdev) int ext4_block_init(struct ext4_blockdev *bdev)
{ {
int rc; int rc;
@ -225,7 +261,7 @@ int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize; pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
bdev->bread_ctr++; bdev->bread_ctr++;
return bdev->bdif->bread(bdev, buf, pba, pb_cnt * cnt); return ext4_bdif_bread(bdev, buf, pba, pb_cnt * cnt);
} }
int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf, int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
@ -241,8 +277,7 @@ int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize; pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize;
bdev->bwrite_ctr++; bdev->bwrite_ctr++;
return ext4_bdif_bwrite(bdev, buf, pba, pb_cnt * cnt);
return bdev->bdif->bwrite(bdev, buf, pba, pb_cnt * cnt);
} }
int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off, int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
@ -275,13 +310,12 @@ int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
? len ? len
: (bdev->bdif->ph_bsize - unalg); : (bdev->bdif->ph_bsize - unalg);
r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
if (r != EOK) if (r != EOK)
return r; return r;
memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen); memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen);
r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
r = bdev->bdif->bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
if (r != EOK) if (r != EOK)
return r; return r;
@ -292,7 +326,7 @@ int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
/*Aligned data*/ /*Aligned data*/
blen = len / bdev->bdif->ph_bsize; blen = len / bdev->bdif->ph_bsize;
r = bdev->bdif->bwrite(bdev, p, block_idx, blen); r = ext4_bdif_bwrite(bdev, p, block_idx, blen);
if (r != EOK) if (r != EOK)
return r; return r;
@ -303,13 +337,12 @@ int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
/*Rest of the data*/ /*Rest of the data*/
if (len) { if (len) {
r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
if (r != EOK) if (r != EOK)
return r; return r;
memcpy(bdev->bdif->ph_bbuf, p, len); memcpy(bdev->bdif->ph_bbuf, p, len);
r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
r = bdev->bdif->bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
if (r != EOK) if (r != EOK)
return r; return r;
} }
@ -347,7 +380,7 @@ int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
? len ? len
: (bdev->bdif->ph_bsize - unalg); : (bdev->bdif->ph_bsize - unalg);
r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
if (r != EOK) if (r != EOK)
return r; return r;
@ -361,7 +394,7 @@ int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
/*Aligned data*/ /*Aligned data*/
blen = len / bdev->bdif->ph_bsize; blen = len / bdev->bdif->ph_bsize;
r = bdev->bdif->bread(bdev, p, block_idx, blen); r = ext4_bdif_bread(bdev, p, block_idx, blen);
if (r != EOK) if (r != EOK)
return r; return r;
@ -372,7 +405,7 @@ int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
/*Rest of the data*/ /*Rest of the data*/
if (len) { if (len) {
r = bdev->bdif->bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1);
if (r != EOK) if (r != EOK)
return r; return r;

16
lwext4/ext4_blockdev.h

@ -71,6 +71,16 @@ struct ext4_blockdev_iface {
* @param bdev block device.*/ * @param bdev block device.*/
int (*close)(struct ext4_blockdev *bdev); int (*close)(struct ext4_blockdev *bdev);
/**@brief Lock block device. Required in multi partition mode
* operations. Not mandatory field.
* @param bdev block device.*/
int (*lock)(struct ext4_blockdev *bdev);
/**@brief Unlock block device. Required in multi partition mode
* operations. Not mandatory field.
* @param bdev block device.*/
int (*unlock)(struct ext4_blockdev *bdev);
/**@brief Block size (bytes): physical*/ /**@brief Block size (bytes): physical*/
uint32_t ph_bsize; uint32_t ph_bsize;
@ -112,14 +122,16 @@ struct ext4_blockdev {
}; };
/**@brief Static initialization of the block device.*/ /**@brief Static initialization of the block device.*/
#define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, \ #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\
__bread, __bwrite, __close) \ __bwrite, __close, __lock, __unlock) \
static uint8_t __name##_ph_bbuf[(__bsize)]; \ static uint8_t __name##_ph_bbuf[(__bsize)]; \
static struct ext4_blockdev_iface __name##_iface = { \ static struct ext4_blockdev_iface __name##_iface = { \
.open = __open, \ .open = __open, \
.bread = __bread, \ .bread = __bread, \
.bwrite = __bwrite, \ .bwrite = __bwrite, \
.close = __close, \ .close = __close, \
.lock = __lock, \
.unlock = __unlock, \
.ph_bsize = __bsize, \ .ph_bsize = __bsize, \
.ph_bcnt = __bcnt, \ .ph_bcnt = __bcnt, \
.ph_bbuf = __name##_ph_bbuf, \ .ph_bbuf = __name##_ph_bbuf, \

Loading…
Cancel
Save