Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can multiple files be stored in the same block?

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.

like image 581
Maxthecat Avatar asked May 08 '15 21:05

Maxthecat


1 Answers

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 */
like image 58
mikyra Avatar answered Sep 30 '22 16:09

mikyra