2 Unix SMB/Netbios implementation.
4 functions to calculate the free disk space
5 Copyright (C) Andrew Tridgell 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
27 /****************************************************************************
28 normalise for DOS usage
29 ****************************************************************************/
30 static void disk_norm(SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
32 /* check if the disk is beyond the max disk size */
33 SMB_BIG_UINT maxdisksize = lp_maxdisksize();
35 /* convert to blocks - and don't overflow */
36 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
37 if (*dsize > maxdisksize) *dsize = maxdisksize;
38 if (*dfree > maxdisksize) *dfree = maxdisksize-1;
39 /* the -1 should stop applications getting div by 0
43 while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
47 if (*bsize > WORDMAX) {
59 /* Return the number of TOSIZE-byte blocks used by
60 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
62 static SMB_BIG_UINT adjust_blocks(SMB_BIG_UINT blocks, SMB_BIG_UINT fromsize, SMB_BIG_UINT tosize)
64 if (fromsize == tosize) /* e.g., from 512 to 512 */
66 else if (fromsize > tosize) /* e.g., from 2048 to 512 */
67 return blocks * (fromsize / tosize);
68 else /* e.g., from 256 to 512 */
69 return (blocks + 1) / (tosize / fromsize);
72 /* this does all of the system specific guff to get the free disk space.
73 It is derived from code in the GNU fileutils package, but has been
74 considerably mangled for use here
76 results are returned in *dfree and *dsize, in 512 byte units
78 static int fsusage(const char *path, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
80 #ifdef STAT_STATFS3_OSF1
81 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
84 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
86 #endif /* STAT_STATFS3_OSF1 */
88 #ifdef STAT_STATFS2_FS_DATA /* Ultrix */
89 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)1024, (SMB_BIG_UINT)512)
92 if (statfs (path, &fsd) != 1)
95 (*dsize) = CONVERT_BLOCKS (fsd.fd_req.btot);
96 (*dfree) = CONVERT_BLOCKS (fsd.fd_req.bfreen);
97 #endif /* STAT_STATFS2_FS_DATA */
99 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */
100 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
103 if (statfs (path, &fsd) < 0)
106 #ifdef STATFS_TRUNCATES_BLOCK_COUNTS
107 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
108 struct statfs are truncated to 2GB. These conditions detect that
109 truncation, presumably without botching the 4.1.1 case, in which
110 the values are not truncated. The correct counts are stored in
111 undocumented spare fields. */
112 if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0) {
113 fsd.f_blocks = fsd.f_spare[0];
114 fsd.f_bfree = fsd.f_spare[1];
115 fsd.f_bavail = fsd.f_spare[2];
117 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
118 #endif /* STAT_STATFS2_BSIZE */
121 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD */
122 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
126 if (statfs (path, &fsd) < 0)
128 #endif /* STAT_STATFS2_FSIZE */
130 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */
131 # if _AIX || defined(_CRAY)
132 # define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
134 # define f_bavail f_bfree
137 # define CONVERT_BLOCKS(B) ((SMB_BIG_UINT)B)
138 # ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx */
139 # ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
140 # define f_bavail f_bfree
147 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
149 /* Empirically, the block counts on most SVR3 and SVR3-derived
150 systems seem to always be in terms of 512-byte blocks,
151 no matter what value f_bsize has. */
153 #endif /* STAT_STATFS4 */
155 #ifdef STAT_STATVFS /* SVR4 */
156 # define CONVERT_BLOCKS(B) \
157 adjust_blocks ((SMB_BIG_UINT)(B), fsd.f_frsize ? (SMB_BIG_UINT)fsd.f_frsize : (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
159 #ifdef STAT_STATVFS64
160 struct statvfs64 fsd;
161 if (statvfs64(path, &fsd) < 0) return -1;
164 if (statvfs(path, &fsd) < 0) return -1;
167 /* f_frsize isn't guaranteed to be supported. */
169 #endif /* STAT_STATVFS */
171 #ifndef CONVERT_BLOCKS
172 /* we don't have any dfree code! */
175 #if !defined(STAT_STATFS2_FS_DATA)
177 (*dsize) = CONVERT_BLOCKS (fsd.f_blocks);
178 (*dfree) = CONVERT_BLOCKS (fsd.f_bavail);
179 #endif /* not STAT_STATFS2_FS_DATA */
185 /****************************************************************************
186 return number of 1K blocks available on a path and total number
187 ****************************************************************************/
188 static SMB_BIG_UINT disk_free(char *path,SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
191 SMB_BIG_UINT dfree_q = 0;
192 SMB_BIG_UINT bsize_q = 0;
193 SMB_BIG_UINT dsize_q = 0;
195 (*dfree) = (*dsize) = 0;
198 fsusage(path, dfree, dsize);
200 if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
202 (*dfree) = MIN(*dfree,dfree_q);
203 (*dsize) = MIN(*dsize,dsize_q);
206 /* FIXME : Any reason for this assumption ? */
208 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
215 DEBUG(0,("WARNING: dfree is broken on this system\n"));
218 *dsize = 20*1024*1024/(*bsize);
219 *dfree = MAX(1,*dfree);
222 disk_norm(bsize,dfree,dsize);
224 if ((*bsize) < 1024) {
225 dfree_retval = (*dfree)/(1024/(*bsize));
227 dfree_retval = ((*bsize)/1024)*(*dfree);
230 return(dfree_retval);
234 /****************************************************************************
235 wrap it to get filenames right
236 ****************************************************************************/
237 SMB_BIG_UINT sys_disk_free(char *path,SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
239 return(disk_free(dos_to_unix(path,False),bsize,dfree,dsize));