2 Unix SMB/CIFS implementation.
3 functions to calculate the free disk space
4 Copyright (C) Andrew Tridgell 1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /****************************************************************************
24 Normalise for DOS usage.
25 ****************************************************************************/
27 static void disk_norm(BOOL small_query, SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
29 /* check if the disk is beyond the max disk size */
30 SMB_BIG_UINT maxdisksize = lp_maxdisksize();
32 /* convert to blocks - and don't overflow */
33 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
34 if (*dsize > maxdisksize) *dsize = maxdisksize;
35 if (*dfree > maxdisksize) *dfree = maxdisksize-1;
36 /* the -1 should stop applications getting div by 0
41 while (*dfree > WORDMAX || *dsize > WORDMAX || *bsize < 512) {
46 * Force max to fit in 16 bit fields.
48 if (*bsize > (WORDMAX*512)) {
49 *bsize = (WORDMAX*512);
62 /****************************************************************************
63 Return number of 1K blocks available on a path and total number.
64 ****************************************************************************/
66 SMB_BIG_UINT sys_disk_free(connection_struct *conn, const char *path, BOOL small_query,
67 SMB_BIG_UINT *bsize,SMB_BIG_UINT *dfree,SMB_BIG_UINT *dsize)
70 SMB_BIG_UINT dfree_q = 0;
71 SMB_BIG_UINT bsize_q = 0;
72 SMB_BIG_UINT dsize_q = 0;
73 const char *dfree_command;
75 (*dfree) = (*dsize) = 0;
79 * If external disk calculation specified, use it.
82 dfree_command = lp_dfree_command(SNUM(conn));
83 if (dfree_command && *dfree_command) {
88 slprintf(syscmd, sizeof(syscmd)-1, "%s %s", dfree_command, path);
89 DEBUG (3, ("disk_free: Running command %s\n", syscmd));
91 lines = file_lines_pload(syscmd, NULL);
93 char *line = lines[0];
95 DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
97 *dsize = STR_TO_SMB_BIG_UINT(line, &p);
98 while (p && *p && isspace(*p))
101 *dfree = STR_TO_SMB_BIG_UINT(p, &p);
102 while (p && *p && isspace(*p))
105 *bsize = STR_TO_SMB_BIG_UINT(p, NULL);
108 file_lines_free(lines);
109 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
110 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
117 DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
118 syscmd, strerror(errno) ));
119 if (sys_fsusage(path, dfree, dsize) != 0) {
120 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
122 return (SMB_BIG_UINT)-1;
126 if (sys_fsusage(path, dfree, dsize) != 0) {
127 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
129 return (SMB_BIG_UINT)-1;
133 if (disk_quotas(path, &bsize_q, &dfree_q, &dsize_q)) {
135 (*dfree) = MIN(*dfree,dfree_q);
136 (*dsize) = MIN(*dsize,dsize_q);
139 /* FIXME : Any reason for this assumption ? */
141 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
148 DEBUG(0,("WARNING: dfree is broken on this system\n"));
151 *dsize = 20*1024*1024/(*bsize);
152 *dfree = MAX(1,*dfree);
155 disk_norm(small_query,bsize,dfree,dsize);
157 if ((*bsize) < 1024) {
158 dfree_retval = (*dfree)/(1024/(*bsize));
160 dfree_retval = ((*bsize)/1024)*(*dfree);
163 return(dfree_retval);
166 /****************************************************************************
167 Potentially returned cached dfree info.
168 ****************************************************************************/
170 SMB_BIG_UINT get_dfree_info(connection_struct *conn,
177 int dfree_cache_time = lp_dfree_cache_time(SNUM(conn));
178 struct dfree_cached_info *dfc = conn->dfree_info;
179 SMB_BIG_UINT dfree_ret;
181 if (!dfree_cache_time) {
182 return SMB_VFS_DISK_FREE(conn,path,small_query,bsize,dfree,dsize);
185 if (dfc && (conn->lastused - dfc->last_dfree_time < dfree_cache_time)) {
186 /* Return cached info. */
190 return dfc->dfree_ret;
193 dfree_ret = SMB_VFS_DISK_FREE(conn,path,small_query,bsize,dfree,dsize);
195 if (dfree_ret == (SMB_BIG_UINT)-1) {
196 /* Don't cache bad data. */
200 /* No cached info or time to refresh. */
202 dfc = TALLOC_P(conn->mem_ctx, struct dfree_cached_info);
206 conn->dfree_info = dfc;
212 dfc->dfree_ret = dfree_ret;
213 dfc->last_dfree_time = conn->lastused;