Skip to content

Commit 78cbc83

Browse files
authored
Merge pull request #8994 from tannewt/ww_default_writable
Grab FS lock to test writability
2 parents 0186d9c + 9cc668b commit 78cbc83

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

ports/espressif/supervisor/internal_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
151151
uint32_t block_address = lba + block;
152152
uint32_t sector_offset = block_address / blocks_per_sector * SECTOR_SIZE;
153153
uint8_t block_offset = block_address % blocks_per_sector;
154-
if (_cache_lba != block_address) {
154+
if (_cache_lba != sector_offset) {
155155
supervisor_flash_read_blocks(_cache, sector_offset / FILESYSTEM_BLOCK_SIZE, blocks_per_sector);
156156
_cache_lba = sector_offset;
157157
}

ports/raspberrypi/supervisor/internal_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
127127
uint32_t sector_offset = block_address / blocks_per_sector * SECTOR_SIZE;
128128
uint8_t block_offset = block_address % blocks_per_sector;
129129

130-
if (_cache_lba != block_address) {
130+
if (_cache_lba != sector_offset) {
131131
port_internal_flash_flush();
132132
memcpy(_cache,
133133
(void *)(XIP_BASE + CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + sector_offset),

supervisor/filesystem.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,23 @@ void filesystem_set_internal_writable_by_usb(bool usb_writable);
4141
void filesystem_set_internal_concurrent_write_protection(bool concurrent_write_protection);
4242
void filesystem_set_writable_by_usb(fs_user_mount_t *vfs, bool usb_writable);
4343
void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concurrent_write_protection);
44+
45+
// Whether user code can modify the filesystem. It doesn't depend on the state
46+
// of USB. Don't use this for a workflow. In workflows, grab the shared file
47+
// system lock.
4448
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs);
49+
50+
// This controls whether USB tries to grab the underlying block device lock
51+
// during enumeration. If another workflow is modifying the filesystem when this
52+
// happens, then USB will be readonly.
4553
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs);
4654

4755
fs_user_mount_t *filesystem_circuitpy(void);
4856
fs_user_mount_t *filesystem_for_path(const char *path_in, const char **path_under_mount);
4957
bool filesystem_native_fatfs(fs_user_mount_t *fs_mount);
5058

5159
// We have two levels of locking. filesystem_* calls grab a shared blockdev lock to allow
52-
// CircuitPython's fatfs code edit the blocks. blockdev_* class grab a lock to mutate blocks
60+
// CircuitPython's fatfs code to edit the blocks. blockdev_* calls grab a lock to mutate blocks
5361
// directly, excluding any filesystem_* locks.
5462

5563
bool filesystem_lock(fs_user_mount_t *fs_mount);

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,11 @@ static void _reply_directory_json(socketpool_socket_obj_t *socket, _request *req
698698
uint32_t total_clusters = fatfs->n_fatent - 2;
699699

700700
const char *writable = "false";
701-
if (filesystem_is_writable_by_python(fs_mount)) {
701+
// Test to see if we can grab the write lock. USB will grab the underlying
702+
// blockdev lock once it says it is writable. Unlock immediately since we
703+
// aren't actually writing.
704+
if (filesystem_lock(fs_mount)) {
705+
filesystem_unlock(fs_mount);
702706
writable = "true";
703707
}
704708
mp_printf(&_socket_print,
@@ -920,7 +924,8 @@ static void _reply_with_diskinfo_json(socketpool_socket_obj_t *socket, _request
920924
size_t total_size = fatfs->n_fatent - 2;
921925

922926
const char *writable = "false";
923-
if (filesystem_is_writable_by_python(fs)) {
927+
if (filesystem_lock(fs)) {
928+
filesystem_unlock(fs);
924929
writable = "true";
925930
}
926931
mp_printf(&_socket_print,

0 commit comments

Comments
 (0)