add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / smbd / dfree.c
blobe55e40c030ddc0c37234b61af0e72331259e9670
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"
24 /****************************************************************************
25 Normalise for DOS usage.
26 ****************************************************************************/
28 static void disk_norm(BOOL small_query, SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
30 /* check if the disk is beyond the max disk size */
31 SMB_BIG_UINT maxdisksize = lp_maxdisksize();
32 if (maxdisksize) {
33 /* convert to blocks - and don't overflow */
34 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
35 if (*dsize > maxdisksize) *dsize = maxdisksize;
36 if (*dfree > maxdisksize) *dfree = maxdisksize-1;
37 /* the -1 should stop applications getting div by 0
38 errors */
41 while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
42 *dfree /= 2;
43 *dsize /= 2;
44 *bsize *= 2;
45 if(small_query) {
47 * Force max to fit in 16 bit fields.
49 if (*bsize > (WORDMAX*512)) {
50 *bsize = (WORDMAX*512);
51 if (*dsize > WORDMAX)
52 *dsize = WORDMAX;
53 if (*dfree > WORDMAX)
54 *dfree = WORDMAX;
55 break;
63 /****************************************************************************
64 Return number of 1K blocks available on a path and total number.
65 ****************************************************************************/
67 static SMB_BIG_UINT disk_free(char *path, BOOL small_query,
68 SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
70 int dfree_retval;
71 SMB_BIG_UINT dfree_q = 0;
72 SMB_BIG_UINT bsize_q = 0;
73 SMB_BIG_UINT dsize_q = 0;
74 char *dfree_command;
76 (*dfree) = (*dsize) = 0;
77 (*bsize) = 512;
80 * If external disk calculation specified, use it.
83 dfree_command = lp_dfree_command();
84 if (dfree_command && *dfree_command) {
85 char *p;
86 char **lines;
87 pstring syscmd;
89 slprintf(syscmd, sizeof(syscmd)-1, "%s %s", dfree_command, path);
90 DEBUG (3, ("disk_free: Running command %s\n", syscmd));
92 lines = file_lines_pload(syscmd, NULL, True);
93 if (lines) {
94 char *line = lines[0];
96 DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
98 *dsize = (SMB_BIG_UINT)strtoul(line, &p, 10);
99 while (p && *p & isspace(*p))
100 p++;
101 if (p && *p)
102 *dfree = (SMB_BIG_UINT)strtoul(p, &p, 10);
103 while (p && *p & isspace(*p))
104 p++;
105 if (p && *p)
106 *bsize = (SMB_BIG_UINT)strtoul(p, NULL, 10);
107 else
108 *bsize = 1024;
109 file_lines_free(lines);
110 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
111 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
113 if (!*dsize)
114 *dsize = 2048;
115 if (!*dfree)
116 *dfree = 1024;
117 } else {
118 DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
119 syscmd, strerror(errno) ));
120 sys_fsusage(path, dfree, dsize);
122 } else
123 sys_fsusage(path, dfree, dsize);
125 if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
126 (*bsize) = bsize_q;
127 (*dfree) = MIN(*dfree,dfree_q);
128 (*dsize) = MIN(*dsize,dsize_q);
131 /* FIXME : Any reason for this assumption ? */
132 if (*bsize < 256) {
133 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
134 *bsize = 512;
137 if ((*dsize)<1) {
138 static int done;
139 if (!done) {
140 DEBUG(0,("WARNING: dfree is broken on this system\n"));
141 done=1;
143 *dsize = 20*1024*1024/(*bsize);
144 *dfree = MAX(1,*dfree);
147 disk_norm(small_query,bsize,dfree,dsize);
149 if ((*bsize) < 1024) {
150 dfree_retval = (*dfree)/(1024/(*bsize));
151 } else {
152 dfree_retval = ((*bsize)/1024)*(*dfree);
155 return(dfree_retval);
158 /****************************************************************************
159 Wrap it to get filenames right.
160 ****************************************************************************/
162 SMB_BIG_UINT sys_disk_free(const char *path, BOOL small_query,
163 SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
165 return(disk_free(dos_to_unix_static(path),small_query, bsize,dfree,dsize));