I am coding some block level operations, and I want to make sure I don't bash over other files. On an ext2/3/4 file system, can multiple files be stored in the same block? My first instinct is to say no way, but I wanted to check with the community.
-
some filesystems have the capability to split a block up into smaller sub-blocks (I specifically remember being taught about BSD's FFS having this capability, back in basic OS courses in Uni). No idea about the ext* filesystems specifically, though.– Marc BMay 8, 2015 at 21:31
-
the ext* file systems, used on *inx OS's allocate a full block (usually 4096 usable bytes) even if they only need 1 byte of the new block. so, the simple answer is NO. I.E. only one file can be kept on a block of the disk. (however, you might want to look at archiving, as that just separates a series of programs via a header, therefore allowing the possibility of having more than one program per block.– user3629249May 9, 2015 at 19:59
1 Answer
The question is difficult to answer. Maybe the correct answer might be in theory yes, but in practice no.
ext2 / ext3
Speaking ext2 and ext3, superblock and inode structures were designed to allow blocks to be fragmented. (see: fs/ext2/ext2.h
and fs/ext3/ext3.h
)
short snippet of fs/ext3/ext3.h
given here:
struct ext3_super_block {
/*00*/ __le32 s_inodes_count; /* Inodes count */
__le32 s_blocks_count; /* Blocks count */
__le32 s_r_blocks_count; /* Reserved blocks count */
__le32 s_free_blocks_count; /* Free blocks count */
/*10*/ __le32 s_free_inodes_count; /* Free inodes count */
__le32 s_first_data_block; /* First Data Block */
__le32 s_log_block_size; /* Block size */
__le32 s_log_frag_size; /* Fragment size */
// ...
struct ext3_inode {
__le16 i_mode; /* File mode */
__le16 i_uid; /* Low 16 bits of Owner Uid */
// ...
__le32 i_faddr; /* Fragment address */
Although prepared for, at least in the linux kernel (up to version 3.13) block fragmentation was never implemented, forcing fragment size to be equal to block size. (see: fs/ext3/super.c
)
if (blocksize != sbi->s_frag_size) {
ext3_msg(sb, KERN_ERR,
"error: fragsize %lu != blocksize %u (unsupported)",
sbi->s_frag_size, blocksize);
goto failed_mount;
}
Afaik GNU/Hurd doesn't implement block fragmentation of ext2/3 filesystems as well. Most probably there will be no OS around, that implements it.
Nevertheless, checking s_log_frag_size
in the superblock before starting your block level operations might not be a bad idea, as you will be on the safe side.
ext4
With ext4 the wohle story becomes less troublesome, as ext4 doesn't allow block fragmentation any more. The superblock field used to store fragment size has been given a new job, and the iode field used to store the fragment address (renamed to i_obso_faddr
) has been marked as obsolete in the sources.
struct ext4_inode {
__le16 i_mode; /* File mode */
__le16 i_uid; /* Low 16 bits of Owner Uid */
// ...
__le32 i_obso_faddr; /* Obsoleted fragment address */