]> git.saurik.com Git - apple/hfs.git/blob - fsck_hfs/cache.h
hfs-407.1.3.tar.gz
[apple/hfs.git] / fsck_hfs / cache.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * User Land Cache Manager
26 *
27 * A user land cache manager.
28 */
29 #ifndef _CACHE_H_
30 #define _CACHE_H_
31 #include <stdint.h>
32
33 /* Different values for initializing cache */
34 enum {
35 /* Default sizes */
36 DefaultCacheBlockSize = 0x8000, /* 32K */
37 DefaultCacheBlocks = 1024,
38 DefaultCacheSize = (DefaultCacheBlockSize * DefaultCacheBlocks), /* 32MBytes */
39
40 /* Minimum allowed sizes */
41 MinCacheBlockSize = 0x8000, /* 32K */
42 MinCacheBlocks = 1024,
43 MinCacheSize = (MinCacheBlockSize * MinCacheBlocks), /* 32MBytes */
44
45 /* Maximum allowed sizes */
46 MaxCacheBlockSize = 0x8000, /* 32K */
47 #ifdef __LP64__
48 MaxCacheBlocks = 0x18000,
49 #else
50 MaxCacheBlocks = 0x8000,
51 #endif
52 /* MaxCacheSize will be 3G for 64-bit, and 1G for 32-bit */
53 MaxCacheSize = ((unsigned)MaxCacheBlockSize * MaxCacheBlocks),
54 CacheHashSize = 257, /* prime number */
55 };
56
57 /*
58 * Some nice lowercase shortcuts.
59 */
60 #define EOK 0
61
62 #define BUF_SPAN 0x80000000 /* Buffer spans several cache blocks */
63
64 typedef struct LRUNode_t
65 {
66 struct LRUNode_t * Next; /* Next node in the LRU */
67 struct LRUNode_t * Prev; /* Previous node in the LRU */
68 } LRUNode_t;
69
70 typedef struct LRU_t
71 {
72 LRUNode_t Head; /* Dummy node for the head of the LRU */
73 LRUNode_t Busy; /* List of busy nodes */
74 } LRU_t;
75
76
77 #define MAXBUFS 48
78 /*
79 * Buf_t
80 *
81 * Buffer structure exchanged between the cache and client. It contains the
82 * data buffer with the requested data, as well as housekeeping information
83 * that the cache needs.
84 */
85 typedef struct Buf_t
86 {
87 struct Buf_t * Next; /* Next active buffer */
88 struct Buf_t * Prev; /* Previous active buffer */
89
90 uint32_t Flags; /* Buffer flags */
91 uint64_t Offset; /* Start offset of the buffer */
92 uint32_t Length; /* Size of the buffer in bytes */
93
94 void * Buffer; /* Buffer */
95 } Buf_t;
96
97 /*
98 * Tag_t
99 *
100 * The cache tag structure is a header for a cache buffer. It contains a
101 * pointer to the cache block and housekeeping information. The type of LRU
102 * algorithm can be swapped out easily.
103 *
104 * NOTE: The LRU field must be the first field, so we can easily cast between
105 * the two.
106 */
107 typedef struct Tag_t
108 {
109 LRUNode_t LRU; /* LRU specific data, must be first! */
110
111 struct Tag_t * Next; /* Next tag in hash chain */
112 struct Tag_t * Prev; /* Previous tag in hash chain */
113
114 uint32_t Flags;
115 uint32_t Refs; /* Reference count */
116 uint64_t Offset; /* Offset of the buffer */
117
118 void * Buffer; /* Cache page */
119 } Tag_t;
120
121
122 /* Tag_t.Flags bit settings */
123 enum {
124 kLazyWrite = 0x00000001, /* only write this page when evicting or forced */
125 kLockWrite = 0x00000002, /* Never evict this page -- will not work with writing yet! */
126 };
127
128 /*
129 * Cache_t
130 *
131 * The main cache data structure. The cache manages access between an open
132 * file and the cache client program.
133 *
134 * NOTE: The LRU field must be the first field, so we can easily cast between
135 * the two.
136 */
137 typedef struct Cache_t
138 {
139 LRU_t LRU; /* LRU replacement data structure */
140
141 int FD_R; /* File descriptor (read-only) */
142 int FD_W; /* File descriptor (write-only) */
143 uint32_t DevBlockSize; /* Device block size */
144
145 Tag_t ** Hash; /* Lookup hash table (move to front) */
146 uint32_t HashSize; /* Size of the hash table */
147 uint32_t BlockSize; /* Size of the cache page */
148
149 void * FreeHead; /* Head of the free list */
150 uint32_t FreeSize; /* Size of the free list */
151
152 Buf_t * ActiveBufs; /* List of active buffers */
153 Buf_t * FreeBufs; /* List of free buffers */
154
155 uint32_t ReqRead; /* Number of read requests */
156 uint32_t ReqWrite; /* Number of write requests */
157
158 uint32_t DiskRead; /* Number of actual disk reads */
159 uint32_t DiskWrite; /* Number of actual disk writes */
160
161 uint32_t Span; /* Requests that spanned cache blocks */
162 } Cache_t;
163
164 extern Cache_t fscache;
165
166 /*
167 * CalculateCacheSizes
168 *
169 * Determine the cache size values (block size and total blocks) that should
170 * be used to initialize the cache.
171 */
172 void CalculateCacheSizes(uint64_t userCacheSize, uint32_t *calcBlockSize, uint32_t *calcTotalBlocks,
173 char debug);
174 /*
175 * CacheInit
176 *
177 * Initializes the cache for use.
178 */
179 int CacheInit (Cache_t *cache, int fdRead, int fdWrite, uint32_t devBlockSize,
180 uint32_t cacheBlockSize, uint32_t cacheSize, uint32_t hashSize,
181 int preTouch);
182
183 /*
184 * CacheDestroy
185 *
186 * Shutdown the cache.
187 */
188 int CacheDestroy (Cache_t *cache);
189
190 /*
191 * CacheRead
192 *
193 * Reads a range of bytes from the cache, returning a pointer to a buffer
194 * containing the requested bytes.
195 *
196 * NOTE: The returned buffer may directly refer to a cache block, or an
197 * anonymous buffer. Do not make any assumptions about the nature of
198 * the returned buffer, except that it is contiguous.
199 */
200 int CacheRead (Cache_t *cache, uint64_t start, uint32_t len, Buf_t **buf);
201
202 /*
203 * CacheWrite
204 *
205 * Writes a buffer through the cache.
206 */
207 int CacheWrite ( Cache_t *cache, Buf_t *buf, int age, uint32_t writeOptions );
208
209 /*
210 * CacheRelease
211 *
212 * Releases a clean buffer.
213 *
214 * NOTE: We don't verify whether it's dirty or not.
215 */
216 int CacheRelease (Cache_t *cache, Buf_t *buf, int age);
217
218 /* CacheRemove
219 *
220 * Disposes of a particular tag and buffer.
221 */
222 int CacheRemove (Cache_t *cache, Tag_t *tag);
223
224 /*
225 * CacheEvict
226 *
227 * Only dispose of the buffer, leave the tag intact.
228 */
229 int CacheEvict (Cache_t *cache, Tag_t *tag);
230
231 /*
232 * CacheFlush
233 *
234 * Write out any blocks that are marked for lazy write.
235 */
236 int
237 CacheFlush( Cache_t *cache );
238
239 /* CacheCopyDiskBlocks
240 *
241 * Perform direct disk block copy from from_offset to to_offset of given length.
242 */
243 int CacheCopyDiskBlocks (Cache_t *cache, uint64_t from_offset, uint64_t to_offset, uint32_t len);
244
245 /* CacheWriteBufferToDisk
246 *
247 * Write data on disk starting at given offset for upto write_len.
248 * The data from given buffer upto buf_len is written to the disk starting
249 * at given offset. If the amount of data written on disk is greater than
250 * the length of buffer, all the remaining data is written as zeros.
251 *
252 * If no buffer is provided or if length of buffer is zero, the function
253 * writes zeros on disk from offset upto write_len bytes.
254 */
255 int CacheWriteBufferToDisk (Cache_t *cache, uint64_t offset, uint32_t write_len, u_char *buffer, uint32_t buf_len);
256 #endif
257