1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is nsDiskCacheBlockFile.cpp, released
17 : * April 12, 2001.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 2001
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Gordon Sheridan <gordon@netscape.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsDiskCache.h"
42 : #include "nsDiskCacheBlockFile.h"
43 : #include "mozilla/FileUtils.h"
44 :
45 : using namespace mozilla;
46 :
47 : /******************************************************************************
48 : * nsDiskCacheBlockFile -
49 : *****************************************************************************/
50 :
51 : /******************************************************************************
52 : * Open
53 : *****************************************************************************/
54 : nsresult
55 708 : nsDiskCacheBlockFile::Open(nsILocalFile * blockFile,
56 : PRUint32 blockSize,
57 : PRUint32 bitMapSize)
58 : {
59 708 : if (bitMapSize % 32)
60 0 : return NS_ERROR_INVALID_ARG;
61 :
62 708 : mBlockSize = blockSize;
63 708 : mBitMapWords = bitMapSize / 32;
64 708 : PRUint32 bitMapBytes = mBitMapWords * 4;
65 :
66 : // open the file - restricted to user, the data could be confidential
67 708 : nsresult rv = blockFile->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE, 00600, &mFD);
68 708 : if (NS_FAILED(rv)) return rv; // unable to open or create file
69 :
70 : // allocate bit map buffer
71 1416 : mBitMap = new PRUint32[mBitMapWords];
72 708 : if (!mBitMap) {
73 0 : rv = NS_ERROR_OUT_OF_MEMORY;
74 0 : goto error_exit;
75 : }
76 :
77 : // check if we just creating the file
78 708 : mFileSize = PR_Available(mFD);
79 708 : if (mFileSize < 0) {
80 : // XXX an error occurred. We could call PR_GetError(), but how would that help?
81 0 : rv = NS_ERROR_UNEXPECTED;
82 0 : goto error_exit;
83 : }
84 708 : if (mFileSize == 0) {
85 : // initialize bit map and write it
86 708 : memset(mBitMap, 0, bitMapBytes);
87 708 : if (!Write(0, mBitMap, bitMapBytes))
88 0 : goto error_exit;
89 :
90 0 : } else if ((PRUint32)mFileSize < bitMapBytes) {
91 0 : rv = NS_ERROR_UNEXPECTED; // XXX NS_ERROR_CACHE_INVALID;
92 0 : goto error_exit;
93 :
94 : } else {
95 : // read the bit map
96 0 : const PRInt32 bytesRead = PR_Read(mFD, mBitMap, bitMapBytes);
97 0 : if ((bytesRead < 0) || ((PRUint32)bytesRead < bitMapBytes)) {
98 0 : rv = NS_ERROR_UNEXPECTED;
99 0 : goto error_exit;
100 : }
101 : #if defined(IS_LITTLE_ENDIAN)
102 : // Swap from network format
103 0 : for (unsigned int i = 0; i < mBitMapWords; ++i)
104 0 : mBitMap[i] = ntohl(mBitMap[i]);
105 : #endif
106 : // validate block file size
107 : // Because not whole blocks are written, the size may be a
108 : // little bit smaller than used blocks times blocksize,
109 : // because the last block will generally not be 'whole'.
110 0 : const PRUint32 estimatedSize = CalcBlockFileSize();
111 0 : if ((PRUint32)mFileSize + blockSize < estimatedSize) {
112 0 : rv = NS_ERROR_UNEXPECTED;
113 0 : goto error_exit;
114 : }
115 : }
116 708 : return NS_OK;
117 :
118 : error_exit:
119 0 : Close(false);
120 0 : return rv;
121 : }
122 :
123 :
124 : /******************************************************************************
125 : * Close
126 : *****************************************************************************/
127 : nsresult
128 1233 : nsDiskCacheBlockFile::Close(bool flush)
129 : {
130 1233 : nsresult rv = NS_OK;
131 :
132 1233 : if (mFD) {
133 708 : if (flush)
134 525 : rv = FlushBitMap();
135 708 : PRStatus err = PR_Close(mFD);
136 708 : if (NS_SUCCEEDED(rv) && (err != PR_SUCCESS))
137 0 : rv = NS_ERROR_UNEXPECTED;
138 708 : mFD = nsnull;
139 : }
140 :
141 1233 : if (mBitMap) {
142 708 : delete [] mBitMap;
143 708 : mBitMap = nsnull;
144 : }
145 :
146 1233 : return rv;
147 : }
148 :
149 :
150 : /******************************************************************************
151 : * AllocateBlocks
152 : *
153 : * Allocates 1-4 blocks, using a first fit strategy,
154 : * so that no group of blocks spans a quad block boundary.
155 : *
156 : * Returns block number of first block allocated or -1 on failure.
157 : *
158 : *****************************************************************************/
159 : PRInt32
160 1136 : nsDiskCacheBlockFile::AllocateBlocks(PRInt32 numBlocks)
161 : {
162 1136 : const int maxPos = 32 - numBlocks;
163 1136 : const PRUint32 mask = (0x01 << numBlocks) - 1;
164 1290 : for (unsigned int i = 0; i < mBitMapWords; ++i) {
165 1290 : PRUint32 mapWord = ~mBitMap[i]; // flip bits so free bits are 1
166 1290 : if (mapWord) { // At least one free bit
167 : // Binary search for first free bit in word
168 1164 : int bit = 0;
169 1164 : if ((mapWord & 0x0FFFF) == 0) { bit |= 16; mapWord >>= 16; }
170 1164 : if ((mapWord & 0x000FF) == 0) { bit |= 8; mapWord >>= 8; }
171 1164 : if ((mapWord & 0x0000F) == 0) { bit |= 4; mapWord >>= 4; }
172 1164 : if ((mapWord & 0x00003) == 0) { bit |= 2; mapWord >>= 2; }
173 1164 : if ((mapWord & 0x00001) == 0) { bit |= 1; mapWord >>= 1; }
174 : // Find first fit for mask
175 1164 : for (; bit <= maxPos; ++bit) {
176 : // all bits selected by mask are 1, so free
177 1136 : if ((mask & mapWord) == mask) {
178 1136 : mBitMap[i] |= mask << bit;
179 1136 : mBitMapDirty = true;
180 1136 : return (PRInt32)i * 32 + bit;
181 : }
182 : }
183 : }
184 : }
185 :
186 0 : return -1;
187 : }
188 :
189 :
190 : /******************************************************************************
191 : * DeallocateBlocks
192 : *****************************************************************************/
193 : nsresult
194 423 : nsDiskCacheBlockFile::DeallocateBlocks( PRInt32 startBlock, PRInt32 numBlocks)
195 : {
196 423 : if (!mFD) return NS_ERROR_NOT_AVAILABLE;
197 :
198 423 : if ((startBlock < 0) || ((PRUint32)startBlock > mBitMapWords * 32 - 1) ||
199 : (numBlocks < 1) || (numBlocks > 4))
200 0 : return NS_ERROR_ILLEGAL_VALUE;
201 :
202 423 : const PRInt32 startWord = startBlock >> 5; // Divide by 32
203 423 : const PRUint32 startBit = startBlock & 31; // Modulo by 32
204 :
205 : // make sure requested deallocation doesn't span a word boundary
206 423 : if (startBit + numBlocks > 32) return NS_ERROR_UNEXPECTED;
207 423 : PRUint32 mask = ((0x01 << numBlocks) - 1) << startBit;
208 :
209 : // make sure requested deallocation is currently allocated
210 423 : if ((mBitMap[startWord] & mask) != mask) return NS_ERROR_ABORT;
211 :
212 423 : mBitMap[startWord] ^= mask; // flips the bits off;
213 423 : mBitMapDirty = true;
214 : // XXX rv = FlushBitMap(); // coherency vs. performance
215 423 : return NS_OK;
216 : }
217 :
218 :
219 : /******************************************************************************
220 : * WriteBlocks
221 : *****************************************************************************/
222 : nsresult
223 1136 : nsDiskCacheBlockFile::WriteBlocks( void * buffer,
224 : PRUint32 size,
225 : PRInt32 numBlocks,
226 : PRInt32 * startBlock)
227 : {
228 : // presume buffer != nsnull and startBlock != nsnull
229 1136 : NS_ENSURE_TRUE(mFD, NS_ERROR_NOT_AVAILABLE);
230 :
231 : // allocate some blocks in the cache block file
232 1136 : *startBlock = AllocateBlocks(numBlocks);
233 1136 : if (*startBlock < 0)
234 0 : return NS_ERROR_NOT_AVAILABLE;
235 :
236 : // seek to block position
237 1136 : PRInt32 blockPos = mBitMapWords * 4 + *startBlock * mBlockSize;
238 :
239 : // write the blocks
240 1136 : return Write(blockPos, buffer, size) ? NS_OK : NS_ERROR_FAILURE;
241 : }
242 :
243 :
244 : /******************************************************************************
245 : * ReadBlocks
246 : *****************************************************************************/
247 : nsresult
248 439 : nsDiskCacheBlockFile::ReadBlocks( void * buffer,
249 : PRInt32 startBlock,
250 : PRInt32 numBlocks,
251 : PRInt32 * bytesRead)
252 : {
253 : // presume buffer != nsnull and bytesRead != bytesRead
254 :
255 439 : if (!mFD) return NS_ERROR_NOT_AVAILABLE;
256 439 : nsresult rv = VerifyAllocation(startBlock, numBlocks);
257 439 : if (NS_FAILED(rv)) return rv;
258 :
259 : // seek to block position
260 439 : PRInt32 blockPos = mBitMapWords * 4 + startBlock * mBlockSize;
261 439 : PRInt32 filePos = PR_Seek(mFD, blockPos, PR_SEEK_SET);
262 439 : if (filePos != blockPos) return NS_ERROR_UNEXPECTED;
263 :
264 : // read the blocks
265 439 : PRInt32 bytesToRead = *bytesRead;
266 439 : if ((bytesToRead <= 0) || ((PRUint32)bytesToRead > mBlockSize * numBlocks)) {
267 0 : bytesToRead = mBlockSize * numBlocks;
268 : }
269 439 : *bytesRead = PR_Read(mFD, buffer, bytesToRead);
270 :
271 439 : return NS_OK;
272 : }
273 :
274 :
275 : /******************************************************************************
276 : * FlushBitMap
277 : *****************************************************************************/
278 : nsresult
279 525 : nsDiskCacheBlockFile::FlushBitMap()
280 : {
281 525 : if (!mBitMapDirty) return NS_OK;
282 :
283 : #if defined(IS_LITTLE_ENDIAN)
284 390 : PRUint32 *bitmap = new PRUint32[mBitMapWords];
285 : // Copy and swap to network format
286 195 : PRUint32 *p = bitmap;
287 534723 : for (unsigned int i = 0; i < mBitMapWords; ++i, ++p)
288 534528 : *p = htonl(mBitMap[i]);
289 : #else
290 : PRUint32 *bitmap = mBitMap;
291 : #endif
292 :
293 : // write bitmap
294 195 : bool written = Write(0, bitmap, mBitMapWords * 4);
295 : #if defined(IS_LITTLE_ENDIAN)
296 195 : delete [] bitmap;
297 : #endif
298 195 : if (!written)
299 0 : return NS_ERROR_UNEXPECTED;
300 :
301 195 : PRStatus err = PR_Sync(mFD);
302 195 : if (err != PR_SUCCESS) return NS_ERROR_UNEXPECTED;
303 :
304 195 : mBitMapDirty = false;
305 195 : return NS_OK;
306 : }
307 :
308 :
309 : /******************************************************************************
310 : * VerifyAllocation
311 : *
312 : * Return values:
313 : * NS_OK if all bits are marked allocated
314 : * NS_ERROR_ILLEGAL_VALUE if parameters don't obey constraints
315 : * NS_ERROR_FAILURE if some or all the bits are marked unallocated
316 : *
317 : *****************************************************************************/
318 : nsresult
319 439 : nsDiskCacheBlockFile::VerifyAllocation( PRInt32 startBlock, PRInt32 numBlocks)
320 : {
321 439 : if ((startBlock < 0) || ((PRUint32)startBlock > mBitMapWords * 32 - 1) ||
322 : (numBlocks < 1) || (numBlocks > 4))
323 0 : return NS_ERROR_ILLEGAL_VALUE;
324 :
325 439 : const PRInt32 startWord = startBlock >> 5; // Divide by 32
326 439 : const PRUint32 startBit = startBlock & 31; // Modulo by 32
327 :
328 : // make sure requested deallocation doesn't span a word boundary
329 439 : if (startBit + numBlocks > 32) return NS_ERROR_ILLEGAL_VALUE;
330 439 : PRUint32 mask = ((0x01 << numBlocks) - 1) << startBit;
331 :
332 : // check if all specified blocks are currently allocated
333 439 : if ((mBitMap[startWord] & mask) != mask) return NS_ERROR_FAILURE;
334 :
335 439 : return NS_OK;
336 : }
337 :
338 :
339 : /******************************************************************************
340 : * CalcBlockFileSize
341 : *
342 : * Return size of the block file according to the bits set in mBitmap
343 : *
344 : *****************************************************************************/
345 : PRUint32
346 0 : nsDiskCacheBlockFile::CalcBlockFileSize()
347 : {
348 : // search for last byte in mBitMap with allocated bits
349 0 : PRUint32 estimatedSize = mBitMapWords * 4;
350 0 : PRInt32 i = mBitMapWords;
351 0 : while (--i >= 0) {
352 0 : if (mBitMap[i]) break;
353 : }
354 :
355 0 : if (i >= 0) {
356 : // binary search to find last allocated bit in byte
357 0 : PRUint32 mapWord = mBitMap[i];
358 0 : PRUint32 lastBit = 31;
359 0 : if ((mapWord & 0xFFFF0000) == 0) { lastBit ^= 16; mapWord <<= 16; }
360 0 : if ((mapWord & 0xFF000000) == 0) { lastBit ^= 8; mapWord <<= 8; }
361 0 : if ((mapWord & 0xF0000000) == 0) { lastBit ^= 4; mapWord <<= 4; }
362 0 : if ((mapWord & 0xC0000000) == 0) { lastBit ^= 2; mapWord <<= 2; }
363 0 : if ((mapWord & 0x80000000) == 0) { lastBit ^= 1; mapWord <<= 1; }
364 0 : estimatedSize += (i * 32 + lastBit + 1) * mBlockSize;
365 : }
366 :
367 0 : return estimatedSize;
368 : }
369 :
370 : /******************************************************************************
371 : * Write
372 : *
373 : * Wrapper around PR_Write that grows file in larger chunks to combat fragmentation
374 : *
375 : *****************************************************************************/
376 : bool
377 2039 : nsDiskCacheBlockFile::Write(PRInt32 offset, const void *buf, PRInt32 amount)
378 : {
379 : /* Grow the file to 4mb right away, then double it until the file grows to 20mb.
380 : 20mb is a magic threshold because OSX stops autodefragging files bigger than that.
381 : Beyond 20mb grow in 4mb chunks.
382 : */
383 2039 : const PRInt32 upTo = offset + amount;
384 : // Use a conservative definition of 20MB
385 2039 : const PRInt32 minPreallocate = 4*1024*1024;
386 2039 : const PRInt32 maxPreallocate = 20*1000*1000;
387 2039 : if (mFileSize < upTo) {
388 : // maximal file size
389 708 : const PRInt32 maxFileSize = mBitMapWords * 4 * (mBlockSize * 8 + 1);
390 708 : if (upTo > maxPreallocate) {
391 : // grow the file as a multiple of minPreallocate
392 0 : mFileSize = ((upTo + minPreallocate - 1) / minPreallocate) * minPreallocate;
393 : } else {
394 : // Grow quickly between 1MB to 20MB
395 708 : if (mFileSize)
396 0 : while(mFileSize < upTo)
397 0 : mFileSize *= 2;
398 708 : mFileSize = clamped(mFileSize, minPreallocate, maxPreallocate);
399 : }
400 708 : mFileSize = NS_MIN(mFileSize, maxFileSize);
401 : // Appears to cause bug 617123? Disabled for now.
402 : //mozilla::fallocate(mFD, mFileSize);
403 : }
404 2039 : if (PR_Seek(mFD, offset, PR_SEEK_SET) != offset)
405 0 : return false;
406 2039 : return PR_Write(mFD, buf, amount) == amount;
407 : }
|