statfs(2)

๐Ÿ‘‘ statfs, fstatfs โ€” get filesystem statistics ๐Ÿ‘‘

hiii bestie ๐Ÿ’– so this is statfs(2) โ€” yeah, section 2, the System Calls Manual, the real syscall zone, not some libc wrapper pretending. We're getting the tea on a mounted filesystem: how much space, how many inodes, what kind of filesystem it even is. Let's get into it, no cap.


โœจ NAME โœจ

statfs, fstatfs โ€” get filesystem statistics ๐Ÿ“


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). yes, the classic. ๐Ÿซถ


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <sys/vfs.h>    /* or <sys/statfs.h> */

int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);
okay so real talk: unless you specifically need the f_type field, you should be using the standard statvfs(3) interface instead. that's the glow-up version, bestie. ๐Ÿ’…

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So statfs() is a system call that hands you info about a mounted filesystem. path is the pathname of any file inside that mounted filesystem โ€” doesn't have to be the mount point itself, any file hanging out in there works. And buf is a pointer to a statfs structure, which looks approximately like this:

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 inodes in filesystem */
    fsfilcnt_t f_ffree;   /* Free inodes 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 */
};

๐ŸŽ€ the f_type mood board ๐ŸŽ€

These are the filesystem types that can show up in f_type. it's giving โœจpokรฉdexโœจ, there's SO many, I'm obsessed:

ADFS_SUPER_MAGIC      0xadf5
AFFS_SUPER_MAGIC      0xadff
AFS_SUPER_MAGIC       0x5346414f
ANON_INODE_FS_MAGIC   0x09041934 /* Anonymous inode FS (for
                                    pseudofiles that have no name;
                                    e.g., epoll, signalfd, bpf) */
AUTOFS_SUPER_MAGIC    0x0187
BDEVFS_MAGIC          0x62646576
BEFS_SUPER_MAGIC      0x42465331
BFS_MAGIC             0x1badface
BINFMTFS_MAGIC        0x42494e4d
BPF_FS_MAGIC          0xcafe4a11
BTRFS_SUPER_MAGIC     0x9123683e
BTRFS_TEST_MAGIC      0x73727279
CGROUP_SUPER_MAGIC    0x27e0eb   /* Cgroup pseudo FS */
CGROUP2_SUPER_MAGIC   0x63677270 /* Cgroup v2 pseudo FS */
CIFS_MAGIC_NUMBER     0xff534d42
CODA_SUPER_MAGIC      0x73757245
COH_SUPER_MAGIC       0x012ff7b7
CRAMFS_MAGIC          0x28cd3d45
DEBUGFS_MAGIC         0x64626720
DEVFS_SUPER_MAGIC     0x1373     /* Linux 2.6.17 and earlier */
DEVPTS_SUPER_MAGIC    0x1cd1
ECRYPTFS_SUPER_MAGIC  0xf15f
EFIVARFS_MAGIC        0xde5e81e4
EFS_SUPER_MAGIC       0x00414a53
EXT_SUPER_MAGIC       0x137d     /* Linux 2.0 and earlier */
EXT2_OLD_SUPER_MAGIC  0xef51
EXT2_SUPER_MAGIC      0xef53
EXT3_SUPER_MAGIC      0xef53
EXT4_SUPER_MAGIC      0xef53
F2FS_SUPER_MAGIC      0xf2f52010
FUSE_SUPER_MAGIC      0x65735546
FUTEXFS_SUPER_MAGIC   0xbad1dea  /* Unused */
HFS_SUPER_MAGIC       0x4244
HOSTFS_SUPER_MAGIC    0x00c0ffee
HPFS_SUPER_MAGIC      0xf995e849
HUGETLBFS_MAGIC       0x958458f6
ISOFS_SUPER_MAGIC     0x9660
JFFS2_SUPER_MAGIC     0x72b6
JFS_SUPER_MAGIC       0x3153464a
MINIX_SUPER_MAGIC     0x137f     /* original minix FS */
MINIX_SUPER_MAGIC2    0x138f     /* 30 char minix FS */
MINIX2_SUPER_MAGIC    0x2468     /* minix V2 FS */
MINIX2_SUPER_MAGIC2   0x2478     /* minix V2 FS, 30 char names */
MINIX3_SUPER_MAGIC    0x4d5a     /* minix V3 FS, 60 char names */
MQUEUE_MAGIC          0x19800202 /* POSIX message queue FS */
MSDOS_SUPER_MAGIC     0x4d44
MTD_INODE_FS_MAGIC    0x11307854
NCP_SUPER_MAGIC       0x564c
NFS_SUPER_MAGIC       0x6969
NILFS_SUPER_MAGIC     0x3434
NSFS_MAGIC            0x6e736673
NTFS_SB_MAGIC         0x5346544e
OCFS2_SUPER_MAGIC     0x7461636f
OPENPROM_SUPER_MAGIC  0x9fa1
OVERLAYFS_SUPER_MAGIC 0x794c7630
PIPEFS_MAGIC          0x50495045
PROC_SUPER_MAGIC      0x9fa0     /* /proc FS */
PSTOREFS_MAGIC        0x6165676c
QNX4_SUPER_MAGIC      0x002f
QNX6_SUPER_MAGIC      0x68191122
RAMFS_MAGIC           0x858458f6
REISERFS_SUPER_MAGIC  0x52654973
ROMFS_MAGIC           0x7275
SECURITYFS_MAGIC      0x73636673
SELINUX_MAGIC         0xf97cff8c
SMACK_MAGIC           0x43415d53
SMB_SUPER_MAGIC       0x517b
SMB2_MAGIC_NUMBER     0xfe534d42
SOCKFS_MAGIC          0x534f434b
SQUASHFS_MAGIC        0x73717368
SYSFS_MAGIC           0x62656572
SYSV2_SUPER_MAGIC     0x012ff7b6
SYSV4_SUPER_MAGIC     0x012ff7b5
TMPFS_MAGIC           0x01021994
TRACEFS_MAGIC         0x74726163
UDF_SUPER_MAGIC       0x15013346
UFS_MAGIC             0x00011954
USBDEVICE_SUPER_MAGIC 0x9fa2
V9FS_MAGIC            0x01021997
VXFS_SUPER_MAGIC      0xa501fcf5
XENFS_SUPER_MAGIC     0xabba1974
XENIX_SUPER_MAGIC     0x012ff7b4
XFS_SUPER_MAGIC       0x58465342
_XIAFS_SUPER_MAGIC    0x012fd16d /* Linux 2.0 and earlier */

