mfs: more accurate stat.st_blocks estimation

On MFS file systems, the stat(2) call now counts indirect blocks as
part of the st_blocks calculation, in addition to proper initial
rounding of the file size. The returned value is now a true upper
bound on the actual number of 512-byte blocks allocated to the file.
As before, it is not accurate for sparse files.
This commit is contained in:
David van Moolenbroek 2012-03-04 12:14:06 +01:00
parent 075f839810
commit fecfd07997

View file

@ -8,6 +8,36 @@
#include "super.h" #include "super.h"
#include <minix/vfsif.h> #include <minix/vfsif.h>
/*===========================================================================*
* estimate_blocks *
*===========================================================================*/
PRIVATE blkcnt_t estimate_blocks(struct inode *rip)
{
/* Return the number of 512-byte blocks used by this file. This includes space
* used by data zones and indirect blocks (actually also zones). Reading in all
* indirect blocks is too costly for a stat call, so we disregard holes and
* return a conservative estimation.
*/
unsigned int zone_size, zones, sindirs, dindirs, nr_indirs, sq_indirs;
/* Compute the number of zones used by the file. */
zone_size = rip->i_sp->s_block_size << rip->i_sp->s_log_zone_size;
zones = (rip->i_size + zone_size - 1) / zone_size;
/* Compute the number of indirect blocks needed for that zone count. */
nr_indirs = rip->i_nindirs;
sq_indirs = nr_indirs * nr_indirs;
sindirs = (zones - rip->i_ndzones + nr_indirs - 1) / nr_indirs;
dindirs = (sindirs - 1 + sq_indirs - 1) / sq_indirs;
/* Return the number of 512-byte blocks corresponding to the number of data
* zones and indirect blocks.
*/
return (zones + sindirs + dindirs) * (zone_size / 512);
}
/*===========================================================================* /*===========================================================================*
* stat_inode * * stat_inode *
*===========================================================================*/ *===========================================================================*/
@ -22,7 +52,6 @@ PRIVATE int stat_inode(
struct stat statbuf; struct stat statbuf;
mode_t mo; mode_t mo;
int r, s; int r, s;
u32_t blocks; /* The unit of this is 512 */
/* Update the atime, ctime, and mtime fields in the inode, if need be. */ /* Update the atime, ctime, and mtime fields in the inode, if need be. */
if (rip->i_update) update_times(rip); if (rip->i_update) update_times(rip);
@ -33,10 +62,6 @@ PRIVATE int stat_inode(
/* true iff special */ /* true iff special */
s = (mo == I_CHAR_SPECIAL || mo == I_BLOCK_SPECIAL); s = (mo == I_CHAR_SPECIAL || mo == I_BLOCK_SPECIAL);
blocks = rip->i_size / 512;
if (rip->i_size % 512 != 0)
blocks += 1;
memset(&statbuf, 0, sizeof(struct stat)); memset(&statbuf, 0, sizeof(struct stat));
statbuf.st_dev = rip->i_dev; statbuf.st_dev = rip->i_dev;
@ -51,7 +76,7 @@ PRIVATE int stat_inode(
statbuf.st_mtime = rip->i_mtime; statbuf.st_mtime = rip->i_mtime;
statbuf.st_ctime = rip->i_ctime; statbuf.st_ctime = rip->i_ctime;
statbuf.st_blksize = fs_block_size; statbuf.st_blksize = fs_block_size;
statbuf.st_blocks = blocks; statbuf.st_blocks = estimate_blocks(rip);
/* Copy the struct to user space. */ /* Copy the struct to user space. */
r = sys_safecopyto(who_e, gid, (vir_bytes) 0, (vir_bytes) &statbuf, r = sys_safecopyto(who_e, gid, (vir_bytes) 0, (vir_bytes) &statbuf,