Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / rs_bitmap.c
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>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #ifdef HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26
27 #include <linux/ext2_fs.h>
28
29 #include "ext2fs.h"
30
31 errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
32                                        ext2fs_generic_bitmap bmap)
33 {
34         size_t  size, new_size;
35         char    *new_bitmap;
36         __u32   bitno;
37
38         if (!bmap)
39                 return EXT2_INVALID_ARGUMENT;
40
41         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
42
43         /*
44          * If we're expanding the bitmap, make sure all of the new
45          * parts of the bitmap are zero.
46          */
47         if (new_end > bmap->end) {
48                 bitno = bmap->real_end;
49                 if (bitno > new_end)
50                         bitno = new_end;
51                 for (; bitno > bmap->end; bitno--)
52                         ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
53         }
54         if (new_real_end == bmap->real_end) {
55                 bmap->end = new_end;
56                 return 0;
57         }
58         
59         size = ((bmap->real_end - bmap->start) / 8) + 1;
60         new_size = ((new_real_end - bmap->start) / 8) + 1;
61
62         new_bitmap = realloc(bmap->bitmap, new_size);
63         if (!new_bitmap)
64                 return EXT2_NO_MEMORY;
65         if (new_size > size)
66                 memset(new_bitmap + size, 0, new_size - size);
67
68         bmap->bitmap = new_bitmap;
69         bmap->end = new_end;
70         bmap->real_end = new_real_end;
71         return 0;
72 }
73
74 errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
75                                      ext2fs_inode_bitmap bmap)
76 {
77         errcode_t       retval;
78         
79         if (!bmap)
80                 return EXT2_INVALID_ARGUMENT;
81
82         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP);
83
84         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
85         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
86                                               bmap);
87         bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
88         return retval;
89 }
90
91 errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
92                                      ext2fs_block_bitmap bmap)
93 {
94         errcode_t       retval;
95         
96         if (!bmap)
97                 return EXT2_INVALID_ARGUMENT;
98
99         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
100
101         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
102         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
103                                               bmap);
104         bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
105         return retval;
106 }
107