Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions filesystem/ext4/ext4.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ The conversion usually happens when a single entry will exceed the capacity of a
1. Find the data blocks for that inode, see [inode to data blocks](#inode-to-data-blocks).
1. Interpret the data blocks.

The extents describe only the blocks that are allocated, so they need not cover the whole file. When reading a logical byte range:

* File blocks that no extent covers (the holes of a sparse file) read as zeros.
* Uninitialized extents (length field > 32768, see [inode to data blocks](#inode-to-data-blocks)) are allocated but were never written; they read as zeros and their disk blocks are not fetched.
* Extents must be sorted by file block and must not overlap. A mapping that violates this is treated as corrupt and the read fails instead of returning data from the wrong place.

### Create File

1. Walk the tree until you find the inode for the parent directory.
Expand Down
118 changes: 66 additions & 52 deletions filesystem/ext4/file.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ext4

import (
"errors"
"fmt"
"io"
"io/fs"
"math"

"github.com/diskfs/go-diskfs/filesystem"
)
Expand Down Expand Up @@ -32,70 +34,82 @@ func (fl *File) Read(b []byte) (int, error) {
if fl.fileType == dirFileTypeDirectory {
return 0, fmt.Errorf("cannot read directory")
}
var (
fileSize = int64(fl.size)
blocksize = uint64(fl.filesystem.superblock.blockSize)
)
if fl.offset < 0 || fl.size > math.MaxInt64 {
return 0, fmt.Errorf("invalid ext4 file size or offset")
}
fileSize := int64(fl.size)
if fl.offset >= fileSize {
return 0, io.EOF
}

// Calculate the number of bytes to read
bytesToRead := int64(len(b))
if fl.offset+bytesToRead > fileSize {
bytesToRead = fileSize - fl.offset
if len(b) == 0 {
return 0, nil
}

// Create a buffer to hold the bytes to be read
readBytes := int64(0)
b = b[:bytesToRead]

// the offset given for reading is relative to the file, so we need to calculate
// where these are in the extents relative to the file
readStartBlock := uint64(fl.offset) / blocksize
for _, e := range fl.extents {
// if the last block of the extent is before the first block we want to read, skip it
// notice: need equal sign
if uint64(e.fileBlock)+uint64(e.count) <= readStartBlock {
continue
blockSize := int64(fl.filesystem.superblock.blockSize)
if blockSize < 1024 || blockSize > 65536 || blockSize&(blockSize-1) != 0 {
return 0, fmt.Errorf("invalid ext4 block size")
}
var previousEnd int64
for _, extent := range fl.extents {
count := int64(extent.count)
if count > 32768 {
count -= 32768
}
// extentSize is the number of bytes on the disk for the extent
extentSize := int64(e.count) * int64(blocksize)
// where do we start and end in the extent?
startPositionInExtent := fl.offset - int64(e.fileBlock)*int64(blocksize)

// Protect
if startPositionInExtent < 0 || startPositionInExtent > extentSize {
return int(readBytes), fmt.Errorf("read offset %d falls outside extent (fileBlock %d, count %d) of file %s", fl.offset, e.fileBlock, e.count, fl.filename)
start := int64(extent.fileBlock) * blockSize
end := start + count*blockSize
if count == 0 || start < previousEnd {
return 0, fmt.Errorf("invalid ext4 extent order or length")
}

leftInExtent := extentSize - startPositionInExtent
// how many bytes are left to read
toReadInOffset := bytesToRead - readBytes
if toReadInOffset > leftInExtent {
toReadInOffset = leftInExtent
if extent.count <= 32768 && (extent.startingBlock > uint64(math.MaxInt64)/uint64(blockSize) ||
int64(extent.startingBlock)*blockSize > math.MaxInt64-count*blockSize) {
return 0, fmt.Errorf("ext4 extent address overflows")
}
// read those bytes
startPosOnDisk := e.startingBlock*blocksize + uint64(startPositionInExtent)
b2 := make([]byte, toReadInOffset)
read, err := fl.filesystem.backend.ReadAt(b2, int64(startPosOnDisk))
if err != nil {
return int(readBytes), fmt.Errorf("failed to read bytes: %v", err)
previousEnd = end
}
want := int64(len(b))
if want > fileSize-fl.offset {
want = fileSize - fl.offset
}
start, end := fl.offset, fl.offset+want
clear(b[:want])
for _, extent := range fl.extents {
count := int64(extent.count)
unwritten := count > 32768
if unwritten {
count -= 32768
}
copy(b[readBytes:], b2[:read])
readBytes += int64(read)
fl.offset += int64(read)

if readBytes >= bytesToRead {
extentStart := int64(extent.fileBlock) * blockSize
extentEnd := extentStart + count*blockSize
if extentStart >= end {
// extents are validated to be sorted, so nothing further can overlap the window
break
}
overlapStart, overlapEnd := max(start, extentStart), min(end, extentEnd)
if overlapStart >= overlapEnd || unwritten {
continue
}
destination := b[overlapStart-start : overlapEnd-start]
physicalOffset := int64(extent.startingBlock)*blockSize + overlapStart - extentStart
n, err := fl.filesystem.backend.ReadAt(destination, physicalOffset)
if n < 0 || n > len(destination) {
fl.offset = overlapStart
return int(overlapStart - start), errors.Join(fmt.Errorf("ext4 backend returned invalid read count"), err)
}
if n != len(destination) {
err = errors.Join(io.ErrUnexpectedEOF, err)
} else if err == io.EOF {
// io.ReaderAt may report io.EOF together with a complete read at the end of the backend
err = nil
}
if err != nil {
fl.offset = overlapStart + int64(n)
return int(fl.offset - start), fmt.Errorf("failed to read extent bytes: %w", err)
}
}
var err error
if fl.offset >= fileSize {
err = io.EOF
fl.offset = end
if end == fileSize {
return int(want), io.EOF
}

return int(readBytes), err
return int(want), nil
}

// Write writes len(b) bytes to the File.
Expand Down
Loading
Loading