Skip to content

Commit ea5ac4b

Browse files
gekysimonqhughes
authored andcommitted
Storage: Last minute changes to the block device api
- Remove write set of functions - Caused confusion with small benefit - Trivial to add later - Remove unused error codes - Initial expirementation indicates most of these may not be useful - Trivial to add later - Removed bd_error_t - Carries to additional type information, int already carries sufficient connotation - Trivial to add later per @c1728p9, @sg-
1 parent 5a977ef commit ea5ac4b

File tree

9 files changed

+97
-209
lines changed

9 files changed

+97
-209
lines changed

features/filesystem/bd/BlockDevice.cpp

Lines changed: 0 additions & 75 deletions
This file was deleted.

features/filesystem/bd/BlockDevice.h

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,13 @@
2222

2323
/** Enum of standard error codes
2424
*
25-
* @enum bd_error_t
25+
* @enum bd_error
2626
*/
2727
enum bd_error {
2828
BD_ERROR_OK = 0, /*!< no error */
29-
BD_ERROR_WOULD_BLOCK = -4001, /*!< operation would block */
30-
BD_ERROR_UNSUPPORTED = -4002, /*!< unsupported operation */
31-
BD_ERROR_PARAMETER = -4003, /*!< invalid parameter */
32-
BD_ERROR_NO_INIT = -4004, /*!< uninitialized */
33-
BD_ERROR_NO_DEVICE = -4005, /*!< device is missing or not connected */
34-
BD_ERROR_WRITE_PROTECTED = -4006, /*!< write protected */
35-
BD_ERROR_DEVICE_ERROR = -4007, /*!< device specific error */
29+
BD_ERROR_DEVICE_ERROR = -4001, /*!< device specific error */
3630
};
3731

38-
/** Type representing either 0 or a negative error code
39-
*/
40-
typedef int32_t bd_error_t;
41-
4232
/** Type representing the address of a specific block
4333
*/
4434
typedef uint64_t bd_addr_t;
@@ -61,13 +51,13 @@ class BlockDevice
6151
*
6252
* @return 0 on success or a negative error code on failure
6353
*/
64-
virtual bd_error_t init() = 0;
54+
virtual int init() = 0;
6555

6656
/** Deinitialize a block device
6757
*
6858
* @return 0 on success or a negative error code on failure
6959
*/
70-
virtual bd_error_t deinit() = 0;
60+
virtual int deinit() = 0;
7161

7262
/** Read blocks from a block device
7363
*
@@ -78,20 +68,7 @@ class BlockDevice
7868
* @param size Size to read in bytes, must be a multiple of read block size
7969
* @return 0 on success, negative error code on failure
8070
*/
81-
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size) = 0;
82-
83-
/** Write blocks to a block device
84-
*
85-
* A write is equivalent to an erase followed by a program
86-
*
87-
* If a failure occurs, it is not possible to determine how many bytes succeeded
88-
*
89-
* @param buffer Buffer of data to write to blocks
90-
* @param addr Address of block to begin writing to
91-
* @param size Size to write in bytes, must be a multiple of write block size
92-
* @return 0 on success, negative error code on failure
93-
*/
94-
virtual bd_error_t write(const void *buffer, bd_addr_t addr, bd_size_t size);
71+
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size) = 0;
9572

9673
/** Program blocks to a block device
9774
*
@@ -104,7 +81,7 @@ class BlockDevice
10481
* @param size Size to write in bytes, must be a multiple of program block size
10582
* @return 0 on success, negative error code on failure
10683
*/
107-
virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size) = 0;
84+
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size) = 0;
10885

10986
/** Erase blocks on a block device
11087
*
@@ -114,22 +91,14 @@ class BlockDevice
11491
* @param size Size to erase in bytes, must be a multiple of erase block size
11592
* @return 0 on success, negative error code on failure
11693
*/
117-
virtual bd_error_t erase(bd_addr_t addr, bd_size_t size) = 0;
94+
virtual int erase(bd_addr_t addr, bd_size_t size) = 0;
11895

11996
/** Get the size of a readable block
12097
*
12198
* @return Size of a readable block in bytes
12299
*/
123100
virtual bd_size_t get_read_size() const = 0;
124101

