ext4_rs 1.3.3

Cross-platform rust ext4.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use bitflags::Flags;

use crate::ext4_defs::*;
use crate::prelude::*;
use crate::return_errno_with_message;
use crate::utils::bitmap::*;

impl Ext4 {
    pub fn get_bgid_of_inode(&self, inode_num: u32) -> u32 {
        inode_num / self.super_block.inodes_per_group()
    }

    pub fn inode_to_bgidx(&self, inode_num: u32) -> u32 {
        inode_num % self.super_block.inodes_per_group()
    }

    /// Get inode disk position.
    pub fn inode_disk_pos(&self, inode_num: u32) -> usize {
        let super_block = self.super_block;
        let inodes_per_group = super_block.inodes_per_group;
        let inode_size = super_block.inode_size as u64;
        let group = (inode_num - 1) / inodes_per_group;
        let index = (inode_num - 1) % inodes_per_group;
        let block_group =
            Ext4BlockGroup::load_new(&self.block_device, &super_block, group as usize);
        let inode_table_blk_num = block_group.get_inode_table_blk_num();

        inode_table_blk_num as usize * BLOCK_SIZE + index as usize * inode_size as usize
    }

    /// Load the inode reference from the disk.
    pub fn get_inode_ref(&self, inode_num: u32) -> Ext4InodeRef {
        let offset = self.inode_disk_pos(inode_num);

        let mut ext4block = Block::load(&self.block_device, offset);

        let inode: &mut Ext4Inode = ext4block.read_as_mut();

        Ext4InodeRef {
            inode_num,
            inode: *inode,
        }
    }

    /// write back inode with checksum
    pub fn write_back_inode(&self, inode_ref: &mut Ext4InodeRef) {
        let inode_pos = self.inode_disk_pos(inode_ref.inode_num);

        // make sure self.super_block is up-to-date
        inode_ref
            .inode
            .set_inode_checksum(&self.super_block, inode_ref.inode_num);
        inode_ref
            .inode
            .sync_inode_to_disk(&self.block_device, inode_pos);
    }

    /// write back inode with checksum
    pub fn write_back_inode_without_csum(&self, inode_ref: &Ext4InodeRef) {
        let inode_pos = self.inode_disk_pos(inode_ref.inode_num);

        inode_ref
            .inode
            .sync_inode_to_disk(&self.block_device, inode_pos);
    }

    /// Get physical block id of a logical block.
    ///
    /// Params:
    /// inode_ref: &Ext4InodeRef - inode reference
    /// lblock: Ext4Lblk - logical block id
    ///
    /// Returns:
    /// `Result<Ext4Fsblk>` - physical block id
    pub fn get_pblock_idx(&self, inode_ref: &Ext4InodeRef, lblock: Ext4Lblk) -> Result<Ext4Fsblk> {
        let search_path = self.find_extent(inode_ref, lblock);
        if let Ok(path) = search_path {
            // get the last path
            let path = path.path.last().unwrap();

            // get physical block id
            let fblock = path.pblock;

            return Ok(fblock);
        }

        return_errno_with_message!(Errno::EIO, "search extent fail");
    }

    /// Allocate a new block
    pub fn allocate_new_block(&self, inode_ref: &mut Ext4InodeRef) -> Result<Ext4Fsblk> {
        let mut super_block = self.super_block;
        let inodes_per_group = super_block.inodes_per_group();
        let bgid = (inode_ref.inode_num - 1) / inodes_per_group;
        let index = (inode_ref.inode_num - 1) % inodes_per_group;

        // load block group
        let mut block_group =
            Ext4BlockGroup::load_new(&self.block_device, &super_block, bgid as usize);

        let block_bitmap_block = block_group.get_block_bitmap_block(&super_block);

        let mut block_bmap_raw_data = self
            .block_device
            .read_offset(block_bitmap_block as usize * BLOCK_SIZE);
        let mut data: &mut Vec<u8> = &mut block_bmap_raw_data;
        let mut rel_blk_idx = 0;

        ext4_bmap_bit_find_clr(data, index, 0x8000, &mut rel_blk_idx);
        ext4_bmap_bit_set(data, rel_blk_idx);

        block_group.set_block_group_balloc_bitmap_csum(&super_block, data);
        self.block_device
            .write_offset(block_bitmap_block as usize * BLOCK_SIZE, data);

        /* Update superblock free blocks count */
        let mut super_blk_free_blocks = super_block.free_blocks_count();
        super_blk_free_blocks -= 1;
        super_block.set_free_blocks_count(super_blk_free_blocks);
        super_block.sync_to_disk_with_csum(&self.block_device);

        /* Update inode blocks (different block size!) count */
        let mut inode_blocks = inode_ref.inode.blocks_count();
        inode_blocks += (BLOCK_SIZE / EXT4_INODE_BLOCK_SIZE) as u64;
        inode_ref.inode.set_blocks_count(inode_blocks);
        self.write_back_inode(inode_ref);

        /* Update block group free blocks count */
        let mut fb_cnt = block_group.get_free_blocks_count();
        fb_cnt -= 1;
        block_group.set_free_blocks_count(fb_cnt as u32);
        block_group.sync_to_disk_with_csum(&self.block_device, bgid as usize, &super_block);

        Ok(rel_blk_idx as Ext4Fsblk)
    }

