File: BlockCache.c

package info (click to toggle)
hfsprogs 332.25-11
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 6,440 kB
  • ctags: 7,805
  • sloc: ansic: 58,120; makefile: 25
file content (413 lines) | stat: -rw-r--r-- 9,730 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (c) 2000-2003, 2005 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * The contents of this file constitute Original Code as defined in and
 * are subject to the Apple Public Source License Version 1.1 (the
 * "License").  You may not use this file except in compliance with the
 * License.  Please obtain a copy of the License at
 * http://www.apple.com/publicsource and read it before using this file.
 *
 * This Original Code and all software distributed under the License are
 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

#if LINUX
#include "missing.h"
#endif
#include "SRuntime.h"
#include "Scavenger.h"
#include "../cache.h"



extern OSErr MapFileBlockC (
	SVCB *   vcb,
	SFCB *   fcb,
	UInt32   numberOfBytes,
	UInt32   sectorOffset,
	UInt64 * startSector,
	UInt32 * availableBytes
);

extern Cache_t fscache;


static OSStatus  ReadFragmentedBlock (SFCB *file, UInt32 blockNum, BlockDescriptor *block);
static OSStatus  WriteFragmentedBlock( 	SFCB *file, 
										BlockDescriptor *block, 
										int age, 
										uint32_t writeOptions );
static OSStatus  ReleaseFragmentedBlock (SFCB *file, BlockDescriptor *block, int age);


void
InitBlockCache(SVCB *volume)
{
	volume->vcbBlockCache = (void *) &fscache;
}


/*
 *  kGetBlock
 *  kForceReadBlock
 *  kGetEmptyBlock
 *  kSkipEndianSwap
 */
OSStatus
GetVolumeBlock (SVCB *volume, UInt64 blockNum, GetBlockOptions options, BlockDescriptor *block)
{
	UInt32  blockSize;
	SInt64  offset;
	UInt16  signature;
	OSStatus result;	
	Buf_t *   buffer;
	Cache_t * cache;

	buffer = NULL;
	cache  = (Cache_t *) volume->vcbBlockCache;
	blockSize = 512;

	offset = (SInt64) ((UInt64) blockNum) << kSectorShift;

	result = CacheRead (cache, offset, blockSize, &buffer);

	if (result == 0) {
		block->blockHeader = buffer;
		block->buffer = buffer->Buffer;
		block->blockNum = blockNum;
		block->blockSize = blockSize;
		block->blockReadFromDisk = 0;
		block->fragmented = 0;
	} else {
		block->blockHeader = NULL;
		block->buffer = NULL;
	}
	
	if (!(options & kSkipEndianSwap) && (result == 0)) {
		HFSMasterDirectoryBlock *mdb;

		mdb = (HFSMasterDirectoryBlock *)block->buffer;
		signature = SWAP_BE16(mdb->drSigWord);
		if (signature == kHFSPlusSigWord || signature == kHFSXSigWord)
			SWAP_HFSPLUSVH(block->buffer);
		else if (signature == kHFSSigWord)
			SWAP_HFSMDB(block->buffer);
	}
	return (result);
}


/*
 *  kReleaseBlock
 *  kForceWriteBlock
 *  kMarkBlockDirty
 *  kTrashBlock
 *  kSkipEndianSwap
 */
OSStatus
ReleaseVolumeBlock (SVCB *volume, BlockDescriptor *block, ReleaseBlockOptions options)
{
	OSStatus  result = 0;
	Cache_t * cache;
	Buf_t *   buffer;
	int       age;
	UInt16  signature;

	cache  = (Cache_t *) volume->vcbBlockCache;
	buffer = (Buf_t *) block->blockHeader;
	age    = ((options & kTrashBlock) != 0);

	/*
	 * Always leave the blocks in the cache in big endian
	 */
	if (!(options & kSkipEndianSwap)) {
		signature = ((HFSMasterDirectoryBlock *)block->buffer)->drSigWord;
		if (signature == kHFSPlusSigWord || signature == kHFSXSigWord)
			SWAP_HFSPLUSVH(block->buffer);
		else if (signature == kHFSSigWord)
			SWAP_HFSMDB(block->buffer);
	}

	if (options & (kMarkBlockDirty | kForceWriteBlock)) {
		result = CacheWrite(cache, buffer, age, 0);
	} else { /* not dirty */
		result = CacheRelease (cache, buffer, age);
	}
	return (result);
}


