Skip to content

C0-11: Store the uncompressed object size so HeadObject does not report the compressed length #170

Closed

Description

@scttfrdmn

Summary

When transparent compression is active, HeadObject reports the compressed byte count as the object size. That value reaches the kernel through Getattr, so the kernel stops every read at the compressed length and every compressed file reads back silently truncated. Compression is enabled by default on the mount path, so this affects default deployments.

This outranks #153 in severity: #153 requires EnableCargoShipOptimization to be reachable (it is not, on the mount path today), whereas this defect is live right now on any default mount.

Background

The full chain, verified end to end:

  1. Nothing records the original size. PutObject (internal/storage/s3/backend.go) sets ContentLength: aws.Int64(int64(len(uploadData))) — uploadData is post-compression. The metadata map it writes contains only objectfs-sha256. Grep for original-size / originalSize / objectfs-size across internal/storage/s3/ returns zero hits. The uncompressed length is computed and then discarded.

  2. HeadObject reads the compressed length back. It assigns Size: aws.ToInt64(result.ContentLength), which for a compressed object is the stored (compressed) size.

  3. That size reaches the kernel. DirectoryNode.Lookup (internal/fuse/filesystem.go:202) calls HeadObject, caches the result via cacheInfo, and builds the child node from it. FileNode.Getattr (filesystem.go:410) then does:

    out.Size = safeInt64ToUint64(f.info.Size)   // compressed size
  4. The kernel truncates the read. stat and ls -l report the compressed size, and the kernel issues no read beyond it. GetObject correctly decompresses to the full original length, but the caller never asks for those bytes. The user receives a correctly-decompressed prefix, silently short.

