咨询:文件大小为0却被分配8个块的原因
abc.dat Show 8 Allocated Blocks? Great question—since you've already ruled out sparse files, symlinks, and basic metadata oddities, let's dive into the most likely causes, including the filesystem configuration quirks you're asking about:
1. Extended Attributes Storing Data in External Blocks
This is the most common culprit for this behavior. Filesystems like ext4, XFS, and Btrfs store small extended attributes (think security labels, custom file metadata) directly in the file's inode. But if you've added large attributes that don't fit in the inode's reserved space, the filesystem will carve out separate disk blocks to hold them.
Even though your file has 0 bytes of actual content, ls counts these attribute blocks in its block tally. To check this, run:
getfattr -d abc.dat
If you see lengthy attributes or values here, that's exactly what's happening.
2. Filesystem Minimum Allocation Rules
Yes, some filesystem configurations or mount options can enforce a minimum block allocation for any file—even empty ones:
- Network filesystems (NFS, SMB): Some setups pre-allocate a small number of blocks for new files to speed up future writes, even if the file starts empty.
- Legacy/optimized filesystems: Older ext2 setups or ext3/4 with specific mount flags might allocate a default block count for files created with certain open flags (like
O_DIRECT).
To check your filesystem type and mount settings, run:
# Get filesystem type df -T abc.dat # Get mount options for that filesystem mount | grep $(df -P abc.dat | tail -1 | awk '{print $1}')
3. Leftover Pre-Allocated Blocks
If someone used a tool like fallocate to pre-allocate blocks for abc.dat, then truncated the file to 0 bytes afterward, some filesystems might leave those blocks allocated (though most will release them immediately). Use du to see actual disk usage vs ls's logical size:
du -h abc.dat
If du shows non-zero usage, those pre-allocated blocks are still hanging around.
4. Filesystem Corruption (Last Resort)
In rare cases, filesystem corruption can cause mismatches between reported file size and allocated blocks. If none of the above checks explain it, run a filesystem check (e.g., fsck.ext4 for ext4) to rule out this issue.
内容的提问来源于stack exchange,提问作者林果皞