    /// Append a new block to the inode and update the extent tree.
    ///
    /// Params:
    /// inode_ref: &mut Ext4InodeRef - inode reference
    /// iblock: Ext4Lblk - logical block id
    ///
    /// Returns:
    /// `Result<Ext4Fsblk>` - physical block id of the new block
    pub fn append_inode_pblk(&self, inode_ref: &mut Ext4InodeRef) -> Result<Ext4Fsblk> {
        let inode_size = inode_ref.inode.size();
        let iblock = ((inode_size as usize + BLOCK_SIZE - 1) / BLOCK_SIZE) as u32;

        let mut newex: Ext4Extent = Ext4Extent::default();

        let new_block = self.balloc_alloc_block(inode_ref, None)?;

        newex.first_block = iblock;
        newex.store_pblock(new_block);
        newex.block_count = min(1, EXT_MAX_BLOCKS - iblock) as u16;

        self.insert_extent(inode_ref, &mut newex)?;

        // Update the inode size
        let mut inode_size = inode_ref.inode.size();
        inode_size += BLOCK_SIZE as u64;
        inode_ref.inode.set_size(inode_size);
        self.write_back_inode(inode_ref);

        Ok(new_block)
    }

    /// Append a new block to the inode and update the extent tree.From a specific bgid
    ///
    /// Params:
    /// inode_ref: &mut Ext4InodeRef - inode reference
    /// bgid: Start bgid of free block search
    ///
    /// Returns:
    /// `Result<Ext4Fsblk>` - physical block id of the new block
    pub fn append_inode_pblk_from(
        &self,
        inode_ref: &mut Ext4InodeRef,
        start_bgid: &mut u32,
    ) -> Result<Ext4Fsblk> {
        let inode_size = inode_ref.inode.size();
        let iblock = ((inode_size as usize + BLOCK_SIZE - 1) / BLOCK_SIZE) as u32;

        let mut newex: Ext4Extent = Ext4Extent::default();

        let new_block = self.balloc_alloc_block_from(inode_ref, start_bgid)?;

        newex.first_block = iblock;
        newex.store_pblock(new_block);
        newex.block_count = min(1, EXT_MAX_BLOCKS - iblock) as u16;

        self.insert_extent(inode_ref, &mut newex)?;

        // Update the inode size
        let mut inode_size = inode_ref.inode.size();
        inode_size += BLOCK_SIZE as u64;
        inode_ref.inode.set_size(inode_size);
        self.write_back_inode(inode_ref);

        Ok(new_block)
    }

    /// Allocate a new inode
    ///
    /// Params:
    /// inode_mode: u16 - inode mode
    ///
    /// Returns:
    /// `Result<u32>` - inode number
    pub fn alloc_inode(&self, is_dir: bool) -> Result<u32> {
        // Allocate inode
        let inode_num = self.ialloc_alloc_inode(is_dir)?;

        Ok(inode_num)
    }

    pub fn correspond_inode_mode(&self, filetype: u8) -> u16 {
        let file_type = DirEntryType::from_bits(filetype).unwrap();
        match file_type {
            DirEntryType::EXT4_DE_REG_FILE => InodeFileType::S_IFREG.bits(),
            DirEntryType::EXT4_DE_DIR => InodeFileType::S_IFDIR.bits(),
            DirEntryType::EXT4_DE_SYMLINK => InodeFileType::S_IFLNK.bits(),
            DirEntryType::EXT4_DE_CHRDEV => InodeFileType::S_IFCHR.bits(),
            DirEntryType::EXT4_DE_BLKDEV => InodeFileType::S_IFBLK.bits(),
            DirEntryType::EXT4_DE_FIFO => InodeFileType::S_IFIFO.bits(),
            DirEntryType::EXT4_DE_SOCK => InodeFileType::S_IFSOCK.bits(),
            _ => {
                // FIXME: unsupported filetype
                InodeFileType::S_IFREG.bits()
            }
        }
    }

