Thread: implemenation of "df" command to find out free disk space information

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    Question implemenation of "df" command to find out free disk space information

    Hi everyone,

    df command can help to print out various disk space information for each file system and its underlying implementation relies on the statfs function.
    I do not know why, in its implementation, the way of getting the available size of a file system is :

    Code:
    f_bavail x f_bsize / 1024 ;
    // free blocks available to non-super user x block size of the file system / 1 Kbyte
    And, why not use

    Code:
    f_bfree x f_bsize / 1024 
    // total free blocks of the file system x a block size of the file system / 1 Kbyte
    As the struct statfs defined, the f_bavail is the free block available to non-super user, and in the embedded (linux) world, most of the time, the system/program runs under the super user mode on the device. In this case, should we use f_bfree instead if we want to find out the total free space available of a file system?

    Thanks~

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's what I think is being referred to here:

    On an ext2/3/4 filesystem -- which may or may not be the filesystem used in embedded devices, but it is on most normal linux boxes -- 5%* is "reserved for the super-user". This is supposedly to insure there will be space available to important privileged processes if for some reason the filesystem fills up.

    AFAIK, it is not a good idea to run any OS in a filesytem that is more than 90% full -- it will result in significant, noticable performance degradation, so, most people don't do it. In reality, then, I would guess that 5% is never used -- which may be good, since that may reduce fragmentation (which man mke2fs claims).

    Pretty sure it is these which are excluded from f_bavail. Based on what you present, it's still not totally clear that it is included in f_bfree, but in any case, IMO the idea is that those reserved super-user blocks are intended for an emergency and should never be intentionally used unless the other 95% is full (and such a situation would be an emergency). Ie, no don't include them in any kind of free or available space count -- either there is other space that should be used, or else these should still be left free for whatever critical operations that will need to occur either to shut the system down or free more space.

    In fact, I'm sure of that, judging by the df manpage:
    <total space>
    The total size of the file system in 512-byte units. The exact
    meaning of this figure is implementation-defined, but should
    include <space used>, <space free>, plus any space reserved by
    the system not normally available to a user.

    <space free>
    The total amount of space available within the file system for
    the creation of new files by unprivileged users, in 512-byte
    units. When this figure is less than or equal to zero, it shall
    not be possible to create any new files on the file system with-
    out first deleting others, unless the process has appropriate
    privileges. The figure written may be less than zero.
    By "not normally available to a user" I believe this includes normal activities of the super-user, Ie, even if there is only one user, the super-user, that user should not make use of the reserved space.

    However, it is possible to create an ext2/3/4 filesystem with 0% reserved blocks (this is what I normally do with usb keys, mounted partitions, etc -- only the root filesystem has reserved space) which is probably the case in embedded environments. If there are no reserved blocks, the issue is moot.

    *by default; you can set this amount when you create the filesystem
    Last edited by MK27; 12-27-2009 at 10:40 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Hi MK27,

    Thank you very much for your help. The information are very clear, informative, and to-the-point. Especially the illustration/analysis from the man page and your personal view. Thanks!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Whether to use f_bfree or f_bavail is a matter of your own policy. If you want to include the superuser blocks in the count, then include them. Otherwise, don't. It's a simple change to the source for df...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    1
    can u help me for "implementationof ls command in C"

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vanita View Post
    can u help me for "implementationof ls command in C"
    Start here:
    Accessing Directories - The GNU C Library
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  3. hard disk space and spyware
    By oldmancan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-23-2003, 01:24 AM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. Information about disk
    By GaPe in forum C Programming
    Replies: 18
    Last Post: 08-05-2002, 06:48 AM