configure configure.in: Added checks for statvfs64. Last bit of 64 bit widening ...
[ira/wip.git] / source3 / smbd / dfree.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    functions to calculate the free disk space
5    Copyright (C) Andrew Tridgell 1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 extern int DEBUGLEVEL;
26
27 /****************************************************************************
28 normalise for DOS usage 
29 ****************************************************************************/
30 static void disk_norm(SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
31 {
32         /* check if the disk is beyond the max disk size */
33         SMB_BIG_UINT maxdisksize = lp_maxdisksize();
34         if (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
40                    errors */
41         }  
42         
43         while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
44                 *dfree /= 2;
45                 *dsize /= 2;
46                 *bsize *= 2;
47                 if (*bsize > WORDMAX) {
48                         *bsize = WORDMAX;
49                         if (*dsize > WORDMAX)
50                                 *dsize = WORDMAX;
51                         if (*dfree >  WORDMAX)
52                                 *dfree = WORDMAX;
53                         break;
54                 }
55         }
56 }
57
58
59 /* Return the number of TOSIZE-byte blocks used by
60    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
61 */
62 static SMB_BIG_UINT adjust_blocks(SMB_BIG_UINT blocks, SMB_BIG_UINT fromsize, SMB_BIG_UINT tosize)
63 {
64         if (fromsize == tosize) /* e.g., from 512 to 512 */
65                 return blocks;
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);
70 }
71
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 
75
76    results are returned in *dfree and *dsize, in 512 byte units
77 */
78 static int fsusage(const char *path, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
79 {
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)
82         struct statfs fsd;
83
84         if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
85                 return -1;
86 #endif /* STAT_STATFS3_OSF1 */
87         
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)      
90         struct fs_data fsd;
91         
92         if (statfs (path, &fsd) != 1)
93                 return -1;
94         
95         (*dsize) = CONVERT_BLOCKS (fsd.fd_req.btot);
96         (*dfree) = CONVERT_BLOCKS (fsd.fd_req.bfreen);
97 #endif /* STAT_STATFS2_FS_DATA */
98         
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)
101         struct statfs fsd;
102         
103         if (statfs (path, &fsd) < 0)
104                 return -1;
105         
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];
116         }
117 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
118 #endif /* STAT_STATFS2_BSIZE */
119         
120
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)
123         
124         struct statfs fsd;
125         
126         if (statfs (path, &fsd) < 0)
127                 return -1;
128 #endif /* STAT_STATFS2_FSIZE */
129         
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)
133 #  ifdef _CRAY
134 #   define f_bavail f_bfree
135 #  endif
136 # else
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
141 #   endif
142 #  endif
143 # endif
144         
145         struct statfs fsd;
146
147         if (statfs (path, &fsd, sizeof fsd, 0) < 0)
148                 return -1;
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.  */
152
153 #endif /* STAT_STATFS4 */
154
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)
158
159         SMB_STRUCT_STATVFS fsd;
160
161         if (sys_statvfs (path, &fsd) < 0)
162                 return -1;
163         /* f_frsize isn't guaranteed to be supported.  */
164
165 #endif /* STAT_STATVFS */
166
167 #ifndef CONVERT_BLOCKS
168         /* we don't have any dfree code! */
169         return -1;
170 #else
171 #if !defined(STAT_STATFS2_FS_DATA)
172         /* !Ultrix */
173         (*dsize) = CONVERT_BLOCKS (fsd.f_blocks);
174         (*dfree) = CONVERT_BLOCKS (fsd.f_bavail);
175 #endif /* not STAT_STATFS2_FS_DATA */
176 #endif
177
178         return 0;
179 }
180
181 /****************************************************************************
182   return number of 1K blocks available on a path and total number 
183 ****************************************************************************/
184 static SMB_BIG_UINT disk_free(char *path,SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
185 {
186         int dfree_retval;
187
188         (*dfree) = (*dsize) = 0;
189         (*bsize) = 512;
190
191         fsusage(path, dfree, dsize);
192
193         if (*bsize < 256) {
194                 *bsize = 512;
195         }
196
197         if ((*dsize)<1) {
198                 static int done;
199                 if (!done) {
200                         DEBUG(0,("WARNING: dfree is broken on this system\n"));
201                         done=1;
202                 }
203                 *dsize = 20*1024*1024/(*bsize);
204                 *dfree = MAX(1,*dfree);
205         }
206
207         disk_norm(bsize,dfree,dsize);
208
209         if ((*bsize) < 1024) {
210                 dfree_retval = (*dfree)/(1024/(*bsize));
211         } else {
212                 dfree_retval = ((*bsize)/1024)*(*dfree);
213         }
214
215         return(dfree_retval);
216 }
217
218
219 /****************************************************************************
220 wrap it to get filenames right
221 ****************************************************************************/
222 SMB_BIG_UINT sys_disk_free(char *path,SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
223 {
224         return(disk_free(dos_to_unix(path,False),bsize,dfree,dsize));
225 }