Compression is on by default for real mounts. buildS3Config (internal/adapter/adapter.go) maps a.config.WriteBuffer.Compression into s3.CompressionConfig, and those defaults (internal/config/config.go:359-364) are Enabled: true, gzip, level 6, MinSize: "1KB". A 1 KB threshold means nearly every file of consequence is compressed. (s3.NewDefaultConfig independently defaults to Enabled: true with zstd/3/4KB — see #157 for that mismatch.)

Why the existing tests miss it. The compression round-trip tests call GetObject(ctx, key, 0, 0) directly and assert on the returned bytes, which is the one path that works. No test calls HeadObject on a compressed object, and no test exercises the FUSE layer. The bug lives entirely in the gap between them.

Implementation

Persist the uncompressed length at write time and prefer it at read time.

  1. In PutObject, when compression was applied, add the original length to the metadata map alongside the existing checksum:

    metadata := map[string]string{"objectfs-sha256": checksumHex}
    if wasCompressed {
        metadata["objectfs-original-size"] = strconv.FormatInt(int64(len(data)), 10)
    }

    Apply the same in initiateMultipartUpload (internal/storage/s3/multipart_upload.go:50), which builds its own metadata map.

  2. In HeadObject, when objectfs-original-size is present and parses, use it for info.Size; otherwise fall back to ContentLength. Objects written before this change have no such key and remain compressed-size — call that out in CHANGELOG.md as a known limitation for pre-0.10.1 compressed objects.

  3. Audit the other ObjectInfo.Size producer, ListObjects, which reports Size from the list response and has no metadata available at all. ListObjects cannot recover the original size without a per-object HeadObject. Decide explicitly: either accept compressed sizes in Readdir (directory listings show wrong sizes but reads still work, because Lookup -> HeadObject supplies the size the kernel actually uses for I/O), or document the divergence. Do not add a HeadObject-per-entry fan-out in Readdir without measuring it.

  4. Also fix the ContentLength on the standard PutObject path only if S3 requires the wire length — it does, so leave ContentLength as the compressed length. This issue is about ObjectInfo.Size, not the HTTP header.

Acceptance Criteria

  • Unit test: PutObject of a compressible payload above MinSize writes objectfs-original-size matching len(data); a payload below MinSize does not write the key.

  • Unit test: HeadObject returns the uncompressed size when the key is present, and falls back to ContentLength when it is absent.

  • Integration test asserting the defect is gone — with compression enabled, write a highly compressible payload, then HeadObject and assert info.Size == len(original), not the stored length:

    AWS_PROFILE=aws AWS_REGION=us-west-2 go test -race -tags=integration ./internal/storage/s3/...
  • FUSE-level test: Getattr on a compressed object reports the uncompressed size. This is the assertion that would have caught the bug.

  • Live mount check — the decisive end-to-end gate:

    # with compression enabled, on a mount
    dd if=/dev/zero of=/mnt/objectfs/test/f bs=1M count=10
    stat -f %z /mnt/objectfs/test/f     # must report 10485760, not the compressed size
    wc -c < /mnt/objectfs/test/f        # must report 10485760
  • go test -race ./internal/storage/s3/... ./internal/fuse/... passes; package coverage stays at or above 80%.

Dependencies

None — independent of #153, #154, and #155, and should land in the same release. Interacts with #157 (which changes which codec runs on the mount path but not whether compression is on) and #156 (the parallel-read path is disabled whenever compression is active, so these two features are mutually exclusive by design today).

Activity

  1. added
    type: bug
    priority: critical
    area: s3
    on Jul 30, 2026
  2. scttfrdmn commented on Jul 30, 2026

    @scttfrdmn
    OwnerAuthor

    Landed

    Fixed on main (uncommitted at time of writing; will reference the commit once pushed).

    The size bug. PutObject now writes objectfs-original-size (uncompressed byte length) into S3 user metadata whenever compression actually shrank the object, and HeadObject reports it via a new originalSize() helper. Objects written before this change have no such key and fall back to ContentLength, so the change is backward compatible.

    A second bug found while fixing the first. putObjectMultipart was calling sha256.Sum256(data) on its own — but by that point data held the compressed bytes, whereas the single-part path hashes the uncompressed content. The two upload paths were writing hashes of different byte streams under the same objectfs-sha256 key. Any read-side verification (#155) would have failed on every compressed multipart object. Fixed by threading the single-part path's metadata map down through putObjectMultipart → initiateMultipartUpload instead of recomputing; the multipart helper now carries a comment explaining why it must not hash locally.

    Verified against real S3 (objectfs-test-scttfrdmn, us-west-2), both paths:

    • TestCompressedObjectSizeReporting/single-part — 64 KB compressible object
    • TestCompressedObjectSizeReporting/multipart — 40 MB object, above MultipartThreshold

    Each asserts uncompressed info.Size, a checksum over the uncompressed content, byte-identical round-trip, and that a read of exactly info.Size bytes returns the whole object. TestZSTDCompression gained the size assertion it was missing, plus raw-client checks that the stored ContentLength is smaller than the logical size and ContentEncoding is zstd — that distinction matters, since originalSize correctly reports the logical size through the ObjectFS API regardless of what is on the wire.

    Confirmed the tests detect the bug. With the fix temporarily neutralized, the 40 MB file reported 4,556 bytes and the 64 KB file 87 bytes.

    Deliberately not fixed here

    ListObjects still reports compressed sizes: the S3 list response carries no user metadata, so recovering the logical size would need a HeadObject fan-out per key — an N-fold request amplification on every readdir. It does not affect correctness of reads, because the kernel takes file size from Lookup → HeadObject, not from readdir. Noted in a code comment at the call site.

  3. scttfrdmn commented on Aug 2, 2026

    @scttfrdmn
    OwnerAuthor

    Fixed in v0.10.1 (27d2324). The uncompressed size is stored as objectfs-original-size and HeadObject reports it, so a compressed object no longer advertises its compressed length. Measured before the fix: a 32 KB object reported 4,556 bytes and a 64 KB object reported 87 bytes, making over 99.99% of the data unreachable through a mount. Objects written before this change fall back to ContentLength as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: s3priority: criticaltype: bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions