]>
git.saurik.com Git - hfs.git/blob - fsck_hfs/cache.h
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * User Land Cache Manager
27 * A user land cache manager.
33 /* Different values for initializing cache */
36 DefaultCacheBlockSize
= 0x8000, /* 32K */
37 DefaultCacheBlocks
= 1024,
38 DefaultCacheSize
= (DefaultCacheBlockSize
* DefaultCacheBlocks
), /* 32MBytes */
40 /* Minimum allowed sizes */
41 MinCacheBlockSize
= 0x8000, /* 32K */
42 MinCacheBlocks
= 1024,
43 MinCacheSize
= (MinCacheBlockSize
* MinCacheBlocks
), /* 32MBytes */
45 /* Maximum allowed sizes */
46 MaxCacheBlockSize
= 0x8000, /* 32K */
48 MaxCacheBlocks
= 0x18000,
50 MaxCacheBlocks
= 0x8000,
52 /* MaxCacheSize will be 3G for 64-bit, and 1G for 32-bit */
53 MaxCacheSize
= ((unsigned)MaxCacheBlockSize
* MaxCacheBlocks
),
54 CacheHashSize
= 257, /* prime number */
58 * Some nice lowercase shortcuts.
62 #define BUF_SPAN 0x80000000 /* Buffer spans several cache blocks */
64 typedef struct LRUNode_t
66 struct LRUNode_t
* Next
; /* Next node in the LRU */
67 struct LRUNode_t
* Prev
; /* Previous node in the LRU */
72 LRUNode_t Head
; /* Dummy node for the head of the LRU */
73 LRUNode_t Busy
; /* List of busy nodes */
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.
87 struct Buf_t
* Next
; /* Next active buffer */
88 struct Buf_t
* Prev
; /* Previous active buffer */
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 */
94 void * Buffer
; /* Buffer */
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.
104 * NOTE: The LRU field must be the first field, so we can easily cast between
109 LRUNode_t LRU
; /* LRU specific data, must be first! */
111 struct Tag_t
* Next
; /* Next tag in hash chain */
112 struct Tag_t
* Prev
; /* Previous tag in hash chain */
115 uint32_t Refs
; /* Reference count */
116 uint64_t Offset
; /* Offset of the buffer */
118 void * Buffer
; /* Cache page */
122 /* Tag_t.Flags bit settings */
124 kLazyWrite
= 0x00000001, /* only write this page when evicting or forced */
125 kLockWrite
= 0x00000002, /* Never evict this page -- will not work with writing yet! */
131 * The main cache data structure. The cache manages access between an open
132 * file and the cache client program.
134 * NOTE: The LRU field must be the first field, so we can easily cast between
137 typedef struct Cache_t
139 LRU_t LRU
; /* LRU replacement data structure */
141 int FD_R
; /* File descriptor (read-only) */
142 int FD_W
; /* File descriptor (write-only) */
143 uint32_t DevBlockSize
; /* Device block size */
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 */
149 void * FreeHead
; /* Head of the free list */
150 uint32_t FreeSize
; /* Size of the free list */
152 Buf_t
* ActiveBufs
; /* List of active buffers */
153 Buf_t
* FreeBufs
; /* List of free buffers */
155 uint32_t ReqRead
; /* Number of read requests */
156 uint32_t ReqWrite
; /* Number of write requests */
158 uint32_t DiskRead
; /* Number of actual disk reads */
159 uint32_t DiskWrite
; /* Number of actual disk writes */
161 uint32_t Span
; /* Requests that spanned cache blocks */
164 extern Cache_t fscache
;
167 * CalculateCacheSizes
169 * Determine the cache size values (block size and total blocks) that should
170 * be used to initialize the cache.
172 void CalculateCacheSizes(uint64_t userCacheSize
, uint32_t *calcBlockSize
, uint32_t *calcTotalBlocks
,
177 * Initializes the cache for use.
179 int CacheInit (Cache_t
*cache
, int fdRead
, int fdWrite
, uint32_t devBlockSize
,
180 uint32_t cacheBlockSize
, uint32_t cacheSize
, uint32_t hashSize
,
186 * Shutdown the cache.
188 int CacheDestroy (Cache_t
*cache
);
193 * Reads a range of bytes from the cache, returning a pointer to a buffer
194 * containing the requested bytes.
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.
200 int CacheRead (Cache_t
*cache
, uint64_t start
, uint32_t len
, Buf_t
**buf
);
205 * Writes a buffer through the cache.
207 int CacheWrite ( Cache_t
*cache
, Buf_t
*buf
, int age
, uint32_t writeOptions
);
212 * Releases a clean buffer.
214 * NOTE: We don't verify whether it's dirty or not.
216 int CacheRelease (Cache_t
*cache
, Buf_t
*buf
, int age
);
220 * Disposes of a particular tag and buffer.
222 int CacheRemove (Cache_t
*cache
, Tag_t
*tag
);
227 * Only dispose of the buffer, leave the tag intact.
229 int CacheEvict (Cache_t
*cache
, Tag_t
*tag
);
234 * Write out any blocks that are marked for lazy write.
237 CacheFlush( Cache_t
*cache
);
239 /* CacheCopyDiskBlocks
241 * Perform direct disk block copy from from_offset to to_offset of given length.
243 int CacheCopyDiskBlocks (Cache_t
*cache
, uint64_t from_offset
, uint64_t to_offset
, uint32_t len
);
245 /* CacheWriteBufferToDisk
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.
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.
255 int CacheWriteBufferToDisk (Cache_t
*cache
, uint64_t offset
, uint32_t write_len
, u_char
*buffer
, uint32_t buf_len
);