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 3 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, see <http://www.gnu.org/licenses/>.
22 /****************************************************************************
23 Normalise for DOS usage.
24 ****************************************************************************/
26 static void disk_norm(bool small_query
, SMB_BIG_UINT
*bsize
,SMB_BIG_UINT
*dfree
,SMB_BIG_UINT
*dsize
)
28 /* check if the disk is beyond the max disk size */
29 SMB_BIG_UINT maxdisksize
= lp_maxdisksize();
31 /* convert to blocks - and don't overflow */
32 maxdisksize
= ((maxdisksize
*1024)/(*bsize
))*1024;
33 if (*dsize
> maxdisksize
) *dsize
= maxdisksize
;
34 if (*dfree
> maxdisksize
) *dfree
= maxdisksize
-1;
35 /* the -1 should stop applications getting div by 0
40 while (*dfree
> WORDMAX
|| *dsize
> WORDMAX
|| *bsize
< 512) {
45 * Force max to fit in 16 bit fields.
47 if (*bsize
> (WORDMAX
*512)) {
48 *bsize
= (WORDMAX
*512);
61 /****************************************************************************
62 Return number of 1K blocks available on a path and total number.
63 ****************************************************************************/
65 SMB_BIG_UINT
sys_disk_free(connection_struct
*conn
, const char *path
, bool small_query
,
66 SMB_BIG_UINT
*bsize
,SMB_BIG_UINT
*dfree
,SMB_BIG_UINT
*dsize
)
68 SMB_BIG_UINT dfree_retval
;
69 SMB_BIG_UINT dfree_q
= 0;
70 SMB_BIG_UINT bsize_q
= 0;
71 SMB_BIG_UINT dsize_q
= 0;
72 const char *dfree_command
;
74 (*dfree
) = (*dsize
) = 0;
78 * If external disk calculation specified, use it.
81 dfree_command
= lp_dfree_command(SNUM(conn
));
82 if (dfree_command
&& *dfree_command
) {
87 syscmd
= talloc_asprintf(talloc_tos(),
93 return (SMB_BIG_UINT
)-1;
96 DEBUG (3, ("disk_free: Running command %s\n", syscmd
));
98 lines
= file_lines_pload(syscmd
, NULL
);
100 char *line
= lines
[0];
102 DEBUG (3, ("Read input from dfree, \"%s\"\n", line
));
104 *dsize
= STR_TO_SMB_BIG_UINT(line
, &p
);
105 while (p
&& *p
&& isspace(*p
))
108 *dfree
= STR_TO_SMB_BIG_UINT(p
, &p
);
109 while (p
&& *p
&& isspace(*p
))
112 *bsize
= STR_TO_SMB_BIG_UINT(p
, NULL
);
115 file_lines_free(lines
);
116 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
117 (unsigned int)*dsize
, (unsigned int)*dfree
, (unsigned int)*bsize
));
124 DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
125 syscmd
, strerror(errno
) ));
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 (sys_fsusage(path
, dfree
, dsize
) != 0) {
134 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
136 return (SMB_BIG_UINT
)-1;
140 if (disk_quotas(path
, &bsize_q
, &dfree_q
, &dsize_q
)) {
142 (*dfree
) = MIN(*dfree
,dfree_q
);
143 (*dsize
) = MIN(*dsize
,dsize_q
);
146 /* FIXME : Any reason for this assumption ? */
148 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize
));
153 static bool done
= false;
155 DEBUG(0,("WARNING: dfree is broken on this system\n"));
158 *dsize
= 20*1024*1024/(*bsize
);
159 *dfree
= MAX(1,*dfree
);
162 disk_norm(small_query
,bsize
,dfree
,dsize
);
164 if ((*bsize
) < 1024) {
165 dfree_retval
= (*dfree
)/(1024/(*bsize
));
167 dfree_retval
= ((*bsize
)/1024)*(*dfree
);
170 return(dfree_retval
);
173 /****************************************************************************
174 Potentially returned cached dfree info.
175 ****************************************************************************/
177 SMB_BIG_UINT
get_dfree_info(connection_struct
*conn
,
184 int dfree_cache_time
= lp_dfree_cache_time(SNUM(conn
));
185 struct dfree_cached_info
*dfc
= conn
->dfree_info
;
186 SMB_BIG_UINT dfree_ret
;
188 if (!dfree_cache_time
) {
189 return SMB_VFS_DISK_FREE(conn
,path
,small_query
,bsize
,dfree
,dsize
);
192 if (dfc
&& (conn
->lastused
- dfc
->last_dfree_time
< dfree_cache_time
)) {
193 /* Return cached info. */
197 return dfc
->dfree_ret
;
200 dfree_ret
= SMB_VFS_DISK_FREE(conn
,path
,small_query
,bsize
,dfree
,dsize
);
202 if (dfree_ret
== (SMB_BIG_UINT
)-1) {
203 /* Don't cache bad data. */
207 /* No cached info or time to refresh. */
209 dfc
= TALLOC_P(conn
->mem_ctx
, struct dfree_cached_info
);
213 conn
->dfree_info
= dfc
;
219 dfc
->dfree_ret
= dfree_ret
;
220 dfc
->last_dfree_time
= conn
->lastused
;