/*
 *  kGetBlock
 *  kForceReadBlock
 *  kGetEmptyBlock
 */
OSStatus
GetFileBlock (SFCB *file, UInt32 blockNum, GetBlockOptions options, BlockDescriptor *block)
{
	UInt64	diskBlock;
	UInt32	contiguousBytes;
	SInt64  offset;

	OSStatus result;	
	Buf_t *   buffer;
	Cache_t * cache;

	buffer = NULL;
	block->buffer = NULL;
	block->blockHeader = NULL;
	cache  = (Cache_t *)file->fcbVolume->vcbBlockCache;

	/* Map file block to volume block */
	result = MapFileBlockC(file->fcbVolume, file, file->fcbBlockSize,
			(((UInt64)blockNum * (UInt64)file->fcbBlockSize) >> kSectorShift),
			&diskBlock, &contiguousBytes);
	if (result) return (result);

	if (contiguousBytes < file->fcbBlockSize)
		return ( ReadFragmentedBlock(file, blockNum, block) );

	offset = (SInt64) ((UInt64) diskBlock) << kSectorShift;

	result = CacheRead (cache, offset, file->fcbBlockSize, &buffer);
	if (result)  return (result);

	block->blockHeader = buffer;
	block->buffer = buffer->Buffer;
	block->blockNum = blockNum;
	block->blockSize = file->fcbBlockSize;
	block->blockReadFromDisk = 0;
	block->fragmented = 0;
	
	return (noErr);
}


/*
 *  kReleaseBlock
 *  kForceWriteBlock
 *  kMarkBlockDirty
 *  kTrashBlock
 */
OSStatus
ReleaseFileBlock (SFCB *file, BlockDescriptor *block, ReleaseBlockOptions options)
{
	OSStatus  result = 0;
	Cache_t * cache;
	Buf_t *   buffer;
	int       age;
	uint32_t  writeOptions = 0;

	cache  = (Cache_t *)file->fcbVolume->vcbBlockCache;
	buffer = (Buf_t *) block->blockHeader;
	age    = ((options & kTrashBlock) != 0);

	if ( (options & kForceWriteBlock) == 0 )
		/* only write if we're forced to */
		writeOptions |= kLazyWrite;

	if (options & (kMarkBlockDirty | kForceWriteBlock)) {
		if (block->fragmented)
			result = WriteFragmentedBlock(file, block, age, writeOptions);
		else
			result = CacheWrite(cache, buffer, age, writeOptions);
	} else { /* not dirty */

		if (block->fragmented)
			result = ReleaseFragmentedBlock(file, block, age);
		else
			result = CacheRelease (cache, buffer, age);
	}
	return (result);
}


/*
 *
 */
OSStatus
SetFileBlockSize (SFCB *file, ByteCount blockSize)
{
	file->fcbBlockSize = blockSize;

	return (0);
}


/*
 * Read a block that is fragmented across 2 or more allocation blocks
 *
 *  - a block descriptor buffer is allocated here
 *  - the blockHeader field holds a list of Buf_t buffers.
 *  - the fragmented flag is set
 */
