2 * gen_bitmap64.c --- routines to read, write, and manipulate the new qinode and
5 * Copyright (C) 2007, 2008 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
26 #include <sys/types.h>
28 #ifdef HAVE_SYS_TIME_H
37 * Design of 64-bit bitmaps
39 * In order maintain ABI compatibility with programs that don't
40 * understand about 64-bit blocks/inodes,
41 * ext2fs_allocate_inode_bitmap() and ext2fs_allocate_block_bitmap()
42 * will create old-style bitmaps unless the application passes the
43 * flag EXT2_FLAG_64BITS to ext2fs_open(). If this flag is
44 * passed, then we know the application has been recompiled, so we can
45 * use the new-style bitmaps. If it is not passed, we have to return
46 * an error if trying to open a filesystem which needs 64-bit bitmaps.
48 * The new bitmaps use a new set of structure magic numbers, so that
49 * both the old-style and new-style interfaces can identify which
50 * version of the data structure was used. Both the old-style and
51 * new-style interfaces will support either type of bitmap, although
52 * of course 64-bit operation will only be possible when both the
53 * new-style interface and the new-style bitmap are used.
55 * For example, the new bitmap interfaces will check the structure
56 * magic numbers and so will be able to detect old-stype bitmap. If
57 * they see an old-style bitmap, they will pass it to the gen_bitmap.c
58 * functions for handling. The same will be true for the old
61 * The new-style interfaces will have several different back-end
62 * implementations, so we can support different encodings that are
63 * appropriate for different applications. In general the default
64 * should be whatever makes sense, and what the application/library
65 * will use. However, e2fsck may need specialized implementations for
66 * its own uses. For example, when doing parent directory pointer
67 * loop detections in pass 3, the bitmap will *always* be sparse, so
68 * e2fsck can request an encoding which is optimized for that.
71 static void warn_bitmap(ext2fs_generic_bitmap bitmap,
75 if (bitmap->description)
76 com_err(0, bitmap->base_error_code+code,
77 "#%llu for %s", arg, bitmap->description);
79 com_err(0, bitmap->base_error_code + code, "#%llu", arg);
83 #ifdef ENABLE_BMAP_STATS_OPS
84 #define INC_STAT(map, name) map->stats.name
86 #define INC_STAT(map, name) ;;
90 errcode_t ext2fs_alloc_generic_bmap(ext2_filsys fs, errcode_t magic,
91 int type, __u64 start, __u64 end,
94 ext2fs_generic_bitmap *ret)
96 ext2fs_generic_bitmap bitmap;
97 struct ext2_bitmap_ops *ops;
102 type = EXT2FS_BMAP64_BITARRAY;
105 case EXT2FS_BMAP64_BITARRAY:
106 ops = &ext2fs_blkmap64_bitarray;
108 case EXT2FS_BMAP64_RBTREE:
109 ops = &ext2fs_blkmap64_rbtree;
111 case EXT2FS_BMAP64_AUTODIR:
112 retval = ext2fs_get_num_dirs(fs, &num_dirs);
113 if (retval || num_dirs > (fs->super->s_inodes_count / 320))
114 ops = &ext2fs_blkmap64_bitarray;
116 ops = &ext2fs_blkmap64_rbtree;
122 retval = ext2fs_get_memzero(sizeof(struct ext2fs_struct_generic_bitmap),
127 #ifdef ENABLE_BMAP_STATS
128 if (gettimeofday(&bitmap->stats.created,
129 (struct timezone *) NULL) == -1) {
130 perror("gettimeofday");
133 bitmap->stats.type = type;
136 /* XXX factor out, repeated in copy_bmap */
137 bitmap->magic = magic;
139 bitmap->start = start;
141 bitmap->real_end = real_end;
142 bitmap->bitmap_ops = ops;
143 bitmap->cluster_bits = 0;
145 case EXT2_ET_MAGIC_INODE_BITMAP64:
146 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
148 case EXT2_ET_MAGIC_BLOCK_BITMAP64:
149 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
150 bitmap->cluster_bits = fs->cluster_ratio_bits;
153 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
156 retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
158 ext2fs_free_mem(&bitmap);
161 strcpy(bitmap->description, descr);
163 bitmap->description = 0;
165 retval = bitmap->bitmap_ops->new_bmap(fs, bitmap);
167 ext2fs_free_mem(&bitmap->description);
168 ext2fs_free_mem(&bitmap);
176 #ifdef ENABLE_BMAP_STATS
177 void ext2fs_print_bmap_statistics(ext2fs_generic_bitmap bitmap)
179 struct ext2_bmap_statistics *stats = &bitmap->stats;
180 #ifdef ENABLE_BMAP_STATS_OPS
181 float mark_seq_perc = 0.0, test_seq_perc = 0.0;
182 float mark_back_perc = 0.0, test_back_perc = 0.0;
187 #ifdef ENABLE_BMAP_STATS_OPS
188 if (stats->test_count) {
189 test_seq_perc = ((float)stats->test_seq /
190 stats->test_count) * 100;
191 test_back_perc = ((float)stats->test_back /
192 stats->test_count) * 100;
195 if (stats->mark_count) {
196 mark_seq_perc = ((float)stats->mark_seq /
197 stats->mark_count) * 100;
198 mark_back_perc = ((float)stats->mark_back /
199 stats->mark_count) * 100;
203 if (gettimeofday(&now, (struct timezone *) NULL) == -1) {
204 perror("gettimeofday");
208 inuse = (double) now.tv_sec + \
209 (((double) now.tv_usec) * 0.000001);
210 inuse -= (double) stats->created.tv_sec + \
211 (((double) stats->created.tv_usec) * 0.000001);
213 fprintf(stderr, "\n[+] %s bitmap (type %d)\n", bitmap->description,
215 fprintf(stderr, "=================================================\n");
216 #ifdef ENABLE_BMAP_STATS_OPS
217 fprintf(stderr, "%16llu bits long\n",
218 bitmap->real_end - bitmap->start);
219 fprintf(stderr, "%16lu copy_bmap\n%16lu resize_bmap\n",
220 stats->copy_count, stats->resize_count);
221 fprintf(stderr, "%16lu mark bmap\n%16lu unmark_bmap\n",
222 stats->mark_count, stats->unmark_count);
223 fprintf(stderr, "%16lu test_bmap\n%16lu mark_bmap_extent\n",
224 stats->test_count, stats->mark_ext_count);
225 fprintf(stderr, "%16lu unmark_bmap_extent\n"
226 "%16lu test_clear_bmap_extent\n",
227 stats->unmark_ext_count, stats->test_ext_count);
228 fprintf(stderr, "%16lu set_bmap_range\n%16lu set_bmap_range\n",
229 stats->set_range_count, stats->get_range_count);
230 fprintf(stderr, "%16lu clear_bmap\n%16lu contiguous bit test (%.2f%%)\n",
231 stats->clear_count, stats->test_seq, test_seq_perc);
232 fprintf(stderr, "%16lu contiguous bit mark (%.2f%%)\n"
233 "%16llu bits tested backwards (%.2f%%)\n",
234 stats->mark_seq, mark_seq_perc,
235 stats->test_back, test_back_perc);
236 fprintf(stderr, "%16llu bits marked backwards (%.2f%%)\n"
237 "%16.2f seconds in use\n",
238 stats->mark_back, mark_back_perc, inuse);
239 #endif /* ENABLE_BMAP_STATS_OPS */
243 void ext2fs_free_generic_bmap(ext2fs_generic_bitmap bmap)
248 if (EXT2FS_IS_32_BITMAP(bmap)) {
249 ext2fs_free_generic_bitmap(bmap);
253 if (!EXT2FS_IS_64_BITMAP(bmap))
256 #ifdef ENABLE_BMAP_STATS
257 if (getenv("E2FSPROGS_BITMAP_STATS")) {
258 ext2fs_print_bmap_statistics(bmap);
259 bmap->bitmap_ops->print_stats(bmap);
263 bmap->bitmap_ops->free_bmap(bmap);
265 if (bmap->description) {
266 ext2fs_free_mem(&bmap->description);
267 bmap->description = 0;
270 ext2fs_free_mem(&bmap);
273 errcode_t ext2fs_copy_generic_bmap(ext2fs_generic_bitmap src,
274 ext2fs_generic_bitmap *dest)
276 char *descr, *new_descr;
277 ext2fs_generic_bitmap new_bmap;
283 if (EXT2FS_IS_32_BITMAP(src))
284 return ext2fs_copy_generic_bitmap(src, dest);
286 if (!EXT2FS_IS_64_BITMAP(src))
289 /* Allocate a new bitmap struct */
290 retval = ext2fs_get_memzero(sizeof(struct ext2fs_struct_generic_bitmap),
296 #ifdef ENABLE_BMAP_STATS_OPS
297 src->stats.copy_count++;
299 #ifdef ENABLE_BMAP_STATS
300 if (gettimeofday(&new_bmap->stats.created,
301 (struct timezone *) NULL) == -1) {
302 perror("gettimeofday");
305 new_bmap->stats.type = src->stats.type;
308 /* Copy all the high-level parts over */
309 new_bmap->magic = src->magic;
310 new_bmap->fs = src->fs;
311 new_bmap->start = src->start;
312 new_bmap->end = src->end;
313 new_bmap->real_end = src->real_end;
314 new_bmap->bitmap_ops = src->bitmap_ops;
315 new_bmap->base_error_code = src->base_error_code;
316 new_bmap->cluster_bits = src->cluster_bits;
318 descr = src->description;
320 retval = ext2fs_get_mem(strlen(descr)+10, &new_descr);
322 ext2fs_free_mem(&new_bmap);
325 strcpy(new_descr, "copy of ");
326 strcat(new_descr, descr);
327 new_bmap->description = new_descr;
330 retval = src->bitmap_ops->copy_bmap(src, new_bmap);
332 ext2fs_free_mem(&new_bmap->description);
333 ext2fs_free_mem(&new_bmap);
342 errcode_t ext2fs_resize_generic_bmap(ext2fs_generic_bitmap bmap,
349 if (EXT2FS_IS_32_BITMAP(bmap))
350 return ext2fs_resize_generic_bitmap(bmap->magic, new_end,
353 if (!EXT2FS_IS_64_BITMAP(bmap))
356 INC_STAT(bmap, resize_count);
358 return bmap->bitmap_ops->resize_bmap(bmap, new_end, new_real_end);
361 errcode_t ext2fs_fudge_generic_bmap_end(ext2fs_generic_bitmap bitmap,
363 __u64 end, __u64 *oend)
368 if (EXT2FS_IS_32_BITMAP(bitmap)) {
372 retval = ext2fs_fudge_generic_bitmap_end(bitmap, bitmap->magic,
373 neq, end, &tmp_oend);
379 if (!EXT2FS_IS_64_BITMAP(bitmap))
382 if (end > bitmap->real_end)
390 __u64 ext2fs_get_generic_bmap_start(ext2fs_generic_bitmap bitmap)
395 if (EXT2FS_IS_32_BITMAP(bitmap))
396 return ext2fs_get_generic_bitmap_start(bitmap);
398 if (!EXT2FS_IS_64_BITMAP(bitmap))
401 return bitmap->start;
404 __u64 ext2fs_get_generic_bmap_end(ext2fs_generic_bitmap bitmap)
409 if (EXT2FS_IS_32_BITMAP(bitmap))
410 return ext2fs_get_generic_bitmap_end(bitmap);
412 if (!EXT2FS_IS_64_BITMAP(bitmap))
418 void ext2fs_clear_generic_bmap(ext2fs_generic_bitmap bitmap)
420 if (EXT2FS_IS_32_BITMAP(bitmap))
421 ext2fs_clear_generic_bitmap(bitmap);
423 bitmap->bitmap_ops->clear_bmap (bitmap);
426 int ext2fs_mark_generic_bmap(ext2fs_generic_bitmap bitmap,
432 if (EXT2FS_IS_32_BITMAP(bitmap)) {
433 if (arg & ~0xffffffffULL) {
434 ext2fs_warn_bitmap2(bitmap,
435 EXT2FS_MARK_ERROR, 0xffffffff);
438 return ext2fs_mark_generic_bitmap(bitmap, arg);
441 if (!EXT2FS_IS_64_BITMAP(bitmap))
444 arg >>= bitmap->cluster_bits;
446 #ifdef ENABLE_BMAP_STATS_OPS
447 if (arg == bitmap->stats.last_marked + 1)
448 bitmap->stats.mark_seq++;
449 if (arg < bitmap->stats.last_marked)
450 bitmap->stats.mark_back++;
451 bitmap->stats.last_marked = arg;
452 bitmap->stats.mark_count++;
455 if ((arg < bitmap->start) || (arg > bitmap->end)) {
456 warn_bitmap(bitmap, EXT2FS_MARK_ERROR, arg);
460 return bitmap->bitmap_ops->mark_bmap(bitmap, arg);
463 int ext2fs_unmark_generic_bmap(ext2fs_generic_bitmap bitmap,
469 if (EXT2FS_IS_32_BITMAP(bitmap)) {
470 if (arg & ~0xffffffffULL) {
471 ext2fs_warn_bitmap2(bitmap, EXT2FS_UNMARK_ERROR,
475 return ext2fs_unmark_generic_bitmap(bitmap, arg);
478 if (!EXT2FS_IS_64_BITMAP(bitmap))
481 arg >>= bitmap->cluster_bits;
483 INC_STAT(bitmap, unmark_count);
485 if ((arg < bitmap->start) || (arg > bitmap->end)) {
486 warn_bitmap(bitmap, EXT2FS_UNMARK_ERROR, arg);
490 return bitmap->bitmap_ops->unmark_bmap(bitmap, arg);
493 int ext2fs_test_generic_bmap(ext2fs_generic_bitmap bitmap,
499 if (EXT2FS_IS_32_BITMAP(bitmap)) {
500 if (arg & ~0xffffffffULL) {
501 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR,
505 return ext2fs_test_generic_bitmap(bitmap, arg);
508 if (!EXT2FS_IS_64_BITMAP(bitmap))
511 arg >>= bitmap->cluster_bits;
513 #ifdef ENABLE_BMAP_STATS_OPS
514 bitmap->stats.test_count++;
515 if (arg == bitmap->stats.last_tested + 1)
516 bitmap->stats.test_seq++;
517 if (arg < bitmap->stats.last_tested)
518 bitmap->stats.test_back++;
519 bitmap->stats.last_tested = arg;
522 if ((arg < bitmap->start) || (arg > bitmap->end)) {
523 warn_bitmap(bitmap, EXT2FS_TEST_ERROR, arg);
527 return bitmap->bitmap_ops->test_bmap(bitmap, arg);
530 errcode_t ext2fs_set_generic_bmap_range(ext2fs_generic_bitmap bmap,
531 __u64 start, unsigned int num,
537 if (EXT2FS_IS_32_BITMAP(bmap)) {
538 if ((start+num-1) & ~0xffffffffULL) {
539 ext2fs_warn_bitmap2(bmap, EXT2FS_UNMARK_ERROR,
543 return ext2fs_set_generic_bitmap_range(bmap, bmap->magic,
547 if (!EXT2FS_IS_64_BITMAP(bmap))
550 INC_STAT(bmap, set_range_count);
552 return bmap->bitmap_ops->set_bmap_range(bmap, start, num, in);
555 errcode_t ext2fs_get_generic_bmap_range(ext2fs_generic_bitmap bmap,
556 __u64 start, unsigned int num,
562 if (EXT2FS_IS_32_BITMAP(bmap)) {
563 if ((start+num-1) & ~0xffffffffULL) {
564 ext2fs_warn_bitmap2(bmap,
565 EXT2FS_UNMARK_ERROR, 0xffffffff);
568 return ext2fs_get_generic_bitmap_range(bmap, bmap->magic,
572 if (!EXT2FS_IS_64_BITMAP(bmap))
575 INC_STAT(bmap, get_range_count);
577 return bmap->bitmap_ops->get_bmap_range(bmap, start, num, out);
580 errcode_t ext2fs_compare_generic_bmap(errcode_t neq,
581 ext2fs_generic_bitmap bm1,
582 ext2fs_generic_bitmap bm2)
588 if (bm1->magic != bm2->magic)
591 /* Now we know both bitmaps have the same magic */
592 if (EXT2FS_IS_32_BITMAP(bm1))
593 return ext2fs_compare_generic_bitmap(bm1->magic, neq, bm1, bm2);
595 if (!EXT2FS_IS_64_BITMAP(bm1))
598 if ((bm1->start != bm2->start) ||
599 (bm1->end != bm2->end))
602 for (i = bm1->end - ((bm1->end - bm1->start) % 8); i <= bm1->end; i++)
603 if (ext2fs_test_generic_bmap(bm1, i) !=
604 ext2fs_test_generic_bmap(bm2, i))
610 void ext2fs_set_generic_bmap_padding(ext2fs_generic_bitmap bmap)
614 if (EXT2FS_IS_32_BITMAP(bmap)) {
615 ext2fs_set_generic_bitmap_padding(bmap);
619 start = bmap->end + 1;
620 num = bmap->real_end - bmap->end;
621 bmap->bitmap_ops->mark_bmap_extent(bmap, start, num);
622 /* XXX ought to warn on error */
625 int ext2fs_test_block_bitmap_range2(ext2fs_block_bitmap bmap,
626 blk64_t block, unsigned int num)
632 return !ext2fs_test_generic_bmap((ext2fs_generic_bitmap)
635 if (EXT2FS_IS_32_BITMAP(bmap)) {
636 if ((block+num-1) & ~0xffffffffULL) {
637 ext2fs_warn_bitmap2((ext2fs_generic_bitmap) bmap,
638 EXT2FS_UNMARK_ERROR, 0xffffffff);
641 return ext2fs_test_block_bitmap_range(
642 (ext2fs_generic_bitmap) bmap, block, num);
645 if (!EXT2FS_IS_64_BITMAP(bmap))
648 INC_STAT(bmap, test_ext_count);
650 return bmap->bitmap_ops->test_clear_bmap_extent(bmap, block, num);
653 void ext2fs_mark_block_bitmap_range2(ext2fs_block_bitmap bmap,
654 blk64_t block, unsigned int num)
659 if (EXT2FS_IS_32_BITMAP(bmap)) {
660 if ((block+num-1) & ~0xffffffffULL) {
661 ext2fs_warn_bitmap2((ext2fs_generic_bitmap) bmap,
662 EXT2FS_UNMARK_ERROR, 0xffffffff);
665 ext2fs_mark_block_bitmap_range((ext2fs_generic_bitmap) bmap,
669 if (!EXT2FS_IS_64_BITMAP(bmap))
672 INC_STAT(bmap, mark_ext_count);
674 if ((block < bmap->start) || (block+num-1 > bmap->end)) {
675 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
680 bmap->bitmap_ops->mark_bmap_extent(bmap, block, num);
683 void ext2fs_unmark_block_bitmap_range2(ext2fs_block_bitmap bmap,
684 blk64_t block, unsigned int num)
689 if (EXT2FS_IS_32_BITMAP(bmap)) {
690 if ((block+num-1) & ~0xffffffffULL) {
691 ext2fs_warn_bitmap2((ext2fs_generic_bitmap) bmap,
692 EXT2FS_UNMARK_ERROR, 0xffffffff);
695 ext2fs_unmark_block_bitmap_range((ext2fs_generic_bitmap) bmap,
699 if (!EXT2FS_IS_64_BITMAP(bmap))
702 INC_STAT(bmap, unmark_ext_count);
704 if ((block < bmap->start) || (block+num-1 > bmap->end)) {
705 ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
710 bmap->bitmap_ops->unmark_bmap_extent(bmap, block, num);
713 void ext2fs_warn_bitmap32(ext2fs_generic_bitmap bitmap, const char *func)
716 if (bitmap && bitmap->description)
717 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
718 "called %s with 64-bit bitmap for %s", func,
719 bitmap->description);
721 com_err(0, EXT2_ET_MAGIC_GENERIC_BITMAP,
722 "called %s with 64-bit bitmap", func);
726 errcode_t ext2fs_convert_subcluster_bitmap(ext2_filsys fs,
727 ext2fs_block_bitmap *bitmap)
729 ext2fs_block_bitmap cmap, bmap;
731 blk64_t i, b_end, c_end;
736 if (fs->cluster_ratio_bits == ext2fs_get_bitmap_granularity(bmap))
737 return 0; /* Nothing to do */
739 retval = ext2fs_allocate_block_bitmap(fs, "converted cluster bitmap",
746 bmap->end = bmap->real_end;
748 cmap->end = cmap->real_end;
750 ratio = 1 << fs->cluster_ratio_bits;
751 while (i < bmap->real_end) {
752 if (ext2fs_test_block_bitmap2(bmap, i)) {
753 ext2fs_mark_block_bitmap2(cmap, i);
764 ext2fs_free_block_bitmap(bmap);
769 errcode_t ext2fs_find_first_zero_generic_bmap(ext2fs_generic_bitmap bitmap,
770 __u64 start, __u64 end, __u64 *out)
777 if (EXT2FS_IS_64_BITMAP(bitmap) && bitmap->bitmap_ops->find_first_zero)
778 return bitmap->bitmap_ops->find_first_zero(bitmap, start,
781 if (EXT2FS_IS_32_BITMAP(bitmap)) {
785 if (((start) & ~0xffffffffULL) ||
786 ((end) & ~0xffffffffULL)) {
787 ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, start);
791 retval = ext2fs_find_first_zero_generic_bitmap(bitmap, start,
798 if (!EXT2FS_IS_64_BITMAP(bitmap))
801 start >>= bitmap->cluster_bits;
802 end >>= bitmap->cluster_bits;
804 if (start < bitmap->start || end > bitmap->end || start > end) {
805 warn_bitmap(bitmap, EXT2FS_TEST_ERROR, start);
809 while (start <= end) {
810 b = bitmap->bitmap_ops->test_bmap(bitmap, start);
812 *out = start << bitmap->cluster_bits;