netwerk/cache/nsDiskCacheBlockFile.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial