spelling. added line about command-line tools equivalent to nt admin tools.
[samba.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 #ifdef STAT_STATVFS64
160         struct statvfs64 fsd;
161         if (statvfs64(path, &fsd) < 0) return -1;
162 #else
163         struct statvfs fsd;
164         if (statvfs(path, &fsd) < 0) return -1;
165 #endif
166
167         /* f_frsize isn't guaranteed to be supported.  */
168
169 #endif /* STAT_STATVFS */
170
171 #ifndef CONVERT_BLOCKS
172         /* we don't have any dfree code! */
173         return -1;
174 #else
175 #if !defined(STAT_STATFS2_FS_DATA)
176         /* !Ultrix */
177         (*dsize) = CONVERT_BLOCKS (fsd.f_blocks);
178         (*dfree) = CONVERT_BLOCKS (fsd.f_bavail);
179 #endif /* not STAT_STATFS2_FS_DATA */
180 #endif
181
182         return 0;
183 }
184
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)
189 {
190         int dfree_retval;
191         SMB_BIG_UINT dfree_q = 0;
192         SMB_BIG_UINT bsize_q = 0;
193         SMB_BIG_UINT dsize_q = 0;
194
195         (*dfree) = (*dsize) = 0;
196         (*bsize) = 512;
197
198         fsusage(path, dfree, dsize);
199
200         if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
201                 (*bsize) = bsize_q;
202                 (*dfree) = MIN(*dfree,dfree_q);
203                 (*dsize) = MIN(*dsize,dsize_q);
204         }
205
206         /* FIXME : Any reason for this assumption ? */
207         if (*bsize < 256) {
208                 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
209                 *bsize = 512;
210         }
211
212         if ((*dsize)<1) {
213                 static int done;
214                 if (!done) {
215                         DEBUG(0,("WARNING: dfree is broken on this system\n"));
216                         done=1;
217                 }
218                 *dsize = 20*1024*1024/(*bsize);
219                 *dfree = MAX(1,*dfree);
220         }
221
222         disk_norm(bsize,dfree,dsize);
223
224         if ((*bsize) < 1024) {
225                 dfree_retval = (*dfree)/(1024/(*bsize));
226         } else {
227                 dfree_retval = ((*bsize)/1024)*(*dfree);
228         }
229
230         return(dfree_retval);
231 }
232
233
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)
238 {
239         return(disk_free(dos_to_unix(path,False),bsize,dfree,dsize));
240 }