diff options
| author | Shell <[email protected]> | 2024-11-13 17:18:40 +0800 |
|---|---|---|
| committer | Meco Man <[email protected]> | 2024-11-13 22:56:07 -0500 |
| commit | e763e51e4985bf6bfca1a18965e604b59fc3b984 (patch) | |
| tree | 2a10a393797cd571e38b9989342ceb10cef6de96 | |
| parent | be0161ec625fe4e75ca39eace3b3cf27be3b4b5a (diff) | |
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]>
| -rw-r--r-- | components/dfs/dfs_v2/filesystems/devfs/devfs.c | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/components/dfs/dfs_v2/filesystems/devfs/devfs.c b/components/dfs/dfs_v2/filesystems/devfs/devfs.c index 22d24bd684..b09c54eb35 100644 --- a/components/dfs/dfs_v2/filesystems/devfs/devfs.c +++ b/components/dfs/dfs_v2/filesystems/devfs/devfs.c @@ -113,6 +113,29 @@ static int dfs_devfs_close(struct dfs_file *file) return ret; } +static rt_ubase_t _get_unit_shift(rt_device_t device) +{ + rt_ubase_t shift = 0; + + /** + * transfer unit size from POSIX RW(in bytes) to rt_device_R/W + * (block size for blk device, otherwise in bytes). + */ + if (device->type == RT_Device_Class_Block) + { + struct rt_device_blk_geometry geometry = {0}; + + /* default to 512 */ + shift = 9; + if (!rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry)) + { + shift = __rt_ffs(geometry.block_size) - 1; + } + } + + return shift; +} + static ssize_t dfs_devfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos) { 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 if (device->ops) #endif /* RT_USING_POSIX_DEVIO */ { - /* read device data */ - ret = rt_device_read(device, *pos, buf, count); - *pos += ret; + rt_ubase_t shift = _get_unit_shift(device); + + ret = rt_device_read(device, *pos, buf, count >> shift); + if (ret > 0) + { + ret <<= shift; + *pos += ret; + } } } @@ -169,9 +197,15 @@ static ssize_t dfs_devfs_write(struct dfs_file *file, const void *buf, size_t co if (device->ops) #endif /* RT_USING_POSIX_DEVIO */ { + rt_ubase_t shift = _get_unit_shift(device); + /* read device data */ - ret = rt_device_write(device, *pos, buf, count); - *pos += ret; + ret = rt_device_write(device, *pos, buf, count >> shift); + if (ret > 0) + { + ret <<= shift; + *pos += ret; + } } } |
