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/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
24 /****************************************************************************
25 Normalise for DOS usage.
26 ****************************************************************************/
28 static void disk_norm(bool small_query
, uint64_t *bsize
,uint64_t *dfree
,uint64_t *dsize
)
30 /* check if the disk is beyond the max disk size */
31 uint64_t maxdisksize
= lp_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
42 while (*dfree
> WORDMAX
|| *dsize
> WORDMAX
|| *bsize
< 512) {
47 * Force max to fit in 16 bit fields.
49 if (*bsize
> (WORDMAX
*512)) {
50 *bsize
= (WORDMAX
*512);
63 /****************************************************************************
64 Return number of 1K blocks available on a path and total number.
65 ****************************************************************************/
67 uint64_t sys_disk_free(connection_struct
*conn
, const char *path
, bool small_query
,
68 uint64_t *bsize
,uint64_t *dfree
,uint64_t *dsize
)
70 uint64_t dfree_retval
;
74 const char *dfree_command
;
76 (*dfree
) = (*dsize
) = 0;
80 * If external disk calculation specified, use it.
83 dfree_command
= lp_dfree_command(SNUM(conn
));
84 if (dfree_command
&& *dfree_command
) {
89 syscmd
= talloc_asprintf(talloc_tos(),
98 DEBUG (3, ("disk_free: Running command %s\n", syscmd
));
100 lines
= file_lines_pload(syscmd
, NULL
);
102 char *line
= lines
[0];
104 DEBUG (3, ("Read input from dfree, \"%s\"\n", line
));
106 *dsize
= STR_TO_SMB_BIG_UINT(line
, &p
);
107 while (p
&& *p
&& isspace(*p
))
110 *dfree
= STR_TO_SMB_BIG_UINT(p
, &p
);
111 while (p
&& *p
&& isspace(*p
))
114 *bsize
= STR_TO_SMB_BIG_UINT(p
, NULL
);
118 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
119 (unsigned int)*dsize
, (unsigned int)*dfree
, (unsigned int)*bsize
));
126 DEBUG (0, ("disk_free: sys_popen() failed for command %s. Error was : %s\n",
127 syscmd
, strerror(errno
) ));
128 if (sys_fsusage(path
, dfree
, dsize
) != 0) {
129 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
135 if (sys_fsusage(path
, dfree
, dsize
) != 0) {
136 DEBUG (0, ("disk_free: sys_fsusage() failed. Error was : %s\n",
142 if (disk_quotas(path
, &bsize_q
, &dfree_q
, &dsize_q
)) {
144 (*dfree
) = MIN(*dfree
,dfree_q
);
145 (*dsize
) = MIN(*dsize
,dsize_q
);
148 /* FIXME : Any reason for this assumption ? */
150 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize
));
156 DEBUG(0,("WARNING: dfree is broken on this system\n"));
159 *dsize
= 20*1024*1024/(*bsize
);
160 *dfree
= MAX(1,*dfree
);
163 disk_norm(small_query
,bsize
,dfree
,dsize
);
165 if ((*bsize
) < 1024) {
166 dfree_retval
= (*dfree
)/(1024/(*bsize
));
168 dfree_retval
= ((*bsize
)/1024)*(*dfree
);
171 return(dfree_retval
);
174 /****************************************************************************
175 Potentially returned cached dfree info.
176 ****************************************************************************/
178 uint64_t get_dfree_info(connection_struct
*conn
,
185 int dfree_cache_time
= lp_dfree_cache_time(SNUM(conn
));
186 struct dfree_cached_info
*dfc
= conn
->dfree_info
;
189 if (!dfree_cache_time
) {
190 return SMB_VFS_DISK_FREE(conn
,path
,small_query
,bsize
,dfree
,dsize
);
193 if (dfc
&& (conn
->lastused
- dfc
->last_dfree_time
< dfree_cache_time
)) {
194 /* Return cached info. */
198 return dfc
->dfree_ret
;
201 dfree_ret
= SMB_VFS_DISK_FREE(conn
,path
,small_query
,bsize
,dfree
,dsize
);
203 if (dfree_ret
== (uint64_t)-1) {
204 /* Don't cache bad data. */
208 /* No cached info or time to refresh. */
210 dfc
= TALLOC_P(conn
, struct dfree_cached_info
);
214 conn
->dfree_info
= dfc
;
220 dfc
->dfree_ret
= dfree_ret
;
221 dfc
->last_dfree_time
= conn
->lastused
;