Most of these MAGIC constants live in /usr/include/linux/magic.h, and a few are just hardcoded straight into kernel sources. iykyk ๐Ÿงš


๐Ÿ”ฅ f_flags โ€” the mount-option bitmask ๐Ÿ”ฅ

The f_flags field is a bit mask that tells you the mount options for the filesystem. It holds zero or more of these bits:

  • ST_MANDLOCK โ€” mandatory locking is permitted on the filesystem (peep fcntl(2) for that one).
  • ST_NOATIME โ€” do not update access times; see mount(2).
  • ST_NODEV โ€” access to device special files is disallowed on this filesystem.
  • ST_NODIRATIME โ€” do not update directory access times; see mount(2).
  • ST_NOEXEC โ€” executing programs is disallowed on this filesystem.
  • ST_NOSUID โ€” the set-user-ID and set-group-ID bits get 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 get synched to the filesystem immediately (check 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 get set to 0. simple as that.

And fstatfs() gives you the exact same info about an open file referenced by the descriptor fd. same energy, different entry point. ๐Ÿ’ซ


๐ŸŒˆ RETURN VALUE ๐ŸŒˆ

  • Success? zero comes back. slay.
  • Error? -1 comes back, and errno gets set to tell you what went wrong.

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EACCES (statfs()) โ€” search permission is denied for a component of the path prefix of path. (also see 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 got interrupted by a signal; see signal(7).
  • EIO โ€” an I/O error happened while reading from the filesystem.
  • ELOOP (statfs()) โ€” too many symbolic links were encountered while translating path.
  • ENAMETOOLONG (statfs()) โ€” path is too long.
  • ENOENT (statfs()) โ€” the file referred to by path does not exist.
  • ENOMEM โ€” not enough 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. ๐Ÿ˜ญ

๐Ÿงš VERSIONS ๐Ÿงš

๐Ÿ’œ the f_fsid field ๐Ÿ’œ

Solaris, Irix, and POSIX all have a system call statvfs(2) that returns a struct statvfs (defined over in <sys/statvfs.h>) containing an unsigned long f_fsid. Meanwhile Linux, SunOS, HP-UX, and 4.4BSD have a system call statfs() returning a struct statfs (defined in <sys/vfs.h>) containing a fsid_t f_fsid, where fsid_t is defined as struct { int val[2]; }. Same deal for FreeBSD, except it pulls from the include file <sys/mount.h>.

The general idea is that f_fsid holds 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. And several operating systems only hand out the f_fsid field to the superuser (zeroing it for unprivileged users), because that field gets used in the filehandle of the filesystem when it's NFS-exported, and giving it out is a security concern. ๐Ÿšจ not the vibes.

Under some operating systems, the fsid can be used as the second argument to the sysfs(2) system call.


โญ STANDARDS โญ

Linux. that's it, that's the standard. periodt. ๐Ÿ’…


๐Ÿ“œ HISTORY ๐Ÿ“œ

The Linux statfs() was inspired by the 4.4BSD one โ€” but they do not use the same structure, so don't get it twisted.

The original Linux statfs() and fstatfs() system calls were not designed with extremely large file sizes in mind. ๐Ÿ‘‘ Then Linux 2.6 came through and added new statfs64() and fstatfs64() system calls that use a new structure, statfs64. That new structure contains the same fields as the original statfs structure, but the sizes of various fields got increased to accommodate large file sizes. The glibc statfs() and fstatfs() wrapper functions transparently deal with the kernel differences, so you don't gotta worry about it. ๐Ÿซถ

LSB has deprecated the library calls statfs() and fstatfs(), and it tells us to use statvfs(3) and fstatvfs(3) instead.


๐Ÿ“ NOTES ๐Ÿ“

The __fsword_t type used for various fields in the statfs structure definition is a glibc internal type, not intended for public use. Which 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 โ€” problem solved, bestie. โœจ

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.


๐Ÿ› 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). we don't talk about that era. ๐Ÿ™ˆ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

stat(2), statvfs(3), path_resolution(7)


okay that's the whole page, you ate it, go check your filesystems ๐Ÿ’– this was Linux man-pages 6.20, dated 2026-02-08 โ€” bye bestie, mwah! ๐ŸŽ€๐ŸŒˆ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms