Skip to content

Commit cb747b3

Browse files
committed
ext4: support bigger blkdev block size
Allows ext4 to be used with block devices that have block size > EXT4_SUPERBLOCK_SIZE (1K).
1 parent eff458d commit cb747b3

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

uspace/lib/ext4/src/superblock.c

+27-6
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,8 @@ errno_t ext4_superblock_read_direct(service_id_t service_id, ext4_superblock_t *
12051205
*/
12061206
errno_t ext4_superblock_write_direct(service_id_t service_id, ext4_superblock_t *sb)
12071207
{
1208+
void *tmp_sb = sb;
1209+
12081210
/* Load physical block size from block device */
12091211
size_t phys_block_size;
12101212
errno_t rc = block_get_bsize(service_id, &phys_block_size);
@@ -1215,14 +1217,33 @@ errno_t ext4_superblock_write_direct(service_id_t service_id, ext4_superblock_t
12151217
uint64_t first_block = EXT4_SUPERBLOCK_OFFSET / phys_block_size;
12161218

12171219
/* Compute number of block to write */
1218-
size_t block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
1219-
1220-
/* Check alignment */
1221-
if (EXT4_SUPERBLOCK_SIZE % phys_block_size)
1222-
block_count++;
1220+
size_t block_count;
1221+
if (phys_block_size <= EXT4_SUPERBLOCK_SIZE) {
1222+
block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
1223+
/* Check alignment */
1224+
if (EXT4_SUPERBLOCK_SIZE % phys_block_size)
1225+
block_count++;
1226+
} else {
1227+
block_count = 1;
1228+
tmp_sb = malloc(phys_block_size);
1229+
if (tmp_sb == NULL)
1230+
return ENOMEM;
1231+
/* Preserve block data */
1232+
rc = block_read_direct(service_id, first_block, 1, tmp_sb);
1233+
if (rc != EOK) {
1234+
free(tmp_sb);
1235+
return rc;
1236+
}
1237+
void *sb_pos = (uint8_t *)tmp_sb + EXT4_SUPERBLOCK_OFFSET;
1238+
memcpy(sb_pos, sb, EXT4_SUPERBLOCK_SIZE);
1239+
}
12231240

12241241
/* Write data */
1225-
return block_write_direct(service_id, first_block, block_count, sb);
1242+
rc = block_write_direct(service_id, first_block, block_count, tmp_sb);
1243+
1244+
if (phys_block_size > EXT4_SUPERBLOCK_SIZE)
1245+
free(tmp_sb);
1246+
return rc;
12261247
}
12271248

12281249
/** Release the memory allocated for the superblock structure

0 commit comments

Comments
 (0)