3 * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
5 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. The rights granted to you under the License
11 * may not be used to create, or enable the creation or redistribution of,
12 * unlawful or unlicensed copies of an Apple operating system, or to
13 * circumvent, violate, or enable the circumvention or violation of, any
14 * terms of an Apple operating system software license agreement.
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this file.
19 * The Original Code and all software distributed under the License are
20 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24 * Please see the License for the specific language governing rights and
25 * limitations under the License.
27 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * Implement copy-on-write shadow map to allow a disk image to be
34 * mounted read-only, yet be writable by transferring writes to a
35 * "shadow" file. Subsequent reads from blocks that have been
36 * written will then go the "shadow" file.
38 * The map has two parts:
39 * 1) a bit map to track which blocks have been written
40 * 2) a band map to map a "band" within the original file to a corresponding
41 * "band" in the shadow file. Each band has the same size.
43 * The band map is used to ensure that blocks that are contiguous in the
44 * original file will remain contiguous in the shadow file.
46 * For debugging purposes, this file can be compiled standalone using:
47 * cc -o shadow shadow.c -DTEST_SHADOW
51 * Modification History
53 * December 21, 2001 Dieter Siegmund (dieter@apple.com)
56 #include <sys/param.h>
57 #include <sys/types.h>
58 #include <mach/boolean.h>
65 #define my_malloc(a) malloc(a)
66 #define my_free(a) free(a)
67 #else /* !TEST_SHADOW */
68 #include <sys/malloc.h>
69 #define my_malloc(a) _MALLOC(a, M_TEMP, M_WAITOK)
70 #define my_free(a) FREE(a, M_TEMP)
71 #include <libkern/libkern.h>
72 #endif /* TEST_SHADOW */
76 #define ULONG_ALL_ONES ((u_long)(-1))
77 #define USHORT_ALL_ONES ((u_short)(-1))
78 #define UCHAR_ALL_ONES ((u_char)(-1))
80 #define my_trunc(value, divisor) ((value) / (divisor) * (divisor))
82 /* a band size of 128K can represent a file up to 8GB */
83 #define BAND_SIZE_DEFAULT_POWER_2 17
84 #define BAND_SIZE_DEFAULT (1 << BAND_SIZE_DEFAULT_POWER_2)
86 typedef u_short band_number_t
;
87 #define BAND_ZERO ((band_number_t)0)
88 #define BAND_MAX ((band_number_t)65535)
91 u_long blocks_per_band
;/* size in blocks */
93 u_char
* block_bitmap
; /* 1 bit per block; 1=written */
94 band_number_t
* bands
; /* band map array */
95 u_long file_size_blocks
; /* size of file in bands */
96 u_long shadow_size_bands
; /* size of shadow in bands */
97 u_long next_band
; /* next free band */
98 u_long zeroth_band
; /* special-case 0th band */
107 static __inline__ u_char
110 return ((u_char
)(1 << b
));
114 * Function: bits_lower
116 * Return a byte value in which bits numbered lower than 'b' are set.
118 static __inline__ u_char
121 return ((u_char
)(bit(b
) - 1));
125 * Function: byte_set_bits
127 * Set the given range of bits within a byte.
129 static __inline__ u_char
130 byte_set_bits(int start
, int end
)
132 return ((u_char
)((~bits_lower(start
)) & (bits_lower(end
) | bit(end
))));
135 static __inline__ bitmap_offset_t
136 bitmap_offset(off_t where
)
140 b
.byte
= where
/ NBBY
;
141 b
.bit
= where
% NBBY
;
146 * Function: bitmap_set
149 * Set the given range of bits.
151 * This algorithm tries to set the extents using the biggest
152 * units, using longs, then a short, then a byte, then bits.
155 bitmap_set(u_char
* map
, u_long start_bit
, u_long bit_count
)
157 bitmap_offset_t start
;
160 start
= bitmap_offset(start_bit
);
161 end
= bitmap_offset(start_bit
+ bit_count
);
162 if (start
.byte
< end
.byte
) {
166 map
[start
.byte
] |= byte_set_bits(start
.bit
, NBBY
- 1);
169 if (start
.byte
== end
.byte
)
173 n_bytes
= end
.byte
- start
.byte
;
175 while (n_bytes
>= (sizeof(u_long
))) {
176 *((u_long
*)(map
+ start
.byte
)) = ULONG_ALL_ONES
;
177 start
.byte
+= sizeof(u_long
);
178 n_bytes
-= sizeof(u_long
);
180 if (n_bytes
>= sizeof(u_short
)) {
181 *((u_short
*)(map
+ start
.byte
)) = USHORT_ALL_ONES
;
182 start
.byte
+= sizeof(u_short
);
183 n_bytes
-= sizeof(u_short
);
186 map
[start
.byte
] = UCHAR_ALL_ONES
;
193 if (end
.bit
> start
.bit
) {
194 map
[start
.byte
] |= byte_set_bits(start
.bit
, end
.bit
- 1);
201 * Function: bitmap_get
204 * Return the number of bits in the range that are the same e.g.
205 * 11101 returns 3 because the first 3 bits are the same (1's), whereas
206 * 001100 returns 2 because the first 2 bits are the same.
207 * This algorithm tries to count things in as big a chunk as possible,
208 * first aligning to a byte offset, then trying to count longs, a short,
209 * a byte, then any remaining bits to find the bit that is different.
213 bitmap_get(u_char
* map
, u_long start_bit
, u_long bit_count
,
214 boolean_t
* ret_is_set
)
219 bitmap_offset_t start
;
222 start
= bitmap_offset(start_bit
);
223 end
= bitmap_offset(start_bit
+ bit_count
);
225 is_set
= (map
[start
.byte
] & bit(start
.bit
)) ? TRUE
: FALSE
;
228 if (start
.byte
< end
.byte
) {
231 if (start
.bit
) { /* try to align to a byte */
232 for (i
= start
.bit
; i
< NBBY
; i
++) {
233 boolean_t this_is_set
;
235 this_is_set
= (map
[start
.byte
] & bit(i
)) ? TRUE
: FALSE
;
236 if (this_is_set
!= is_set
) {
237 goto done
; /* found bit that was different, we're done */
241 start
.bit
= 0; /* made it to the next byte */
243 if (start
.byte
== end
.byte
)
244 goto end
; /* no more bytes, check for any leftover bits */
246 /* calculate how many bytes are left in the range */
247 n_bytes
= end
.byte
- start
.byte
;
249 /* check for 4 bytes of the same bits */
250 while (n_bytes
>= sizeof(u_long
)) {
251 u_long
* valPtr
= (u_long
*)(map
+ start
.byte
);
252 if ((is_set
&& *valPtr
== ULONG_ALL_ONES
)
253 || (!is_set
&& *valPtr
== 0)) {
254 count
+= sizeof(*valPtr
) * NBBY
;
255 start
.byte
+= sizeof(*valPtr
);
256 n_bytes
-= sizeof(*valPtr
);
259 break; /* bits differ */
262 /* check for 2 bytes of the same bits */
263 if (n_bytes
>= sizeof(u_short
)) {
264 u_short
* valPtr
= (u_short
*)(map
+ start
.byte
);
266 if ((is_set
&& *valPtr
== USHORT_ALL_ONES
)
267 || (!is_set
&& (*valPtr
== 0))) {
268 count
+= sizeof(*valPtr
) * NBBY
;
269 start
.byte
+= sizeof(*valPtr
);
270 n_bytes
-= sizeof(*valPtr
);
274 /* check for 1 byte of the same bits */
276 if ((is_set
&& map
[start
.byte
] == UCHAR_ALL_ONES
)
277 || (!is_set
&& map
[start
.byte
] == 0)) {
282 /* we found bits that were different, find the first one */
284 for (i
= 0; i
< NBBY
; i
++) {
285 boolean_t this_is_set
;
287 this_is_set
= (map
[start
.byte
] & bit(i
)) ? TRUE
: FALSE
;
288 if (this_is_set
!= is_set
) {
299 for (i
= start
.bit
; i
< (int)end
.bit
; i
++) {
300 boolean_t this_is_set
= (map
[start
.byte
] & bit(i
)) ? TRUE
: FALSE
;
302 if (this_is_set
!= is_set
) {
309 *ret_is_set
= is_set
;
313 static __inline__ band_number_t
314 shadow_map_block_to_band(shadow_map_t
* map
, unsigned long block
)
316 return (block
/ map
->blocks_per_band
);
320 * Function: shadow_map_mapped_band
322 * Return the mapped band for the given band.
323 * If map_it is FALSE, and the band is not mapped, return FALSE.
324 * If map_it is TRUE, then this function will always return TRUE.
327 shadow_map_mapped_band(shadow_map_t
* map
, band_number_t band
,
328 boolean_t map_it
, band_number_t
* mapped_band
)
330 boolean_t is_mapped
= FALSE
;
332 if (band
== map
->zeroth_band
) {
333 *mapped_band
= BAND_ZERO
;
337 *mapped_band
= map
->bands
[band
];
338 if (*mapped_band
== BAND_ZERO
) {
341 if (map
->next_band
== 0) {
342 /* remember the zero'th band */
343 map
->zeroth_band
= band
;
345 *mapped_band
= map
->bands
[band
] = map
->next_band
++;
357 * Function: shadow_map_contiguous
360 * Return the first offset within the range position..(position + count)
361 * that is not a contiguous mapped band.
363 * If called with is_write = TRUE, this function will map bands as it goes.
366 shadow_map_contiguous(shadow_map_t
* map
, u_long start_block
,
367 u_long num_blocks
, boolean_t is_write
)
369 band_number_t band
= shadow_map_block_to_band(map
, start_block
);
370 u_long end_block
= start_block
+ num_blocks
;
372 band_number_t mapped_band
;
373 u_long ret_end_block
= end_block
;
376 is_mapped
= shadow_map_mapped_band(map
, band
, is_write
, &mapped_band
);
377 if (is_write
== FALSE
&& is_mapped
== FALSE
) {
378 static int happened
= 0;
379 /* this can't happen */
381 printf("shadow_map_contiguous: this can't happen!\n");
384 return (start_block
);
386 for (p
= my_trunc(start_block
+ map
->blocks_per_band
,
387 map
->blocks_per_band
);
388 p
< end_block
; p
+= map
->blocks_per_band
) {
389 band_number_t next_mapped_band
;
392 is_mapped
= shadow_map_mapped_band(map
, band
, is_write
,
394 if (is_write
== FALSE
&& is_mapped
== FALSE
) {
397 if ((mapped_band
+ 1) != next_mapped_band
) {
402 mapped_band
= next_mapped_band
;
404 return (ret_end_block
);
409 * Function: block_bitmap_size
411 * The number of bytes required in a block bitmap to represent a file of size
414 * The bytes required is the number of blocks in the file,
415 * divided by the number of bits per byte.
417 * An 8GB file requires (assuming 512 byte block):
418 * 2^33 / 2^9 / 2^3 = 2^21 = 2MB
419 * of bitmap space. This is a non-trival amount of memory,
420 * particularly since most of the bits will be zero.
421 * A sparse bitmap would really help in this case.
423 static __inline__ u_long
424 block_bitmap_size(off_t file_size
, u_long block_size
)
426 off_t blocks
= howmany(file_size
, block_size
);
427 return (howmany(blocks
, NBBY
));
431 * Function: shadow_map_read
434 * Calculate the block offset within the shadow to read, and the number
435 * blocks to read. The input values (block_offset, block_count) refer
436 * to the original file.
438 * The output values (*incr_block_offset, *incr_block_count) refer to the
439 * shadow file if the return value is TRUE. They refer to the original
440 * file if the return value is FALSE.
442 * Blocks within a band may or may not have been written, in addition,
443 * Bands are not necessarily contiguous, therefore:
444 * *incr_block_count <= block_count
445 * The caller must be prepared to call this function interatively
446 * to complete the whole i/o.
448 * TRUE if the shadow file should be read, FALSE if the original file
452 shadow_map_read(shadow_map_t
* map
, u_long block_offset
, u_long block_count
,
453 u_long
* incr_block_offset
, u_long
* incr_block_count
)
455 boolean_t written
= FALSE
;
458 if (block_offset
>= map
->file_size_blocks
459 || (block_offset
+ block_count
) > map
->file_size_blocks
) {
460 printf("shadow_map_read: request (%ld, %ld) exceeds file size %ld\n",
461 block_offset
, block_count
, map
->file_size_blocks
);
462 *incr_block_count
= 0;
464 n_blocks
= bitmap_get(map
->block_bitmap
, block_offset
, block_count
,
466 if (written
== FALSE
) {
467 *incr_block_count
= n_blocks
;
468 *incr_block_offset
= block_offset
;
470 else { /* start has been written, and therefore mapped */
471 band_number_t mapped_band
;
474 mapped_band
= map
->bands
[shadow_map_block_to_band(map
, block_offset
)];
475 *incr_block_offset
= mapped_band
* map
->blocks_per_band
476 + (block_offset
% map
->blocks_per_band
);
478 = shadow_map_contiguous(map
, block_offset
, block_count
, FALSE
);
479 *incr_block_count
= band_limit
- block_offset
;
480 if (*incr_block_count
> n_blocks
) {
481 *incr_block_count
= n_blocks
;
488 * Function: shadow_map_write
491 * Calculate the block offset within the shadow to write, and the number
492 * blocks to write. The input values (block_offset, block_count) refer
493 * to the original file. The output values
494 * (*incr_block_offset, *incr_block_count) refer to the shadow file.
496 * Bands are not necessarily contiguous, therefore:
497 * *incr_block_count <= block_count
498 * The caller must be prepared to call this function interatively
499 * to complete the whole i/o.
501 * TRUE if the shadow file was grown, FALSE otherwise.
504 shadow_map_write(shadow_map_t
* map
, u_long block_offset
,
505 u_long block_count
, u_long
* incr_block_offset
,
506 u_long
* incr_block_count
)
509 band_number_t mapped_band
;
510 boolean_t shadow_grew
= FALSE
;
512 if (block_offset
>= map
->file_size_blocks
513 || (block_offset
+ block_count
) > map
->file_size_blocks
) {
514 printf("shadow_map_write: request (%ld, %ld) exceeds file size %ld\n",
515 block_offset
, block_count
, map
->file_size_blocks
);
516 *incr_block_count
= 0;
519 band_limit
= shadow_map_contiguous(map
, block_offset
, block_count
, TRUE
);
520 mapped_band
= map
->bands
[shadow_map_block_to_band(map
, block_offset
)];
521 *incr_block_offset
= mapped_band
* map
->blocks_per_band
522 + (block_offset
% map
->blocks_per_band
);
523 *incr_block_count
= band_limit
- block_offset
;
525 /* mark these blocks as written */
526 bitmap_set(map
->block_bitmap
, block_offset
, *incr_block_count
);
528 if (map
->next_band
> map
->shadow_size_bands
) {
529 map
->shadow_size_bands
= map
->next_band
;
532 return (shadow_grew
);
536 shadow_map_is_written(shadow_map_t
* map
, u_long block_offset
)
540 b
= bitmap_offset(block_offset
);
541 return ((map
->block_bitmap
[b
.byte
] & bit(b
.bit
)) ? TRUE
: FALSE
);
545 * Function: shadow_map_shadow_size
548 * To return the size of the shadow file in blocks.
551 shadow_map_shadow_size(shadow_map_t
* map
)
553 return (map
->shadow_size_bands
* map
->blocks_per_band
);
557 * Function: shadow_map_create
560 * Allocate the dynamic data for keeping track of the shadow dirty blocks
561 * and the band mapping table.
563 * NULL if an error occurred.
566 shadow_map_create(off_t file_size
, off_t shadow_size
,
567 u_long band_size
, u_long block_size
)
569 void * block_bitmap
= 0;
571 band_number_t
* bands
= 0;
575 if (band_size
== 0) {
576 band_size
= BAND_SIZE_DEFAULT
;
579 n_bands
= howmany(file_size
, band_size
);
580 if (n_bands
> (BAND_MAX
+ 1)) {
581 printf("file is too big: %ld > %d\n",
586 /* create a block bitmap, one bit per block */
587 bitmap_size
= block_bitmap_size(file_size
, block_size
);
588 block_bitmap
= my_malloc(bitmap_size
);
589 if (block_bitmap
== NULL
) {
590 printf("failed to allocate bitmap\n");
593 bzero(block_bitmap
, bitmap_size
);
595 /* get the band map */
596 bands
= (band_number_t
*)my_malloc(n_bands
* sizeof(band_number_t
));
598 printf("failed to allocate bands\n");
601 bzero(bands
, n_bands
* sizeof(band_number_t
));
603 map
= my_malloc(sizeof(*map
));
605 printf("failed to allocate map\n");
608 map
->blocks_per_band
= band_size
/ block_size
;
609 map
->block_bitmap
= block_bitmap
;
611 map
->file_size_blocks
= n_bands
* map
->blocks_per_band
;
613 map
->zeroth_band
= -1;
614 map
->shadow_size_bands
= howmany(shadow_size
, band_size
);
615 map
->block_size
= block_size
;
620 my_free(block_bitmap
);
627 * Function: shadow_map_free
629 * Frees the data structure to deal with the shadow map.
632 shadow_map_free(shadow_map_t
* map
)
634 if (map
->block_bitmap
)
635 my_free(map
->block_bitmap
);
638 map
->block_bitmap
= 0;
645 #define BAND_SIZE_BLOCKS (BAND_SIZE_DEFAULT / 512)
663 block_request_t requests
[] = {
664 { WriteRequest
, BAND_SIZE_BLOCKS
* 2, 1 },
665 { ReadRequest
, BAND_SIZE_BLOCKS
/ 2, BAND_SIZE_BLOCKS
* 2 - 2 },
666 { WriteRequest
, BAND_SIZE_BLOCKS
* 1, 5 * BAND_SIZE_BLOCKS
+ 3},
667 { ReadRequest
, 0, BAND_SIZE_BLOCKS
* 10 },
668 { WriteRequest
, BAND_SIZE_BLOCKS
* (BAND_MAX
- 1),
669 BAND_SIZE_BLOCKS
* 2},
673 map
= shadow_map_create(1024 * 1024 * 1024 * 8ULL, 0, 0, 512);
675 printf("shadow_map_create failed\n");
678 for (i
= 0; TRUE
; i
++) {
681 boolean_t shadow_grew
;
682 boolean_t read_shadow
;
684 if (requests
[i
].count
== 0) {
687 offset
= requests
[i
].offset
;
688 resid
= requests
[i
].count
;
689 printf("\n%s REQUEST (%ld, %ld)\n",
690 requests
[i
].type
== WriteRequest
? "WRITE" : "READ",
692 switch (requests
[i
].type
) {
698 shadow_grew
= shadow_map_write(map
, offset
,
702 printf("\t(%ld, %ld) => (%ld, %ld)",
703 offset
, resid
, this_offset
, this_count
);
705 offset
+= this_count
;
707 printf(" shadow grew to %ld", shadow_map_shadow_size(map
));
717 read_shadow
= shadow_map_read(map
, offset
,
721 printf("\t(%ld, %ld) => (%ld, %ld)%s\n",
722 offset
, resid
, this_offset
, this_count
,
723 read_shadow
? " from shadow" : "");
724 if (this_count
== 0) {
725 printf("this_count is 0, aborting\n");
729 offset
+= this_count
;
737 shadow_map_free(map
);