Whamcloud - gitweb
8712e2d580f5a752fd66ba34ba8b1d1783094408
[tools/e2fsprogs.git] / lib / ext2fs / bitmaps.c
1 /*
2  * bitmaps.c --- routines to read, write, and manipulate the inode and
3  * block bitmaps.
4  *
5  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
6  * under the terms of the GNU Public License.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <time.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17
18 #include <linux/ext2_fs.h>
19
20 #include "ext2fs.h"
21
22 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
23                                        const char *descr,
24                                        ext2fs_inode_bitmap *ret)
25 {
26         ext2fs_inode_bitmap bitmap;
27         int     size;
28
29         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
30
31         fs->write_bitmaps = ext2fs_write_bitmaps;
32
33         bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
34         if (!bitmap)
35                 return ENOMEM;
36
37         bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
38         bitmap->fs = fs;
39         bitmap->start = 1;
40         bitmap->end = fs->super->s_inodes_count;
41         bitmap->real_end = (EXT2_INODES_PER_GROUP(fs->super)
42                             * fs->group_desc_count);
43         if (descr) {
44                 bitmap->description = malloc(strlen(descr)+1);
45                 if (!bitmap->description) {
46                         free(bitmap);
47                         return ENOMEM;
48                 }
49                 strcpy(bitmap->description, descr);
50         } else
51                 bitmap->description = 0;
52
53         size = ((bitmap->real_end - bitmap->start) / 8) + 1;
54         bitmap->bitmap = malloc(size);
55         if (!bitmap->bitmap) {
56                 free(bitmap->description);
57                 free(bitmap);
58                 return ENOMEM;
59         }
60
61         memset(bitmap->bitmap, 0, size);
62         *ret = bitmap;
63         return 0;
64 }
65
66 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
67                                        const char *descr,
68                                        ext2fs_block_bitmap *ret)
69 {
70         ext2fs_block_bitmap bitmap;
71         int     size;
72
73         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
74
75         fs->write_bitmaps = ext2fs_write_bitmaps;
76
77         bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
78         if (!bitmap)
79                 return ENOMEM;
80
81         bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
82         bitmap->fs = fs;
83         bitmap->start = fs->super->s_first_data_block;
84         bitmap->end = fs->super->s_blocks_count-1;
85         bitmap->real_end = (EXT2_BLOCKS_PER_GROUP(fs->super) 
86                             * fs->group_desc_count)-1 + bitmap->start;
87         if (descr) {
88                 bitmap->description = malloc(strlen(descr)+1);
89                 if (!bitmap->description) {
90                         free(bitmap);
91                         return ENOMEM;
92                 }
93                 strcpy(bitmap->description, descr);
94         } else
95                 bitmap->description = 0;
96
97         size = ((bitmap->real_end - bitmap->start) / 8) + 1;
98         bitmap->bitmap = malloc(size);
99         if (!bitmap->bitmap) {
100                 free(bitmap->description);
101                 free(bitmap);
102                 return ENOMEM;
103         }
104
105         memset(bitmap->bitmap, 0, size);
106         *ret = bitmap;
107         return 0;
108 }
109
110 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
111                                         ino_t end, ino_t *oend)
112 {
113         EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
114         
115         if (end > bitmap->real_end)
116                 return EXT2_ET_FUDGE_INODE_BITMAP_END;
117         if (oend)
118                 *oend = bitmap->end;
119         bitmap->end = end;
120         return 0;
121 }
122
123 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
124                                         blk_t end, blk_t *oend)
125 {
126         EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
127         
128         if (end > bitmap->real_end)
129                 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
130         if (oend)
131                 *oend = bitmap->end;
132         bitmap->end = end;
133         return 0;
134 }
135
136 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
137 {
138         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
139                 return;
140
141         memset(bitmap->bitmap, 0,
142                ((bitmap->real_end - bitmap->start) / 8) + 1);
143 }
144
145 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
146 {
147         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
148                 return;
149
150         memset(bitmap->bitmap, 0,
151                ((bitmap->real_end - bitmap->start) / 8) + 1);
152 }
153
154 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
155 {
156         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
157                 return;
158
159         bitmap->magic = 0;
160         if (bitmap->description) {
161                 free(bitmap->description);
162                 bitmap->description = 0;
163         }
164         if (bitmap->bitmap) {
165                 free(bitmap->bitmap);
166                 bitmap->bitmap = 0;
167         }
168         free(bitmap);
169 }
170
171 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
172 {
173         if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
174                 return;
175
176         bitmap->magic = 0;
177         if (bitmap->description) {
178                 free(bitmap->description);
179                 bitmap->description = 0;
180         }
181         if (bitmap->bitmap) {
182                 free(bitmap->bitmap);
183                 bitmap->bitmap = 0;
184         }
185         free(bitmap);
186 }
187