statfs (2) - Linux Manuals
statfs: get filesystem statistics
NAME
statfs, fstatfs - get filesystem statistics
SYNOPSIS
#include <sys/vfs.h> /* or <sys/statfs.h> */
int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);
DESCRIPTION
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 {
The following filesystem types may appear in f_type:
ADFS_SUPER_MAGIC 0xadf5
AFFS_SUPER_MAGIC 0xadff
AFS_SUPER_MAGIC 0x5346414f
ANON_INODE_FS_MAGIC 0x09041934 /* Anonymous inode FS (for
Most of these MAGIC constants are defined in /usr/include/linux/magic.h, and some are hardcoded in kernel sources.
The f_flags field is a bit mask indicating mount options for the filesystem. It contains zero or more of the following bits:
- ST_MANDLOCK
- Mandatory locking is permitted on the filesystem (see fcntl(2)).
- ST_NOATIME
- Do not update access times; see mount(2).
- ST_NODEV
- Disallow access to device special files on this filesystem.
- ST_NODIRATIME
- Do not update directory access times; see mount(2).
- ST_NOEXEC
- Execution of programs is disallowed on this filesystem.
- ST_NOSUID
- The set-user-ID and set-group-ID bits are ignored by exec(3) for executable files on this filesystem
- ST_RDONLY
- This filesystem is mounted read-only.
- ST_RELATIME
- Update atime relative to mtime/ctime; see mount(2).
- ST_SYNCHRONOUS
- Writes are synched to the filesystem immediately (see the description of O_SYNC in open(2)).
- ST_NOSYMFOLLOW (since Linux 5.10)
- Symbolic links are not followed when resolving paths; see mount(2).
Nobody knows what f_fsid is supposed to contain (but see below).
Fields that are undefined for a particular filesystem are set to 0.
fstatfs() returns the same information about an open file referenced by descriptor fd.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.ERRORS
- EACCES
- (statfs()) Search permission is denied for a component of the path prefix of path. (See also path_resolution(7).)
- EBADF
- (fstatfs()) fd is not a valid open file descriptor.
- EFAULT
- buf or path points to an invalid address.
- EINTR
- The call was interrupted by a signal; see signal(7).
- EIO
- An I/O error occurred while reading from the filesystem.
- ELOOP
- (statfs()) Too many symbolic links were encountered in translating path.
- ENAMETOOLONG
- (statfs()) path is too long.
- ENOENT
- (statfs()) The file referred to by path does not exist.
- ENOMEM
- Insufficient kernel memory was available.
- ENOSYS
- The filesystem does not support this call.
- ENOTDIR
- (statfs()) A component of the path prefix of path is not a directory.
- EOVERFLOW
- Some values were too large to be represented in the returned struct.
CONFORMING TO
Linux-specific. The Linux statfs() was inspired by the 4.4BSD one (but they do not use the same structure).NOTES
The __fsword_t type used for various fields in the statfs structure definition is a glibc internal type, not intended for public use. This leaves the programmer in a bit of a conundrum when trying to copy or compare these fields to local variables in a program. Using unsigned int for such variables suffices on most systems.The original Linux statfs() and fstatfs() system calls were not designed with extremely large file sizes in mind. Subsequently, Linux 2.6 added new statfs64() and fstatfs64() system calls that employ a new structure, statfs64. The new structure contains the same fields as the original statfs structure, but the sizes of various fields are increased, to accommodate large file sizes. The glibc statfs() and fstatfs() wrapper functions transparently deal with the kernel differences.
Some systems have only <sys/vfs.h>, other systems also have <sys/statfs.h>, where the former includes the latter. So it seems including the former is the best choice.
LSB has deprecated the library calls statfs() and fstatfs() and tells us to use statvfs(2) and fstatvfs(2) instead.
The f_fsid field
Solaris, Irix and POSIX have a system call statvfs(2) that returns a struct statvfs (defined in <sys/statvfs.h>) containing an unsigned long f_fsid. Linux, SunOS, HP-UX, 4.4BSD have a system call statfs() that returns a struct statfs (defined in <sys/vfs.h>) containing a fsid_t f_fsid, where fsid_t is defined as struct { int val[2]; }. The same holds for FreeBSD, except that it uses the include file <sys/mount.h>.The general idea is that f_fsid contains some random stuff such that the pair (f_fsid,ino) uniquely determines a file. Some operating systems use (a variation on) the device number, or the device number combined with the filesystem type. Several operating systems restrict giving out the f_fsid field to the superuser only (and zero it for unprivileged users), because this field is used in the filehandle of the filesystem when NFS-exported, and giving it out is a security concern.
Under some operating systems, the fsid can be used as the second argument to the sysfs(2) system call.
BUGS
From Linux 2.6.38 up to and including Linux 3.1, fstatfs() failed with the error ENOSYS for file descriptors created by pipe(2).COLOPHON
This page is part of release 5.10 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.