s3: VFS: Protect errno if sys_getwd() fails across free() call.
[Samba.git] / source3 / smbd / dfree.c
blob7e58daa45cf5d0bb642ed57e2a6c8e4a86b35007
1 /*
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/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "lib/util_file.h"
25 /****************************************************************************
26 Normalise for DOS usage.
27 ****************************************************************************/
29 static void disk_norm(uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
31 /* check if the disk is beyond the max disk size */
32 uint64_t maxdisksize = lp_max_disk_size();
33 if (maxdisksize) {
34 /* convert to blocks - and don't overflow */
35 maxdisksize = ((maxdisksize*1024)/(*bsize))*1024;
36 if (*dsize > maxdisksize) {
37 *dsize = maxdisksize;
39 if (*dfree > maxdisksize) {
40 *dfree = maxdisksize - 1;
42 /* the -1 should stop applications getting div by 0
43 errors */
49 /****************************************************************************
50 Return number of 1K blocks available on a path and total number.
51 ****************************************************************************/
53 uint64_t sys_disk_free(connection_struct *conn, struct smb_filename *fname,
54 uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
56 uint64_t dfree_retval;
57 uint64_t dfree_q = 0;
58 uint64_t bsize_q = 0;
59 uint64_t dsize_q = 0;
60 const char *dfree_command;
61 static bool dfree_broken = false;
62 const char *path = fname->base_name;
64 (*dfree) = (*dsize) = 0;
65 (*bsize) = 512;
68 * If external disk calculation specified, use it.
71 dfree_command = lp_dfree_command(talloc_tos(), SNUM(conn));
72 if (dfree_command && *dfree_command) {
73 const char *p;
74 char **lines = NULL;
75 char *syscmd = NULL;
77 syscmd = talloc_asprintf(talloc_tos(),
78 "%s %s",
79 dfree_command,
80 path);
82 if (!syscmd) {
83 return (uint64_t)-1;
86 DEBUG (3, ("disk_free: Running command '%s'\n", syscmd));
88 lines = file_lines_pload(talloc_tos(), syscmd, NULL);
89 if (lines != NULL) {
90 char *line = lines[0];
92 DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
94 *dsize = STR_TO_SMB_BIG_UINT(line, &p);
95 while (p && *p && isspace(*p))
96 p++;
97 if (p && *p)
98 *dfree = STR_TO_SMB_BIG_UINT(p, &p);
99 while (p && *p && isspace(*p))
100 p++;
101 if (p && *p)
102 *bsize = STR_TO_SMB_BIG_UINT(p, NULL);
103 else
104 *bsize = 1024;
105 TALLOC_FREE(lines);
106 DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
107 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
109 if (!*dsize)
110 *dsize = 2048;
111 if (!*dfree)
112 *dfree = 1024;
114 goto dfree_done;
116 DEBUG (0, ("disk_free: file_lines_load() failed for "
117 "command '%s'. Error was : %s\n",
118 syscmd, strerror(errno) ));
121 if (SMB_VFS_DISK_FREE(conn, path, bsize, dfree, dsize) ==
122 (uint64_t)-1) {
123 DBG_ERR("VFS disk_free failed. Error was : %s\n",
124 strerror(errno));
125 return (uint64_t)-1;
128 if (disk_quotas(conn, fname, &bsize_q, &dfree_q, &dsize_q)) {
129 uint64_t min_bsize = MIN(*bsize, bsize_q);
131 (*dfree) = (*dfree) * (*bsize) / min_bsize;
132 (*dsize) = (*dsize) * (*bsize) / min_bsize;
133 dfree_q = dfree_q * bsize_q / min_bsize;
134 dsize_q = dsize_q * bsize_q / min_bsize;
136 (*bsize) = min_bsize;
137 (*dfree) = MIN(*dfree,dfree_q);
138 (*dsize) = MIN(*dsize,dsize_q);
141 /* FIXME : Any reason for this assumption ? */
142 if (*bsize < 256) {
143 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
144 *bsize = 512;
147 if ((*dsize)<1) {
148 if (!dfree_broken) {
149 DEBUG(0,("WARNING: dfree is broken on this system\n"));
150 dfree_broken=true;
152 *dsize = 20*1024*1024/(*bsize);
153 *dfree = MAX(1,*dfree);
156 dfree_done:
157 disk_norm(bsize, dfree, dsize);
159 if ((*bsize) < 1024) {
160 dfree_retval = (*dfree)/(1024/(*bsize));
161 } else {
162 dfree_retval = ((*bsize)/1024)*(*dfree);
165 return(dfree_retval);
168 /****************************************************************************
169 Potentially returned cached dfree info.
170 ****************************************************************************/
172 uint64_t get_dfree_info(connection_struct *conn, struct smb_filename *fname,
173 uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
175 int dfree_cache_time = lp_dfree_cache_time(SNUM(conn));
176 struct dfree_cached_info *dfc = conn->dfree_info;
177 uint64_t dfree_ret;
179 if (!dfree_cache_time) {
180 return sys_disk_free(conn, fname, bsize, dfree, dsize);
183 if (dfc && (conn->lastused - dfc->last_dfree_time < dfree_cache_time)) {
184 /* Return cached info. */
185 *bsize = dfc->bsize;
186 *dfree = dfc->dfree;
187 *dsize = dfc->dsize;
188 return dfc->dfree_ret;
191 dfree_ret = sys_disk_free(conn, fname, bsize, dfree, dsize);
193 if (dfree_ret == (uint64_t)-1) {
194 /* Don't cache bad data. */
195 return dfree_ret;
198 /* No cached info or time to refresh. */
199 if (!dfc) {
200 dfc = talloc(conn, struct dfree_cached_info);
201 if (!dfc) {
202 return dfree_ret;
204 conn->dfree_info = dfc;
207 dfc->bsize = *bsize;
208 dfc->dfree = *dfree;
209 dfc->dsize = *dsize;
210 dfc->dfree_ret = dfree_ret;
211 dfc->last_dfree_time = conn->lastused;
213 return dfree_ret;