Mismatch between statvfs call and df command

When I use the statvfs command on a Linux machine to get available free space on a mounted file system, the number I get is slightly different from what df reports.

For example, on the computer that I have with a 500G hard drive, I get the following output from df:

# df --block-size=1 --no-sync
Filesystem           1B-blocks      Used Available Use% Mounted on
/dev/md0             492256247808 3422584832 463828406272   1% /
tmpfs                2025721856         0 2025721856   0% /lib/init/rw
varrun               2025721856    114688 2025607168   1% /var/run
varlock              2025721856      4096 2025717760   1% /var/lock
udev                 2025721856    147456 2025574400   1% /dev
tmpfs                2025721856     94208 2025627648   1% /dev/shm

The statvfs call gives me a block size of 4096 and 119344155 free blocks, so there should be 488 833 658 880 bytes for free. However, df reports that there are 463,828,406,272 bytes. Why is there a mismatch here?

+5
source share
2 answers

5% [1], , root, , df → f_bfree statvfs → f_bavail, df.

[1]: (488833658880 - 463828406272)/492256247808 = 0,0508

+7
#include <stdio.h>
#include <sys/statvfs.h>
int main(){
    struct statvfs stat;
    int a=statvfs("/",&stat);
    printf("total free disk space of the partition: %d GB \n",(stat.f_bavail)*8/2097152);
    //512 is 2^9 - one half of a kilobyte. 
    //A kilobyte is 2^10. A megabyte is 2^20. A gigabyte is 2^30. A terabyte
    //is 2^40. And so on. The common computer units go up by 10 of powers
    //of 2 like that.
    //So you need to divide by 2^(30-9) == 2^21 == 2097152 to get gigabytes.
    //And multiply by 8 because 1 byte=8bit
    return 0;
}

, Gb, , . , ,

0

All Articles