119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project/*
23984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * gen_bitmap.c --- Generic (32-bit) bitmap routines
33984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt *
419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * Copyright (C) 2001 Theodore Ts'o.
519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project *
619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * %Begin-Header%
78558eab78390d1924cd6b255686ceef133f294d5Ken Sumrall * This file may be redistributed under the terms of the GNU Library
88558eab78390d1924cd6b255686ceef133f294d5Ken Sumrall * General Public License, version 2.
919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * %End-Header%
1019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project */
1119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
1219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
1319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdio.h>
1419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <string.h>
1519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#if HAVE_UNISTD_H
1619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <unistd.h>
1719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif
1819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <fcntl.h>
1919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <time.h>
2019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#if HAVE_SYS_STAT_H
2119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <sys/stat.h>
2219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif
2319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#if HAVE_SYS_TYPES_H
2419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <sys/types.h>
2519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif
2619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
2719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include "ext2_fs.h"
2819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include "ext2fs.h"
2919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
303984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtstruct ext2fs_struct_generic_bitmap {
313984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	errcode_t	magic;
323984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	ext2_filsys 	fs;
333984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	__u32		start, end;
343984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	__u32		real_end;
353984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	char	*	description;
363984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	char	*	bitmap;
373984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	errcode_t	base_error_code;
383984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	__u32		reserved[7];
393984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt};
403984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
413984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt/*
423984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * Used by previously inlined function, so we have to export this and
433984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * not change the function signature
443984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt */
453984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtvoid ext2fs_warn_bitmap2(ext2fs_generic_bitmap bitmap,
463984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			    int code, unsigned long arg)
473984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
483984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt#ifndef OMIT_COM_ERR
493984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (bitmap->description)
503984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		com_err(0, bitmap->base_error_code+code,
513984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			"#%lu for %s", arg, bitmap->description);
523984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	else
533984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		com_err(0, bitmap->base_error_code + code, "#%lu", arg);
543984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt#endif
553984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
563984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
573984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtstatic errcode_t check_magic(ext2fs_generic_bitmap bitmap)
583984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
593984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (!bitmap || !((bitmap->magic == EXT2_ET_MAGIC_GENERIC_BITMAP) ||
603984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			 (bitmap->magic == EXT2_ET_MAGIC_INODE_BITMAP) ||
613984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			 (bitmap->magic == EXT2_ET_MAGIC_BLOCK_BITMAP)))
623984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return EXT2_ET_MAGIC_GENERIC_BITMAP;
633984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 0;
643984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
653984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
663984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_make_generic_bitmap(errcode_t magic, ext2_filsys fs,
673984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				     __u32 start, __u32 end, __u32 real_end,
683984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				     const char *descr, char *init_map,
693984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				     ext2fs_generic_bitmap *ret)
703984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
713984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	ext2fs_generic_bitmap	bitmap;
723984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	errcode_t		retval;
733984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	size_t			size;
743984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
753984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
763984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				&bitmap);
773984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (retval)
783984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return retval;
793984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
803984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bitmap->magic = magic;
813984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bitmap->fs = fs;
823984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bitmap->start = start;
833984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bitmap->end = end;
843984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bitmap->real_end = real_end;
853984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	switch (magic) {
863984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	case EXT2_ET_MAGIC_INODE_BITMAP:
873984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
883984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		break;
893984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	case EXT2_ET_MAGIC_BLOCK_BITMAP:
903984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
913984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		break;
923984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	default:
933984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
943984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
953984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (descr) {
963984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
973984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (retval) {
983984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			ext2fs_free_mem(&bitmap);
993984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return retval;
1003984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		}
1013984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		strcpy(bitmap->description, descr);
1023984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	} else
1033984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bitmap->description = 0;
1043984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1053984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
1063984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	/* Round up to allow for the BT x86 instruction */
1073984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	size = (size + 7) & ~3;
1083984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	retval = ext2fs_get_mem(size, &bitmap->bitmap);
1093984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (retval) {
1103984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_free_mem(&bitmap->description);
1113984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_free_mem(&bitmap);
1123984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return retval;
1133984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
1143984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1153984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (init_map)
1163984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		memcpy(bitmap->bitmap, init_map, size);
1173984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	else
1183984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		memset(bitmap->bitmap, 0, size);
1193984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	*ret = bitmap;
1203984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 0;
1213984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
1223984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1233984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_allocate_generic_bitmap(__u32 start,
1243984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					 __u32 end,
1253984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					 __u32 real_end,
1263984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					 const char *descr,
1273984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					 ext2fs_generic_bitmap *ret)
1283984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
1293984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_GENERIC_BITMAP, 0,
1303984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  start, end, real_end, descr, 0, ret);
1313984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
1323984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1333984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_copy_generic_bitmap(ext2fs_generic_bitmap src,
1343984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				     ext2fs_generic_bitmap *dest)
1353984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
1363984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return (ext2fs_make_generic_bitmap(src->magic, src->fs,
1373984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					   src->start, src->end,
1383984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					   src->real_end,
1393984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					   src->description, src->bitmap,
1403984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					   dest));
1413984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
1423984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1433984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtvoid ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap)
1443984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
1453984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (check_magic(bitmap))
1463984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return;
1473984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1483984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bitmap->magic = 0;
1493984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (bitmap->description) {
1503984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_free_mem(&bitmap->description);
1513984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bitmap->description = 0;
1523984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
1533984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (bitmap->bitmap) {
1543984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_free_mem(&bitmap->bitmap);
1553984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bitmap->bitmap = 0;
1563984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
1573984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	ext2fs_free_mem(&bitmap);
1583984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
1593984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1603984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtint ext2fs_test_generic_bitmap(ext2fs_generic_bitmap bitmap,
1613984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					blk_t bitno)
1623984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
1633984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
1643984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_warn_bitmap2(bitmap, EXT2FS_TEST_ERROR, bitno);
1653984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return 0;
1663984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
1673984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return ext2fs_test_bit(bitno - bitmap->start, bitmap->bitmap);
1683984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
1693984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
17019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectint ext2fs_mark_generic_bitmap(ext2fs_generic_bitmap bitmap,
17119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project					 __u32 bitno)
17219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{
17319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
17419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, bitno);
17519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		return 0;
17619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
17719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	return ext2fs_set_bit(bitno - bitmap->start, bitmap->bitmap);
17819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project}
17919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
18019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectint ext2fs_unmark_generic_bitmap(ext2fs_generic_bitmap bitmap,
18119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project					   blk_t bitno)
18219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{
18319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if ((bitno < bitmap->start) || (bitno > bitmap->end)) {
18419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		ext2fs_warn_bitmap2(bitmap, EXT2FS_UNMARK_ERROR, bitno);
18519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		return 0;
18619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
18719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	return ext2fs_clear_bit(bitno - bitmap->start, bitmap->bitmap);
18819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project}
1893984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1903984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt__u32 ext2fs_get_generic_bitmap_start(ext2fs_generic_bitmap bitmap)
1913984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
1923984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return bitmap->start;
1933984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
1943984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
1953984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt__u32 ext2fs_get_generic_bitmap_end(ext2fs_generic_bitmap bitmap)
1963984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
1973984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return bitmap->end;
1983984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
1993984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2003984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtvoid ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap)
2013984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
2023984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (check_magic(bitmap))
2033984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return;
2043984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2053984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	memset(bitmap->bitmap, 0,
2063984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	       (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
2073984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
2083984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2093984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_fudge_generic_bitmap_end(ext2fs_inode_bitmap bitmap,
2103984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  errcode_t magic, errcode_t neq,
2113984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  ext2_ino_t end, ext2_ino_t *oend)
2123984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
2133984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	EXT2_CHECK_MAGIC(bitmap, magic);
2143984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2153984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (end > bitmap->real_end)
2163984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return neq;
2173984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (oend)
2183984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		*oend = bitmap->end;
2193984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bitmap->end = end;
2203984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 0;
2213984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
2223984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2233984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_resize_generic_bitmap(errcode_t magic,
2243984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				       __u32 new_end, __u32 new_real_end,
2253984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				       ext2fs_generic_bitmap bmap)
2263984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
2273984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	errcode_t	retval;
2283984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	size_t		size, new_size;
2293984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	__u32		bitno;
2303984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2313984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (!bmap || (bmap->magic != magic))
2323984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return magic;
2333984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2343984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	/*
2353984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	 * If we're expanding the bitmap, make sure all of the new
2363984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	 * parts of the bitmap are zero.
2373984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	 */
2383984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (new_end > bmap->end) {
2393984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bitno = bmap->real_end;
2403984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (bitno > new_end)
2413984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			bitno = new_end;
2423984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		for (; bitno > bmap->end; bitno--)
2433984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
2443984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
2453984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (new_real_end == bmap->real_end) {
2463984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		bmap->end = new_end;
2473984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return 0;
2483984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
2493984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2503984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	size = ((bmap->real_end - bmap->start) / 8) + 1;
2513984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	new_size = ((new_real_end - bmap->start) / 8) + 1;
2523984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2533984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (size != new_size) {
2543984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		retval = ext2fs_resize_mem(size, new_size, &bmap->bitmap);
2553984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (retval)
2563984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return retval;
2573984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
2583984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (new_size > size)
2593984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		memset(bmap->bitmap + size, 0, new_size - size);
2603984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2613984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bmap->end = new_end;
2623984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	bmap->real_end = new_real_end;
2633984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 0;
2643984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
2653984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2663984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_compare_generic_bitmap(errcode_t magic, errcode_t neq,
2673984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					ext2fs_generic_bitmap bm1,
2683984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					ext2fs_generic_bitmap bm2)
2693984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
2703984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	blk_t	i;
2713984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2723984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (!bm1 || bm1->magic != magic)
2733984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return magic;
2743984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (!bm2 || bm2->magic != magic)
2753984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return magic;
2763984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2773984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((bm1->start != bm2->start) ||
2783984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	    (bm1->end != bm2->end) ||
2793984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	    (memcmp(bm1->bitmap, bm2->bitmap,
2803984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		    (size_t) (bm1->end - bm1->start)/8)))
2813984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return neq;
2823984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2833984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	for (i = bm1->end - ((bm1->end - bm1->start) % 8); i <= bm1->end; i++)
2843984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (ext2fs_fast_test_block_bitmap(bm1, i) !=
2853984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		    ext2fs_fast_test_block_bitmap(bm2, i))
2863984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return neq;
2873984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2883984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 0;
2893984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
2903984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2913984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtvoid ext2fs_set_generic_bitmap_padding(ext2fs_generic_bitmap map)
2923984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
2933984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	__u32	i, j;
2943984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
2953984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	/* Protect loop from wrap-around if map->real_end is maxed */
2963984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	for (i=map->end+1, j = i - map->start;
2973984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	     i <= map->real_end && i > map->end;
2983984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	     i++, j++)
2993984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_set_bit(j, map->bitmap);
3003984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
3013984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3023984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_get_generic_bitmap_range(ext2fs_generic_bitmap bmap,
3033984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  errcode_t magic,
3043984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  __u32 start, __u32 num,
3053984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  void *out)
3063984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
3073984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (!bmap || (bmap->magic != magic))
3083984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return magic;
3093984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3103984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((start < bmap->start) || (start+num-1 > bmap->real_end))
3113984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return EXT2_ET_INVALID_ARGUMENT;
3123984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3133984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	memcpy(out, bmap->bitmap + (start >> 3), (num+7) >> 3);
3143984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 0;
3153984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
3163984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3173984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidterrcode_t ext2fs_set_generic_bitmap_range(ext2fs_generic_bitmap bmap,
3183984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  errcode_t magic,
3193984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  __u32 start, __u32 num,
3203984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					  void *in)
3213984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
3223984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (!bmap || (bmap->magic != magic))
3233984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return magic;
3243984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3253984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((start < bmap->start) || (start+num-1 > bmap->real_end))
3263984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return EXT2_ET_INVALID_ARGUMENT;
3273984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3283984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	memcpy(bmap->bitmap + (start >> 3), in, (num+7) >> 3);
3293984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 0;
3303984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
3313984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3323984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt/*
3333984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * Compare @mem to zero buffer by 256 bytes.
3343984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * Return 1 if @mem is zeroed memory, otherwise return 0.
3353984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt */
3363984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtstatic int mem_is_zero(const char *mem, size_t len)
3373984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
3383984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	static const char zero_buf[256];
3393984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3403984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	while (len >= sizeof(zero_buf)) {
3413984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (memcmp(mem, zero_buf, sizeof(zero_buf)))
3423984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return 0;
3433984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		len -= sizeof(zero_buf);
3443984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		mem += sizeof(zero_buf);
3453984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
3463984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	/* Deal with leftover bytes. */
3473984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (len)
3483984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return !memcmp(mem, zero_buf, len);
3493984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return 1;
3503984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
3513984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3523984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt/*
3533984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * Return true if all of the bits in a specified range are clear
3543984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt */
3553984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtstatic int ext2fs_test_clear_generic_bitmap_range(ext2fs_generic_bitmap bitmap,
3563984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt						  unsigned int start,
3573984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt						  unsigned int len)
3583984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
3593984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	size_t start_byte, len_byte = len >> 3;
3603984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	unsigned int start_bit, len_bit = len % 8;
3613984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	int first_bit = 0;
3623984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	int last_bit  = 0;
3633984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	int mark_count = 0;
3643984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	int mark_bit = 0;
3653984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	int i;
3663984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	const char *ADDR = bitmap->bitmap;
3673984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3683984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	start -= bitmap->start;
3693984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	start_byte = start >> 3;
3703984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	start_bit = start % 8;
3713984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3723984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (start_bit != 0) {
3733984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		/*
3743984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * The compared start block number or start inode number
3753984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * is not the first bit in a byte.
3763984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 */
3773984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		mark_count = 8 - start_bit;
3783984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (len < 8 - start_bit) {
3793984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			mark_count = (int)len;
3803984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			mark_bit = len + start_bit - 1;
3813984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		} else
3823984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			mark_bit = 7;
3833984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3843984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		for (i = mark_count; i > 0; i--, mark_bit--)
3853984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			first_bit |= 1 << mark_bit;
3863984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3873984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		/*
3883984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * Compare blocks or inodes in the first byte.
3893984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * If there is any marked bit, this function returns 0.
3903984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 */
3913984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (first_bit & ADDR[start_byte])
3923984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return 0;
3933984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		else if (len <= 8 - start_bit)
3943984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return 1;
3953984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
3963984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		start_byte++;
3973984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		len_bit = (len - mark_count) % 8;
3983984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		len_byte = (len - mark_count) >> 3;
3993984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
4003984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4013984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	/*
4023984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	 * The compared start block number or start inode number is
4033984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	 * the first bit in a byte.
4043984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	 */
4053984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if (len_bit != 0) {
4063984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		/*
4073984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * The compared end block number or end inode number is
4083984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * not the last bit in a byte.
4093984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 */
4103984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		for (mark_bit = len_bit - 1; mark_bit >= 0; mark_bit--)
4113984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			last_bit |= 1 << mark_bit;
4123984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4133984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		/*
4143984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * Compare blocks or inodes in the last byte.
4153984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 * If there is any marked bit, this function returns 0.
4163984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		 */
4173984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		if (last_bit & ADDR[start_byte + len_byte])
4183984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return 0;
4193984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		else if (len_byte == 0)
4203984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt			return 1;
4213984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
4223984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4233984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	/* Check whether all bytes are 0 */
4243984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return mem_is_zero(ADDR + start_byte, len_byte);
4253984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
4263984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4273984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtint ext2fs_test_block_bitmap_range(ext2fs_block_bitmap bitmap,
4283984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				   blk_t block, int num)
4293984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
4303984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
4313984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((block < bitmap->start) || (block+num-1 > bitmap->real_end)) {
4323984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_TEST,
4333984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				   block, bitmap->description);
4343984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return 0;
4353984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
4363984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
4373984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt						      bitmap, block, num);
4383984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
4393984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4403984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtint ext2fs_test_inode_bitmap_range(ext2fs_inode_bitmap bitmap,
4413984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				   ino_t inode, int num)
4423984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
4433984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
4443984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((inode < bitmap->start) || (inode+num-1 > bitmap->real_end)) {
4453984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_warn_bitmap(EXT2_ET_BAD_INODE_TEST,
4463984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				   inode, bitmap->description);
4473984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return 0;
4483984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
4493984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	return ext2fs_test_clear_generic_bitmap_range((ext2fs_generic_bitmap)
4503984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt						      bitmap, inode, num);
4513984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
4523984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4533984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtvoid ext2fs_mark_block_bitmap_range(ext2fs_block_bitmap bitmap,
4543984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				    blk_t block, int num)
4553984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
4563984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	int	i;
4573984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4583984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
4593984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_MARK, block,
4603984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				   bitmap->description);
4613984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return;
4623984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
4633984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	for (i=0; i < num; i++)
4643984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_fast_set_bit(block + i - bitmap->start, bitmap->bitmap);
4653984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
4663984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4673984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidtvoid ext2fs_unmark_block_bitmap_range(ext2fs_block_bitmap bitmap,
4683984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt					       blk_t block, int num)
4693984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt{
4703984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	int	i;
4713984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt
4723984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	if ((block < bitmap->start) || (block+num-1 > bitmap->end)) {
4733984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_warn_bitmap(EXT2_ET_BAD_BLOCK_UNMARK, block,
4743984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				   bitmap->description);
4753984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		return;
4763984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	}
4773984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt	for (i=0; i < num; i++)
4783984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt		ext2fs_fast_clear_bit(block + i - bitmap->start,
4793984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt				      bitmap->bitmap);
4803984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt}
481