3 * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * The contents of this file constitute Original Code as defined in and
8 * are subject to the Apple Public Source License Version 1.1 (the
9 * "License"). You may not use this file except in compliance with the
10 * License. Please obtain a copy of the License at
11 * http://www.apple.com/publicsource and read it before using this file.
13 * This Original Code and all software distributed under the License are
14 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
18 * License for the specific language governing rights and limitations
21 * @APPLE_LICENSE_HEADER_END@
27 * Implement copy-on-write shadow map to allow a disk image to be
28 * mounted read-only, yet be writable by transferring writes to a
29 * "shadow" file. Subsequent reads from blocks that have been
30 * written will then go the "shadow" file.
32 * The map has two parts:
33 * 1) a bit map to track which blocks have been written
34 * 2) a band map to map a "band" within the original file to a corresponding
35 * "band" in the shadow file. Each band has the same size.
37 * The band map is used to ensure that blocks that are contiguous in the
38 * original file will remain contiguous in the shadow file.
40 * For debugging purposes, this file can be compiled standalone using:
41 * cc -o shadow shadow.c -DTEST_SHADOW
45 * Modification History
47 * December 21, 2001 Dieter Siegmund (dieter@apple.com)
50 #include <sys/param.h>
51 #include <sys/types.h>
52 #include <mach/boolean.h>
59 #define my_malloc(a) malloc(a)
60 #define my_free(a) free(a)
62 #include <sys/malloc.h>
63 #define my_malloc(a) _MALLOC(a, M_TEMP, M_WAITOK)
64 #define my_free(a) FREE(a, M_TEMP)
69 #define ULONG_ALL_ONES ((u_long)(-1))
70 #define USHORT_ALL_ONES ((u_short)(-1))
71 #define UCHAR_ALL_ONES ((u_char)(-1))
73 #define my_trunc(value, divisor) ((value) / (divisor) * (divisor))
75 /* a band size of 128K can represent a file up to 8GB */
76 #define BAND_SIZE_DEFAULT_POWER_2 17
77 #define BAND_SIZE_DEFAULT (1 << BAND_SIZE_DEFAULT_POWER_2)
79 typedef u_short band_number_t
;
80 #define BAND_ZERO ((band_number_t)0)
81 #define BAND_MAX ((band_number_t)65535)
84 u_long blocks_per_band
;/* size in blocks */
86 u_char
* block_bitmap
; /* 1 bit per block; 1=written */
87 band_number_t
* bands
; /* band map array */
88 u_long file_size_blocks
; /* size of file in bands */
89 u_long shadow_size_bands
; /* size of shadow in bands */
90 u_long next_band
; /* next free band */
91 u_long zeroth_band
; /* special-case 0th band */
100 static __inline__ u_char
103 return ((u_char
)(1 << b
));
107 * Function: bits_lower
109 * Return a byte value in which bits numbered lower than 'b' are set.
111 static __inline__ u_char
114 return ((u_char
)(bit(b
) - 1));
118 * Function: byte_set_bits
120 * Set the given range of bits within a byte.
122 static __inline__ u_char
123 byte_set_bits(int start
, int end
)
125 return ((u_char
)((~bits_lower(start
)) & (bits_lower(end
) | bit(end
))));
128 static __inline__ bitmap_offset_t
129 bitmap_offset(off_t where
)
133 b
.byte
= where
/ NBBY
;
134 b
.bit
= where
% NBBY
;
139 * Function: bitmap_set
142 * Set the given range of bits.
144 * This algorithm tries to set the extents using the biggest
145 * units, using longs, then a short, then a byte, then bits.
148 bitmap_set(u_char
* map
, u_long start_bit
, u_long bit_count
)
150 bitmap_offset_t start
;
153 start
= bitmap_offset(start_bit
);
154 end
= bitmap_offset(start_bit
+ bit_count
);
155 if (start
.byte
< end
.byte
) {
159 map
[start
.byte
] |= byte_set_bits(start
.bit
, NBBY
- 1);
162 if (start
.byte
== end
.byte
)
166 n_bytes
= end
.byte
- start
.byte
;
168 while (n_bytes
>= (sizeof(u_long
))) {
169 *((u_long
*)(map
+ start
.byte
)) = ULONG_ALL_ONES
;
170 start
.byte
+= sizeof(u_long
);
171 n_bytes
-= sizeof(u_long
);
173 if (n_bytes
>= sizeof(u_short
)) {
174 *((u_short
*)(map
+ start
.byte
)) = USHORT_ALL_ONES
;
175 start
.byte
+= sizeof(u_short
);
176 n_bytes
-= sizeof(u_short
);
179 map
[start
.byte
] = UCHAR_ALL_ONES
;
186 if (end
.bit
> start
.bit
) {
187 map
[start
.byte
] |= byte_set_bits(start
.bit
, end
.bit
- 1);
194 * Function: bitmap_get
197 * Return the number of bits in the range that are the same e.g.
198 * 11101 returns 3 because the first 3 bits are the same (1's), whereas
199 * 001100 returns 2 because the first 2 bits are the same.
200 * This algorithm tries to count things in as big a chunk as possible,
201 * first aligning to a byte offset, then trying to count longs, a short,
202 * a byte, then any remaining bits to find the bit that is different.
206 bitmap_get(u_char
* map
, u_long start_bit
, u_long bit_count
,
207 boolean_t
* ret_is_set
)
212 bitmap_offset_t start
;
215 start
= bitmap_offset(start_bit
);
216 end
= bitmap_offset(start_bit
+ bit_count
);
218 is_set
= (map
[start
.byte
] & bit(start
.bit
)) ? TRUE
: FALSE
;
221 if (start
.byte
< end
.byte
) {
224 if (start
.bit
) { /* try to align to a byte */
225 for (i
= start
.bit
; i
< NBBY
; i
++) {
226 boolean_t this_is_set
;
228 this_is_set
= (map
[start
.byte
] & bit(i
)) ? TRUE
: FALSE
;
229 if (this_is_set
!= is_set
) {
230 goto done
; /* found bit that was different, we're done */
234 start
.bit
= 0; /* made it to the next byte */
236 if (start
.byte
== end
.byte
)
237 goto end
; /* no more bytes, check for any leftover bits */
239 /* calculate how many bytes are left in the range */
240 n_bytes
= end
.byte
- start
.byte
;
242 /* check for 4 bytes of the same bits */
243 while (n_bytes
>= sizeof(u_long
)) {
244 u_long
* valPtr
= (u_long
*)(map
+ start
.byte
);
245 if ((is_set
&& *valPtr
== ULONG_ALL_ONES
)
246 || (!is_set
&& *valPtr
== 0)) {
247 count
+= sizeof(*valPtr
) * NBBY
;
248 start
.byte
+= sizeof(*valPtr
);
249 n_bytes
-= sizeof(*valPtr
);
252 break; /* bits differ */
255 /* check for 2 bytes of the same bits */
256 if (n_bytes
>= sizeof(u_short
)) {
257 u_short
* valPtr
= (u_short
*)(map
+ start
.byte
);
259 if ((is_set
&& *valPtr
== USHORT_ALL_ONES
)
260 || (!is_set
&& (*valPtr
== 0))) {
261 count
+= sizeof(*valPtr
) * NBBY
;
262 start
.byte
+= sizeof(*valPtr
);
263 n_bytes
-= sizeof(*valPtr
);
267 /* check for 1 byte of the same bits */
269 if ((is_set
&& map
[start
.byte
] == UCHAR_ALL_ONES
)
270 || (!is_set
&& map
[start
.byte
] == 0)) {
275 /* we found bits that were different, find the first one */
277 for (i
= 0; i
< NBBY
; i
++) {
278 boolean_t this_is_set
;
280 this_is_set
= (map
[start
.byte
] & bit(i
)) ? TRUE
: FALSE
;
281 if (this_is_set
!= is_set
) {
292 for (i
= start
.bit
; i
< end
.bit
; i
++) {
293 boolean_t this_is_set
= (map
[start
.byte
] & bit(i
)) ? TRUE
: FALSE
;
295 if (this_is_set
!= is_set
) {
302 *ret_is_set
= is_set
;
306 static __inline__ band_number_t
307 shadow_map_block_to_band(shadow_map_t
* map
, unsigned long block
)
309 return (block
/ map
->blocks_per_band
);
313 * Function: shadow_map_mapped_band
315 * Return the mapped band for the given band.
316 * If map_it is FALSE, and the band is not mapped, return FALSE.
317 * If map_it is TRUE, then this function will always return TRUE.
320 shadow_map_mapped_band(shadow_map_t
* map
, band_number_t band
,
321 boolean_t map_it
, band_number_t
* mapped_band
)
323 boolean_t is_mapped
= FALSE
;
325 if (band
== map
->zeroth_band
) {
326 *mapped_band
= BAND_ZERO
;
330 *mapped_band
= map
->bands
[band
];
331 if (*mapped_band
== BAND_ZERO
) {
334 if (map
->next_band
== 0) {
335 /* remember the zero'th band */
336 map
->zeroth_band
= band
;
338 *mapped_band
= map
->bands
[band
] = map
->next_band
++;
350 * Function: shadow_map_contiguous
353 * Return the first offset within the range position..(position + count)
354 * that is not a contiguous mapped band.
356 * If called with is_write = TRUE, this function will map bands as it goes.
359 shadow_map_contiguous(shadow_map_t
* map
, u_long start_block
,
360 u_long num_blocks
, boolean_t is_write
)
362 band_number_t band
= shadow_map_block_to_band(map
, start_block
);
363 u_long end_block
= start_block
+ num_blocks
;
365 band_number_t mapped_band
;
366 u_long ret_end_block
= end_block
;
369 is_mapped
= shadow_map_mapped_band(map
, band
, is_write
, &mapped_band
);
370 if (is_write
== FALSE
&& is_mapped
== FALSE
) {
371 static int happened
= 0;
372 /* this can't happen */
374 printf("shadow_map_contiguous: this can't happen!\n");
377 return (start_block
);
379 for (p
= my_trunc(start_block
+ map
->blocks_per_band
,
380 map
->blocks_per_band
);
381 p
< end_block
; p
+= map
->blocks_per_band
) {
382 band_number_t next_mapped_band
;
385 is_mapped
= shadow_map_mapped_band(map
, band
, is_write
,
387 if (is_write
== FALSE
&& is_mapped
== FALSE
) {
390 if ((mapped_band
+ 1) != next_mapped_band
) {
395 mapped_band
= next_mapped_band
;
397 return (ret_end_block
);
402 * Function: block_bitmap_size
404 * The number of bytes required in a block bitmap to represent a file of size
407 * The bytes required is the number of blocks in the file,
408 * divided by the number of bits per byte.
410 * An 8GB file requires (assuming 512 byte block):
411 * 2^33 / 2^9 / 2^3 = 2^21 = 2MB
412 * of bitmap space. This is a non-trival amount of memory,
413 * particularly since most of the bits will be zero.
414 * A sparse bitmap would really help in this case.
416 static __inline__ u_long
417 block_bitmap_size(off_t file_size
, u_long block_size
)
419 off_t blocks
= howmany(file_size
, block_size
);
420 return (howmany(blocks
, NBBY
));
424 * Function: shadow_map_read
427 * Calculate the block offset within the shadow to read, and the number
428 * blocks to read. The input values (block_offset, block_count) refer
429 * to the original file.
431 * The output values (*incr_block_offset, *incr_block_count) refer to the
432 * shadow file if the return value is TRUE. They refer to the original
433 * file if the return value is FALSE.
435 * Blocks within a band may or may not have been written, in addition,
436 * Bands are not necessarily contiguous, therefore:
437 * *incr_block_count <= block_count
438 * The caller must be prepared to call this function interatively
439 * to complete the whole i/o.
441 * TRUE if the shadow file should be read, FALSE if the original file
445 shadow_map_read(shadow_map_t
* map
, u_long block_offset
, u_long block_count
,
446 u_long
* incr_block_offset
, u_long
* incr_block_count
)
448 boolean_t written
= FALSE
;
451 if (block_offset
>= map
->file_size_blocks
452 || (block_offset
+ block_count
) > map
->file_size_blocks
) {
453 printf("shadow_map_read: request (%ld, %ld) exceeds file size %ld\n",
454 block_offset
, block_count
, map
->file_size_blocks
);
455 *incr_block_count
= 0;
457 n_blocks
= bitmap_get(map
->block_bitmap
, block_offset
, block_count
,
459 if (written
== FALSE
) {
460 *incr_block_count
= n_blocks
;
461 *incr_block_offset
= block_offset
;
463 else { /* start has been written, and therefore mapped */
464 band_number_t mapped_band
;
467 mapped_band
= map
->bands
[shadow_map_block_to_band(map
, block_offset
)];
468 *incr_block_offset
= mapped_band
* map
->blocks_per_band
469 + (block_offset
% map
->blocks_per_band
);
471 = shadow_map_contiguous(map
, block_offset
, block_count
, FALSE
);
472 *incr_block_count
= band_limit
- block_offset
;
473 if (*incr_block_count
> n_blocks
) {
474 *incr_block_count
= n_blocks
;
481 * Function: shadow_map_write
484 * Calculate the block offset within the shadow to write, and the number
485 * blocks to write. The input values (block_offset, block_count) refer
486 * to the original file. The output values
487 * (*incr_block_offset, *incr_block_count) refer to the shadow file.
489 * Bands are not necessarily contiguous, therefore:
490 * *incr_block_count <= block_count
491 * The caller must be prepared to call this function interatively
492 * to complete the whole i/o.
494 * TRUE if the shadow file was grown, FALSE otherwise.
497 shadow_map_write(shadow_map_t
* map
, u_long block_offset
,
498 u_long block_count
, u_long
* incr_block_offset
,
499 u_long
* incr_block_count
)
502 band_number_t mapped_band
;
503 boolean_t shadow_grew
= FALSE
;
505 if (block_offset
>= map
->file_size_blocks
506 || (block_offset
+ block_count
) > map
->file_size_blocks
) {
507 printf("shadow_map_write: request (%ld, %ld) exceeds file size %ld\n",
508 block_offset
, block_count
, map
->file_size_blocks
);
509 *incr_block_count
= 0;
512 band_limit
= shadow_map_contiguous(map
, block_offset
, block_count
, TRUE
);
513 mapped_band
= map
->bands
[shadow_map_block_to_band(map
, block_offset
)];
514 *incr_block_offset
= mapped_band
* map
->blocks_per_band
515 + (block_offset
% map
->blocks_per_band
);
516 *incr_block_count
= band_limit
- block_offset
;
518 /* mark these blocks as written */
519 bitmap_set(map
->block_bitmap
, block_offset
, *incr_block_count
);
521 if (map
->next_band
> map
->shadow_size_bands
) {
522 map
->shadow_size_bands
= map
->next_band
;
525 return (shadow_grew
);
529 * Function: shadow_map_shadow_size
532 * To return the size of the shadow file in blocks.
535 shadow_map_shadow_size(shadow_map_t
* map
)
537 return (map
->shadow_size_bands
* map
->blocks_per_band
);
541 * Function: shadow_map_create
544 * Allocate the dynamic data for keeping track of the shadow dirty blocks
545 * and the band mapping table.
547 * NULL if an error occurred.
550 shadow_map_create(off_t file_size
, off_t shadow_size
,
551 u_long band_size
, u_long block_size
)
553 void * block_bitmap
= 0;
555 band_number_t
* bands
= 0;
559 if (band_size
== 0) {
560 band_size
= BAND_SIZE_DEFAULT
;
563 n_bands
= howmany(file_size
, band_size
);
564 if (n_bands
> (BAND_MAX
+ 1)) {
565 printf("file is too big: %ld > %d\n",
570 /* create a block bitmap, one bit per block */
571 bitmap_size
= block_bitmap_size(file_size
, block_size
);
572 block_bitmap
= my_malloc(bitmap_size
);
573 if (block_bitmap
== NULL
) {
574 printf("failed to allocate bitmap\n");
577 bzero(block_bitmap
, bitmap_size
);
579 /* get the band map */
580 bands
= (band_number_t
*)my_malloc(n_bands
* sizeof(band_number_t
));
582 printf("failed to allocate bands\n");
585 bzero(bands
, n_bands
* sizeof(band_number_t
));
587 map
= my_malloc(sizeof(*map
));
589 printf("failed to allocate map\n");
592 map
->blocks_per_band
= band_size
/ block_size
;
593 map
->block_bitmap
= block_bitmap
;
595 map
->file_size_blocks
= n_bands
* map
->blocks_per_band
;
597 map
->zeroth_band
= -1;
598 map
->shadow_size_bands
= howmany(shadow_size
, band_size
);
599 map
->block_size
= block_size
;
604 my_free(block_bitmap
);
611 * Function: shadow_map_free
613 * Frees the data structure to deal with the shadow map.
616 shadow_map_free(shadow_map_t
* map
)
618 if (map
->block_bitmap
)
619 my_free(map
->block_bitmap
);
622 map
->block_bitmap
= 0;
629 #define BAND_SIZE_BLOCKS (BAND_SIZE_DEFAULT / 512)
647 block_request_t requests
[] = {
648 { WriteRequest
, BAND_SIZE_BLOCKS
* 2, 1 },
649 { ReadRequest
, BAND_SIZE_BLOCKS
/ 2, BAND_SIZE_BLOCKS
* 2 - 2 },
650 { WriteRequest
, BAND_SIZE_BLOCKS
* 1, 5 * BAND_SIZE_BLOCKS
+ 3},
651 { ReadRequest
, 0, BAND_SIZE_BLOCKS
* 10 },
652 { WriteRequest
, BAND_SIZE_BLOCKS
* (BAND_MAX
- 1),
653 BAND_SIZE_BLOCKS
* 2},
657 map
= shadow_map_create(1024 * 1024 * 1024 * 8ULL, 0, 0, 512);
659 printf("shadow_map_create failed\n");
662 for (i
= 0; TRUE
; i
++) {
665 boolean_t shadow_grew
;
666 boolean_t read_shadow
;
668 if (requests
[i
].count
== 0) {
671 offset
= requests
[i
].offset
;
672 resid
= requests
[i
].count
;
673 printf("\n%s REQUEST (%ld, %ld)\n",
674 requests
[i
].type
== WriteRequest
? "WRITE" : "READ",
676 switch (requests
[i
].type
) {
682 shadow_grew
= shadow_map_write(map
, offset
,
686 printf("\t(%ld, %ld) => (%ld, %ld)",
687 offset
, resid
, this_offset
, this_count
);
689 offset
+= this_count
;
691 printf(" shadow grew to %ld", shadow_map_shadow_size(map
));
701 read_shadow
= shadow_map_read(map
, offset
,
705 printf("\t(%ld, %ld) => (%ld, %ld)%s\n",
706 offset
, resid
, this_offset
, this_count
,
707 read_shadow
? " from shadow" : "");
708 if (this_count
== 0) {
709 printf("this_count is 0, aborting\n");
713 offset
+= this_count
;
721 shadow_map_free(map
);