nsDiskCacheBlockFile.cpp 14 KB

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