]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fs/ufs/ufscommon.h
815e3b2d641402220bf88a49f7abbd995089368c
[thirdparty/squid.git] / src / fs / ufs / ufscommon.h
1 /*
2 * $Id$
3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
29 *
30 */
31 #ifndef SQUID_UFSCOMMON_H
32 #define SQUID_UFSCOMMON_H
33
34
35 #define DefaultLevelOneDirs 16
36 #define DefaultLevelTwoDirs 256
37 #define STORE_META_BUFSZ 4096
38
39 class UFSStrategy;
40 class ConfigOptionVector;
41 class DiskIOModule;
42 class StoreSearch;
43
44 #include "SwapDir.h"
45
46 /// \ingroup UFS
47 class UFSSwapDir : public SwapDir
48 {
49
50 public:
51 static int IsUFSDir(SwapDir* sd);
52 static int DirClean(int swap_index);
53 static int FilenoBelongsHere(int fn, int F0, int F1, int F2);
54
55 UFSSwapDir(char const *aType, const char *aModuleType);
56 virtual void init();
57 virtual void create();
58 virtual void dump(StoreEntry &) const;
59 ~UFSSwapDir();
60 virtual StoreSearch *search(String const url, HttpRequest *);
61 virtual bool doubleCheck(StoreEntry &);
62 virtual void unlink(StoreEntry &);
63 virtual void statfs(StoreEntry &)const;
64 virtual void maintain();
65 virtual int canStore(StoreEntry const &)const;
66 virtual void reference(StoreEntry &);
67 virtual void dereference(StoreEntry &);
68 virtual StoreIOState::Pointer createStoreIO(StoreEntry &, StoreIOState::STFNCB *, StoreIOState::STIOCB *, void *);
69 virtual StoreIOState::Pointer openStoreIO(StoreEntry &, StoreIOState::STFNCB *, StoreIOState::STIOCB *, void *);
70 virtual void openLog();
71 virtual void closeLog();
72 virtual int writeCleanStart();
73 virtual void writeCleanDone();
74 virtual void logEntry(const StoreEntry & e, int op) const;
75 virtual void parse(int index, char *path);
76 virtual void reconfigure(int, char *);
77 virtual int callback();
78 virtual void sync();
79
80 void unlinkFile(sfileno f);
81 // move down when unlink is a virtual method
82 //protected:
83 UFSStrategy *IO;
84 char *fullPath(sfileno, char *) const;
85 /* temp */
86 void closeTmpSwapLog();
87 FILE *openTmpSwapLog(int *clean_flag, int *zero_flag);
88 char *swapSubDir(int subdirn) const;
89 int mapBitTest(sfileno filn);
90 void mapBitReset(sfileno filn);
91 void mapBitSet(sfileno filn);
92 StoreEntry *addDiskRestore(const cache_key * key,
93 sfileno file_number,
94 uint64_t swap_file_sz,
95 time_t expires,
96 time_t timestamp,
97 time_t lastref,
98 time_t lastmod,
99 u_int32_t refcount,
100 u_int16_t flags,
101 int clean);
102 int validFileno(sfileno filn, int flag) const;
103 int mapBitAllocate();
104 virtual ConfigOption *getOptionTree() const;
105
106 void *fsdata;
107
108 bool validL2(int) const;
109 bool validL1(int) const;
110
111 void replacementAdd(StoreEntry *e);
112 void replacementRemove(StoreEntry *e);
113
114 protected:
115 fileMap *map;
116 int suggest;
117 int l1;
118 int l2;
119
120 private:
121 void parseSizeL1L2();
122 static int NumberOfUFSDirs;
123 static int * UFSDirToGlobalDirMapping;
124 bool pathIsDirectory(const char *path)const;
125 int swaplog_fd;
126 static EVH CleanEvent;
127 bool verifyCacheDirs();
128 void rebuild();
129 int createDirectory(const char *path, int);
130 void createSwapSubDirs();
131 void dumpEntry(StoreEntry &) const;
132 char *logFile(char const *ext = NULL)const;
133 void changeIO(DiskIOModule *);
134 bool optionIOParse(char const *option, const char *value, int reconfiguring);
135 void optionIODump(StoreEntry * e) const;
136 mutable ConfigOptionVector *currentIOOptions;
137 char const *ioType;
138
139 };
140
141 #include "RefCount.h"
142 #include "DiskIO/IORequestor.h"
143
144 /**
145 * UFS dir specific IO calls
146 *
147 \todo This should be whittled away.
148 * DiskIOModule should be providing the entire needed API.
149 */
150
151 class DiskIOStrategy;
152
153 class DiskFile;
154
155 /// \ingroup UFS
156 class UFSStrategy
157 {
158
159 public:
160 UFSStrategy (DiskIOStrategy *);
161 virtual ~UFSStrategy ();
162 /* Not implemented */
163 UFSStrategy (UFSStrategy const &);
164 UFSStrategy &operator=(UFSStrategy const &);
165
166 virtual bool shedLoad();
167
168 virtual int load();
169
170 StoreIOState::Pointer createState(SwapDir *SD, StoreEntry *e, StoreIOState::STIOCB * callback, void *callback_data) const;
171 /* UFS specific */
172 virtual RefCount<DiskFile> newFile (char const *path);
173 StoreIOState::Pointer open(SwapDir *, StoreEntry *, StoreIOState::STFNCB *,
174 StoreIOState::STIOCB *, void *);
175 StoreIOState::Pointer create(SwapDir *, StoreEntry *, StoreIOState::STFNCB *,
176 StoreIOState::STIOCB *, void *);
177
178 virtual void unlinkFile (char const *);
179 virtual void sync();
180
181 virtual int callback();
182
183 /** Init per-instance logic */
184 virtual void init();
185
186 /** cachemgr output on the IO instance stats */
187 virtual void statfs(StoreEntry & sentry)const;
188
189 /** The io strategy in use */
190 DiskIOStrategy *io;
191 protected:
192
193 friend class UFSSwapDir;
194 };
195
196 /** Common ufs-store-dir logic */
197
198 class ReadRequest;
199
200 /// \ingroup UFS
201 class UFSStoreState : public StoreIOState, public IORequestor
202 {
203
204 public:
205 void * operator new (size_t);
206 void operator delete (void *);
207 UFSStoreState(SwapDir * SD, StoreEntry * anEntry, STIOCB * callback_, void *callback_data_);
208 ~UFSStoreState();
209 virtual void close();
210 virtual void closeCompleted();
211 // protected:
212 virtual void ioCompletedNotification();
213 virtual void readCompleted(const char *buf, int len, int errflag, RefCount<ReadRequest>);
214 virtual void writeCompleted(int errflag, size_t len, RefCount<WriteRequest>);
215 RefCount<DiskFile> theFile;
216 bool opening;
217 bool creating;
218 bool closing;
219 bool reading;
220 bool writing;
221 void read_(char *buf, size_t size, off_t offset, STRCB * callback, void *callback_data);
222 void write(char const *buf, size_t size, off_t offset, FREE * free_func);
223
224 protected:
225 virtual void doCloseCallback (int errflag);
226
227 class _queued_read
228 {
229
230 public:
231 MEMPROXY_CLASS(UFSStoreState::_queued_read);
232 char *buf;
233 size_t size;
234 off_t offset;
235 STRCB *callback;
236 void *callback_data;
237
238 };
239
240 class _queued_write
241 {
242
243 public:
244 MEMPROXY_CLASS(UFSStoreState::_queued_write);
245 char const *buf;
246 size_t size;
247 off_t offset;
248 FREE *free_func;
249
250 };
251
252 /** \todo These should be in the IO strategy */
253
254 struct {
255 /**
256 * DPW 2006-05-24
257 * the write_draining flag is used to avoid recursion inside
258 * the UFSStoreState::drainWriteQueue() method.
259 */
260 bool write_draining;
261 /**
262 * DPW 2006-05-24
263 * The try_closing flag is set by UFSStoreState::tryClosing()
264 * when UFSStoreState wants to close the file, but cannot
265 * because of pending I/Os. If set, UFSStoreState will
266 * try to close again in the I/O callbacks.
267 */
268 bool try_closing;
269 } flags;
270 link_list *pending_reads;
271 link_list *pending_writes;
272 void queueRead(char *, size_t, off_t, STRCB *, void *);
273 void queueWrite(char const *, size_t, off_t, FREE *);
274 bool kickReadQueue();
275 void drainWriteQueue();
276 void tryClosing();
277 char *read_buf;
278
279 private:
280 CBDATA_CLASS(UFSStoreState);
281 void openDone();
282 void freePending();
283 void doWrite();
284 };
285
286 MEMPROXY_CLASS_INLINE(UFSStoreState::_queued_read);
287 MEMPROXY_CLASS_INLINE(UFSStoreState::_queued_write);
288
289
290 #include "StoreSearch.h"
291
292 /// \ingroup UFS
293 class StoreSearchUFS : public StoreSearch
294 {
295
296 public:
297 StoreSearchUFS(RefCount<UFSSwapDir> sd);
298 StoreSearchUFS(StoreSearchUFS const &);
299 virtual ~StoreSearchUFS();
300
301 /** \todo Iterator API - garh, wrong place */
302 /**
303 * callback the client when a new StoreEntry is available
304 * or an error occurs
305 */
306 virtual void next(void (callback)(void *cbdata), void *cbdata);
307
308 /**
309 \retval true if a new StoreEntry is immediately available
310 \retval false if a new StoreEntry is NOT immediately available
311 */
312 virtual bool next();
313
314 virtual bool error() const;
315 virtual bool isDone() const;
316 virtual StoreEntry *currentItem();
317
318 RefCount<UFSSwapDir> sd;
319 RemovalPolicyWalker *walker;
320
321 private:
322 CBDATA_CLASS2(StoreSearchUFS);
323 /// \bug (callback) should be hidden behind a proper human readable name
324 void (callback)(void *cbdata);
325 void *cbdata;
326 StoreEntry * current;
327 bool _done;
328 };
329
330
331 class StoreSwapLogData;
332
333 /// \ingroup UFS
334 class UFSSwapLogParser
335 {
336
337 public:
338 FILE *log;
339 int log_entries;
340 int record_size;
341
342 UFSSwapLogParser(FILE *fp):log(fp),log_entries(-1), record_size(0) {
343 }
344 virtual ~UFSSwapLogParser() {};
345
346 static UFSSwapLogParser *GetUFSSwapLogParser(FILE *fp);
347
348 virtual bool ReadRecord(StoreSwapLogData &swapData) = 0;
349 int SwapLogEntries();
350 void Close() {
351 if (log) {
352 fclose(log);
353 log = NULL;
354 }
355 }
356 };
357
358
359 /// \ingroup UFS
360 class RebuildState : public RefCountable
361 {
362
363 public:
364 static EVH RebuildStep;
365
366 RebuildState(RefCount<UFSSwapDir> sd);
367 ~RebuildState();
368
369 /** \todo Iterator API - garh, wrong place */
370 /**
371 * callback the client when a new StoreEntry is available
372 * or an error occurs
373 */
374 virtual void next(void (callback)(void *cbdata), void *cbdata);
375
376 /**
377 \retval true if a new StoreEntry is immediately available
378 \retval false if a new StoreEntry is NOT immediately available
379 */
380 virtual bool next();
381 virtual bool error() const;
382 virtual bool isDone() const;
383 virtual StoreEntry *currentItem();
384
385 RefCount<UFSSwapDir> sd;
386 int n_read;
387 /* FILE *log;*/
388 UFSSwapLogParser *LogParser;
389 int speed;
390 int curlvl1;
391 int curlvl2;
392
393 struct {
394 unsigned int need_to_validate:1;
395 unsigned int clean:1;
396 unsigned int init:1;
397 } flags;
398 int in_dir;
399 int done;
400 int fn;
401
402 struct dirent *entry;
403 DIR *td;
404 char fullpath[SQUID_MAXPATHLEN];
405 char fullfilename[SQUID_MAXPATHLEN];
406
407 struct _store_rebuild_data counts;
408
409 private:
410 CBDATA_CLASS2(RebuildState);
411 void rebuildFromDirectory();
412 void rebuildFromSwapLog();
413 void rebuildStep();
414 int getNextFile(sfileno *, int *size);
415 StoreEntry *currentEntry() const;
416 void currentEntry(StoreEntry *);
417 StoreEntry *e;
418 bool fromLog;
419 bool _done;
420 /// \bug (callback) should be hidden behind a proper human readable name
421 void (callback)(void *cbdata);
422 void *cbdata;
423 };
424
425 #ifdef _USE_INLINE_
426 #include "ufscommon.cci"
427 #endif
428
429 #endif /* SQUID_UFSCOMMON_H */