From: David van Moolenbroek Date: Sun, 4 Mar 2012 11:14:06 +0000 (+0100) Subject: mfs: more accurate stat.st_blocks estimation X-Git-Tag: v3.2.1~679 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=fecfd07997f9b70afd4d54fd861fdc550c8a25b6;p=minix.git 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. --- diff --git a/servers/mfs/stadir.c b/servers/mfs/stadir.c index c34d00321..02314bcb0 100644 --- a/servers/mfs/stadir.c +++ b/servers/mfs/stadir.c @@ -8,6 +8,36 @@ #include "super.h" #include +/*===========================================================================* + * 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 * *===========================================================================*/ @@ -22,7 +52,6 @@ PRIVATE int stat_inode( struct stat statbuf; mode_t mo; 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. */ if (rip->i_update) update_times(rip); @@ -33,10 +62,6 @@ PRIVATE int stat_inode( /* true iff 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)); statbuf.st_dev = rip->i_dev; @@ -51,7 +76,7 @@ PRIVATE int stat_inode( statbuf.st_mtime = rip->i_mtime; statbuf.st_ctime = rip->i_ctime; statbuf.st_blksize = fs_block_size; - statbuf.st_blocks = blocks; + statbuf.st_blocks = estimate_blocks(rip); /* Copy the struct to user space. */ r = sys_safecopyto(who_e, gid, (vir_bytes) 0, (vir_bytes) &statbuf,