Skip to content

Commit e763e51

Browse files
polarvidmysterywolf
authored andcommitted
feat: devfs: handling unit size in POSIX RW request
POSIX.1 says: > read() attempts to read up to count bytes from file descriptor fd > into the buffer starting at buf. On the other hand, for rt_device_read, the `@size` is defined as the number of unit (block size for blk device, otherwise one byte). Changes: - Transferred unit size from POSIX R/W(in bytes) to rt_device_R/W during request of file R/W operations. Signed-off-by: Shell <[email protected]>
1 parent be0161e commit e763e51

File tree

1 file changed

+39
-5
lines changed
  • components/dfs/dfs_v2/filesystems/devfs

1 file changed

+39
-5
lines changed

components/dfs/dfs_v2/filesystems/devfs/devfs.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,29 @@ static int dfs_devfs_close(struct dfs_file *file)
113113
return ret;
114114
}
115115

116+
static rt_ubase_t _get_unit_shift(rt_device_t device)
117+
{
118+
rt_ubase_t shift = 0;
119+
120+
/**
121+
* transfer unit size from POSIX RW(in bytes) to rt_device_R/W
122+
* (block size for blk device, otherwise in bytes).
123+
*/
124+
if (device->type == RT_Device_Class_Block)
125+
{
126+
struct rt_device_blk_geometry geometry = {0};
127+
128+
/* default to 512 */
129+
shift = 9;
130+
if (!rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry))
131+
{
132+
shift = __rt_ffs(geometry.block_size) - 1;
133+
}
134+
}
135+
136+
return shift;
137+
}
138+
116139
static ssize_t dfs_devfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
117140
{
118141
ssize_t ret = -RT_EIO;
@@ -135,9 +158,14 @@ static ssize_t dfs_devfs_read(struct dfs_file *file, void *buf, size_t count, of
135158
if (device->ops)
136159
#endif /* RT_USING_POSIX_DEVIO */
137160
{
138-
/* read device data */
139-
ret = rt_device_read(device, *pos, buf, count);
140-
*pos += ret;
161+
rt_ubase_t shift = _get_unit_shift(device);
162+
163+
ret = rt_device_read(device, *pos, buf, count >> shift);
164+
if (ret > 0)
165+
{
166+
ret <<= shift;
167+
*pos += ret;
168+
}
141169
}
142170
}
143171

@@ -169,9 +197,15 @@ static ssize_t dfs_devfs_write(struct dfs_file *file, const void *buf, size_t co
169197
if (device->ops)
170198
#endif /* RT_USING_POSIX_DEVIO */
171199
{
200+
rt_ubase_t shift = _get_unit_shift(device);
201+
172202
/* read device data */
173-
ret = rt_device_write(device, *pos, buf, count);
174-
*pos += ret;
203+
ret = rt_device_write(device, *pos, buf, count >> shift);
204+
if (ret > 0)
205+
{
206+
ret <<= shift;
207+
*pos += ret;
208+
}
175209
}
176210
}
177211

0 commit comments

Comments
 (0)