125-
/** Get the size of a writeable block
126-
*
127-
* @return Size of a writeable block in bytes
128-
* @note Must be a multiple of the read size, this is
129-
* equivalent to the erase size of the device
130-
*/
131-
virtual bd_size_t get_write_size() const;
132-
133102
/** Get the size of a programable block
134103
*
135104
* @return Size of a programable block in bytes
@@ -156,31 +125,41 @@ class BlockDevice
156125
* @param size Size to read in bytes
157126
* @return True if read is valid for underlying block device
158127
*/
159-
bool is_valid_read(bd_addr_t addr, bd_size_t size);
160-
161-
/** Convenience function for checking block write validity
162-
*
163-
* @param addr Address of block to begin writing to
164-
* @param size Size to write in bytes
165-
* @return True if write is valid for underlying block device
166-
*/
167-
bool is_valid_write(bd_addr_t addr, bd_size_t size);
128+
bool is_valid_read(bd_addr_t addr, bd_size_t size)
129+
{
130+
return (
131+
addr % get_read_size() == 0 &&
132+
size % get_read_size() == 0 &&
133+
addr + size <= this->size());
134+
}
168135

169136
/** Convenience function for checking block program validity
170137
*
171138
* @param addr Address of block to begin writing to
172139
* @param size Size to write in bytes
173140
* @return True if program is valid for underlying block device
174141
*/
175-
bool is_valid_program(bd_addr_t addr, bd_size_t size);
142+
bool is_valid_program(bd_addr_t addr, bd_size_t size)
143+
{
144+
return (
145+
addr % get_program_size() == 0 &&
146+
size % get_program_size() == 0 &&
147+
addr + size <= this->size());
148+
}
176149

177150
/** Convenience function for checking block erase validity
178151
*
179152
* @param addr Address of block to begin erasing
180153
* @param size Size to erase in bytes
181154
* @return True if erase is valid for underlying block device
182155
*/
183-
bool is_valid_erase(bd_addr_t addr, bd_size_t size);
156+
bool is_valid_erase(bd_addr_t addr, bd_size_t size)
157+
{
158+
return (
159+
addr % get_erase_size() == 0 &&
160+
size % get_erase_size() == 0 &&
161+
addr + size <= this->size());
162+
}
184163
};
185164

186165

features/filesystem/bd/ChainingBlockDevice.cpp

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static bool is_aligned(uint64_t x, uint64_t alignment)
2828
return (x / alignment) * alignment == x;
2929
}
3030

31-
bd_error_t ChainingBlockDevice::init()
31+
int ChainingBlockDevice::init()
3232
{
3333
_read_size = 0;
3434
_program_size = 0;
@@ -40,30 +40,30 @@ bd_error_t ChainingBlockDevice::init()
4040
// the constructor since some block devices may need to be
4141
// initialized before they know their block size/count
4242
for (size_t i = 0; i < _bd_count; i++) {
43-
bd_error_t err = _bds[i]->init();
43+
int err = _bds[i]->init();
4444
if (err) {
4545
return err;
4646
}
4747

4848
bd_size_t read = _bds[i]->get_read_size();
4949
if (i == 0 || (read >= _read_size && is_aligned(read, _read_size))) {
5050
_read_size = read;
51-
} else if (!(_read_size > read && is_aligned(_read_size, read))) {
52-
return BD_ERROR_PARAMETER;
51+
} else {
52+
MBED_ASSERT(_read_size > read && is_aligned(_read_size, read));
5353
}
5454

5555
bd_size_t program = _bds[i]->get_program_size();
5656
if (i == 0 || (program >= _program_size && is_aligned(program, _program_size))) {
5757
_program_size = program;
58-
} else if (!(_program_size > program && is_aligned(_program_size, program))) {
59-
return BD_ERROR_PARAMETER;
58+
} else {
59+
MBED_ASSERT(_program_size > program && is_aligned(_program_size, program));
6060
}
6161

6262
bd_size_t erase = _bds[i]->get_erase_size();
6363
if (i == 0 || (erase >= _erase_size && is_aligned(erase, _erase_size))) {
6464
_erase_size = erase;
65-
} else if (!(_erase_size > erase && is_aligned(_erase_size, erase))) {
66-
return BD_ERROR_PARAMETER;
65+
} else {
66+
MBED_ASSERT(_erase_size > erase && is_aligned(_erase_size, erase));
6767
}
6868

6969
_size += _bds[i]->size();
@@ -72,10 +72,10 @@ bd_error_t ChainingBlockDevice::init()
7272
return 0;
7373
}
7474

75-
bd_error_t ChainingBlockDevice::deinit()
75+
int ChainingBlockDevice::deinit()
7676
{
7777
for (size_t i = 0; i < _bd_count; i++) {
78-
bd_error_t err = _bds[i]->deinit();
78+
int err = _bds[i]->deinit();
7979
if (err) {
8080
return err;
8181
}
@@ -84,12 +84,9 @@ bd_error_t ChainingBlockDevice::deinit()
8484
return 0;
8585
}
8686

87-
bd_error_t ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
87+
int ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
8888
{
89-
if (!is_valid_read(addr, size)) {
90-
return BD_ERROR_PARAMETER;
91-
}
92-
89+
MBED_ASSERT(is_valid_read(addr, size));
9390
uint8_t *buffer = static_cast<uint8_t*>(b);
9491

9592
// Find block devices containing blocks, may span multiple block devices
@@ -102,7 +99,7 @@ bd_error_t ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
10299
read = bdsize - addr;
103100
}
104101

105-
bd_error_t err = _bds[i]->read(buffer, addr, read);
102+
int err = _bds[i]->read(buffer, addr, read);
106103
if (err) {
107104
return err;
108105
}
@@ -118,12 +115,9 @@ bd_error_t ChainingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
118115
return 0;
119116
}
120117

121-
bd_error_t ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
118+
int ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
122119
{
123-
if (!is_valid_program(addr, size)) {
124-
return BD_ERROR_PARAMETER;
125-
}
126-
120+
MBED_ASSERT(is_valid_program(addr, size));
127121
const uint8_t *buffer = static_cast<const uint8_t*>(b);
128122

129123
// Find block devices containing blocks, may span multiple block devices
@@ -136,7 +130,7 @@ bd_error_t ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t
136130
program = bdsize - addr;
137131
}
138132

139-
bd_error_t err = _bds[i]->program(buffer, addr, program);
133+
int err = _bds[i]->program(buffer, addr, program);
140134
if (err) {
141135
return err;
142136
}
@@ -152,11 +146,9 @@ bd_error_t ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t
152146
return 0;
153147
}
154148

