diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llvfs/llvfs.cpp | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to '')
-rw-r--r-- | linden/indra/llvfs/llvfs.cpp | 2071 |
1 files changed, 2071 insertions, 0 deletions
diff --git a/linden/indra/llvfs/llvfs.cpp b/linden/indra/llvfs/llvfs.cpp new file mode 100644 index 0000000..ed1c313 --- /dev/null +++ b/linden/indra/llvfs/llvfs.cpp | |||
@@ -0,0 +1,2071 @@ | |||
1 | /** | ||
2 | * @file llvfs.cpp | ||
3 | * @brief Implementation of virtual file system | ||
4 | * | ||
5 | * Copyright (c) 2002-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #include "linden_common.h" | ||
29 | |||
30 | #include <stdio.h> | ||
31 | #include <sys/stat.h> | ||
32 | #include <time.h> | ||
33 | #include <set> | ||
34 | #include <map> | ||
35 | #if LL_WINDOWS | ||
36 | #include <share.h> | ||
37 | #else | ||
38 | #include <sys/file.h> | ||
39 | #endif | ||
40 | |||
41 | #include "llvfs.h" | ||
42 | #include "llstl.h" | ||
43 | |||
44 | const S32 FILE_BLOCK_MASK = 0x000003FF; // 1024-byte blocks | ||
45 | const S32 VFS_CLEANUP_SIZE = 5242880; // how much space we free up in a single stroke | ||
46 | const S32 BLOCK_LENGTH_INVALID = -1; // mLength for invalid LLVFSFileBlocks | ||
47 | |||
48 | LLVFS *gVFS = NULL; | ||
49 | |||
50 | // internal class definitions | ||
51 | class LLVFSBlock | ||
52 | { | ||
53 | public: | ||
54 | LLVFSBlock() | ||
55 | { | ||
56 | mLocation = 0; | ||
57 | mLength = 0; | ||
58 | } | ||
59 | |||
60 | LLVFSBlock(U32 loc, S32 size) | ||
61 | { | ||
62 | mLocation = loc; | ||
63 | mLength = size; | ||
64 | } | ||
65 | |||
66 | static BOOL insertFirstLL(LLVFSBlock *first, LLVFSBlock *second) | ||
67 | { | ||
68 | return first->mLocation != second->mLocation | ||
69 | ? first->mLocation < second->mLocation | ||
70 | : first->mLength < second->mLength; | ||
71 | |||
72 | } | ||
73 | |||
74 | public: | ||
75 | U32 mLocation; | ||
76 | S32 mLength; // allocated block size | ||
77 | }; | ||
78 | |||
79 | LLVFSFileSpecifier::LLVFSFileSpecifier() | ||
80 | : mFileID(), | ||
81 | mFileType( LLAssetType::AT_NONE ) | ||
82 | { | ||
83 | } | ||
84 | |||
85 | LLVFSFileSpecifier::LLVFSFileSpecifier(const LLUUID &file_id, const LLAssetType::EType file_type) | ||
86 | { | ||
87 | mFileID = file_id; | ||
88 | mFileType = file_type; | ||
89 | } | ||
90 | |||
91 | bool LLVFSFileSpecifier::operator<(const LLVFSFileSpecifier &rhs) const | ||
92 | { | ||
93 | return (mFileID == rhs.mFileID) | ||
94 | ? mFileType < rhs.mFileType | ||
95 | : mFileID < rhs.mFileID; | ||
96 | } | ||
97 | |||
98 | bool LLVFSFileSpecifier::operator==(const LLVFSFileSpecifier &rhs) const | ||
99 | { | ||
100 | return (mFileID == rhs.mFileID && | ||
101 | mFileType == rhs.mFileType); | ||
102 | } | ||
103 | |||
104 | |||
105 | class LLVFSFileBlock : public LLVFSBlock, public LLVFSFileSpecifier | ||
106 | { | ||
107 | public: | ||
108 | LLVFSFileBlock() : LLVFSBlock(), LLVFSFileSpecifier() | ||
109 | { | ||
110 | init(); | ||
111 | } | ||
112 | |||
113 | LLVFSFileBlock(const LLUUID &file_id, LLAssetType::EType file_type, U32 loc = 0, S32 size = 0) | ||
114 | : LLVFSBlock(loc, size), LLVFSFileSpecifier( file_id, file_type ) | ||
115 | { | ||
116 | init(); | ||
117 | } | ||
118 | |||
119 | void init() | ||
120 | { | ||
121 | mSize = 0; | ||
122 | mIndexLocation = -1; | ||
123 | mAccessTime = (U32)time(NULL); | ||
124 | |||
125 | for (S32 i = 0; i < (S32)VFSLOCK_COUNT; i++) | ||
126 | { | ||
127 | mLocks[(EVFSLock)i] = 0; | ||
128 | } | ||
129 | } | ||
130 | |||
131 | #ifdef LL_LITTLE_ENDIAN | ||
132 | inline void swizzleCopy(void *dst, void *src, int size) { memcpy(dst, src, size); } | ||
133 | |||
134 | #else | ||
135 | |||
136 | inline U32 swizzle32(U32 x) | ||
137 | { | ||
138 | return(((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) | ((x << 8) & 0x00FF0000) |((x << 24) & 0xFF000000)); | ||
139 | } | ||
140 | |||
141 | inline U16 swizzle16(U16 x) | ||
142 | { | ||
143 | return( ((x >> 8) & 0x000000FF) | ((x << 8) & 0x0000FF00) ); | ||
144 | } | ||
145 | |||
146 | inline void swizzleCopy(void *dst, void *src, int size) | ||
147 | { | ||
148 | if(size == 4) | ||
149 | { | ||
150 | ((U32*)dst)[0] = swizzle32(((U32*)src)[0]); | ||
151 | } | ||
152 | else if(size == 2) | ||
153 | { | ||
154 | ((U16*)dst)[0] = swizzle16(((U16*)src)[0]); | ||
155 | } | ||
156 | else | ||
157 | { | ||
158 | // Perhaps this should assert... | ||
159 | memcpy(dst, src, size); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | #endif | ||
164 | |||
165 | void serialize(U8 *buffer) | ||
166 | { | ||
167 | swizzleCopy(buffer, &mLocation, 4); | ||
168 | buffer += 4; | ||
169 | swizzleCopy(buffer, &mLength, 4); | ||
170 | buffer +=4; | ||
171 | swizzleCopy(buffer, &mAccessTime, 4); | ||
172 | buffer +=4; | ||
173 | memcpy(buffer, &mFileID.mData, 16); | ||
174 | buffer += 16; | ||
175 | S16 temp_type = mFileType; | ||
176 | swizzleCopy(buffer, &temp_type, 2); | ||
177 | buffer += 2; | ||
178 | swizzleCopy(buffer, &mSize, 4); | ||
179 | } | ||
180 | |||
181 | void deserialize(U8 *buffer, const S32 index_loc) | ||
182 | { | ||
183 | mIndexLocation = index_loc; | ||
184 | |||
185 | swizzleCopy(&mLocation, buffer, 4); | ||
186 | buffer += 4; | ||
187 | swizzleCopy(&mLength, buffer, 4); | ||
188 | buffer += 4; | ||
189 | swizzleCopy(&mAccessTime, buffer, 4); | ||
190 | buffer += 4; | ||
191 | memcpy(&mFileID.mData, buffer, 16); | ||
192 | buffer += 16; | ||
193 | S16 temp_type; | ||
194 | swizzleCopy(&temp_type, buffer, 2); | ||
195 | mFileType = (LLAssetType::EType)temp_type; | ||
196 | buffer += 2; | ||
197 | swizzleCopy(&mSize, buffer, 4); | ||
198 | } | ||
199 | |||
200 | static BOOL insertLRU(LLVFSFileBlock* const& first, | ||
201 | LLVFSFileBlock* const& second) | ||
202 | { | ||
203 | return (first->mAccessTime == second->mAccessTime) | ||
204 | ? *first < *second | ||
205 | : first->mAccessTime < second->mAccessTime; | ||
206 | } | ||
207 | |||
208 | public: | ||
209 | S32 mSize; | ||
210 | S32 mIndexLocation; // location of index entry | ||
211 | U32 mAccessTime; | ||
212 | BOOL mLocks[VFSLOCK_COUNT]; // number of outstanding locks of each type | ||
213 | |||
214 | static const S32 SERIAL_SIZE; | ||
215 | }; | ||
216 | |||
217 | // Helper structure for doing lru w/ stl... is there a simpler way? | ||
218 | struct LLVFSFileBlock_less | ||
219 | { | ||
220 | bool operator()(LLVFSFileBlock* const& lhs, LLVFSFileBlock* const& rhs) const | ||
221 | { | ||
222 | return (LLVFSFileBlock::insertLRU(lhs, rhs)) ? true : false; | ||
223 | } | ||
224 | }; | ||
225 | |||
226 | |||
227 | const S32 LLVFSFileBlock::SERIAL_SIZE = 34; | ||
228 | |||
229 | |||
230 | LLVFS::LLVFS(const char *index_filename, const char *data_filename, const BOOL read_only, const U32 presize, const BOOL remove_after_crash) | ||
231 | : mRemoveAfterCrash(remove_after_crash) | ||
232 | { | ||
233 | mDataMutex = new LLMutex(0); | ||
234 | |||
235 | S32 i; | ||
236 | for (i = 0; i < VFSLOCK_COUNT; i++) | ||
237 | { | ||
238 | mLockCounts[i] = 0; | ||
239 | } | ||
240 | mValid = VFSVALID_OK; | ||
241 | mReadOnly = read_only; | ||
242 | mIndexFilename = new char[strlen(index_filename) + 1]; | ||
243 | mDataFilename = new char[strlen(data_filename) + 1]; | ||
244 | strcpy(mIndexFilename, index_filename); | ||
245 | strcpy(mDataFilename, data_filename); | ||
246 | |||
247 | const char *file_mode = mReadOnly ? "rb" : "r+b"; | ||
248 | |||
249 | if (! (mDataFP = openAndLock(mDataFilename, file_mode, mReadOnly))) | ||
250 | { | ||
251 | |||
252 | if (mReadOnly) | ||
253 | { | ||
254 | llwarns << "Can't find " << mDataFilename << " to open read-only VFS" << llendl; | ||
255 | mValid = VFSVALID_BAD_CANNOT_OPEN_READONLY; | ||
256 | return; | ||
257 | } | ||
258 | |||
259 | if((mDataFP = openAndLock(mDataFilename, "w+b", FALSE))) | ||
260 | { | ||
261 | // Since we're creating this data file, assume any index file is bogus | ||
262 | // remove the index, since this vfs is now blank | ||
263 | LLFile::remove(mIndexFilename); | ||
264 | } | ||
265 | else | ||
266 | { | ||
267 | llwarns << "Can't open VFS data file " << mDataFilename << " attempting to use alternate" << llendl; | ||
268 | |||
269 | char *temp_index = new char[strlen(mIndexFilename) + 10]; | ||
270 | char *temp_data = new char[strlen(mDataFilename) + 10]; | ||
271 | |||
272 | for (U32 count = 0; count < 256; count++) | ||
273 | { | ||
274 | sprintf(temp_index, "%s.%u", mIndexFilename, count); | ||
275 | sprintf(temp_data, "%s.%u", mDataFilename, count); | ||
276 | |||
277 | // try just opening, then creating, each alternate | ||
278 | if ((mDataFP = openAndLock(temp_data, "r+b", FALSE))) | ||
279 | { | ||
280 | break; | ||
281 | } | ||
282 | |||
283 | if ((mDataFP = openAndLock(temp_data, "w+b", FALSE))) | ||
284 | { | ||
285 | // we're creating the datafile, so nuke the indexfile | ||
286 | LLFile::remove(temp_index); | ||
287 | break; | ||
288 | } | ||
289 | } | ||
290 | |||
291 | if (! mDataFP) | ||
292 | { | ||
293 | llwarns << "Couldn't open vfs data file after trying many alternates" << llendl; | ||
294 | mValid = VFSVALID_BAD_CANNOT_CREATE; | ||
295 | return; | ||
296 | } | ||
297 | |||
298 | delete[] mIndexFilename; | ||
299 | delete[] mDataFilename; | ||
300 | |||
301 | mIndexFilename = temp_index; | ||
302 | mDataFilename = temp_data; | ||
303 | } | ||
304 | |||
305 | if (presize) | ||
306 | { | ||
307 | presizeDataFile(presize); | ||
308 | } | ||
309 | } | ||
310 | |||
311 | // Did we leave this file open for writing last time? | ||
312 | // If so, close it and start over. | ||
313 | if (!mReadOnly && mRemoveAfterCrash) | ||
314 | { | ||
315 | llstat marker_info; | ||
316 | char* marker = new char[strlen(mDataFilename) + strlen(".open") + 1]; | ||
317 | sprintf(marker, "%s.open", mDataFilename); | ||
318 | if (!LLFile::stat(marker, &marker_info)) | ||
319 | { | ||
320 | // marker exists, kill the lock and the VFS files | ||
321 | unlockAndClose(mDataFP); | ||
322 | mDataFP = NULL; | ||
323 | |||
324 | llwarns << "VFS: File left open on last run, removing old VFS file " << mDataFilename << llendl; | ||
325 | LLFile::remove(mIndexFilename); | ||
326 | LLFile::remove(mDataFilename); | ||
327 | LLFile::remove(marker); | ||
328 | |||
329 | mDataFP = openAndLock(mDataFilename, "w+b", FALSE); | ||
330 | if (!mDataFP) | ||
331 | { | ||
332 | llwarns << "Can't open VFS data file in crash recovery" << llendl; | ||
333 | mValid = VFSVALID_BAD_CANNOT_CREATE; | ||
334 | return; | ||
335 | } | ||
336 | |||
337 | if (presize) | ||
338 | { | ||
339 | presizeDataFile(presize); | ||
340 | } | ||
341 | } | ||
342 | delete [] marker; | ||
343 | marker = NULL; | ||
344 | } | ||
345 | |||
346 | // determine the real file size | ||
347 | fseek(mDataFP, 0, SEEK_END); | ||
348 | U32 data_size = ftell(mDataFP); | ||
349 | |||
350 | // read the index file | ||
351 | // make sure there's at least one file in it too | ||
352 | // if not, we'll treat this as a new vfs | ||
353 | llstat fbuf; | ||
354 | if (! LLFile::stat(mIndexFilename, &fbuf) && | ||
355 | fbuf.st_size >= LLVFSFileBlock::SERIAL_SIZE && | ||
356 | (mIndexFP = openAndLock(mIndexFilename, file_mode, mReadOnly)) | ||
357 | ) | ||
358 | { | ||
359 | U8 *buffer = new U8[fbuf.st_size]; | ||
360 | fread(buffer, fbuf.st_size, 1, mIndexFP); | ||
361 | |||
362 | U8 *tmp_ptr = buffer; | ||
363 | |||
364 | LLLinkedList<LLVFSBlock> files_by_loc; | ||
365 | files_by_loc.setInsertBefore(LLVFSBlock::insertFirstLL); | ||
366 | |||
367 | while (tmp_ptr < buffer + fbuf.st_size) | ||
368 | { | ||
369 | LLVFSFileBlock *block = new LLVFSFileBlock(); | ||
370 | |||
371 | block->deserialize(tmp_ptr, (S32)(tmp_ptr - buffer)); | ||
372 | |||
373 | // Do sanity check on this block. | ||
374 | // Note that this skips zero size blocks, which helps VFS | ||
375 | // to heal after some errors. JC | ||
376 | if (block->mLength > 0 && | ||
377 | (U32)block->mLength <= data_size && | ||
378 | block->mLocation >= 0 && | ||
379 | block->mLocation < data_size && | ||
380 | block->mSize > 0 && | ||
381 | block->mSize <= block->mLength && | ||
382 | block->mFileType >= LLAssetType::AT_NONE && | ||
383 | block->mFileType < LLAssetType::AT_COUNT) | ||
384 | { | ||
385 | mFileBlocks.insert(fileblock_map::value_type(*block, block)); | ||
386 | files_by_loc.addDataSorted(block); | ||
387 | } | ||
388 | else | ||
389 | if (block->mLength && block->mSize > 0) | ||
390 | { | ||
391 | // this is corrupt, not empty | ||
392 | llwarns << "VFS corruption: " << block->mFileID << " (" << block->mFileType << ") at index " << block->mIndexLocation << " DS: " << data_size << llendl; | ||
393 | llwarns << "Length: " << block->mLength << "\tLocation: " << block->mLocation << "\tSize: " << block->mSize << llendl; | ||
394 | llwarns << "File has bad data - VFS removed" << llendl; | ||
395 | |||
396 | delete[] buffer; | ||
397 | delete block; | ||
398 | |||
399 | unlockAndClose( mIndexFP ); | ||
400 | mIndexFP = NULL; | ||
401 | LLFile::remove( mIndexFilename ); | ||
402 | |||
403 | unlockAndClose( mDataFP ); | ||
404 | mDataFP = NULL; | ||
405 | LLFile::remove( mDataFilename ); | ||
406 | |||
407 | mValid = VFSVALID_BAD_CORRUPT; | ||
408 | return; | ||
409 | } | ||
410 | else | ||
411 | { | ||
412 | // this is a null or bad entry, skip it | ||
413 | S32 index_loc = (S32)(tmp_ptr - buffer); | ||
414 | mIndexHoles.push_back(index_loc); | ||
415 | |||
416 | delete block; | ||
417 | } | ||
418 | |||
419 | tmp_ptr += block->SERIAL_SIZE; | ||
420 | } | ||
421 | delete[] buffer; | ||
422 | |||
423 | // discover all the free blocks | ||
424 | LLVFSFileBlock *last_file_block = (LLVFSFileBlock*)files_by_loc.getFirstData(); | ||
425 | |||
426 | if (last_file_block) | ||
427 | { | ||
428 | // check for empty space at the beginning | ||
429 | if (last_file_block->mLocation > 0) | ||
430 | { | ||
431 | LLVFSBlock *block = new LLVFSBlock(0, last_file_block->mLocation); | ||
432 | addFreeBlock(block); | ||
433 | } | ||
434 | |||
435 | LLVFSFileBlock *cur_file_block; | ||
436 | while ((cur_file_block = (LLVFSFileBlock*)files_by_loc.getNextData())) | ||
437 | { | ||
438 | if (cur_file_block->mLocation == last_file_block->mLocation | ||
439 | && cur_file_block->mLength == last_file_block->mLength) | ||
440 | { | ||
441 | llwarns << "VFS: removing duplicate entry" | ||
442 | << " at " << cur_file_block->mLocation | ||
443 | << " length " << cur_file_block->mLength | ||
444 | << " size " << cur_file_block->mSize | ||
445 | << " ID " << cur_file_block->mFileID | ||
446 | << " type " << cur_file_block->mFileType | ||
447 | << llendl; | ||
448 | |||
449 | // Duplicate entries. Nuke them both for safety. | ||
450 | mFileBlocks.erase(*cur_file_block); // remove ID/type entry | ||
451 | if (cur_file_block->mLength > 0) | ||
452 | { | ||
453 | // convert to hole | ||
454 | LLVFSBlock* block = new LLVFSBlock(cur_file_block->mLocation, | ||
455 | cur_file_block->mLength); | ||
456 | addFreeBlock(block); | ||
457 | } | ||
458 | lockData(); // needed for sync() | ||
459 | sync(cur_file_block, TRUE); // remove first on disk | ||
460 | sync(last_file_block, TRUE); // remove last on disk | ||
461 | unlockData(); // needed for sync() | ||
462 | last_file_block = cur_file_block; | ||
463 | continue; | ||
464 | } | ||
465 | |||
466 | U32 loc = last_file_block->mLocation + last_file_block->mLength; | ||
467 | S32 length = cur_file_block->mLocation - loc; | ||
468 | |||
469 | if (length < 0 || loc < 0 || loc > data_size) | ||
470 | { | ||
471 | // Invalid VFS | ||
472 | unlockAndClose( mIndexFP ); | ||
473 | mIndexFP = NULL; | ||
474 | LLFile::remove( mIndexFilename ); | ||
475 | |||
476 | unlockAndClose( mDataFP ); | ||
477 | mDataFP = NULL; | ||
478 | LLFile::remove( mDataFilename ); | ||
479 | |||
480 | llwarns << "VFS: overlapping entries" | ||
481 | << " at " << cur_file_block->mLocation | ||
482 | << " length " << cur_file_block->mLength | ||
483 | << " ID " << cur_file_block->mFileID | ||
484 | << " type " << cur_file_block->mFileType | ||
485 | << llendl; | ||
486 | mValid = VFSVALID_BAD_CORRUPT; | ||
487 | return; | ||
488 | } | ||
489 | |||
490 | if (length > 0) | ||
491 | { | ||
492 | LLVFSBlock *block = new LLVFSBlock(loc, length); | ||
493 | addFreeBlock(block); | ||
494 | } | ||
495 | |||
496 | last_file_block = cur_file_block; | ||
497 | } | ||
498 | |||
499 | // also note any empty space at the end | ||
500 | U32 loc = last_file_block->mLocation + last_file_block->mLength; | ||
501 | if (loc < data_size) | ||
502 | { | ||
503 | LLVFSBlock *block = new LLVFSBlock(loc, data_size - loc); | ||
504 | addFreeBlock(block); | ||
505 | } | ||
506 | } | ||
507 | else | ||
508 | { | ||
509 | LLVFSBlock *first_block = new LLVFSBlock(0, data_size); | ||
510 | addFreeBlock(first_block); | ||
511 | } | ||
512 | } | ||
513 | else | ||
514 | { | ||
515 | if (mReadOnly) | ||
516 | { | ||
517 | llwarns << "Can't find " << mIndexFilename << " to open read-only VFS" << llendl; | ||
518 | mValid = VFSVALID_BAD_CANNOT_OPEN_READONLY; | ||
519 | return; | ||
520 | } | ||
521 | |||
522 | |||
523 | mIndexFP = openAndLock(mIndexFilename, "w+b", FALSE); | ||
524 | if (!mIndexFP) | ||
525 | { | ||
526 | llwarns << "Couldn't open an index file for the VFS, probably a sharing violation!" << llendl; | ||
527 | |||
528 | unlockAndClose( mDataFP ); | ||
529 | mDataFP = NULL; | ||
530 | LLFile::remove( mDataFilename ); | ||
531 | |||
532 | mValid = VFSVALID_BAD_CANNOT_CREATE; | ||
533 | return; | ||
534 | } | ||
535 | |||
536 | // no index file, start from scratch w/ 1GB allocation | ||
537 | LLVFSBlock *first_block = new LLVFSBlock(0, data_size ? data_size : 0x40000000); | ||
538 | addFreeBlock(first_block); | ||
539 | } | ||
540 | |||
541 | // Open marker file to look for bad shutdowns | ||
542 | if (!mReadOnly && mRemoveAfterCrash) | ||
543 | { | ||
544 | char* marker = new char[strlen(mDataFilename) + strlen(".open") + 1]; | ||
545 | sprintf(marker, "%s.open", mDataFilename); | ||
546 | FILE* marker_fp = LLFile::fopen(marker, "w"); | ||
547 | if (marker_fp) | ||
548 | { | ||
549 | fclose(marker_fp); | ||
550 | marker_fp = NULL; | ||
551 | } | ||
552 | delete [] marker; | ||
553 | marker = NULL; | ||
554 | } | ||
555 | |||
556 | llinfos << "VFS: Using index file " << mIndexFilename << " and data file " << mDataFilename << llendl; | ||
557 | |||
558 | mValid = VFSVALID_OK; | ||
559 | } | ||
560 | |||
561 | LLVFS::~LLVFS() | ||
562 | { | ||
563 | if (mDataMutex->isLocked()) | ||
564 | { | ||
565 | llerrs << "LLVFS destroyed with mutex locked" << llendl; | ||
566 | } | ||
567 | |||
568 | unlockAndClose(mIndexFP); | ||
569 | mIndexFP = NULL; | ||
570 | |||
571 | fileblock_map::const_iterator it; | ||
572 | for (it = mFileBlocks.begin(); it != mFileBlocks.end(); ++it) | ||
573 | { | ||
574 | delete (*it).second; | ||
575 | } | ||
576 | mFileBlocks.clear(); | ||
577 | |||
578 | mFreeBlocksByLength.clear(); | ||
579 | |||
580 | for_each(mFreeBlocksByLocation.begin(), mFreeBlocksByLocation.end(), DeletePairedPointer()); | ||
581 | |||
582 | unlockAndClose(mDataFP); | ||
583 | mDataFP = NULL; | ||
584 | |||
585 | // Remove marker file | ||
586 | if (!mReadOnly && mRemoveAfterCrash) | ||
587 | { | ||
588 | char* marker_file = new char[strlen(mDataFilename) + strlen(".open") + 1]; | ||
589 | sprintf(marker_file, "%s.open", mDataFilename); | ||
590 | LLFile::remove(marker_file); | ||
591 | delete [] marker_file; | ||
592 | marker_file = NULL; | ||
593 | } | ||
594 | |||
595 | delete[] mIndexFilename; | ||
596 | mIndexFilename = NULL; | ||
597 | delete[] mDataFilename; | ||
598 | mDataFilename = NULL; | ||
599 | |||
600 | delete mDataMutex; | ||
601 | } | ||
602 | |||
603 | void LLVFS::presizeDataFile(const U32 size) | ||
604 | { | ||
605 | if (!mDataFP) | ||
606 | { | ||
607 | llerrs << "LLVFS::presizeDataFile() with no data file open" << llendl; | ||
608 | } | ||
609 | |||
610 | // we're creating this file for the first time, size it | ||
611 | fseek(mDataFP, size-1, SEEK_SET); | ||
612 | S32 tmp = 0; | ||
613 | tmp = (S32)fwrite(&tmp, 1, 1, mDataFP); | ||
614 | // fflush(mDataFP); | ||
615 | |||
616 | // also remove any index, since this vfs is now blank | ||
617 | LLFile::remove(mIndexFilename); | ||
618 | |||
619 | if (tmp) | ||
620 | { | ||
621 | llinfos << "Pre-sized VFS data file to " << ftell(mDataFP) << " bytes" << llendl; | ||
622 | } | ||
623 | else | ||
624 | { | ||
625 | llwarns << "Failed to pre-size VFS data file" << llendl; | ||
626 | } | ||
627 | } | ||
628 | |||
629 | BOOL LLVFS::getExists(const LLUUID &file_id, const LLAssetType::EType file_type) | ||
630 | { | ||
631 | LLVFSFileBlock *block = NULL; | ||
632 | |||
633 | if (!isValid()) | ||
634 | { | ||
635 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
636 | } | ||
637 | |||
638 | lockData(); | ||
639 | |||
640 | LLVFSFileSpecifier spec(file_id, file_type); | ||
641 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
642 | if (it != mFileBlocks.end()) | ||
643 | { | ||
644 | block = (*it).second; | ||
645 | block->mAccessTime = (U32)time(NULL); | ||
646 | } | ||
647 | |||
648 | BOOL res = (block && block->mLength > 0) ? TRUE : FALSE; | ||
649 | |||
650 | unlockData(); | ||
651 | |||
652 | return res; | ||
653 | } | ||
654 | |||
655 | S32 LLVFS::getSize(const LLUUID &file_id, const LLAssetType::EType file_type) | ||
656 | { | ||
657 | S32 size = 0; | ||
658 | |||
659 | if (!isValid()) | ||
660 | { | ||
661 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
662 | |||
663 | } | ||
664 | |||
665 | lockData(); | ||
666 | |||
667 | LLVFSFileSpecifier spec(file_id, file_type); | ||
668 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
669 | if (it != mFileBlocks.end()) | ||
670 | { | ||
671 | LLVFSFileBlock *block = (*it).second; | ||
672 | |||
673 | block->mAccessTime = (U32)time(NULL); | ||
674 | size = block->mSize; | ||
675 | } | ||
676 | |||
677 | unlockData(); | ||
678 | |||
679 | return size; | ||
680 | } | ||
681 | |||
682 | S32 LLVFS::getMaxSize(const LLUUID &file_id, const LLAssetType::EType file_type) | ||
683 | { | ||
684 | S32 size = 0; | ||
685 | |||
686 | if (!isValid()) | ||
687 | { | ||
688 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
689 | } | ||
690 | |||
691 | lockData(); | ||
692 | |||
693 | LLVFSFileSpecifier spec(file_id, file_type); | ||
694 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
695 | if (it != mFileBlocks.end()) | ||
696 | { | ||
697 | LLVFSFileBlock *block = (*it).second; | ||
698 | |||
699 | block->mAccessTime = (U32)time(NULL); | ||
700 | size = block->mLength; | ||
701 | } | ||
702 | |||
703 | unlockData(); | ||
704 | |||
705 | return size; | ||
706 | } | ||
707 | |||
708 | BOOL LLVFS::checkAvailable(S32 max_size) | ||
709 | { | ||
710 | blocks_length_map_t::iterator iter = mFreeBlocksByLength.lower_bound(max_size); // first entry >= size | ||
711 | return (iter == mFreeBlocksByLength.end()) ? FALSE : TRUE; | ||
712 | } | ||
713 | |||
714 | BOOL LLVFS::setMaxSize(const LLUUID &file_id, const LLAssetType::EType file_type, S32 max_size) | ||
715 | { | ||
716 | if (!isValid()) | ||
717 | { | ||
718 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
719 | } | ||
720 | if (mReadOnly) | ||
721 | { | ||
722 | llerrs << "Attempt to write to read-only VFS" << llendl; | ||
723 | } | ||
724 | if (max_size <= 0) | ||
725 | { | ||
726 | llwarns << "VFS: Attempt to assign size " << max_size << " to vfile " << file_id << llendl; | ||
727 | return FALSE; | ||
728 | } | ||
729 | |||
730 | lockData(); | ||
731 | |||
732 | LLVFSFileSpecifier spec(file_id, file_type); | ||
733 | LLVFSFileBlock *block = NULL; | ||
734 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
735 | if (it != mFileBlocks.end()) | ||
736 | { | ||
737 | block = (*it).second; | ||
738 | } | ||
739 | |||
740 | // round all sizes upward to KB increments | ||
741 | if (max_size & FILE_BLOCK_MASK) | ||
742 | { | ||
743 | max_size += FILE_BLOCK_MASK; | ||
744 | max_size &= ~FILE_BLOCK_MASK; | ||
745 | } | ||
746 | |||
747 | if (block && block->mLength > 0) | ||
748 | { | ||
749 | block->mAccessTime = (U32)time(NULL); | ||
750 | |||
751 | if (max_size == block->mLength) | ||
752 | { | ||
753 | unlockData(); | ||
754 | return TRUE; | ||
755 | } | ||
756 | else if (max_size < block->mLength) | ||
757 | { | ||
758 | // this file is shrinking | ||
759 | LLVFSBlock *free_block = new LLVFSBlock(block->mLocation + max_size, block->mLength - max_size); | ||
760 | |||
761 | addFreeBlock(free_block); | ||
762 | |||
763 | block->mLength = max_size; | ||
764 | |||
765 | if (block->mLength < block->mSize) | ||
766 | { | ||
767 | // JC: Was a warning, but Ian says it's bad. | ||
768 | llerrs << "Truncating virtual file " << file_id << " to " << block->mLength << " bytes" << llendl; | ||
769 | block->mSize = block->mLength; | ||
770 | } | ||
771 | |||
772 | sync(block); | ||
773 | //mergeFreeBlocks(); | ||
774 | |||
775 | unlockData(); | ||
776 | return TRUE; | ||
777 | } | ||
778 | else if (max_size > block->mLength) | ||
779 | { | ||
780 | // this file is growing | ||
781 | // first check for an adjacent free block to grow into | ||
782 | S32 size_increase = max_size - block->mLength; | ||
783 | |||
784 | // Find the first free block with and addres > block->mLocation | ||
785 | LLVFSBlock *free_block; | ||
786 | blocks_location_map_t::iterator iter = mFreeBlocksByLocation.upper_bound(block->mLocation); | ||
787 | if (iter != mFreeBlocksByLocation.end()) | ||
788 | { | ||
789 | free_block = iter->second; | ||
790 | |||
791 | if (free_block->mLocation == block->mLocation + block->mLength && | ||
792 | free_block->mLength >= size_increase) | ||
793 | { | ||
794 | // this free block is at the end of the file and is large enough | ||
795 | |||
796 | // Must call useFreeSpace before sync(), as sync() | ||
797 | // unlocks data structures. | ||
798 | useFreeSpace(free_block, size_increase); | ||
799 | block->mLength += size_increase; | ||
800 | sync(block); | ||
801 | |||
802 | unlockData(); | ||
803 | return TRUE; | ||
804 | } | ||
805 | } | ||
806 | |||
807 | // no adjecent free block, find one in the list | ||
808 | free_block = findFreeBlock(max_size, block); | ||
809 | |||
810 | if (free_block) | ||
811 | { | ||
812 | if (block->mLength > 0) | ||
813 | { | ||
814 | // create a new free block where this file used to be | ||
815 | LLVFSBlock *new_free_block = new LLVFSBlock(block->mLocation, block->mLength); | ||
816 | |||
817 | addFreeBlock(new_free_block); | ||
818 | |||
819 | if (block->mSize > 0) | ||
820 | { | ||
821 | // move the file into the new block | ||
822 | U8 *buffer = new U8[block->mSize]; | ||
823 | fseek(mDataFP, block->mLocation, SEEK_SET); | ||
824 | fread(buffer, block->mSize, 1, mDataFP); | ||
825 | fseek(mDataFP, free_block->mLocation, SEEK_SET); | ||
826 | fwrite(buffer, block->mSize, 1, mDataFP); | ||
827 | // fflush(mDataFP); | ||
828 | |||
829 | delete[] buffer; | ||
830 | } | ||
831 | } | ||
832 | |||
833 | block->mLocation = free_block->mLocation; | ||
834 | |||
835 | block->mLength = max_size; | ||
836 | |||
837 | // Must call useFreeSpace before sync(), as sync() | ||
838 | // unlocks data structures. | ||
839 | useFreeSpace(free_block, max_size); | ||
840 | |||
841 | sync(block); | ||
842 | |||
843 | unlockData(); | ||
844 | return TRUE; | ||
845 | } | ||
846 | else | ||
847 | { | ||
848 | llwarns << "VFS: No space (" << max_size << ") to resize existing vfile " << file_id << llendl; | ||
849 | //dumpMap(); | ||
850 | unlockData(); | ||
851 | dumpStatistics(); | ||
852 | return FALSE; | ||
853 | } | ||
854 | } | ||
855 | } | ||
856 | else | ||
857 | { | ||
858 | // find a free block in the list | ||
859 | LLVFSBlock *free_block = findFreeBlock(max_size); | ||
860 | |||
861 | if (free_block) | ||
862 | { | ||
863 | if (block) | ||
864 | { | ||
865 | block->mLocation = free_block->mLocation; | ||
866 | block->mLength = max_size; | ||
867 | } | ||
868 | else | ||
869 | { | ||
870 | // this file doesn't exist, create it | ||
871 | block = new LLVFSFileBlock(file_id, file_type, free_block->mLocation, max_size); | ||
872 | mFileBlocks.insert(fileblock_map::value_type(spec, block)); | ||
873 | } | ||
874 | |||
875 | // Must call useFreeSpace before sync(), as sync() | ||
876 | // unlocks data structures. | ||
877 | useFreeSpace(free_block, max_size); | ||
878 | block->mAccessTime = (U32)time(NULL); | ||
879 | |||
880 | sync(block); | ||
881 | } | ||
882 | else | ||
883 | { | ||
884 | llwarns << "VFS: No space (" << max_size << ") for new virtual file " << file_id << llendl; | ||
885 | //dumpMap(); | ||
886 | unlockData(); | ||
887 | dumpStatistics(); | ||
888 | return FALSE; | ||
889 | } | ||
890 | } | ||
891 | unlockData(); | ||
892 | return TRUE; | ||
893 | } | ||
894 | |||
895 | |||
896 | // WARNING: HERE BE DRAGONS! | ||
897 | // rename is the weirdest VFS op, because the file moves but the locks don't! | ||
898 | void LLVFS::renameFile(const LLUUID &file_id, const LLAssetType::EType file_type, | ||
899 | const LLUUID &new_id, const LLAssetType::EType &new_type) | ||
900 | { | ||
901 | if (!isValid()) | ||
902 | { | ||
903 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
904 | } | ||
905 | if (mReadOnly) | ||
906 | { | ||
907 | llerrs << "Attempt to write to read-only VFS" << llendl; | ||
908 | } | ||
909 | |||
910 | lockData(); | ||
911 | |||
912 | LLVFSFileSpecifier new_spec(new_id, new_type); | ||
913 | LLVFSFileSpecifier old_spec(file_id, file_type); | ||
914 | |||
915 | fileblock_map::iterator it = mFileBlocks.find(old_spec); | ||
916 | if (it != mFileBlocks.end()) | ||
917 | { | ||
918 | LLVFSFileBlock *src_block = (*it).second; | ||
919 | |||
920 | // this will purge the data but leave the file block in place, w/ locks, if any | ||
921 | // WAS: removeFile(new_id, new_type); NOW uses removeFileBlock() to avoid mutex lock recursion | ||
922 | fileblock_map::iterator new_it = mFileBlocks.find(new_spec); | ||
923 | if (new_it != mFileBlocks.end()) | ||
924 | { | ||
925 | LLVFSFileBlock *new_block = (*new_it).second; | ||
926 | removeFileBlock(new_block); | ||
927 | } | ||
928 | |||
929 | // if there's something in the target location, remove it but inherit its locks | ||
930 | it = mFileBlocks.find(new_spec); | ||
931 | if (it != mFileBlocks.end()) | ||
932 | { | ||
933 | LLVFSFileBlock *dest_block = (*it).second; | ||
934 | |||
935 | for (S32 i = 0; i < (S32)VFSLOCK_COUNT; i++) | ||
936 | { | ||
937 | if(dest_block->mLocks[i]) | ||
938 | { | ||
939 | llerrs << "Renaming VFS block to a locked file." << llendl; | ||
940 | } | ||
941 | dest_block->mLocks[i] = src_block->mLocks[i]; | ||
942 | } | ||
943 | |||
944 | mFileBlocks.erase(new_spec); | ||
945 | delete dest_block; | ||
946 | } | ||
947 | |||
948 | src_block->mFileID = new_id; | ||
949 | src_block->mFileType = new_type; | ||
950 | src_block->mAccessTime = (U32)time(NULL); | ||
951 | |||
952 | mFileBlocks.erase(old_spec); | ||
953 | mFileBlocks.insert(fileblock_map::value_type(new_spec, src_block)); | ||
954 | |||
955 | sync(src_block); | ||
956 | } | ||
957 | else | ||
958 | { | ||
959 | llwarns << "VFS: Attempt to rename nonexistent vfile " << file_id << ":" << file_type << llendl; | ||
960 | } | ||
961 | unlockData(); | ||
962 | } | ||
963 | |||
964 | // mDataMutex must be LOCKED before calling this | ||
965 | void LLVFS::removeFileBlock(LLVFSFileBlock *fileblock) | ||
966 | { | ||
967 | // convert this into an unsaved, dummy fileblock to preserve locks | ||
968 | // a more rubust solution would store the locks in a seperate data structure | ||
969 | sync(fileblock, TRUE); | ||
970 | |||
971 | if (fileblock->mLength > 0) | ||
972 | { | ||
973 | // turn this file into an empty block | ||
974 | LLVFSBlock *free_block = new LLVFSBlock(fileblock->mLocation, fileblock->mLength); | ||
975 | |||
976 | addFreeBlock(free_block); | ||
977 | } | ||
978 | |||
979 | fileblock->mLocation = 0; | ||
980 | fileblock->mSize = 0; | ||
981 | fileblock->mLength = BLOCK_LENGTH_INVALID; | ||
982 | fileblock->mIndexLocation = -1; | ||
983 | |||
984 | //mergeFreeBlocks(); | ||
985 | } | ||
986 | |||
987 | void LLVFS::removeFile(const LLUUID &file_id, const LLAssetType::EType file_type) | ||
988 | { | ||
989 | if (!isValid()) | ||
990 | { | ||
991 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
992 | } | ||
993 | if (mReadOnly) | ||
994 | { | ||
995 | llerrs << "Attempt to write to read-only VFS" << llendl; | ||
996 | } | ||
997 | |||
998 | lockData(); | ||
999 | |||
1000 | LLVFSFileSpecifier spec(file_id, file_type); | ||
1001 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
1002 | if (it != mFileBlocks.end()) | ||
1003 | { | ||
1004 | LLVFSFileBlock *block = (*it).second; | ||
1005 | removeFileBlock(block); | ||
1006 | } | ||
1007 | else | ||
1008 | { | ||
1009 | llwarns << "VFS: attempting to remove nonexistent file " << file_id << " type " << file_type << llendl; | ||
1010 | } | ||
1011 | |||
1012 | unlockData(); | ||
1013 | } | ||
1014 | |||
1015 | |||
1016 | S32 LLVFS::getData(const LLUUID &file_id, const LLAssetType::EType file_type, U8 *buffer, S32 location, S32 length) | ||
1017 | { | ||
1018 | S32 bytesread = 0; | ||
1019 | |||
1020 | if (!isValid()) | ||
1021 | { | ||
1022 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
1023 | } | ||
1024 | llassert(location >= 0); | ||
1025 | llassert(length >= 0); | ||
1026 | |||
1027 | BOOL do_read = FALSE; | ||
1028 | |||
1029 | lockData(); | ||
1030 | |||
1031 | LLVFSFileSpecifier spec(file_id, file_type); | ||
1032 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
1033 | if (it != mFileBlocks.end()) | ||
1034 | { | ||
1035 | LLVFSFileBlock *block = (*it).second; | ||
1036 | |||
1037 | block->mAccessTime = (U32)time(NULL); | ||
1038 | |||
1039 | if (location > block->mSize) | ||
1040 | { | ||
1041 | llwarns << "VFS: Attempt to read location " << location << " in file " << file_id << " of length " << block->mSize << llendl; | ||
1042 | } | ||
1043 | else | ||
1044 | { | ||
1045 | if (length > block->mSize - location) | ||
1046 | { | ||
1047 | length = block->mSize - location; | ||
1048 | } | ||
1049 | location += block->mLocation; | ||
1050 | do_read = TRUE; | ||
1051 | } | ||
1052 | } | ||
1053 | |||
1054 | unlockData(); | ||
1055 | |||
1056 | if (do_read) | ||
1057 | { | ||
1058 | fseek(mDataFP, location, SEEK_SET); | ||
1059 | bytesread = (S32)fread(buffer, 1, length, mDataFP); | ||
1060 | } | ||
1061 | |||
1062 | return bytesread; | ||
1063 | } | ||
1064 | |||
1065 | S32 LLVFS::storeData(const LLUUID &file_id, const LLAssetType::EType file_type, const U8 *buffer, S32 location, S32 length) | ||
1066 | { | ||
1067 | if (!isValid()) | ||
1068 | { | ||
1069 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
1070 | } | ||
1071 | if (mReadOnly) | ||
1072 | { | ||
1073 | llerrs << "Attempt to write to read-only VFS" << llendl; | ||
1074 | } | ||
1075 | |||
1076 | llassert(length > 0); | ||
1077 | |||
1078 | lockData(); | ||
1079 | |||
1080 | LLVFSFileSpecifier spec(file_id, file_type); | ||
1081 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
1082 | if (it != mFileBlocks.end()) | ||
1083 | { | ||
1084 | LLVFSFileBlock *block = (*it).second; | ||
1085 | |||
1086 | S32 in_loc = location; | ||
1087 | if (location == -1) | ||
1088 | { | ||
1089 | location = block->mSize; | ||
1090 | } | ||
1091 | llassert(location >= 0); | ||
1092 | |||
1093 | block->mAccessTime = (U32)time(NULL); | ||
1094 | |||
1095 | if (block->mLength == BLOCK_LENGTH_INVALID) | ||
1096 | { | ||
1097 | // Block was removed, ignore write | ||
1098 | llwarns << "VFS: Attempt to write to invalid block" | ||
1099 | << " in file " << file_id | ||
1100 | << " location: " << in_loc | ||
1101 | << " bytes: " << length | ||
1102 | << llendl; | ||
1103 | unlockData(); | ||
1104 | return length; | ||
1105 | } | ||
1106 | else if (location > block->mLength) | ||
1107 | { | ||
1108 | llwarns << "VFS: Attempt to write to location " << location | ||
1109 | << " in file " << file_id | ||
1110 | << " type " << S32(file_type) | ||
1111 | << " of size " << block->mSize | ||
1112 | << " block length " << block->mLength | ||
1113 | << llendl; | ||
1114 | unlockData(); | ||
1115 | return length; | ||
1116 | } | ||
1117 | else | ||
1118 | { | ||
1119 | if (length > block->mLength - location ) | ||
1120 | { | ||
1121 | llwarns << "VFS: Truncating write to virtual file " << file_id << " type " << S32(file_type) << llendl; | ||
1122 | length = block->mLength - location; | ||
1123 | } | ||
1124 | U32 file_location = location + block->mLocation; | ||
1125 | |||
1126 | unlockData(); | ||
1127 | |||
1128 | fseek(mDataFP, file_location, SEEK_SET); | ||
1129 | S32 write_len = (S32)fwrite(buffer, 1, length, mDataFP); | ||
1130 | if (write_len != length) | ||
1131 | { | ||
1132 | llwarns << llformat("VFS Write Error: %d != %d",write_len,length) << llendl; | ||
1133 | } | ||
1134 | // fflush(mDataFP); | ||
1135 | |||
1136 | lockData(); | ||
1137 | if (location + length > block->mSize) | ||
1138 | { | ||
1139 | block->mSize = location + write_len; | ||
1140 | sync(block); | ||
1141 | } | ||
1142 | unlockData(); | ||
1143 | |||
1144 | return write_len; | ||
1145 | } | ||
1146 | } | ||
1147 | else | ||
1148 | { | ||
1149 | unlockData(); | ||
1150 | return 0; | ||
1151 | } | ||
1152 | } | ||
1153 | |||
1154 | void LLVFS::incLock(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock) | ||
1155 | { | ||
1156 | lockData(); | ||
1157 | |||
1158 | LLVFSFileSpecifier spec(file_id, file_type); | ||
1159 | LLVFSFileBlock *block; | ||
1160 | |||
1161 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
1162 | if (it != mFileBlocks.end()) | ||
1163 | { | ||
1164 | block = (*it).second; | ||
1165 | } | ||
1166 | else | ||
1167 | { | ||
1168 | // Create a dummy block which isn't saved | ||
1169 | block = new LLVFSFileBlock(file_id, file_type, 0, BLOCK_LENGTH_INVALID); | ||
1170 | block->mAccessTime = (U32)time(NULL); | ||
1171 | mFileBlocks.insert(fileblock_map::value_type(spec, block)); | ||
1172 | } | ||
1173 | |||
1174 | block->mLocks[lock]++; | ||
1175 | mLockCounts[lock]++; | ||
1176 | |||
1177 | unlockData(); | ||
1178 | } | ||
1179 | |||
1180 | void LLVFS::decLock(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock) | ||
1181 | { | ||
1182 | lockData(); | ||
1183 | |||
1184 | LLVFSFileSpecifier spec(file_id, file_type); | ||
1185 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
1186 | if (it != mFileBlocks.end()) | ||
1187 | { | ||
1188 | LLVFSFileBlock *block = (*it).second; | ||
1189 | |||
1190 | if (block->mLocks[lock] > 0) | ||
1191 | { | ||
1192 | block->mLocks[lock]--; | ||
1193 | } | ||
1194 | else | ||
1195 | { | ||
1196 | llwarns << "VFS: Decrementing zero-value lock " << lock << llendl; | ||
1197 | } | ||
1198 | mLockCounts[lock]--; | ||
1199 | } | ||
1200 | |||
1201 | unlockData(); | ||
1202 | } | ||
1203 | |||
1204 | BOOL LLVFS::isLocked(const LLUUID &file_id, const LLAssetType::EType file_type, EVFSLock lock) | ||
1205 | { | ||
1206 | lockData(); | ||
1207 | |||
1208 | BOOL res = FALSE; | ||
1209 | |||
1210 | LLVFSFileSpecifier spec(file_id, file_type); | ||
1211 | fileblock_map::iterator it = mFileBlocks.find(spec); | ||
1212 | if (it != mFileBlocks.end()) | ||
1213 | { | ||
1214 | LLVFSFileBlock *block = (*it).second; | ||
1215 | res = (block->mLocks[lock] > 0); | ||
1216 | } | ||
1217 | |||
1218 | unlockData(); | ||
1219 | |||
1220 | return res; | ||
1221 | } | ||
1222 | |||
1223 | //============================================================================ | ||
1224 | // protected | ||
1225 | //============================================================================ | ||
1226 | |||
1227 | void LLVFS::eraseBlockLength(LLVFSBlock *block) | ||
1228 | { | ||
1229 | // find the corresponding map entry in the length map and erase it | ||
1230 | S32 length = block->mLength; | ||
1231 | blocks_length_map_t::iterator iter = mFreeBlocksByLength.lower_bound(length); | ||
1232 | blocks_length_map_t::iterator end = mFreeBlocksByLength.end(); | ||
1233 | while(iter != end) | ||
1234 | { | ||
1235 | LLVFSBlock *tblock = iter->second; | ||
1236 | llassert(tblock->mLength == length); // there had -better- be an entry with our length! | ||
1237 | if (tblock == block) | ||
1238 | { | ||
1239 | mFreeBlocksByLength.erase(iter); | ||
1240 | break; | ||
1241 | } | ||
1242 | ++iter; | ||
1243 | } | ||
1244 | if (iter == end) | ||
1245 | { | ||
1246 | llerrs << "eraseBlock could not find block" << llendl; | ||
1247 | } | ||
1248 | } | ||
1249 | |||
1250 | |||
1251 | // Remove block from both free lists (by location and by length). | ||
1252 | void LLVFS::eraseBlock(LLVFSBlock *block) | ||
1253 | { | ||
1254 | eraseBlockLength(block); | ||
1255 | // find the corresponding map entry in the location map and erase it | ||
1256 | U32 location = block->mLocation; | ||
1257 | llverify(mFreeBlocksByLocation.erase(location) == 1); // we should only have one entry per location. | ||
1258 | } | ||
1259 | |||
1260 | |||
1261 | // Add the region specified by block location and length to the free lists. | ||
1262 | // Also incrementally defragment by merging with previous and next free blocks. | ||
1263 | void LLVFS::addFreeBlock(LLVFSBlock *block) | ||
1264 | { | ||
1265 | #if LL_DEBUG | ||
1266 | size_t dbgcount = mFreeBlocksByLocation.count(block->mLocation); | ||
1267 | if(dbgcount > 0) | ||
1268 | { | ||
1269 | llerrs << "addFreeBlock called with block already in list" << llendl; | ||
1270 | } | ||
1271 | #endif | ||
1272 | |||
1273 | // Get a pointer to the next free block (by location). | ||
1274 | blocks_location_map_t::iterator next_free_it = mFreeBlocksByLocation.lower_bound(block->mLocation); | ||
1275 | |||
1276 | // We can merge with previous if it ends at our requested location. | ||
1277 | LLVFSBlock* prev_block = NULL; | ||
1278 | bool merge_prev = false; | ||
1279 | if (next_free_it != mFreeBlocksByLocation.begin()) | ||
1280 | { | ||
1281 | blocks_location_map_t::iterator prev_free_it = next_free_it; | ||
1282 | --prev_free_it; | ||
1283 | prev_block = prev_free_it->second; | ||
1284 | merge_prev = (prev_block->mLocation + prev_block->mLength == block->mLocation); | ||
1285 | } | ||
1286 | |||
1287 | // We can merge with next if our block ends at the next block's location. | ||
1288 | LLVFSBlock* next_block = NULL; | ||
1289 | bool merge_next = false; | ||
1290 | if (next_free_it != mFreeBlocksByLocation.end()) | ||
1291 | { | ||
1292 | next_block = next_free_it->second; | ||
1293 | merge_next = (block->mLocation + block->mLength == next_block->mLocation); | ||
1294 | } | ||
1295 | |||
1296 | if (merge_prev && merge_next) | ||
1297 | { | ||
1298 | // llinfos << "VFS merge BOTH" << llendl; | ||
1299 | // Previous block is changing length (a lot), so only need to update length map. | ||
1300 | // Next block is going away completely. JC | ||
1301 | eraseBlockLength(prev_block); | ||
1302 | eraseBlock(next_block); | ||
1303 | prev_block->mLength += block->mLength + next_block->mLength; | ||
1304 | mFreeBlocksByLength.insert(blocks_length_map_t::value_type(prev_block->mLength, prev_block)); | ||
1305 | delete block; | ||
1306 | block = NULL; | ||
1307 | delete next_block; | ||
1308 | next_block = NULL; | ||
1309 | } | ||
1310 | else if (merge_prev) | ||
1311 | { | ||
1312 | // llinfos << "VFS merge previous" << llendl; | ||
1313 | // Previous block is maintaining location, only changing length, | ||
1314 | // therefore only need to update the length map. JC | ||
1315 | eraseBlockLength(prev_block); | ||
1316 | prev_block->mLength += block->mLength; | ||
1317 | mFreeBlocksByLength.insert(blocks_length_map_t::value_type(prev_block->mLength, prev_block)); // multimap insert | ||
1318 | delete block; | ||
1319 | block = NULL; | ||
1320 | } | ||
1321 | else if (merge_next) | ||
1322 | { | ||
1323 | // llinfos << "VFS merge next" << llendl; | ||
1324 | // Next block is changing both location and length, | ||
1325 | // so both free lists must update. JC | ||
1326 | eraseBlock(next_block); | ||
1327 | next_block->mLocation = block->mLocation; | ||
1328 | next_block->mLength += block->mLength; | ||
1329 | // Don't hint here, next_free_it iterator may be invalid. | ||
1330 | mFreeBlocksByLocation.insert(blocks_location_map_t::value_type(next_block->mLocation, next_block)); // multimap insert | ||
1331 | mFreeBlocksByLength.insert(blocks_length_map_t::value_type(next_block->mLength, next_block)); // multimap insert | ||
1332 | delete block; | ||
1333 | block = NULL; | ||
1334 | } | ||
1335 | else | ||
1336 | { | ||
1337 | // Can't merge with other free blocks. | ||
1338 | // Hint that insert should go near next_free_it. | ||
1339 | mFreeBlocksByLocation.insert(next_free_it, blocks_location_map_t::value_type(block->mLocation, block)); // multimap insert | ||
1340 | mFreeBlocksByLength.insert(blocks_length_map_t::value_type(block->mLength, block)); // multimap insert | ||
1341 | } | ||
1342 | } | ||
1343 | |||
1344 | // Superceeded by new addFreeBlock which does incremental free space merging. | ||
1345 | // Incremental is faster overall. | ||
1346 | //void LLVFS::mergeFreeBlocks() | ||
1347 | //{ | ||
1348 | // if (!isValid()) | ||
1349 | // { | ||
1350 | // llerrs << "Attempting to use invalid VFS!" << llendl; | ||
1351 | // } | ||
1352 | // // TODO: could we optimize this with hints from the calling code? | ||
1353 | // blocks_location_map_t::iterator iter = mFreeBlocksByLocation.begin(); | ||
1354 | // blocks_location_map_t::iterator end = mFreeBlocksByLocation.end(); | ||
1355 | // LLVFSBlock *first_block = iter->second; | ||
1356 | // while(iter != end) | ||
1357 | // { | ||
1358 | // blocks_location_map_t::iterator first_iter = iter; // save for if we do a merge | ||
1359 | // if (++iter == end) | ||
1360 | // break; | ||
1361 | // LLVFSBlock *second_block = iter->second; | ||
1362 | // if (first_block->mLocation + first_block->mLength == second_block->mLocation) | ||
1363 | // { | ||
1364 | // // remove the first block from the length map | ||
1365 | // eraseBlockLength(first_block); | ||
1366 | // // merge first_block with second_block, since they're adjacent | ||
1367 | // first_block->mLength += second_block->mLength; | ||
1368 | // // add the first block to the length map (with the new size) | ||
1369 | // mFreeBlocksByLength.insert(blocks_length_map_t::value_type(first_block->mLength, first_block)); // multimap insert | ||
1370 | // | ||
1371 | // // erase and delete the second block | ||
1372 | // eraseBlock(second_block); | ||
1373 | // delete second_block; | ||
1374 | // | ||
1375 | // // reset iterator | ||
1376 | // iter = first_iter; // haven't changed first_block, so corresponding iterator is still valid | ||
1377 | // end = mFreeBlocksByLocation.end(); | ||
1378 | // } | ||
1379 | // first_block = second_block; | ||
1380 | // } | ||
1381 | //} | ||
1382 | |||
1383 | |||
1384 | void LLVFS::useFreeSpace(LLVFSBlock *free_block, S32 length) | ||
1385 | { | ||
1386 | if (free_block->mLength == length) | ||
1387 | { | ||
1388 | eraseBlock(free_block); | ||
1389 | delete free_block; | ||
1390 | } | ||
1391 | else | ||
1392 | { | ||
1393 | eraseBlock(free_block); | ||
1394 | |||
1395 | free_block->mLocation += length; | ||
1396 | free_block->mLength -= length; | ||
1397 | |||
1398 | addFreeBlock(free_block); | ||
1399 | } | ||
1400 | } | ||
1401 | |||
1402 | // NOTE! mDataMutex must be LOCKED before calling this | ||
1403 | // sync this index entry out to the index file | ||
1404 | // we need to do this constantly to avoid corruption on viewer crash | ||
1405 | void LLVFS::sync(LLVFSFileBlock *block, BOOL remove) | ||
1406 | { | ||
1407 | if (!isValid()) | ||
1408 | { | ||
1409 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
1410 | } | ||
1411 | if (mReadOnly) | ||
1412 | { | ||
1413 | llwarns << "Attempt to sync read-only VFS" << llendl; | ||
1414 | return; | ||
1415 | } | ||
1416 | if (block->mLength == BLOCK_LENGTH_INVALID) | ||
1417 | { | ||
1418 | // This is a dummy file, don't save | ||
1419 | return; | ||
1420 | } | ||
1421 | if (block->mLength == 0) | ||
1422 | { | ||
1423 | llerrs << "VFS syncing zero-length block" << llendl; | ||
1424 | } | ||
1425 | |||
1426 | BOOL set_index_to_end = FALSE; | ||
1427 | S32 seek_pos = block->mIndexLocation; | ||
1428 | |||
1429 | if (-1 == seek_pos) | ||
1430 | { | ||
1431 | if (!mIndexHoles.empty()) | ||
1432 | { | ||
1433 | seek_pos = mIndexHoles.front(); | ||
1434 | mIndexHoles.pop_front(); | ||
1435 | } | ||
1436 | else | ||
1437 | { | ||
1438 | set_index_to_end = TRUE; | ||
1439 | } | ||
1440 | } | ||
1441 | |||
1442 | if (set_index_to_end) | ||
1443 | { | ||
1444 | // Need fseek/ftell to update the seek_pos and hence data | ||
1445 | // structures, so can't unlockData() before this. | ||
1446 | fseek(mIndexFP, 0, SEEK_END); | ||
1447 | seek_pos = ftell(mIndexFP); | ||
1448 | } | ||
1449 | |||
1450 | block->mIndexLocation = seek_pos; | ||
1451 | if (remove) | ||
1452 | { | ||
1453 | mIndexHoles.push_back(seek_pos); | ||
1454 | } | ||
1455 | |||
1456 | U8 buffer[LLVFSFileBlock::SERIAL_SIZE]; | ||
1457 | if (remove) | ||
1458 | { | ||
1459 | memset(buffer, 0, LLVFSFileBlock::SERIAL_SIZE); | ||
1460 | } | ||
1461 | else | ||
1462 | { | ||
1463 | block->serialize(buffer); | ||
1464 | } | ||
1465 | |||
1466 | unlockData(); | ||
1467 | |||
1468 | // If set_index_to_end, file pointer is already at seek_pos | ||
1469 | // and we don't need to do anything. Only seek if not at end. | ||
1470 | if (!set_index_to_end) | ||
1471 | { | ||
1472 | fseek(mIndexFP, seek_pos, SEEK_SET); | ||
1473 | } | ||
1474 | |||
1475 | fwrite(buffer, LLVFSFileBlock::SERIAL_SIZE, 1, mIndexFP); | ||
1476 | // fflush(mIndexFP); | ||
1477 | |||
1478 | lockData(); | ||
1479 | |||
1480 | return; | ||
1481 | } | ||
1482 | |||
1483 | // mDataMutex must be LOCKED before calling this | ||
1484 | // Can initiate LRU-based file removal to make space. | ||
1485 | // The immune file block will not be removed. | ||
1486 | LLVFSBlock *LLVFS::findFreeBlock(S32 size, LLVFSFileBlock *immune) | ||
1487 | { | ||
1488 | if (!isValid()) | ||
1489 | { | ||
1490 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
1491 | } | ||
1492 | |||
1493 | LLVFSBlock *block = NULL; | ||
1494 | BOOL have_lru_list = FALSE; | ||
1495 | |||
1496 | typedef std::set<LLVFSFileBlock*, LLVFSFileBlock_less> lru_set; | ||
1497 | lru_set lru_list; | ||
1498 | |||
1499 | LLTimer timer; | ||
1500 | |||
1501 | while (! block) | ||
1502 | { | ||
1503 | // look for a suitable free block | ||
1504 | blocks_length_map_t::iterator iter = mFreeBlocksByLength.lower_bound(size); // first entry >= size | ||
1505 | if (iter != mFreeBlocksByLength.end()) | ||
1506 | block = iter->second; | ||
1507 | |||
1508 | // no large enough free blocks, time to clean out some junk | ||
1509 | if (! block) | ||
1510 | { | ||
1511 | // create a list of files sorted by usage time | ||
1512 | // this is far faster than sorting a linked list | ||
1513 | if (! have_lru_list) | ||
1514 | { | ||
1515 | for (fileblock_map::iterator it = mFileBlocks.begin(); it != mFileBlocks.end(); ++it) | ||
1516 | { | ||
1517 | LLVFSFileBlock *tmp = (*it).second; | ||
1518 | |||
1519 | if (tmp != immune && | ||
1520 | tmp->mLength > 0 && | ||
1521 | ! tmp->mLocks[VFSLOCK_READ] && | ||
1522 | ! tmp->mLocks[VFSLOCK_APPEND] && | ||
1523 | ! tmp->mLocks[VFSLOCK_OPEN]) | ||
1524 | { | ||
1525 | lru_list.insert(tmp); | ||
1526 | } | ||
1527 | } | ||
1528 | |||
1529 | have_lru_list = TRUE; | ||
1530 | } | ||
1531 | |||
1532 | if (lru_list.size() == 0) | ||
1533 | { | ||
1534 | // No more files to delete, and still not enough room! | ||
1535 | llwarns << "VFS: Can't make " << size << " bytes of free space in VFS, giving up" << llendl; | ||
1536 | break; | ||
1537 | } | ||
1538 | |||
1539 | // is the oldest file big enough? (Should be about half the time) | ||
1540 | lru_set::iterator it = lru_list.begin(); | ||
1541 | LLVFSFileBlock *file_block = *it; | ||
1542 | if (file_block->mLength >= size && file_block != immune) | ||
1543 | { | ||
1544 | // ditch this file and look again for a free block - should find it | ||
1545 | // TODO: it'll be faster just to assign the free block and break | ||
1546 | llinfos << "LRU: Removing " << file_block->mFileID << ":" << file_block->mFileType << llendl; | ||
1547 | lru_list.erase(it); | ||
1548 | removeFileBlock(file_block); | ||
1549 | file_block = NULL; | ||
1550 | continue; | ||
1551 | } | ||
1552 | |||
1553 | |||
1554 | llinfos << "VFS: LRU: Aggressive: " << (S32)lru_list.size() << " files remain" << llendl; | ||
1555 | dumpLockCounts(); | ||
1556 | |||
1557 | // Now it's time to aggressively make more space | ||
1558 | // Delete the oldest 5MB of the vfs or enough to hold the file, which ever is larger | ||
1559 | // This may yield too much free space, but we'll use it up soon enough | ||
1560 | U32 cleanup_target = (size > VFS_CLEANUP_SIZE) ? size : VFS_CLEANUP_SIZE; | ||
1561 | U32 cleaned_up = 0; | ||
1562 | for (it = lru_list.begin(); | ||
1563 | it != lru_list.end() && cleaned_up < cleanup_target; | ||
1564 | ) | ||
1565 | { | ||
1566 | file_block = *it; | ||
1567 | |||
1568 | // TODO: it would be great to be able to batch all these sync() calls | ||
1569 | // llinfos << "LRU2: Removing " << file_block->mFileID << ":" << file_block->mFileType << " last accessed" << file_block->mAccessTime << llendl; | ||
1570 | |||
1571 | cleaned_up += file_block->mLength; | ||
1572 | lru_list.erase(it++); | ||
1573 | removeFileBlock(file_block); | ||
1574 | file_block = NULL; | ||
1575 | } | ||
1576 | //mergeFreeBlocks(); | ||
1577 | } | ||
1578 | } | ||
1579 | |||
1580 | F32 time = timer.getElapsedTimeF32(); | ||
1581 | if (time > 0.5f) | ||
1582 | { | ||
1583 | llwarns << "VFS: Spent " << time << " seconds in findFreeBlock!" << llendl; | ||
1584 | } | ||
1585 | |||
1586 | return block; | ||
1587 | } | ||
1588 | |||
1589 | //============================================================================ | ||
1590 | // public | ||
1591 | //============================================================================ | ||
1592 | |||
1593 | void LLVFS::pokeFiles() | ||
1594 | { | ||
1595 | if (!isValid()) | ||
1596 | { | ||
1597 | llerrs << "Attempting to use invalid VFS!" << llendl; | ||
1598 | } | ||
1599 | U32 word; | ||
1600 | |||
1601 | // only write data if we actually read 4 bytes | ||
1602 | // otherwise we're writing garbage and screwing up the file | ||
1603 | fseek(mDataFP, 0, SEEK_SET); | ||
1604 | if (fread(&word, 1, 4, mDataFP) == 4) | ||
1605 | { | ||
1606 | fseek(mDataFP, 0, SEEK_SET); | ||
1607 | fwrite(&word, 1, 4, mDataFP); | ||
1608 | fflush(mDataFP); | ||
1609 | } | ||
1610 | |||
1611 | fseek(mIndexFP, 0, SEEK_SET); | ||
1612 | if (fread(&word, 1, 4, mIndexFP) == 4) | ||
1613 | { | ||
1614 | fseek(mIndexFP, 0, SEEK_SET); | ||
1615 | fwrite(&word, 1, 4, mIndexFP); | ||
1616 | fflush(mIndexFP); | ||
1617 | } | ||
1618 | } | ||
1619 | |||
1620 | |||
1621 | void LLVFS::dumpMap() | ||
1622 | { | ||
1623 | llinfos << "Files:" << llendl; | ||
1624 | for (fileblock_map::iterator it = mFileBlocks.begin(); it != mFileBlocks.end(); ++it) | ||
1625 | { | ||
1626 | LLVFSFileBlock *file_block = (*it).second; | ||
1627 | llinfos << "Location: " << file_block->mLocation << "\tLength: " << file_block->mLength << "\t" << file_block->mFileID << "\t" << file_block->mFileType << llendl; | ||
1628 | } | ||
1629 | |||
1630 | llinfos << "Free Blocks:" << llendl; | ||
1631 | for (blocks_location_map_t::iterator iter = mFreeBlocksByLocation.begin(), | ||
1632 | end = mFreeBlocksByLocation.end(); | ||
1633 | iter != end; iter++) | ||
1634 | { | ||
1635 | LLVFSBlock *free_block = iter->second; | ||
1636 | llinfos << "Location: " << free_block->mLocation << "\tLength: " << free_block->mLength << llendl; | ||
1637 | } | ||
1638 | } | ||
1639 | |||
1640 | // verify that the index file contents match the in-memory file structure | ||
1641 | // Very slow, do not call routinely. JC | ||
1642 | void LLVFS::audit() | ||
1643 | { | ||
1644 | // Lock the mutex through this whole function. | ||
1645 | LLMutexLock lock_data(mDataMutex); | ||
1646 | |||
1647 | fflush(mIndexFP); | ||
1648 | |||
1649 | fseek(mIndexFP, 0, SEEK_END); | ||
1650 | S32 index_size = ftell(mIndexFP); | ||
1651 | fseek(mIndexFP, 0, SEEK_SET); | ||
1652 | |||
1653 | U8 *buffer = new U8[index_size]; | ||
1654 | fread(buffer, index_size, 1, mIndexFP); | ||
1655 | |||
1656 | U8 *tmp_ptr = buffer; | ||
1657 | |||
1658 | std::map<LLVFSFileSpecifier, LLVFSFileBlock*> found_files; | ||
1659 | U32 cur_time = (U32)time(NULL); | ||
1660 | |||
1661 | BOOL vfs_corrupt = FALSE; | ||
1662 | |||
1663 | std::vector<LLVFSFileBlock*> audit_blocks; | ||
1664 | while (tmp_ptr < buffer + index_size) | ||
1665 | { | ||
1666 | LLVFSFileBlock *block = new LLVFSFileBlock(); | ||
1667 | audit_blocks.push_back(block); | ||
1668 | |||
1669 | block->deserialize(tmp_ptr, (S32)(tmp_ptr - buffer)); | ||
1670 | tmp_ptr += block->SERIAL_SIZE; | ||
1671 | |||
1672 | // do sanity check on this block | ||
1673 | if (block->mLength >= 0 && | ||
1674 | block->mLocation >= 0 && | ||
1675 | block->mSize >= 0 && | ||
1676 | block->mSize <= block->mLength && | ||
1677 | block->mFileType >= LLAssetType::AT_NONE && | ||
1678 | block->mFileType < LLAssetType::AT_COUNT && | ||
1679 | block->mAccessTime <= cur_time && | ||
1680 | block->mFileID != LLUUID::null) | ||
1681 | { | ||
1682 | if (mFileBlocks.find(*block) == mFileBlocks.end()) | ||
1683 | { | ||
1684 | llwarns << "VFile " << block->mFileID << ":" << block->mFileType << " on disk, not in memory, loc " << block->mIndexLocation << llendl; | ||
1685 | } | ||
1686 | else if (found_files.find(*block) != found_files.end()) | ||
1687 | { | ||
1688 | std::map<LLVFSFileSpecifier, LLVFSFileBlock*>::iterator it; | ||
1689 | it = found_files.find(*block); | ||
1690 | LLVFSFileBlock* dupe = it->second; | ||
1691 | // try to keep data from being lost | ||
1692 | unlockAndClose(mIndexFP); | ||
1693 | mIndexFP = NULL; | ||
1694 | unlockAndClose(mDataFP); | ||
1695 | mDataFP = NULL; | ||
1696 | llwarns << "VFS: Original block index " << block->mIndexLocation | ||
1697 | << " location " << block->mLocation | ||
1698 | << " length " << block->mLength | ||
1699 | << " size " << block->mSize | ||
1700 | << " id " << block->mFileID | ||
1701 | << " type " << block->mFileType | ||
1702 | << llendl; | ||
1703 | llwarns << "VFS: Duplicate block index " << dupe->mIndexLocation | ||
1704 | << " location " << dupe->mLocation | ||
1705 | << " length " << dupe->mLength | ||
1706 | << " size " << dupe->mSize | ||
1707 | << " id " << dupe->mFileID | ||
1708 | << " type " << dupe->mFileType | ||
1709 | << llendl; | ||
1710 | llwarns << "VFS: Index size " << index_size << llendl; | ||
1711 | llwarns << "VFS: INDEX CORRUPT" << llendl; | ||
1712 | vfs_corrupt = TRUE; | ||
1713 | break; | ||
1714 | } | ||
1715 | else | ||
1716 | { | ||
1717 | found_files[*block] = block; | ||
1718 | } | ||
1719 | } | ||
1720 | else | ||
1721 | { | ||
1722 | if (block->mLength) | ||
1723 | { | ||
1724 | llwarns << "VFile " << block->mFileID << ":" << block->mFileType << " corrupt on disk" << llendl; | ||
1725 | } | ||
1726 | // else this is just a hole | ||
1727 | } | ||
1728 | } | ||
1729 | |||
1730 | delete[] buffer; | ||
1731 | |||
1732 | if (vfs_corrupt) | ||
1733 | { | ||
1734 | for (std::vector<LLVFSFileBlock*>::iterator iter = audit_blocks.begin(); | ||
1735 | iter != audit_blocks.end(); ++iter) | ||
1736 | { | ||
1737 | delete *iter; | ||
1738 | } | ||
1739 | audit_blocks.clear(); | ||
1740 | return; | ||
1741 | } | ||
1742 | |||
1743 | for (fileblock_map::iterator it = mFileBlocks.begin(); it != mFileBlocks.end(); ++it) | ||
1744 | { | ||
1745 | LLVFSFileBlock* block = (*it).second; | ||
1746 | |||
1747 | if (block->mSize > 0) | ||
1748 | { | ||
1749 | if (! found_files.count(*block)) | ||
1750 | { | ||
1751 | llwarns << "VFile " << block->mFileID << ":" << block->mFileType << " in memory, not on disk, loc " << block->mIndexLocation<< llendl; | ||
1752 | fseek(mIndexFP, block->mIndexLocation, SEEK_SET); | ||
1753 | U8 buf[LLVFSFileBlock::SERIAL_SIZE]; | ||
1754 | fread(buf, LLVFSFileBlock::SERIAL_SIZE, 1, mIndexFP); | ||
1755 | |||
1756 | LLVFSFileBlock disk_block; | ||
1757 | disk_block.deserialize(buf, block->mIndexLocation); | ||
1758 | |||
1759 | llwarns << "Instead found " << disk_block.mFileID << ":" << block->mFileType << llendl; | ||
1760 | } | ||
1761 | else | ||
1762 | { | ||
1763 | block = found_files.find(*block)->second; | ||
1764 | found_files.erase(*block); | ||
1765 | delete block; | ||
1766 | } | ||
1767 | } | ||
1768 | } | ||
1769 | |||
1770 | for (std::map<LLVFSFileSpecifier, LLVFSFileBlock*>::iterator iter = found_files.begin(); | ||
1771 | iter != found_files.end(); iter++) | ||
1772 | { | ||
1773 | LLVFSFileBlock* block = iter->second; | ||
1774 | llwarns << "VFile " << block->mFileID << ":" << block->mFileType << " szie:" << block->mSize << " leftover" << llendl; | ||
1775 | } | ||
1776 | |||
1777 | llinfos << "VFS: audit OK" << llendl; | ||
1778 | // mutex released by LLMutexLock() destructor. | ||
1779 | } | ||
1780 | |||
1781 | |||
1782 | // quick check for uninitialized blocks | ||
1783 | // Slow, do not call in release. | ||
1784 | void LLVFS::checkMem() | ||
1785 | { | ||
1786 | lockData(); | ||
1787 | |||
1788 | for (fileblock_map::iterator it = mFileBlocks.begin(); it != mFileBlocks.end(); ++it) | ||
1789 | { | ||
1790 | LLVFSFileBlock *block = (*it).second; | ||
1791 | llassert(block->mFileType >= LLAssetType::AT_NONE && | ||
1792 | block->mFileType < LLAssetType::AT_COUNT && | ||
1793 | block->mFileID != LLUUID::null); | ||
1794 | |||
1795 | for (std::deque<S32>::iterator iter = mIndexHoles.begin(); | ||
1796 | iter != mIndexHoles.end(); ++iter) | ||
1797 | { | ||
1798 | S32 index_loc = *iter; | ||
1799 | if (index_loc == block->mIndexLocation) | ||
1800 | { | ||
1801 | llwarns << "VFile block " << block->mFileID << ":" << block->mFileType << " is marked as a hole" << llendl; | ||
1802 | } | ||
1803 | } | ||
1804 | } | ||
1805 | |||
1806 | llinfos << "VFS: mem check OK" << llendl; | ||
1807 | |||
1808 | unlockData(); | ||
1809 | } | ||
1810 | |||
1811 | void LLVFS::dumpLockCounts() | ||
1812 | { | ||
1813 | S32 i; | ||
1814 | for (i = 0; i < VFSLOCK_COUNT; i++) | ||
1815 | { | ||
1816 | llinfos << "LockType: " << i << ": " << mLockCounts[i] << llendl; | ||
1817 | } | ||
1818 | } | ||
1819 | |||
1820 | void LLVFS::dumpStatistics() | ||
1821 | { | ||
1822 | lockData(); | ||
1823 | |||
1824 | // Investigate file blocks. | ||
1825 | std::map<S32, S32> size_counts; | ||
1826 | std::map<U32, S32> location_counts; | ||
1827 | std::map<LLAssetType::EType, std::pair<S32,S32> > filetype_counts; | ||
1828 | |||
1829 | S32 max_file_size = 0; | ||
1830 | S32 total_file_size = 0; | ||
1831 | S32 invalid_file_count = 0; | ||
1832 | for (fileblock_map::iterator it = mFileBlocks.begin(); it != mFileBlocks.end(); ++it) | ||
1833 | { | ||
1834 | LLVFSFileBlock *file_block = (*it).second; | ||
1835 | if (file_block->mLength == BLOCK_LENGTH_INVALID) | ||
1836 | { | ||
1837 | invalid_file_count++; | ||
1838 | } | ||
1839 | else if (file_block->mLength <= 0) | ||
1840 | { | ||
1841 | llinfos << "Bad file block at: " << file_block->mLocation << "\tLength: " << file_block->mLength << "\t" << file_block->mFileID << "\t" << file_block->mFileType << llendl; | ||
1842 | size_counts[file_block->mLength]++; | ||
1843 | location_counts[file_block->mLocation]++; | ||
1844 | } | ||
1845 | else | ||
1846 | { | ||
1847 | total_file_size += file_block->mLength; | ||
1848 | } | ||
1849 | |||
1850 | if (file_block->mLength > max_file_size) | ||
1851 | { | ||
1852 | max_file_size = file_block->mLength; | ||
1853 | } | ||
1854 | |||
1855 | filetype_counts[file_block->mFileType].first++; | ||
1856 | filetype_counts[file_block->mFileType].second += file_block->mLength; | ||
1857 | } | ||
1858 | |||
1859 | for (std::map<S32,S32>::iterator it = size_counts.begin(); it != size_counts.end(); ++it) | ||
1860 | { | ||
1861 | S32 size = it->first; | ||
1862 | S32 size_count = it->second; | ||
1863 | llinfos << "Bad files size " << size << " count " << size_count << llendl; | ||
1864 | } | ||
1865 | for (std::map<U32,S32>::iterator it = location_counts.begin(); it != location_counts.end(); ++it) | ||
1866 | { | ||
1867 | U32 location = it->first; | ||
1868 | S32 location_count = it->second; | ||
1869 | llinfos << "Bad files location " << location << " count " << location_count << llendl; | ||
1870 | } | ||
1871 | |||
1872 | // Investigate free list. | ||
1873 | S32 max_free_size = 0; | ||
1874 | S32 total_free_size = 0; | ||
1875 | std::map<S32, S32> free_length_counts; | ||
1876 | for (blocks_location_map_t::iterator iter = mFreeBlocksByLocation.begin(), | ||
1877 | end = mFreeBlocksByLocation.end(); | ||
1878 | iter != end; iter++) | ||
1879 | { | ||
1880 | LLVFSBlock *free_block = iter->second; | ||
1881 | if (free_block->mLength <= 0) | ||
1882 | { | ||
1883 | llinfos << "Bad free block at: " << free_block->mLocation << "\tLength: " << free_block->mLength << llendl; | ||
1884 | } | ||
1885 | else | ||
1886 | { | ||
1887 | llinfos << "Block: " << free_block->mLocation | ||
1888 | << "\tLength: " << free_block->mLength | ||
1889 | << "\tEnd: " << free_block->mLocation + free_block->mLength | ||
1890 | << llendl; | ||
1891 | total_free_size += free_block->mLength; | ||
1892 | } | ||
1893 | |||
1894 | if (free_block->mLength > max_free_size) | ||
1895 | { | ||
1896 | max_free_size = free_block->mLength; | ||
1897 | } | ||
1898 | |||
1899 | free_length_counts[free_block->mLength]++; | ||
1900 | } | ||
1901 | |||
1902 | // Dump histogram of free block sizes | ||
1903 | for (std::map<S32,S32>::iterator it = free_length_counts.begin(); it != free_length_counts.end(); ++it) | ||
1904 | { | ||
1905 | llinfos << "Free length " << it->first << " count " << it->second << llendl; | ||
1906 | } | ||
1907 | |||
1908 | llinfos << "Invalid blocks: " << invalid_file_count << llendl; | ||
1909 | llinfos << "File blocks: " << mFileBlocks.size() << llendl; | ||
1910 | |||
1911 | S32 length_list_count = (S32)mFreeBlocksByLength.size(); | ||
1912 | S32 location_list_count = (S32)mFreeBlocksByLocation.size(); | ||
1913 | if (length_list_count == location_list_count) | ||
1914 | { | ||
1915 | llinfos << "Free list lengths match, free blocks: " << location_list_count << llendl; | ||
1916 | } | ||
1917 | else | ||
1918 | { | ||
1919 | llwarns << "Free list lengths do not match!" << llendl; | ||
1920 | llwarns << "By length: " << length_list_count << llendl; | ||
1921 | llwarns << "By location: " << location_list_count << llendl; | ||
1922 | } | ||
1923 | llinfos << "Max file: " << max_file_size/1024 << "K" << llendl; | ||
1924 | llinfos << "Max free: " << max_free_size/1024 << "K" << llendl; | ||
1925 | llinfos << "Total file size: " << total_file_size/1024 << "K" << llendl; | ||
1926 | llinfos << "Total free size: " << total_free_size/1024 << "K" << llendl; | ||
1927 | llinfos << "Sum: " << (total_file_size + total_free_size) << " bytes" << llendl; | ||
1928 | llinfos << llformat("%.0f%% full",((F32)(total_file_size)/(F32)(total_file_size+total_free_size))*100.f) << llendl; | ||
1929 | |||
1930 | llinfos << " " << llendl; | ||
1931 | for (std::map<LLAssetType::EType, std::pair<S32,S32> >::iterator iter = filetype_counts.begin(); | ||
1932 | iter != filetype_counts.end(); ++iter) | ||
1933 | { | ||
1934 | llinfos << "Type: " << LLAssetType::getDesc(iter->first) | ||
1935 | << " Count: " << iter->second.first | ||
1936 | << " Bytes: " << (iter->second.second>>20) << " MB" << llendl; | ||
1937 | } | ||
1938 | |||
1939 | // Look for potential merges | ||
1940 | { | ||
1941 | blocks_location_map_t::iterator iter = mFreeBlocksByLocation.begin(); | ||
1942 | blocks_location_map_t::iterator end = mFreeBlocksByLocation.end(); | ||
1943 | LLVFSBlock *first_block = iter->second; | ||
1944 | while(iter != end) | ||
1945 | { | ||
1946 | if (++iter == end) | ||
1947 | break; | ||
1948 | LLVFSBlock *second_block = iter->second; | ||
1949 | if (first_block->mLocation + first_block->mLength == second_block->mLocation) | ||
1950 | { | ||
1951 | llinfos << "Potential merge at " << first_block->mLocation << llendl; | ||
1952 | } | ||
1953 | first_block = second_block; | ||
1954 | } | ||
1955 | } | ||
1956 | unlockData(); | ||
1957 | } | ||
1958 | |||
1959 | // Debug Only! | ||
1960 | #include "llapr.h" | ||
1961 | void LLVFS::dumpFiles() | ||
1962 | { | ||
1963 | lockData(); | ||
1964 | |||
1965 | for (fileblock_map::iterator it = mFileBlocks.begin(); it != mFileBlocks.end(); ++it) | ||
1966 | { | ||
1967 | LLVFSFileSpecifier file_spec = it->first; | ||
1968 | LLVFSFileBlock *file_block = it->second; | ||
1969 | S32 length = file_block->mLength; | ||
1970 | S32 size = file_block->mSize; | ||
1971 | if (length != BLOCK_LENGTH_INVALID && size > 0) | ||
1972 | { | ||
1973 | LLUUID id = file_spec.mFileID; | ||
1974 | LLAssetType::EType type = file_spec.mFileType; | ||
1975 | U8* buffer = new U8[size]; | ||
1976 | |||
1977 | unlockData(); | ||
1978 | getData(id, type, buffer, 0, size); | ||
1979 | lockData(); | ||
1980 | |||
1981 | LLString extention = ".data"; | ||
1982 | switch(type) | ||
1983 | { | ||
1984 | case LLAssetType::AT_TEXTURE: | ||
1985 | extention = ".jp2"; // ".j2c"; // IrfanView recognizes .jp2 -sjb | ||
1986 | break; | ||
1987 | default: | ||
1988 | break; | ||
1989 | } | ||
1990 | LLString filename = id.getString() + extention; | ||
1991 | llinfos << " Writing " << filename << llendl; | ||
1992 | apr_file_t* file = ll_apr_file_open(filename, LL_APR_WB); | ||
1993 | ll_apr_file_write(file, buffer, size); | ||
1994 | apr_file_close(file); | ||
1995 | delete[] buffer; | ||
1996 | } | ||
1997 | } | ||
1998 | |||
1999 | unlockData(); | ||
2000 | } | ||
2001 | |||
2002 | //============================================================================ | ||
2003 | // protected | ||
2004 | //============================================================================ | ||
2005 | |||
2006 | // static | ||
2007 | FILE *LLVFS::openAndLock(const char *filename, const char *mode, BOOL read_lock) | ||
2008 | { | ||
2009 | #if LL_WINDOWS | ||
2010 | |||
2011 | return LLFile::_fsopen(filename, mode, (read_lock ? _SH_DENYWR : _SH_DENYRW)); | ||
2012 | |||
2013 | #else | ||
2014 | |||
2015 | FILE *fp; | ||
2016 | int fd; | ||
2017 | |||
2018 | // first test the lock in a non-destructive way | ||
2019 | if (strstr(mode, "w")) | ||
2020 | { | ||
2021 | fp = LLFile::fopen(filename, "rb"); | ||
2022 | if (fp) | ||
2023 | { | ||
2024 | fd = fileno(fp); | ||
2025 | if (flock(fd, (read_lock ? LOCK_SH : LOCK_EX) | LOCK_NB) == -1) | ||
2026 | { | ||
2027 | fclose(fp); | ||
2028 | return NULL; | ||
2029 | } | ||
2030 | |||
2031 | fclose(fp); | ||
2032 | } | ||
2033 | } | ||
2034 | |||
2035 | // now actually open the file for use | ||
2036 | fp = LLFile::fopen(filename, mode); | ||
2037 | if (fp) | ||
2038 | { | ||
2039 | fd = fileno(fp); | ||
2040 | if (flock(fd, (read_lock ? LOCK_SH : LOCK_EX) | LOCK_NB) == -1) | ||
2041 | { | ||
2042 | fclose(fp); | ||
2043 | fp = NULL; | ||
2044 | } | ||
2045 | } | ||
2046 | |||
2047 | return fp; | ||
2048 | |||
2049 | #endif | ||
2050 | } | ||
2051 | |||
2052 | // static | ||
2053 | void LLVFS::unlockAndClose(FILE *fp) | ||
2054 | { | ||
2055 | if (fp) | ||
2056 | { | ||
2057 | // IW: we don't actually want to unlock on linux | ||
2058 | // this is because a forked process can kill the parent's lock | ||
2059 | // with an explicit unlock | ||
2060 | // however, fclose() will implicitly remove the lock | ||
2061 | // but only once both parent and child have closed the file | ||
2062 | /* | ||
2063 | #if !LL_WINDOWS | ||
2064 | int fd = fileno(fp); | ||
2065 | flock(fd, LOCK_UN); | ||
2066 | #endif | ||
2067 | */ | ||
2068 | |||
2069 | fclose(fp); | ||
2070 | } | ||
2071 | } | ||