static OSStatus
ReadFragmentedBlock (SFCB *file, UInt32 blockNum, BlockDescriptor *block)
{
	UInt64	sector;
	UInt32	fragSize, blockSize;
	UInt64  fileOffset; 
	SInt64  diskOffset;
	SVCB *  volume;
	int     i, maxFrags;
	OSStatus result;	
	Buf_t **   bufs;   /* list of Buf_t pointers */
	Cache_t * cache;
	char *	buffer;

	volume = file->fcbVolume;
	cache  = (Cache_t *)volume->vcbBlockCache;

	blockSize = file->fcbBlockSize;
	maxFrags = blockSize / volume->vcbBlockSize;
	fileOffset = (UInt64)blockNum * (UInt64)blockSize;
	
	buffer = (char *) AllocateMemory(blockSize);
	bufs = (Buf_t **) AllocateClearMemory(maxFrags * sizeof(Buf_t *));
	if (buffer == NULL || bufs == NULL) {
		result = memFullErr;
		return (result);
	}
	
	block->buffer = buffer;
	block->blockHeader = bufs;
	block->blockNum = blockNum;
	block->blockSize = blockSize;
	block->blockReadFromDisk = false;
	block->fragmented = true;
	
	for (i = 0; (i < maxFrags) && (blockSize > 0); ++i) {
		result = MapFileBlockC (volume, file, blockSize,
					fileOffset >> kSectorShift,
					&sector, &fragSize);
		if (result) goto ErrorExit;

		diskOffset = (SInt64) (sector) << kSectorShift;
		result = CacheRead (cache, diskOffset, fragSize, &bufs[i]);
		if (result) goto ErrorExit;
		
		if (bufs[i]->Length != fragSize) {
			printf("ReadFragmentedBlock: cache failure (Length != fragSize)\n");
			result = -1;
			goto ErrorExit;
		}

		CopyMemory(bufs[i]->Buffer, buffer, fragSize);
		buffer     += fragSize;
		fileOffset += fragSize;
		blockSize  -= fragSize;
	}
	
	return (noErr);

ErrorExit:
	i = 0;
	while (bufs[i] != NULL) {
		(void) CacheRelease (cache, bufs[i], true);
		++i;
	}

	DisposeMemory(block->buffer);
	DisposeMemory(block->blockHeader);

	block->blockHeader = NULL;
	block->buffer = NULL;

	return (result);
}


/*
 * Write a block that is fragmented across 2 or more allocation blocks
 *
 */
static OSStatus
WriteFragmentedBlock( SFCB *file, BlockDescriptor *block, int age, uint32_t writeOptions )
{
	Cache_t * cache;
	Buf_t **  bufs;  /* list of Buf_t pointers */
	char *	  buffer;
	char *    bufEnd;
	UInt32	  fragSize;
	OSStatus  result;
	int i = 0;

	result = 0;
	cache  = (Cache_t *) file->fcbVolume->vcbBlockCache;
	bufs   = (Buf_t **) block->blockHeader;
	buffer = (char *) block->buffer;
	bufEnd = buffer + file->fcbBlockSize;

	if (bufs == NULL) {
		printf("WriteFragmentedBlock: NULL bufs list!\n");
		return (-1);
	}
	
	while ((bufs[i] != NULL) && (buffer < bufEnd)) {
		fragSize = bufs[i]->Length;
		
		/* copy data for this fragment */
		CopyMemory(buffer, bufs[i]->Buffer, fragSize);
		
		/* write it back to cache */
		result = CacheWrite(cache, bufs[i], age, writeOptions);
		if (result) break;

		buffer += fragSize;
		++i;
	}
	
	DisposeMemory(block->buffer);
	DisposeMemory(block->blockHeader);

	block->buffer = NULL;
	block->blockHeader = NULL;
	block->fragmented = false;

	return (result);
}


/*
 * Release a block that is fragmented across 2 or more allocation blocks
 *
 */
static OSStatus
ReleaseFragmentedBlock (SFCB *file, BlockDescriptor *block, int age)
{
	Cache_t * cache;
	Buf_t **   bufs;  /* list of Buf_t pointers */
	int i = 0;

	cache = (Cache_t *)file->fcbVolume->vcbBlockCache;
	bufs  = (Buf_t **) block->blockHeader;

	if (bufs == NULL) {
		printf("ReleaseFragmentedBlock: NULL buf list!\n");
		return (-1);
	}

	while (bufs[i] != NULL && bufs[i]->Length) {
		(void) CacheRelease (cache, bufs[i], true);
		++i;
	}
	
	DisposeMemory(block->buffer);
	DisposeMemory(block->blockHeader);

	block->buffer = NULL;
	block->blockHeader = NULL;
	block->fragmented = false;

	return (noErr);
}