1

I know there are tools like df which show the disk space remaining on disks but I could not find any info on how this tool actually gets this info. I would imagine the filesystem keeps track of this information somehow but I can't find info on this either.

Is there a plain explanation on how this information is gathered from filesystem (specifically ext4) or any terms that would help for finding this information?

4

You can see what df does using strace:

$ strace df / |& grep -i ext
statfs("/", {f_type=EXT2_SUPER_MAGIC, f_bsize=4096, f_blocks=4611519, f_bfree=864281, f_bavail=624269, f_files=1179648, f_ffree=620737, f_fsid={126240841, 1491846125}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_RELATIME}) = 0

And from man 2 statfs:

The statfs() system call returns information about a mounted
filesystem.  path is the pathname of any file within the mounted
filesystem.  buf is a pointer to a statfs structure defined
approximately as follows:

   struct statfs {
       __fsword_t f_type;    /* Type of filesystem (see below) */
       __fsword_t f_bsize;   /* Optimal transfer block size */
       fsblkcnt_t f_blocks;  /* Total data blocks in filesystem */
       fsblkcnt_t f_bfree;   /* Free blocks in filesystem */
       fsblkcnt_t f_bavail;  /* Free blocks available to
                                unprivileged user */
       fsfilcnt_t f_files;   /* Total file nodes in filesystem */
       fsfilcnt_t f_ffree;   /* Free file nodes in filesystem */
       fsid_t     f_fsid;    /* Filesystem ID */
       __fsword_t f_namelen; /* Maximum length of filenames */
       __fsword_t f_frsize;  /* Fragment size (since Linux 2.6) */
       __fsword_t f_flags;   /* Mount flags of filesystem
                                (since Linux 2.6.36) */
       __fsword_t f_spare[xxx];
                       /* Padding bytes reserved for future use */
   };

If you just want the freespace of a mount point, statfs seems to be way to go. Free blocks * block size = free space ( - reserved space, etc.).


I imagine ext4 must keep the count of free blocks somewhere in the superblock, which you then use with the block size to get the free space.

  • @RuiFRibeiro I think it's that element, but I don't know what lo stands for, hence the vagueness. – muru Mar 9 '18 at 6:25
  • Maybe a mnemonic to remember in some part of the code it is a 32 bits value, I suspect. Not sure either. – Rui F Ribeiro Mar 9 '18 at 6:33
  • 1
    Did neither of you notice the corresponding _hi members a few lines further down? And the comment just above those _hi members? – JdeBP Mar 9 '18 at 6:36
  • @JdeBP nope. I only looked in the immediate vicinity. So s_free_blocks_count_hi*2^32+s_free_blocks_count_lo in 64-bit systems? – muru Mar 9 '18 at 6:43
  • 1
    @JdeBP Even after you talking about them, I was not finding them, had to use the search function. I was telling initially to muru it would be part of the low value of a 64-bit number, but then did not find the the higher part and deleted the comment... dummy. – Rui F Ribeiro Mar 9 '18 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.