Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rs_bitmap.c --- routine for changing the size of a bitmap |
| 3 | * |
| 4 | * Copyright (C) 1996, 1997 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #include <time.h> |
Theodore Ts'o | e72fdc6 | 1997-10-20 00:33:00 +0000 | [diff] [blame] | 19 | #ifdef HAVE_SYS_STAT_H |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Theodore Ts'o | e72fdc6 | 1997-10-20 00:33:00 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #ifdef HAVE_SYS_TYPES_H |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
Theodore Ts'o | e72fdc6 | 1997-10-20 00:33:00 +0000 | [diff] [blame] | 24 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 26 | #if EXT2_FLAT_INCLUDES |
| 27 | #include "ext2_fs.h" |
| 28 | #else |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 29 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 30 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 31 | |
| 32 | #include "ext2fs.h" |
| 33 | |
| 34 | errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end, |
| 35 | ext2fs_generic_bitmap bmap) |
| 36 | { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 37 | errcode_t retval; |
| 38 | size_t size, new_size; |
| 39 | __u32 bitno; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 40 | |
| 41 | if (!bmap) |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 42 | return EXT2_ET_INVALID_ARGUMENT; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 43 | |
| 44 | EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * If we're expanding the bitmap, make sure all of the new |
| 48 | * parts of the bitmap are zero. |
| 49 | */ |
| 50 | if (new_end > bmap->end) { |
| 51 | bitno = bmap->real_end; |
| 52 | if (bitno > new_end) |
| 53 | bitno = new_end; |
| 54 | for (; bitno > bmap->end; bitno--) |
| 55 | ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap); |
| 56 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 57 | if (new_real_end == bmap->real_end) { |
| 58 | bmap->end = new_end; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | size = ((bmap->real_end - bmap->start) / 8) + 1; |
| 63 | new_size = ((new_real_end - bmap->start) / 8) + 1; |
| 64 | |
Theodore Ts'o | 76f875d | 1998-04-27 01:41:13 +0000 | [diff] [blame] | 65 | if (size != new_size) { |
| 66 | retval = ext2fs_resize_mem(size, new_size, |
| 67 | (void **) &bmap->bitmap); |
| 68 | if (retval) |
| 69 | return retval; |
| 70 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 71 | if (new_size > size) |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 72 | memset(bmap->bitmap + size, 0, new_size - size); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 73 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 74 | bmap->end = new_end; |
| 75 | bmap->real_end = new_real_end; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end, |
| 80 | ext2fs_inode_bitmap bmap) |
| 81 | { |
| 82 | errcode_t retval; |
| 83 | |
| 84 | if (!bmap) |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 85 | return EXT2_ET_INVALID_ARGUMENT; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 86 | |
| 87 | EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP); |
| 88 | |
| 89 | bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP; |
| 90 | retval = ext2fs_resize_generic_bitmap(new_end, new_real_end, |
| 91 | bmap); |
| 92 | bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP; |
| 93 | return retval; |
| 94 | } |
| 95 | |
| 96 | errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end, |
| 97 | ext2fs_block_bitmap bmap) |
| 98 | { |
| 99 | errcode_t retval; |
| 100 | |
| 101 | if (!bmap) |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 102 | return EXT2_ET_INVALID_ARGUMENT; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 103 | |
| 104 | EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP); |
| 105 | |
| 106 | bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP; |
| 107 | retval = ext2fs_resize_generic_bitmap(new_end, new_real_end, |
| 108 | bmap); |
| 109 | bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP; |
| 110 | return retval; |
| 111 | } |
| 112 | |