C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-27-2009, 12:31 AM   #1
Registered User
 
Join Date: Dec 2009
Posts: 4
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~
learn is offline   Reply With Quote
Old 12-27-2009, 10:30 AM   #2
mastering the obvious
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,071
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:
Quote:
<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
__________________

“The essential element in the black art of obscurantism is not that it wants to darken individual understanding, but that it wants to blacken our picture of the world, and darken our idea of existence.” [Friedrich Wilhelm Nietzsche, 1878]

Last edited by MK27; 12-27-2009 at 10:40 AM.
MK27 is online now   Reply With Quote
Old 12-27-2009, 04:46 PM   #3
Registered User
 
Join Date: Dec 2009
Posts: 4
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!
learn is offline   Reply With Quote
Old 12-28-2009, 12:05 AM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,753
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...
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is online now   Reply With Quote
Old 01-18-2010, 12:33 AM   #5
Registered User
 
Join Date: Jan 2010
Posts: 1
can u help me for "implementationof ls command in C"
vanita is offline   Reply With Quote
Old 01-18-2010, 07:25 AM   #6
mastering the obvious
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,071
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
__________________

“The essential element in the black art of obscurantism is not that it wants to darken individual understanding, but that it wants to blacken our picture of the world, and darken our idea of existence.” [Friedrich Wilhelm Nietzsche, 1878]
MK27 is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What about the problem of "no matching symbolic information found"in this function? gjgcstick C Programming 5 06-01-2009 07:54 PM
Going out of scope nickname_changed C++ Programming 9 10-12-2003 06:27 PM
hard disk space and spyware oldmancan A Brief History of Cprogramming.com 5 01-23-2003 01:24 AM
Special Allegro Information TechWins Game Programming 12 08-20-2002 11:35 PM
Information about disk GaPe C Programming 18 08-05-2002 06:48 AM


All times are GMT -6. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22