Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / samba / source / smbd / dfree.c
blob2078021af412a26c010a30a340e4f0ca66e18a6d
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
22 #include "includes.h"
25 extern int DEBUGLEVEL;
27 /****************************************************************************
28 normalise for DOS usage
29 ****************************************************************************/
30 static void disk_norm(BOOL small_query, 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();
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 */
43 while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
44 *dfree /= 2;
45 *dsize /= 2;
46 *bsize *= 2;
47 if(small_query) {
49 * Force max to fit in 16 bit fields.
51 if (*bsize > (WORDMAX*512)) {
52 *bsize = (WORDMAX*512);
53 if (*dsize > WORDMAX)
54 *dsize = WORDMAX;
55 if (*dfree > WORDMAX)
56 *dfree = WORDMAX;
57 break;
64 /* Return the number of TOSIZE-byte blocks used by
65 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
67 static SMB_BIG_UINT adjust_blocks(SMB_BIG_UINT blocks, SMB_BIG_UINT fromsize, SMB_BIG_UINT tosize)
69 if (fromsize == tosize) /* e.g., from 512 to 512 */
70 return blocks;
71 else if (fromsize > tosize) /* e.g., from 2048 to 512 */
72 return blocks * (fromsize / tosize);
73 else /* e.g., from 256 to 512 */
74 return (blocks + 1) / (tosize / fromsize);
77 /* this does all of the system specific guff to get the free disk space.
78 It is derived from code in the GNU fileutils package, but has been
79 considerably mangled for use here
81 results are returned in *dfree and *dsize, in 512 byte units
83 static int fsusage(const char *path, SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
85 #ifdef STAT_STATFS3_OSF1
86 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
87 struct statfs fsd;
89 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
90 return -1;
91 #endif /* STAT_STATFS3_OSF1 */
93 #ifdef STAT_STATFS2_FS_DATA /* Ultrix */
94 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)1024, (SMB_BIG_UINT)512)
95 struct fs_data fsd;
97 if (statfs (path, &fsd) != 1)
98 return -1;
100 (*dsize) = CONVERT_BLOCKS (fsd.fd_req.btot);
101 (*dfree) = CONVERT_BLOCKS (fsd.fd_req.bfreen);
102 #endif /* STAT_STATFS2_FS_DATA */
104 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX */
105 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
106 struct statfs fsd;
108 if (statfs (path, &fsd) < 0)
109 return -1;
111 #ifdef STATFS_TRUNCATES_BLOCK_COUNTS
112 /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
113 struct statfs are truncated to 2GB. These conditions detect that
114 truncation, presumably without botching the 4.1.1 case, in which
115 the values are not truncated. The correct counts are stored in
116 undocumented spare fields. */
117 if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0) {
118 fsd.f_blocks = fsd.f_spare[0];
119 fsd.f_bfree = fsd.f_spare[1];
120 fsd.f_bavail = fsd.f_spare[2];
122 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
123 #endif /* STAT_STATFS2_BSIZE */
126 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD */
127 #define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_fsize, (SMB_BIG_UINT)512)
129 struct statfs fsd;
131 if (statfs (path, &fsd) < 0)
132 return -1;
133 #endif /* STAT_STATFS2_FSIZE */
135 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX */
136 # if _AIX || defined(_CRAY)
137 # define CONVERT_BLOCKS(B) adjust_blocks ((SMB_BIG_UINT)(B), (SMB_BIG_UINT)fsd.f_bsize, (SMB_BIG_UINT)512)
138 # ifdef _CRAY
139 # define f_bavail f_bfree
140 # endif
141 # else
142 # define CONVERT_BLOCKS(B) ((SMB_BIG_UINT)B)
143 # ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx */
144 # ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
145 # define f_bavail f_bfree
146 # endif
147 # endif
148 # endif
150 struct statfs fsd;
152 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
153 return -1;
154 /* Empirically, the block counts on most SVR3 and SVR3-derived
155 systems seem to always be in terms of 512-byte blocks,
156 no matter what value f_bsize has. */
158 #endif /* STAT_STATFS4 */
160 #if defined(STAT_STATVFS) || defined(STAT_STATVFS64) /* SVR4 */
161 # define CONVERT_BLOCKS(B) \
162 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)
164 #ifdef STAT_STATVFS64
165 struct statvfs64 fsd;
166 if (statvfs64(path, &fsd) < 0) return -1;
167 #else
168 struct statvfs fsd;
169 if (statvfs(path, &fsd) < 0) return -1;
170 #endif
172 /* f_frsize isn't guaranteed to be supported. */
174 #endif /* STAT_STATVFS */
176 #ifndef CONVERT_BLOCKS
177 /* we don't have any dfree code! */
178 return -1;
179 #else
180 #if !defined(STAT_STATFS2_FS_DATA)
181 /* !Ultrix */
182 (*dsize) = CONVERT_BLOCKS (fsd.f_blocks);
183 (*dfree) = CONVERT_BLOCKS (fsd.f_bavail);
184 #endif /* not STAT_STATFS2_FS_DATA */
185 #endif
187 return 0;
190 /****************************************************************************
191 return number of 1K blocks available on a path and total number
192 ****************************************************************************/
194 static SMB_BIG_UINT disk_free(char *path, BOOL small_query,
195 SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
197 int dfree_retval;
198 SMB_BIG_UINT dfree_q = 0;
199 SMB_BIG_UINT bsize_q = 0;
200 SMB_BIG_UINT dsize_q = 0;
201 char *dfree_command;
203 (*dfree) = (*dsize) = 0;
204 (*bsize) = 512;
208 * If external disk calculation specified, use it.
211 dfree_command = lp_dfree_command();
212 if (dfree_command && *dfree_command) {
213 pstring line;
214 char *p;
215 FILE *pp;
217 slprintf (line, sizeof(pstring) - 1, "%s %s", dfree_command, path);
218 DEBUG (3, ("disk_free: Running command %s\n", line));
220 pp = sys_popen(line, "r", False);
221 if (pp) {
222 fgets(line, sizeof(pstring), pp);
223 line[sizeof(pstring)-1] = '\0';
224 if (strlen(line) > 0)
225 line[strlen(line)-1] = '\0';
227 DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
229 *dsize = (SMB_BIG_UINT)strtoul(line, &p, 10);
230 while (p && *p & isspace(*p))
231 p++;
232 if (p && *p)
233 *dfree = (SMB_BIG_UINT)strtoul(p, &p, 10);
234 while (p && *p & isspace(*p))
235 p++;
236 if (p && *p)
237 *bsize = (SMB_BIG_UINT)strtoul(p, NULL, 10);
238 else
239 *bsize = 1024;
240 sys_pclose (pp);
241 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
242 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
244 if (!*dsize)
245 *dsize = 2048;
246 if (!*dfree)
247 *dfree = 1024;
248 } else {
249 DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
250 line, strerror(errno) ));
251 fsusage(path, dfree, dsize);
253 } else
254 fsusage(path, dfree, dsize);
256 if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
257 (*bsize) = bsize_q;
258 (*dfree) = MIN(*dfree,dfree_q);
259 (*dsize) = MIN(*dsize,dsize_q);
262 /* FIXME : Any reason for this assumption ? */
263 if (*bsize < 256) {
264 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
265 *bsize = 512;
268 if ((*dsize)<1) {
269 static int done;
270 if (!done) {
271 DEBUG(2,("WARNING: dfree is broken on this system\n"));
272 done=1;
274 *dsize = 20*1024*1024/(*bsize);
275 *dfree = MAX(1,*dfree);
278 disk_norm(small_query,bsize,dfree,dsize);
280 if ((*bsize) < 1024) {
281 dfree_retval = (*dfree)/(1024/(*bsize));
282 } else {
283 dfree_retval = ((*bsize)/1024)*(*dfree);
286 return(dfree_retval);
290 /****************************************************************************
291 wrap it to get filenames right
292 ****************************************************************************/
293 SMB_BIG_UINT sys_disk_free(char *path, BOOL small_query,
294 SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
296 return(disk_free(dos_to_unix(path,False),small_query, bsize,dfree,dsize));