]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_dir.cc
Continuing the process of supporting multiple filesystem types. Now
[thirdparty/squid.git] / src / store_dir.cc
1
2 /*
3 * $Id: store_dir.cc,v 1.92 1999/05/22 07:42:11 wessels Exp $
4 *
5 * DEBUG: section 47 Store Directory Routines
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37
38 const char *SwapDirType[] =
39 {
40 "ufs",
41 "!ERROR!"
42 };
43
44 void
45 storeDirInit(void)
46 {
47 storeUfsDirInit();
48 }
49
50 char *
51 storeSwapFullPath(sfileno f, char *buf)
52 {
53 return storeUfsFullPath(f, buf);
54 }
55
56 void
57 storeCreateSwapDirectories(void)
58 {
59 storeUfsCreateSwapDirectories();
60 }
61
62 /*
63 *Spread load across least 3/4 of the store directories
64 */
65 static int
66 storeDirSelectSwapDir(void)
67 {
68 double least_used = 1.0;
69 double high = (double) Config.Swap.highWaterMark / 100.0;
70 double u;
71 int dirn;
72 int i, j;
73 SwapDir *SD;
74 static int nleast = 0;
75 static int nconf = 0;
76 static int *dirq = NULL;
77 static double *diru = NULL;
78 /*
79 * Handle simplest case of a single swap directory immediately
80 */
81 if (Config.cacheSwap.n_configured == 1)
82 return 0;
83 /*
84 * Initialise dirq on the first call or on change of number of dirs
85 */
86 if (nconf != Config.cacheSwap.n_configured) {
87 nconf = Config.cacheSwap.n_configured;
88 nleast = (nconf * 3) / 4;
89 safe_free(dirq);
90 dirq = (int *) xmalloc(sizeof(int) * nleast);
91 safe_free(diru);
92 diru = (double *) xmalloc(sizeof(double) * nconf);
93 for (j = 0; j < nleast; j++)
94 dirq[j] = -1;
95 }
96 /*
97 * Scan for a non-negative dirn in the dirq array and return that one
98 */
99 dirn = -1;
100 for (j = 0; j < nleast; j++) {
101 dirn = dirq[j];
102 if (dirn < 0)
103 continue;
104 dirq[j] = -1;
105 break;
106 }
107 /*
108 * If we found a valid dirn return it
109 */
110 if (dirn >= 0)
111 return dirn;
112 /*
113 * Now for the real guts of the algorithm - building the dirq array
114 */
115 for (i = 0; i < nconf; i++) {
116 diru[i] = 1.1;
117 SD = &Config.cacheSwap.swapDirs[i];
118 SD->flags.selected = 0;
119 if (SD->flags.read_only)
120 continue;
121 u = (double) SD->cur_size / SD->max_size;
122 if (u > high)
123 continue;
124 diru[i] = u;
125 }
126 for (j = 0; j < nleast; j++) {
127 dirq[j] = -1;
128 least_used = 1.0;
129 dirn = -1;
130 for (i = 0; i < nconf; i++) {
131 if (diru[i] < least_used) {
132 least_used = diru[i];
133 dirn = i;
134 }
135 }
136 if (dirn < 0)
137 break;
138 dirq[j] = dirn;
139 diru[dirn] = 1.1;
140 /* set selected flag for debugging/cachemgr only */
141 Config.cacheSwap.swapDirs[dirn].flags.selected = 1;
142 }
143 /*
144 * Setup default return of 0 if no least found
145 */
146 if (dirq[0] < 0)
147 dirq[0] = 0;
148 dirn = dirq[0];
149 dirq[0] = -1;
150 return dirn;
151 }
152
153 int
154 storeDirValidFileno(int fn)
155 {
156 int dirn = fn >> SWAP_DIR_SHIFT;
157 int filn = fn & SWAP_FILE_MASK;
158 if (dirn > Config.cacheSwap.n_configured)
159 return 0;
160 if (dirn < 0)
161 return 0;
162 if (filn < 0)
163 return 0;
164 if (filn > Config.cacheSwap.swapDirs[dirn].map->max_n_files)
165 return 0;
166 return 1;
167 }
168
169 int
170 storeDirMapBitTest(int fn)
171 {
172 int dirn = fn >> SWAP_DIR_SHIFT;
173 int filn = fn & SWAP_FILE_MASK;
174 return file_map_bit_test(Config.cacheSwap.swapDirs[dirn].map, filn);
175 }
176
177 void
178 storeDirMapBitSet(int fn)
179 {
180 int dirn = fn >> SWAP_DIR_SHIFT;
181 int filn = fn & SWAP_FILE_MASK;
182 file_map_bit_set(Config.cacheSwap.swapDirs[dirn].map, filn);
183 }
184
185 void
186 storeDirMapBitReset(int fn)
187 {
188 int dirn = fn >> SWAP_DIR_SHIFT;
189 int filn = fn & SWAP_FILE_MASK;
190 file_map_bit_reset(Config.cacheSwap.swapDirs[dirn].map, filn);
191 }
192
193 int
194 storeDirMapAllocate(void)
195 {
196 int dirn = storeDirSelectSwapDir();
197 SwapDir *SD = &Config.cacheSwap.swapDirs[dirn];
198 int filn = file_map_allocate(SD->map, SD->suggest);
199 SD->suggest = filn + 1;
200 return (dirn << SWAP_DIR_SHIFT) | (filn & SWAP_FILE_MASK);
201 }
202
203 char *
204 storeSwapDir(int dirn)
205 {
206 assert(0 <= dirn && dirn < Config.cacheSwap.n_configured);
207 return Config.cacheSwap.swapDirs[dirn].path;
208 }
209
210 int
211 storeDirNumber(int swap_file_number)
212 {
213 return swap_file_number >> SWAP_DIR_SHIFT;
214 }
215
216 int
217 storeDirProperFileno(int dirn, int fn)
218 {
219 return (dirn << SWAP_DIR_SHIFT) | (fn & SWAP_FILE_MASK);
220 }
221
222 /*
223 * An entry written to the swap log MUST have the following
224 * properties.
225 * 1. It MUST be a public key. It does no good to log
226 * a public ADD, change the key, then log a private
227 * DEL. So we need to log a DEL before we change a
228 * key from public to private.
229 * 2. It MUST have a valid (> -1) swap_file_number.
230 */
231 void
232 storeDirSwapLog(const StoreEntry * e, int op)
233 {
234 int dirn = e->swap_file_number >> SWAP_DIR_SHIFT;
235 assert(dirn < Config.cacheSwap.n_configured);
236 assert(!EBIT_TEST(e->flags, KEY_PRIVATE));
237 assert(e->swap_file_number >= 0);
238 /*
239 * icons and such; don't write them to the swap log
240 */
241 if (EBIT_TEST(e->flags, ENTRY_SPECIAL))
242 return;
243 assert(op > SWAP_LOG_NOP && op < SWAP_LOG_MAX);
244 debug(20, 3) ("storeDirSwapLog: %s %s %08X\n",
245 swap_log_op_str[op],
246 storeKeyText(e->key),
247 e->swap_file_number);
248 storeUfsDirSwapLog(e, op);
249 }
250
251 char *
252 storeDirSwapLogFile(int dirn, const char *ext)
253 {
254 return storeUfsDirSwapLogFile(dirn, ext);
255 }
256
257 void
258 storeDirUpdateSwapSize(int fn, size_t size, int sign)
259 {
260 int dirn = (fn >> SWAP_DIR_SHIFT) % Config.cacheSwap.n_configured;
261 int k = ((size + 1023) >> 10) * sign;
262 Config.cacheSwap.swapDirs[dirn].cur_size += k;
263 store_swap_size += k;
264 if (sign > 0)
265 n_disk_objects++;
266 else if (sign < 0)
267 n_disk_objects--;
268 }
269
270 void
271 storeDirStats(StoreEntry * sentry)
272 {
273 storeAppendPrintf(sentry, "Store Directory Statistics:\n");
274 storeAppendPrintf(sentry, "Store Entries : %d\n",
275 memInUse(MEM_STOREENTRY));
276 storeAppendPrintf(sentry, "Maximum Swap Size : %8d KB\n",
277 Config.Swap.maxSize);
278 storeAppendPrintf(sentry, "Current Store Swap Size: %8d KB\n",
279 store_swap_size);
280 storeAppendPrintf(sentry, "Current Capacity : %d%% used, %d%% free\n",
281 percent((int) store_swap_size, (int) Config.Swap.maxSize),
282 percent((int) (Config.Swap.maxSize - store_swap_size), (int) Config.Swap.maxSize));
283 storeUfsDirStats(sentry);
284 }
285
286 int
287 storeDirMapBitsInUse(void)
288 {
289 int i;
290 int n = 0;
291 for (i = 0; i < Config.cacheSwap.n_configured; i++)
292 n += Config.cacheSwap.swapDirs[i].map->n_files_in_map;
293 return n;
294 }
295
296 /*
297 * storeDirWriteCleanLogs
298 *
299 * Writes a "clean" swap log file from in-memory metadata.
300 */
301 int
302 storeDirWriteCleanLogs(int reopen)
303 {
304 return storeUfsDirWriteCleanLogs(reopen);
305 }
306
307 void
308 storeDirConfigure(void)
309 {
310 SwapDir *SD;
311 int n;
312 int i;
313 fileMap *fm;
314 Config.Swap.maxSize = 0;
315 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
316 SD = &Config.cacheSwap.swapDirs[i];;
317 Config.Swap.maxSize += SD->max_size;
318 n = 2 * SD->max_size / Config.Store.avgObjectSize;
319 if (NULL == SD->map) {
320 /* first time */
321 SD->map = file_map_create(n);
322 } else if (n > SD->map->max_n_files) {
323 /* it grew, need to expand */
324 fm = file_map_create(n);
325 filemapCopy(SD->map, fm);
326 filemapFreeMemory(SD->map);
327 SD->map = fm;
328 }
329 /* else it shrunk, and we leave the old one in place */
330 }
331 }
332
333 void
334 storeDirDiskFull(int fn)
335 {
336 int dirn = fn >> SWAP_DIR_SHIFT;
337 SwapDir *SD = &Config.cacheSwap.swapDirs[dirn];
338 assert(0 <= dirn && dirn < Config.cacheSwap.n_configured);
339 SD->max_size = SD->cur_size;
340 debug(20, 1) ("WARNING: Shrinking cache_dir #%d to %d KB\n",
341 dirn, SD->cur_size);
342 }
343
344 void
345 storeDirOpenSwapLogs(void)
346 {
347 return storeUfsDirOpenSwapLogs();
348 }
349
350 void
351 storeDirCloseSwapLogs(void)
352 {
353 return storeUfsDirCloseSwapLogs();
354 }
355
356 void
357 storeDirCloseTmpSwapLog(int dirn)
358 {
359 return storeUfsDirCloseTmpSwapLog(dirn);
360 }