    /// Append multiple blocks to the inode and update the extent tree.
    ///
    /// Params:
    /// inode_ref: &mut Ext4InodeRef - inode reference
    /// start_bgid: &mut u32 - start block group id for allocation
    /// block_count: usize - number of blocks to allocate
    ///
    /// Returns:
    /// `Result<Vec<Ext4Fsblk>>` - vector of physical block ids of the new blocks
    pub fn append_inode_pblk_batch(
        &self,
        inode_ref: &mut Ext4InodeRef,
        start_bgid: &mut u32,
        block_count: usize,
    ) -> Result<Vec<Ext4Fsblk>> {
        let inode_size = inode_ref.inode.size();
        let iblock = ((inode_size as usize + BLOCK_SIZE - 1) / BLOCK_SIZE) as u32;

        // Use new optimized block allocation function
        let allocated_blocks = self.balloc_alloc_block_batch(inode_ref, start_bgid, block_count)?;

        if allocated_blocks.is_empty() {
            log::warn!("[Batch Append] No blocks could be allocated");
            return Ok(Vec::new());
        }

        // Record the actual number of allocated blocks
        let actual_allocated = allocated_blocks.len();
        if actual_allocated < block_count {
            log::warn!(
                "[Batch Append] Partial allocation: {}/{} blocks",
                actual_allocated,
                block_count
            );
        }

        // Check the current state of the extent tree
        let root_header = inode_ref.inode.root_extent_header();
        log::info!(
            "[Batch Append] Current extent tree state: magic={:x}, entries={}, max={}, depth={}",
            root_header.magic,
            root_header.entries_count,
            root_header.max_entries_count,
            root_header.depth
        );

        // Find the starting logical block position
        let mut current_iblk = iblock;
        let mut last_extent_end = if root_header.entries_count > 0 {
            // Get the end position of the last extent
            let last_extent = match self.get_last_extent(inode_ref) {
                Ok(extent) => extent.first_block + extent.block_count as u32,
                Err(_) => {
                    log::warn!(
                        "[Batch Append] Could not get last extent, starting at block {}",
                        iblock
                    );
                    iblock
                }
            };
            last_extent
        } else {
            0
        };

        // Ensure new extents start after the end of the last extent
        if current_iblk < last_extent_end {
            current_iblk = last_extent_end;
        }

        // Group allocated physical blocks into contiguous segments
        let mut contiguous_segments = Vec::new();
        let mut current_segment = Vec::new();

        // Add the first block to the current segment
        if !allocated_blocks.is_empty() {
            current_segment.push(allocated_blocks[0]);
        }

        // Check for continuity starting from the second block
        for i in 1..allocated_blocks.len() {
            let prev_block = allocated_blocks[i - 1];
            let curr_block = allocated_blocks[i];

            // If the current block is contiguous with the previous block
            if curr_block == prev_block + 1 {
                current_segment.push(curr_block);
            } else {
                // If not contiguous, end the current segment and start a new one
                if !current_segment.is_empty() {
                    contiguous_segments.push(current_segment);
                    current_segment = Vec::new();
                }
                current_segment.push(curr_block);
            }
        }

        // Add the last segment
        if !current_segment.is_empty() {
            contiguous_segments.push(current_segment);
        }

        log::info!(
            "[Batch Append] Split {} allocated blocks into {} contiguous segments",
            allocated_blocks.len(),
            contiguous_segments.len()
        );

        // Define maximum extent length
        const MAX_EXTENT_LENGTH: usize = EXT_INIT_MAX_LEN as usize;

        // Create extents for each contiguous segment
        for segment in contiguous_segments {
            if segment.is_empty() {
                continue;
            }

            // If segment length exceeds maximum extent length, split
            let mut segment_start = 0;
            while segment_start < segment.len() {
                // Calculate current segment length, ensuring it doesn't exceed MAX_EXTENT_LENGTH
                let sub_segment_length =
                    core::cmp::min(MAX_EXTENT_LENGTH, segment.len() - segment_start);
                let first_physical_block = segment[segment_start];

                // Create new extent
                let mut newex = Ext4Extent::default();
                newex.first_block = current_iblk;
                newex.store_pblock(first_physical_block);
                newex.block_count = sub_segment_length as u16;

                log::info!("[Batch Append] Inserting extent: first_block={}, block_count={}, physical_block={}", 
                    current_iblk, sub_segment_length, first_physical_block);

                // Validate extent validity
                if !self.is_valid_extent(&newex, inode_ref) {
                    log::error!(
                        "[Batch Append] Invalid extent detected: first_block={}, block_count={}",
                        newex.first_block,
                        newex.block_count
                    );
                    return return_errno_with_message!(Errno::EINVAL, "Invalid extent detected");
                }

                // Insert extent
                self.insert_extent(inode_ref, &mut newex)?;

                // Update next logical block position
                current_iblk = match current_iblk.checked_add(sub_segment_length as u32) {
                    Some(v) => v,
                    None => {
                        return return_errno_with_message!(
                            Errno::EINVAL,
                            "Logical block number overflow"
                        )
                    }
                };

                // Move to next segment
                segment_start += sub_segment_length;
            }

            // Update end position of last extent
            last_extent_end = current_iblk;

            // Validate extent tree state
            let root_header = inode_ref.inode.root_extent_header();
            log::info!("[Batch Append] Updated extent tree state: magic={:x}, entries={}, max={}, depth={}", 
                root_header.magic,
                root_header.entries_count,
                root_header.max_entries_count,
                root_header.depth);
        }

        // Update inode size, ensuring it doesn't overflow
        let new_size = match inode_size.checked_add((allocated_blocks.len() * BLOCK_SIZE) as u64) {
            Some(v) => v,
            None => return return_errno_with_message!(Errno::EINVAL, "File size overflow"),
        };
        inode_ref.inode.set_size(new_size);
        self.write_back_inode(inode_ref);

        Ok(allocated_blocks)
    }