155-
bd_error_t ChainingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
149+
int ChainingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
156150
{
157-
if (!is_valid_erase(addr, size)) {
158-
return BD_ERROR_PARAMETER;
159-
}
151+
MBED_ASSERT(is_valid_erase(addr, size));
160152

161153
// Find block devices containing blocks, may span multiple block devices
162154
for (size_t i = 0; i < _bd_count && size > 0; i++) {
@@ -168,7 +160,7 @@ bd_error_t ChainingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
168160
erase = bdsize - addr;
169161
}
170162

171-
bd_error_t err = _bds[i]->erase(addr, erase);
163+
int err = _bds[i]->erase(addr, erase);
172164
if (err) {
173165
return err;
174166
}

features/filesystem/bd/ChainingBlockDevice.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ class ChainingBlockDevice : public BlockDevice
7878
*
7979
* @return 0 on success or a negative error code on failure
8080
*/
81-
virtual bd_error_t init();
81+
virtual int init();
8282

8383
/** Deinitialize a block device
8484
*
8585
* @return 0 on success or a negative error code on failure
8686
*/
87-
virtual bd_error_t deinit();
87+
virtual int deinit();
8888

8989
/** Read blocks from a block device
9090
*
@@ -93,7 +93,7 @@ class ChainingBlockDevice : public BlockDevice
9393
* @param size Size to read in bytes, must be a multiple of read block size
9494
* @return 0 on success, negative error code on failure
9595
*/
96-
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size);
96+
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
9797

9898
/** Program blocks to a block device
9999
*
@@ -104,7 +104,7 @@ class ChainingBlockDevice : public BlockDevice
104104
* @param size Size to write in bytes, must be a multiple of program block size
105105
* @return 0 on success, negative error code on failure
106106
*/
107-
virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size);
107+
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
108108

109109
/** Erase blocks on a block device
110110
*
@@ -114,7 +114,7 @@ class ChainingBlockDevice : public BlockDevice
114114
* @param size Size to erase in bytes, must be a multiple of erase block size
115115
* @return 0 on success, negative error code on failure
116116
*/
117-
virtual bd_error_t erase(bd_addr_t addr, bd_size_t size);
117+
virtual int erase(bd_addr_t addr, bd_size_t size);
118118

119119
/** Get the size of a readable block
120120
*

0 commit comments

Comments
 (0)