1 /* fsusage.c -- return space usage of mounted filesystems
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <sys/types.h>
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
32 #ifdef HAVE_SYS_MOUNT_H
33 #include <sys/mount.h>
40 #if defined(HAVE_SYS_FILSYS_H) && !defined(_CRAY)
41 #include <sys/filsys.h> /* SVR2. */
48 #ifdef HAVE_SYS_STATFS_H
49 #include <sys/statfs.h>
52 #ifdef HAVE_DUSTAT_H /* AIX PS/2. */
53 #include <sys/dustat.h>
56 #ifdef HAVE_SYS_STATVFS_H /* SVR4. */
57 #include <sys/statvfs.h>
63 /* Return the number of TOSIZE-byte blocks used by
64 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
65 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
68 adjust_blocks (blocks, fromsize, tosize)
77 if (fromsize == tosize) /* E.g., from 512 to 512. */
79 else if (fromsize > tosize) /* E.g., from 2048 to 512. */
80 return blocks * (fromsize / tosize);
81 else /* E.g., from 256 to 512. */
82 return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
85 /* Fill in the fields of FSP with information about space usage for
86 the filesystem on which PATH resides.
87 DISK is the device on which PATH is mounted, for space-getting
88 methods that need to know it.
89 Return 0 if successful, -1 if not. */
92 get_fs_usage (path, disk, fsp)
97 #if defined (STAT_STATFS3_OSF1)
100 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
102 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
103 #endif /* STAT_STATFS3_OSF1 */
105 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
108 if (statfs (path, &fsd) != 1)
110 #define CONVERT_BLOCKS(b) adjust_blocks ((b), 1024, 512)
111 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
112 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
113 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
114 fsp->fsu_files = fsd.fd_req.gtot;
115 fsp->fsu_ffree = fsd.fd_req.gfree;
118 #ifdef STAT_READ_FILSYS /* SVR2. */
120 #define SUPERBOFF (SUPERB * 512)
125 fd = open (disk, O_RDONLY);
128 lseek (fd, (long) SUPERBOFF, 0);
129 if (safe_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
135 #define CONVERT_BLOCKS(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
136 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.s_fsize);
137 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.s_tfree);
138 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.s_tfree);
139 fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1);
140 fsp->fsu_ffree = fsd.s_tinode;
143 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
146 if (statfs (path, &fsd) < 0)
149 #ifdef STATFS_TRUNCATES_BLOCK_COUNTS
150 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
151 struct statfs are truncated to 2GB. These conditions detect that
152 truncation, presumably without botching the 4.1.1 case, in which
153 the values are not truncated. The correct counts are stored in
154 undocumented spare fields. */
155 if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0)
157 fsd.f_blocks = fsd.f_spare[0];
158 fsd.f_bfree = fsd.f_spare[1];
159 fsd.f_bavail = fsd.f_spare[2];
161 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
163 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
166 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
169 if (statfs (path, &fsd) < 0)
171 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
174 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */
177 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
179 /* Empirically, the block counts on most SVR3 and SVR3-derived
180 systems seem to always be in terms of 512-byte blocks,
181 no matter what value f_bsize has. */
183 # define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
185 # define CONVERT_BLOCKS(b) (b)
186 # ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
187 # ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
188 # define f_bavail f_bfree
194 #ifdef STAT_STATVFS /* SVR4. */
197 if (statvfs (path, &fsd) < 0)
199 /* f_frsize isn't guaranteed to be supported. */
200 #define CONVERT_BLOCKS(b) \
201 adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
204 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */
205 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
206 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
207 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
208 fsp->fsu_files = fsd.f_files;
209 fsp->fsu_ffree = fsd.f_ffree;
215 #if defined(_AIX) && defined(_I386)
216 /* AIX PS/2 does not supply statfs. */
226 if (stat (path, &stats))
228 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
231 fsb->f_bsize = fsd.du_bsize;
232 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
233 fsb->f_bfree = fsd.du_tfree;
234 fsb->f_bavail = fsd.du_tfree;
235 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
236 fsb->f_ffree = fsd.du_tinode;
237 fsb->f_fsid.val[0] = fsd.du_site;
238 fsb->f_fsid.val[1] = fsd.du_pckno;
241 #endif /* _AIX && _I386 */