The Linux Programming Interface

(nextflipdebug5) #1

276 Chapter 14


14.11 Obtaining Information About a File System: statvfs()......................................................


The statvfs() and fstatvfs() library functions obtain information about a mounted file
system.

The only difference between these two functions is in how the file system is identi-
fied. For statvfs(), we use pathname to specify the name of any file in the file system.
For fstatvfs(), we specify an open file descriptor, fd, referring to any file in the file
system. Both functions return a statvfs structure containing information about the
file system in the buffer pointed to by statvfsbuf. This structure has the following
form:

struct statvfs {
unsigned long f_bsize; /* File-system block size (in bytes) */
unsigned long f_frsize; /* Fundamental file-system block size
(in bytes) */
fsblkcnt_t f_blocks; /* Total number of blocks in file
system (in units of 'f_frsize') */
fsblkcnt_t f_bfree; /* Total number of free blocks */
fsblkcnt_t f_bavail; /* Number of free blocks available to
unprivileged process */
fsfilcnt_t f_files; /* Total number of i-nodes */
fsfilcnt_t f_ffree; /* Total number of free i-nodes */
fsfilcnt_t f_favail; /* Number of i-nodes available to unprivileged
process (set to 'f_ffree' on Linux) */
unsigned long f_fsid; /* File-system ID */
unsigned long f_flag; /* Mount flags */
unsigned long f_namemax; /* Maximum length of filenames on
this file system */
};

The purpose of most of the fields in the statvfs structure is made clear in the com-
ments above. We note a few further points regarding some fields:

z The fsblkcnt_t and fsfilcnt_t data types are integer types specified by SUSv3.
z For most file Linux systems, the values of f_bsize and f_frsize are the same. How-
ever, some file systems support the notion of block fragments, which can be
used to allocate a smaller unit of storage at the end of the file if a full block is
not required. This avoids the waste of space that would otherwise occur if a full
block was allocated. On such file systems, f_frsize is the size of a fragment, and
f_bsize is the size of a whole block. (The notion of fragments in UNIX file sys-
tems first appeared in the early 1980s with the 4.2BSD Fast File System,
described in [McKusick et al., 1984].)

#include <sys/statvfs.h>

int statvfs(const char *pathname, struct statvfs *statvfsbuf);
int fstatvfs(int fd, struct statvfs *statvfsbuf);
Both return 0 on success, or –1 on error
Free download pdf