blob: 240994fe5e12242faa3550b06f14b3e6bd926e93 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
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'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000017#include <fcntl.h>
18#include <time.h>
Theodore Ts'oe72fdc61997-10-20 00:33:00 +000019#ifdef HAVE_SYS_STAT_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000020#include <sys/stat.h>
Theodore Ts'oe72fdc61997-10-20 00:33:00 +000021#endif
22#ifdef HAVE_SYS_TYPES_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000023#include <sys/types.h>
Theodore Ts'oe72fdc61997-10-20 00:33:00 +000024#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#if EXT2_FLAT_INCLUDES
27#include "ext2_fs.h"
28#else
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000029#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000030#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000031
32#include "ext2fs.h"
33
34errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
35 ext2fs_generic_bitmap bmap)
36{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000037 errcode_t retval;
38 size_t size, new_size;
39 __u32 bitno;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000040
41 if (!bmap)
Theodore Ts'o1f0b6c11997-10-31 06:07:47 +000042 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000043
44 EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
Theodore Ts'o1e1da291997-06-09 14:51:29 +000045
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'o19c78dc1997-04-29 16:17:09 +000057 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'o76f875d1998-04-27 01:41:13 +000065 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'o19c78dc1997-04-29 16:17:09 +000071 if (new_size > size)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000072 memset(bmap->bitmap + size, 0, new_size - size);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000073
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000074 bmap->end = new_end;
75 bmap->real_end = new_real_end;
76 return 0;
77}
78
79errcode_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'o1f0b6c11997-10-31 06:07:47 +000085 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000086
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
96errcode_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'o1f0b6c11997-10-31 06:07:47 +0000102 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000103
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