    /// Get the last extent in the extent tree
    fn get_last_extent(&self, inode_ref: &Ext4InodeRef) -> Result<Ext4Extent> {
        let root_header = inode_ref.inode.root_extent_header();
        if root_header.entries_count == 0 {
            return return_errno_with_message!(Errno::ENOENT, "No extents found");
        }

        let mut current_header = root_header;
        let mut current_block = inode_ref.inode.root_extent_block();
        let mut depth = root_header.depth;

        while depth > 0 {
            let index_block = Block::load(&self.block_device, current_block as usize * BLOCK_SIZE);
            let index_header = Ext4ExtentHeader::load_from_u8(&index_block.data[..]);
            if index_header.entries_count == 0 {
                return return_errno_with_message!(Errno::ENOENT, "Invalid extent tree");
            }

            // Get the last index entry
            let last_idx = Ext4ExtentIndex::load_from_u8(
                &index_block.data[EXT4_EXTENT_HEADER_SIZE
                    + (index_header.entries_count - 1) as usize * EXT4_EXTENT_INDEX_SIZE..],
            );
            current_block = last_idx.leaf_lo as u64 | ((last_idx.leaf_hi as u64) << 32);
            current_header = index_header;
            depth -= 1;
        }

        // Get the last extent entry
        let extent_block = Block::load(&self.block_device, current_block as usize * BLOCK_SIZE);
        let extent_header = Ext4ExtentHeader::load_from_u8(&extent_block.data[..]);
        if extent_header.entries_count == 0 {
            return return_errno_with_message!(Errno::ENOENT, "No extent entries found");
        }

        let last_extent = Ext4Extent::load_from_u8(
            &extent_block.data[EXT4_EXTENT_HEADER_SIZE
                + (extent_header.entries_count - 1) as usize * EXT4_EXTENT_SIZE..],
        );

        Ok(last_extent)
    }

    /// Validate an extent
    fn is_valid_extent(&self, extent: &Ext4Extent, inode_ref: &Ext4InodeRef) -> bool {
        // Check if the extent is within valid range
        if extent.first_block >= EXT_MAX_BLOCKS {
            log::error!(
                "[Extent Validation] Extent first block {} exceeds maximum",
                extent.first_block
            );
            return false;
        }

        // Check if the extent length is valid
        if extent.block_count == 0 || extent.block_count > EXT_INIT_MAX_LEN {
            log::error!(
                "[Extent Validation] Invalid extent length {}",
                extent.block_count
            );
            return false;
        }

        // Check if the extent would cause overflow
        if let Some(end_block) = extent.first_block.checked_add(extent.block_count as u32) {
            if end_block > EXT_MAX_BLOCKS {
                log::error!(
                    "[Extent Validation] Extent end block {} exceeds maximum",
                    end_block
                );
                return false;
            }
        } else {
            log::error!("[Extent Validation] Extent block range overflow");
            return false;
        }

        // Check if the physical block is valid
        let pblock = extent.get_pblock();
        if pblock == 0 {
            log::error!("[Extent Validation] Invalid physical block number");
            return false;
        }

        true
    }
}