1 /* fsusage.c -- return space usage of mounted file systems
3 Copyright (C) 1991-1992, 1996, 1998-1999, 2002-2006, 2009-2017 Free Software
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
26 #if STAT_STATVFS || STAT_STATVFS64 /* POSIX 1003.1-2001 (and later) with XSI */
27 # include <sys/statvfs.h>
29 /* Don't include backward-compatibility files unless they're needed.
30 Eventually we'd like to remove all this cruft. */
33 # include <sys/stat.h>
35 # include <sys/param.h>
38 # include <sys/mount.h>
43 # if HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */
44 # include <sys/fs/s5param.h>
46 # if HAVE_SYS_STATFS_H
47 # include <sys/statfs.h>
49 # if HAVE_DUSTAT_H /* AIX PS/2 */
50 # include <sys/dustat.h>
54 /* Many space usage primitives use all 1 bits to denote a value that is
55 not applicable or unknown. Propagate this information by returning
56 a uintmax_t value that is all 1 bits if X is all 1 bits, even if X
57 is unsigned and narrower than uintmax_t. */
58 #define PROPAGATE_ALL_ONES(x) \
59 ((sizeof (x) < sizeof (uintmax_t) \
60 && (~ (x) == (sizeof (x) < sizeof (int) \
61 ? - (1 << (sizeof (x) * CHAR_BIT)) \
63 ? UINTMAX_MAX : (uintmax_t) (x))
65 /* Extract the top bit of X as an uintmax_t value. */
66 #define EXTRACT_TOP_BIT(x) ((x) \
67 & ((uintmax_t) 1 << (sizeof (x) * CHAR_BIT - 1)))
69 /* If a value is negative, many space usage primitives store it into an
70 integer variable by assignment, even if the variable's type is unsigned.
71 So, if a space usage variable X's top bit is set, convert X to the
72 uintmax_t value V such that (- (uintmax_t) V) is the negative of
73 the original value. If X's top bit is clear, just yield X.
74 Use PROPAGATE_TOP_BIT if the original value might be negative;
75 otherwise, use PROPAGATE_ALL_ONES. */
76 #define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1))
79 /* Return true if statvfs works. This is false for statvfs on systems
80 with GNU libc on Linux kernels before 2.6.36, which stats all
81 preceding entries in /proc/mounts; that makes df hang if even one
82 of the corresponding file systems is hard-mounted but not available. */
83 # if ! (__linux__ && (__GLIBC__ || __UCLIBC__))
84 /* The FRSIZE fallback is not required in this case. */
85 # undef STAT_STATFS2_FRSIZE
86 static int statvfs_works (void) { return 1; }
88 # include <string.h> /* for strverscmp */
89 # include <sys/utsname.h>
90 # include <sys/statfs.h>
91 # define STAT_STATFS2_BSIZE 1
96 static int statvfs_works_cache
= -1;
98 if (statvfs_works_cache
< 0)
99 statvfs_works_cache
= (uname (&name
) == 0
100 && 0 <= strverscmp (name
.release
, "2.6.36"));
101 return statvfs_works_cache
;
107 /* Fill in the fields of FSP with information about space usage for
108 the file system on which FILE resides.
109 DISK is the device on which FILE is mounted, for space-getting
110 methods that need to know it.
111 Return 0 if successful, -1 if not. When returning -1, ensure that
112 ERRNO is either a system error value, or zero if DISK is NULL
113 on a system that requires a non-NULL value. */
115 get_fs_usage (char const *file
, char const *disk
, struct fs_usage
*fsp
)
117 #ifdef STAT_STATVFS /* POSIX, except pre-2.6.36 glibc/Linux */
119 if (statvfs_works ())
123 if (statvfs (file
, &vfsd
) < 0)
126 /* f_frsize isn't guaranteed to be supported. */
127 fsp
->fsu_blocksize
= (vfsd
.f_frsize
128 ? PROPAGATE_ALL_ONES (vfsd
.f_frsize
)
129 : PROPAGATE_ALL_ONES (vfsd
.f_bsize
));
131 fsp
->fsu_blocks
= PROPAGATE_ALL_ONES (vfsd
.f_blocks
);
132 fsp
->fsu_bfree
= PROPAGATE_ALL_ONES (vfsd
.f_bfree
);
133 fsp
->fsu_bavail
= PROPAGATE_TOP_BIT (vfsd
.f_bavail
);
134 fsp
->fsu_bavail_top_bit_set
= EXTRACT_TOP_BIT (vfsd
.f_bavail
) != 0;
135 fsp
->fsu_files
= PROPAGATE_ALL_ONES (vfsd
.f_files
);
136 fsp
->fsu_ffree
= PROPAGATE_ALL_ONES (vfsd
.f_ffree
);
142 #if defined STAT_STATVFS64 /* AIX */
144 struct statvfs64 fsd
;
146 if (statvfs64 (file
, &fsd
) < 0)
149 /* f_frsize isn't guaranteed to be supported. */
150 fsp
->fsu_blocksize
= (fsd
.f_frsize
151 ? PROPAGATE_ALL_ONES (fsd
.f_frsize
)
152 : PROPAGATE_ALL_ONES (fsd
.f_bsize
));
154 #elif defined STAT_STATFS2_FS_DATA /* Ultrix */
158 if (statfs (file
, &fsd
) != 1)
161 fsp
->fsu_blocksize
= 1024;
162 fsp
->fsu_blocks
= PROPAGATE_ALL_ONES (fsd
.fd_req
.btot
);
163 fsp
->fsu_bfree
= PROPAGATE_ALL_ONES (fsd
.fd_req
.bfree
);
164 fsp
->fsu_bavail
= PROPAGATE_TOP_BIT (fsd
.fd_req
.bfreen
);
165 fsp
->fsu_bavail_top_bit_set
= EXTRACT_TOP_BIT (fsd
.fd_req
.bfreen
) != 0;
166 fsp
->fsu_files
= PROPAGATE_ALL_ONES (fsd
.fd_req
.gtot
);
167 fsp
->fsu_ffree
= PROPAGATE_ALL_ONES (fsd
.fd_req
.gfree
);
169 #elif defined STAT_STATFS3_OSF1 /* OSF/1 */
173 if (statfs (file
, &fsd
, sizeof (struct statfs
)) != 0)
176 fsp
->fsu_blocksize
= PROPAGATE_ALL_ONES (fsd
.f_fsize
);
178 #elif defined STAT_STATFS2_FRSIZE /* 2.6 < glibc/Linux < 2.6.36 */
182 if (statfs (file
, &fsd
) < 0)
185 fsp
->fsu_blocksize
= PROPAGATE_ALL_ONES (fsd
.f_frsize
);
187 #elif defined STAT_STATFS2_BSIZE /* glibc/Linux < 2.6, 4.3BSD, SunOS 4, \
188 Mac OS X < 10.4, FreeBSD < 5.0, \
189 NetBSD < 3.0, OpenBSD < 4.4 */
193 if (statfs (file
, &fsd
) < 0)
196 fsp
->fsu_blocksize
= PROPAGATE_ALL_ONES (fsd
.f_bsize
);
198 # ifdef STATFS_TRUNCATES_BLOCK_COUNTS
200 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
201 struct statfs are truncated to 2GB. These conditions detect that
202 truncation, presumably without botching the 4.1.1 case, in which
203 the values are not truncated. The correct counts are stored in
204 undocumented spare fields. */
205 if (fsd
.f_blocks
== 0x7fffffff / fsd
.f_bsize
&& fsd
.f_spare
[0] > 0)
207 fsd
.f_blocks
= fsd
.f_spare
[0];
208 fsd
.f_bfree
= fsd
.f_spare
[1];
209 fsd
.f_bavail
= fsd
.f_spare
[2];
211 # endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
213 #elif defined STAT_STATFS2_FSIZE /* 4.4BSD and older NetBSD */
217 if (statfs (file
, &fsd
) < 0)
220 fsp
->fsu_blocksize
= PROPAGATE_ALL_ONES (fsd
.f_fsize
);
222 #elif defined STAT_STATFS4 /* SVR3, Dynix, old Irix, old AIX, \
225 # if !_AIX && !defined _SEQUENT_ && !defined DOLPHIN
226 # define f_bavail f_bfree
231 if (statfs (file
, &fsd
, sizeof fsd
, 0) < 0)
234 /* Empirically, the block counts on most SVR3 and SVR3-derived
235 systems seem to always be in terms of 512-byte blocks,
236 no matter what value f_bsize has. */
237 # if _AIX || defined _CRAY
238 fsp
->fsu_blocksize
= PROPAGATE_ALL_ONES (fsd
.f_bsize
);
240 fsp
->fsu_blocksize
= 512;
245 #if (defined STAT_STATVFS64 || defined STAT_STATFS3_OSF1 \
246 || defined STAT_STATFS2_FRSIZE || defined STAT_STATFS2_BSIZE \
247 || defined STAT_STATFS2_FSIZE || defined STAT_STATFS4)
249 fsp
->fsu_blocks
= PROPAGATE_ALL_ONES (fsd
.f_blocks
);
250 fsp
->fsu_bfree
= PROPAGATE_ALL_ONES (fsd
.f_bfree
);
251 fsp
->fsu_bavail
= PROPAGATE_TOP_BIT (fsd
.f_bavail
);
252 fsp
->fsu_bavail_top_bit_set
= EXTRACT_TOP_BIT (fsd
.f_bavail
) != 0;
253 fsp
->fsu_files
= PROPAGATE_ALL_ONES (fsd
.f_files
);
254 fsp
->fsu_ffree
= PROPAGATE_ALL_ONES (fsd
.f_ffree
);
258 (void) disk
; /* avoid argument-unused warning */
262 #if defined _AIX && defined _I386
263 /* AIX PS/2 does not supply statfs. */
266 statfs (char *file
, struct statfs
*fsb
)
271 if (stat (file
, &stats
) != 0)
273 if (dustat (stats
.st_dev
, 0, &fsd
, sizeof (fsd
)))
276 fsb
->f_bsize
= fsd
.du_bsize
;
277 fsb
->f_blocks
= fsd
.du_fsize
- fsd
.du_isize
;
278 fsb
->f_bfree
= fsd
.du_tfree
;
279 fsb
->f_bavail
= fsd
.du_tfree
;
280 fsb
->f_files
= (fsd
.du_isize
- 2) * fsd
.du_inopb
;
281 fsb
->f_ffree
= fsd
.du_tinode
;
282 fsb
->f_fsid
.val
[0] = fsd
.du_site
;
283 fsb
->f_fsid
.val
[1] = fsd
.du_pckno
;
287 #endif /* _AIX && _I386 */