smbd: Re-register notify requests
[vlendec/samba-autobuild/.git] / source3 / smbd / dfree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    functions to calculate the free disk space
4    Copyright (C) Andrew Tridgell 1998
5    
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "lib/util_file.h"
24
25 /****************************************************************************
26  Normalise for DOS usage.
27 ****************************************************************************/
28
29 static void disk_norm(uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
30 {
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;
38                 }
39                 if (*dfree > maxdisksize) {
40                         *dfree = maxdisksize - 1;
41                 }
42                 /* the -1 should stop applications getting div by 0
43                    errors */
44         }
45 }
46
47
48
49 /****************************************************************************
50  Return number of 1K blocks available on a path and total number.
51 ****************************************************************************/
52
53 uint64_t sys_disk_free(connection_struct *conn, const char *path,
54                        uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
55 {
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
63         (*dfree) = (*dsize) = 0;
64         (*bsize) = 512;
65
66         /*
67          * If external disk calculation specified, use it.
68          */
69
70         dfree_command = lp_dfree_command(talloc_tos(), SNUM(conn));
71         if (dfree_command && *dfree_command) {
72                 const char *p;
73                 char **lines = NULL;
74                 char *syscmd = NULL;
75
76                 syscmd = talloc_asprintf(talloc_tos(),
77                                 "%s %s",
78                                 dfree_command,
79                                 path);
80
81                 if (!syscmd) {
82                         return (uint64_t)-1;
83                 }
84
85                 DEBUG (3, ("disk_free: Running command '%s'\n", syscmd));
86
87                 lines = file_lines_pload(talloc_tos(), syscmd, NULL);
88                 if (lines != NULL) {
89                         char *line = lines[0];
90
91                         DEBUG (3, ("Read input from dfree, \"%s\"\n", line));
92
93                         *dsize = STR_TO_SMB_BIG_UINT(line, &p);
94                         while (p && *p && isspace(*p))
95                                 p++;
96                         if (p && *p)
97                                 *dfree = STR_TO_SMB_BIG_UINT(p, &p);
98                         while (p && *p && isspace(*p))
99                                 p++;
100                         if (p && *p)
101                                 *bsize = STR_TO_SMB_BIG_UINT(p, NULL);
102                         else
103                                 *bsize = 1024;
104                         TALLOC_FREE(lines);
105                         DEBUG (3, ("Parsed output of dfree, dsize=%u, dfree=%u, bsize=%u\n",
106                                 (unsigned int)*dsize, (unsigned int)*dfree, (unsigned int)*bsize));
107
108                         if (!*dsize)
109                                 *dsize = 2048;
110                         if (!*dfree)
111                                 *dfree = 1024;
112
113                         goto dfree_done;
114                 }
115                 DEBUG (0, ("disk_free: file_lines_load() failed for "
116                            "command '%s'. Error was : %s\n",
117                            syscmd, strerror(errno) ));
118         }
119
120         if (SMB_VFS_DISK_FREE(conn, path, bsize, dfree, dsize) ==
121             (uint64_t)-1) {
122                 DBG_ERR("VFS disk_free failed. Error was : %s\n",
123                         strerror(errno));
124                 return (uint64_t)-1;
125         }
126
127         if (disk_quotas(conn, path, &bsize_q, &dfree_q, &dsize_q)) {
128                 uint64_t min_bsize = MIN(*bsize, bsize_q);
129
130                 (*dfree) = (*dfree) * (*bsize) / min_bsize;
131                 (*dsize) = (*dsize) * (*bsize) / min_bsize;
132                 dfree_q = dfree_q * bsize_q / min_bsize;
133                 dsize_q = dsize_q * bsize_q / min_bsize;
134
135                 (*bsize) = min_bsize;
136                 (*dfree) = MIN(*dfree,dfree_q);
137                 (*dsize) = MIN(*dsize,dsize_q);
138         }
139
140         /* FIXME : Any reason for this assumption ? */
141         if (*bsize < 256) {
142                 DEBUG(5,("disk_free:Warning: bsize == %d < 256 . Changing to assumed correct bsize = 512\n",(int)*bsize));
143                 *bsize = 512;
144         }
145
146         if ((*dsize)<1) {
147                 if (!dfree_broken) {
148                         DEBUG(0,("WARNING: dfree is broken on this system\n"));
149                         dfree_broken=true;
150                 }
151                 *dsize = 20*1024*1024/(*bsize);
152                 *dfree = MAX(1,*dfree);
153         }
154
155 dfree_done:
156         disk_norm(bsize, dfree, dsize);
157
158         if ((*bsize) < 1024) {
159                 dfree_retval = (*dfree)/(1024/(*bsize));
160         } else {
161                 dfree_retval = ((*bsize)/1024)*(*dfree);
162         }
163
164         return(dfree_retval);
165 }
166
167 /****************************************************************************
168  Potentially returned cached dfree info.
169 ****************************************************************************/
170
171 uint64_t get_dfree_info(connection_struct *conn,
172                         const char *path,
173                         uint64_t *bsize,
174                         uint64_t *dfree,
175                         uint64_t *dsize)
176 {
177         int dfree_cache_time = lp_dfree_cache_time(SNUM(conn));
178         struct dfree_cached_info *dfc = conn->dfree_info;
179         uint64_t dfree_ret;
180
181         if (!dfree_cache_time) {
182                 return sys_disk_free(conn, path, bsize, dfree, dsize);
183         }
184
185         if (dfc && (conn->lastused - dfc->last_dfree_time < dfree_cache_time)) {
186                 /* Return cached info. */
187                 *bsize = dfc->bsize;
188                 *dfree = dfc->dfree;
189                 *dsize = dfc->dsize;
190                 return dfc->dfree_ret;
191         }
192
193         dfree_ret = sys_disk_free(conn, path, bsize, dfree, dsize);
194
195         if (dfree_ret == (uint64_t)-1) {
196                 /* Don't cache bad data. */
197                 return dfree_ret;
198         }
199
200         /* No cached info or time to refresh. */
201         if (!dfc) {
202                 dfc = talloc(conn, struct dfree_cached_info);
203                 if (!dfc) {
204                         return dfree_ret;
205                 }
206                 conn->dfree_info = dfc;
207         }
208
209         dfc->bsize = *bsize;
210         dfc->dfree = *dfree;
211         dfc->dsize = *dsize;
212         dfc->dfree_ret = dfree_ret;
213         dfc->last_dfree_time = conn->lastused;
214
215         return dfree_ret;
216 }