sys/param.h: Add DEV_BSIZE and make use of it.

POSIX-2024 says:

> There is no correlation between values of the st_blocks and
> st_blksize, and the f_bsize (from <sys/statvfs.h>) structure members.

Some code in Haiku has mixed that up in the past (e.g. the write_overlay).
This will allow a constant to be used instead, clarifying what's happening.

POSIX-2024 does not actually require this constant, but it does say:

> Traditionally, some implementations defined the multiplier for
> st_blocks in <sys/param.h> as the symbol DEV_BSIZE.

And indeed, glibc, musl, and FreeBSD (at least) all define it.
So it seems to make sense for us to do the same.

Related to #19251.
This commit is contained in:
Augustin Cavalier
2026-01-20 14:43:37 -05:00
parent 79e95cdfc7
commit 999bb2e88a
16 changed files with 22 additions and 25 deletions
@@ -90,7 +90,7 @@ fill_stat_buffer(Inode* inode, struct stat& stat)
} else
stat.st_size = inode->Size();
stat.st_blocks = inode->AllocatedSize() / 512;
stat.st_blocks = inode->AllocatedSize() / DEV_BSIZE;
}
@@ -2226,7 +2226,7 @@ bfs_stat_index(fs_volume* _volume, const char* name, struct stat* stat)
stat->st_mode = node.Mode();
stat->st_size = node.data.Size();
stat->st_blocks = index.Node()->AllocatedSize() / 512;
stat->st_blocks = index.Node()->AllocatedSize() / DEV_BSIZE;
stat->st_nlink = 1;
stat->st_blksize = 65536;
@@ -431,7 +431,7 @@ btrfs_read_stat(fs_volume* _volume, fs_vnode* _node, struct stat* stat)
inode->GetCreationTime(stat->st_crtim);
stat->st_size = inode->Size();
stat->st_blocks = (inode->Size() + 511) / 512;
stat->st_blocks = (inode->Size() + DEV_BSIZE - 1) / DEV_BSIZE;
return B_OK;
}
@@ -519,7 +519,7 @@ fill_stat_buffer(Volume* volume, Inode* inode, Attribute* attribute,
stat.st_type = attribute->Type();
} else {
stat.st_size = inode->Size() + sizeof(wav_header);
stat.st_blocks = inode->Size() / 512;
stat.st_blocks = inode->Size() / DEV_BSIZE;
stat.st_mode = inode->Type();
stat.st_type = 0;
}
@@ -445,7 +445,7 @@ exfat_read_stat(fs_volume* _volume, fs_vnode* _node, struct stat* stat)
inode->GetCreationTime(stat->st_crtim);
stat->st_size = inode->Size();
stat->st_blocks = (inode->Size() + 511) / 512;
stat->st_blocks = (inode->Size() + DEV_BSIZE - 1) / DEV_BSIZE;
return B_OK;
}
@@ -48,17 +48,6 @@
#include "sys/types.h"
#define DEV_BSHIFT 9 /* log2(DEV_BSIZE) */
#define DEV_BSIZE (1 << DEV_BSHIFT)
#ifndef MAXPHYS /* max raw I/O transfer size */
#ifdef __ILP32__
#define MAXPHYS (128 * 1024)
#else
#define MAXPHYS (1024 * 1024)
#endif
#endif
/*
* Machine-independent constants (some used in following include files).
*/
@@ -1783,7 +1783,7 @@ dosfs_rstat(fs_volume* volume, fs_vnode* vnode, struct stat* stat)
fattime2timespec(fatNode->de_CDate, fatNode->de_CTime, fatNode->de_CHun, 1, &stat->st_crtim);
stat->st_blocks = howmany(fatNode->de_FileSize, 512);
stat->st_blocks = howmany(fatNode->de_FileSize, DEV_BSIZE);
RETURN_ERROR(status);
}
@@ -496,7 +496,7 @@ fs_read_stat(fs_volume* _volume, fs_vnode* _node, struct stat* st)
// Same for file/dir in ISO9660
st->st_size = node->dataLen[FS_DATA_FORMAT];
st->st_blocks = (st->st_size + 511) / 512;
st->st_blocks = (st->st_size + DEV_BSIZE) / DEV_BSIZE;
if (ConvertRecDate(&(node->recordDate), &time) == B_NO_ERROR) {
st->st_ctim.tv_sec = st->st_mtim.tv_sec = st->st_atim.tv_sec = time;
st->st_ctim.tv_nsec = st->st_mtim.tv_nsec = st->st_atim.tv_nsec = 0;
@@ -1087,7 +1087,7 @@ AttributeEntry::ReadStat(struct stat *stat)
stat->st_mode = S_ATTR | 0x0777;
stat->st_type = fEntry->type;
stat->st_atime = stat->st_mtime = stat->st_crtime = time(NULL);
stat->st_blocks = (fEntry->size + stat->st_blksize - 1) / stat->st_blksize;
stat->st_blocks = (fEntry->size + DEV_BSIZE - 1) / DEV_BSIZE;
return B_OK;
}
@@ -502,7 +502,7 @@ OverlayInode::ReadStat(struct stat *stat)
_PopulateStat();
memcpy(stat, &fStat, sizeof(struct stat));
stat->st_blocks = (stat->st_size + stat->st_blksize - 1) / stat->st_blksize;
stat->st_blocks = (stat->st_size + DEV_BSIZE - 1) / DEV_BSIZE;
return B_OK;
}
@@ -642,8 +642,8 @@ Inode::GetStat(struct stat* st, OpenAttrCookie* attr)
delete[] values;
st->st_blksize = fFileSystem->Root()->IOSize();
st->st_blocks = st->st_size / st->st_blksize;
st->st_blocks += st->st_size % st->st_blksize == 0 ? 0 : 1;
st->st_blocks = st->st_size / DEV_BSIZE;
st->st_blocks += (st->st_size % DEV_BSIZE) == 0 ? 0 : 1;
return B_OK;
}
@@ -385,7 +385,7 @@ packagefs_read_stat(fs_volume* fsVolume, fs_vnode* fsNode, struct stat* st)
st->st_ctim = st->st_mtim;
// TODO: Perhaps manage a changed time (particularly for directories)?
st->st_crtim = st->st_mtim;
st->st_blocks = (st->st_size + 511) / 512;
st->st_blocks = (st->st_size + DEV_BSIZE - 1) / DEV_BSIZE;
return B_OK;
}
@@ -700,7 +700,7 @@ ramfs_read_stat(fs_volume* _volume, fs_vnode* _node, struct stat *st)
st->st_gid = node->GetGID();
st->st_size = node->GetSize();
st->st_blksize = kOptimalIOSize;
st->st_blocks = (st->st_size + st->st_blksize - 1) / st->st_blksize;
st->st_blocks = (st->st_size + DEV_BSIZE - 1) / DEV_BSIZE;
st->st_atime = node->GetATime();
st->st_mtime = node->GetMTime();
st->st_ctime = node->GetCTime();
@@ -291,7 +291,7 @@ udf_read_stat(fs_volume *_volume, fs_vnode *node, struct stat *stat)
stat->st_mode = icb->Mode();
stat->st_size = icb->Length();
stat->st_blocks = (stat->st_size + 511) / 512;
stat->st_blocks = (stat->st_size + DEV_BSIZE - 1) / DEV_BSIZE;
// File times. For now, treat the modification time as creation
// time as well, since true creation time is an optional extended