1 /*
2 * rw_bitmaps.c --- routines to read and write the inode and block bitmaps.
3 *
4 * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
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 "ext2_fs.h"
28 #include "ext2fs.h"
29 #include "e2image.h"
30
write_bitmaps(ext2_filsys fs,int do_inode,int do_block)31 static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)
32 {
33 dgrp_t i;
34 unsigned int j;
35 int block_nbytes, inode_nbytes;
36 unsigned int nbits;
37 errcode_t retval;
38 char *block_buf = NULL, *inode_buf = NULL;
39 int csum_flag;
40 blk64_t blk;
41 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
42 ext2_ino_t ino_itr = 1;
43
44 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
45
46 if (!(fs->flags & EXT2_FLAG_RW))
47 return EXT2_ET_RO_FILSYS;
48
49 csum_flag = ext2fs_has_group_desc_csum(fs);
50
51 inode_nbytes = block_nbytes = 0;
52 if (do_block) {
53 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
54 retval = io_channel_alloc_buf(fs->io, 0, &block_buf);
55 if (retval)
56 goto errout;
57 memset(block_buf, 0xff, fs->blocksize);
58 }
59 if (do_inode) {
60 inode_nbytes = (size_t)
61 ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
62 retval = io_channel_alloc_buf(fs->io, 0, &inode_buf);
63 if (retval)
64 goto errout;
65 memset(inode_buf, 0xff, fs->blocksize);
66 }
67
68 for (i = 0; i < fs->group_desc_count; i++) {
69 if (!do_block)
70 goto skip_block_bitmap;
71
72 if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)
73 )
74 goto skip_this_block_bitmap;
75
76 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
77 blk_itr, block_nbytes << 3, block_buf);
78 if (retval)
79 goto errout;
80
81 if (i == fs->group_desc_count - 1) {
82 /* Force bitmap padding for the last group */
83 nbits = EXT2FS_NUM_B2C(fs,
84 ((ext2fs_blocks_count(fs->super)
85 - (__u64) fs->super->s_first_data_block)
86 % (__u64) EXT2_BLOCKS_PER_GROUP(fs->super)));
87 if (nbits)
88 for (j = nbits; j < fs->blocksize * 8; j++)
89 ext2fs_set_bit(j, block_buf);
90 }
91
92 retval = ext2fs_block_bitmap_csum_set(fs, i, block_buf,
93 block_nbytes);
94 if (retval)
95 return retval;
96 ext2fs_group_desc_csum_set(fs, i);
97 fs->flags |= EXT2_FLAG_DIRTY;
98
99 blk = ext2fs_block_bitmap_loc(fs, i);
100 if (blk) {
101 retval = io_channel_write_blk64(fs->io, blk, 1,
102 block_buf);
103 if (retval) {
104 retval = EXT2_ET_BLOCK_BITMAP_WRITE;
105 goto errout;
106 }
107 }
108 skip_this_block_bitmap:
109 blk_itr += block_nbytes << 3;
110 skip_block_bitmap:
111
112 if (!do_inode)
113 continue;
114
115 if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)
116 )
117 goto skip_this_inode_bitmap;
118
119 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
120 ino_itr, inode_nbytes << 3, inode_buf);
121 if (retval)
122 goto errout;
123
124 retval = ext2fs_inode_bitmap_csum_set(fs, i, inode_buf,
125 inode_nbytes);
126 if (retval)
127 goto errout;
128 ext2fs_group_desc_csum_set(fs, i);
129 fs->flags |= EXT2_FLAG_DIRTY;
130
131 blk = ext2fs_inode_bitmap_loc(fs, i);
132 if (blk) {
133 retval = io_channel_write_blk64(fs->io, blk, 1,
134 inode_buf);
135 if (retval) {
136 retval = EXT2_ET_INODE_BITMAP_WRITE;
137 goto errout;
138 }
139 }
140 skip_this_inode_bitmap:
141 ino_itr += inode_nbytes << 3;
142
143 }
144 if (do_block) {
145 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
146 ext2fs_free_mem(&block_buf);
147 }
148 if (do_inode) {
149 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
150 ext2fs_free_mem(&inode_buf);
151 }
152 return 0;
153 errout:
154 if (inode_buf)
155 ext2fs_free_mem(&inode_buf);
156 if (block_buf)
157 ext2fs_free_mem(&block_buf);
158 return retval;
159 }
160
mark_uninit_bg_group_blocks(ext2_filsys fs)161 static errcode_t mark_uninit_bg_group_blocks(ext2_filsys fs)
162 {
163 dgrp_t i;
164 blk64_t blk;
165 ext2fs_block_bitmap bmap = fs->block_map;
166
167 for (i = 0; i < fs->group_desc_count; i++) {
168 if (!ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT))
169 continue;
170
171 ext2fs_reserve_super_and_bgd(fs, i, bmap);
172
173 /*
174 * Mark the blocks used for the inode table
175 */
176 blk = ext2fs_inode_table_loc(fs, i);
177 if (blk)
178 ext2fs_mark_block_bitmap_range2(bmap, blk,
179 fs->inode_blocks_per_group);
180
181 /*
182 * Mark block used for the block bitmap
183 */
184 blk = ext2fs_block_bitmap_loc(fs, i);
185 if (blk)
186 ext2fs_mark_block_bitmap2(bmap, blk);
187
188 /*
189 * Mark block used for the inode bitmap
190 */
191 blk = ext2fs_inode_bitmap_loc(fs, i);
192 if (blk)
193 ext2fs_mark_block_bitmap2(bmap, blk);
194 }
195 return 0;
196 }
197
bitmap_tail_verify(unsigned char * bitmap,int first,int last)198 static int bitmap_tail_verify(unsigned char *bitmap, int first, int last)
199 {
200 int i;
201
202 for (i = first; i <= last; i++)
203 if (bitmap[i] != 0xff)
204 return 0;
205 return 1;
206 }
207
read_bitmaps(ext2_filsys fs,int do_inode,int do_block)208 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
209 {
210 dgrp_t i;
211 char *block_bitmap = 0, *inode_bitmap = 0;
212 char *buf;
213 errcode_t retval;
214 int block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
215 int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
216 int tail_flags = 0;
217 int csum_flag;
218 unsigned int cnt;
219 blk64_t blk;
220 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
221 blk64_t blk_cnt;
222 ext2_ino_t ino_itr = 1;
223 ext2_ino_t ino_cnt;
224
225 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
226
227 if ((block_nbytes > (int) fs->blocksize) ||
228 (inode_nbytes > (int) fs->blocksize))
229 return EXT2_ET_CORRUPT_SUPERBLOCK;
230
231 fs->write_bitmaps = ext2fs_write_bitmaps;
232
233 csum_flag = ext2fs_has_group_desc_csum(fs);
234
235 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
236 if (retval)
237 return retval;
238 if (do_block) {
239 if (fs->block_map)
240 ext2fs_free_block_bitmap(fs->block_map);
241 strcpy(buf, "block bitmap for ");
242 strcat(buf, fs->device_name);
243 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
244 if (retval)
245 goto cleanup;
246 retval = io_channel_alloc_buf(fs->io, 0, &block_bitmap);
247 if (retval)
248 goto cleanup;
249 } else
250 block_nbytes = 0;
251 if (do_inode) {
252 if (fs->inode_map)
253 ext2fs_free_inode_bitmap(fs->inode_map);
254 strcpy(buf, "inode bitmap for ");
255 strcat(buf, fs->device_name);
256 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
257 if (retval)
258 goto cleanup;
259 retval = io_channel_alloc_buf(fs->io, 0, &inode_bitmap);
260 if (retval)
261 goto cleanup;
262 } else
263 inode_nbytes = 0;
264 ext2fs_free_mem(&buf);
265
266 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
267 blk = (ext2fs_le32_to_cpu(fs->image_header->offset_inodemap) / fs->blocksize);
268 ino_cnt = fs->super->s_inodes_count;
269 while (inode_bitmap && ino_cnt > 0) {
270 retval = io_channel_read_blk64(fs->image_io, blk++,
271 1, inode_bitmap);
272 if (retval)
273 goto cleanup;
274 cnt = fs->blocksize << 3;
275 if (cnt > ino_cnt)
276 cnt = ino_cnt;
277 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
278 ino_itr, cnt, inode_bitmap);
279 if (retval)
280 goto cleanup;
281 ino_itr += cnt;
282 ino_cnt -= cnt;
283 }
284 blk = (ext2fs_le32_to_cpu(fs->image_header->offset_blockmap) /
285 fs->blocksize);
286 blk_cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super,
287 fs->group_desc_count);
288 while (block_bitmap && blk_cnt > 0) {
289 retval = io_channel_read_blk64(fs->image_io, blk++,
290 1, block_bitmap);
291 if (retval)
292 goto cleanup;
293 cnt = fs->blocksize << 3;
294 if (cnt > blk_cnt)
295 cnt = blk_cnt;
296 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
297 blk_itr, cnt, block_bitmap);
298 if (retval)
299 goto cleanup;
300 blk_itr += cnt;
301 blk_cnt -= cnt;
302 }
303 goto success_cleanup;
304 }
305
306 for (i = 0; i < fs->group_desc_count; i++) {
307 if (block_bitmap) {
308 blk = ext2fs_block_bitmap_loc(fs, i);
309 if (csum_flag &&
310 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
311 ext2fs_group_desc_csum_verify(fs, i))
312 blk = 0;
313 if (blk) {
314 retval = io_channel_read_blk64(fs->io, blk,
315 1, block_bitmap);
316 if (retval) {
317 retval = EXT2_ET_BLOCK_BITMAP_READ;
318 goto cleanup;
319 }
320 /* verify block bitmap checksum */
321 if (!(fs->flags &
322 EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
323 !ext2fs_block_bitmap_csum_verify(fs, i,
324 block_bitmap, block_nbytes)) {
325 retval =
326 EXT2_ET_BLOCK_BITMAP_CSUM_INVALID;
327 goto cleanup;
328 }
329 if (!bitmap_tail_verify((unsigned char *) block_bitmap,
330 block_nbytes, fs->blocksize - 1))
331 tail_flags |= EXT2_FLAG_BBITMAP_TAIL_PROBLEM;
332 } else
333 memset(block_bitmap, 0, block_nbytes);
334 cnt = block_nbytes << 3;
335 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
336 blk_itr, cnt, block_bitmap);
337 if (retval)
338 goto cleanup;
339 blk_itr += block_nbytes << 3;
340 }
341 if (inode_bitmap) {
342 blk = ext2fs_inode_bitmap_loc(fs, i);
343 if (csum_flag &&
344 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) &&
345 ext2fs_group_desc_csum_verify(fs, i))
346 blk = 0;
347 if (blk) {
348 retval = io_channel_read_blk64(fs->io, blk,
349 1, inode_bitmap);
350 if (retval) {
351 retval = EXT2_ET_INODE_BITMAP_READ;
352 goto cleanup;
353 }
354
355 /* verify inode bitmap checksum */
356 if (!(fs->flags &
357 EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
358 !ext2fs_inode_bitmap_csum_verify(fs, i,
359 inode_bitmap, inode_nbytes)) {
360 retval =
361 EXT2_ET_INODE_BITMAP_CSUM_INVALID;
362 goto cleanup;
363 }
364 if (!bitmap_tail_verify((unsigned char *) inode_bitmap,
365 inode_nbytes, fs->blocksize - 1))
366 tail_flags |= EXT2_FLAG_IBITMAP_TAIL_PROBLEM;
367 } else
368 memset(inode_bitmap, 0, inode_nbytes);
369 cnt = inode_nbytes << 3;
370 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
371 ino_itr, cnt, inode_bitmap);
372 if (retval)
373 goto cleanup;
374 ino_itr += inode_nbytes << 3;
375 }
376 }
377
378 /* Mark group blocks for any BLOCK_UNINIT groups */
379 if (do_block) {
380 retval = mark_uninit_bg_group_blocks(fs);
381 if (retval)
382 goto cleanup;
383 }
384
385 success_cleanup:
386 if (inode_bitmap) {
387 ext2fs_free_mem(&inode_bitmap);
388 fs->flags &= ~EXT2_FLAG_IBITMAP_TAIL_PROBLEM;
389 }
390 if (block_bitmap) {
391 ext2fs_free_mem(&block_bitmap);
392 fs->flags &= ~EXT2_FLAG_BBITMAP_TAIL_PROBLEM;
393 }
394 fs->flags |= tail_flags;
395 return 0;
396
397 cleanup:
398 if (do_block) {
399 ext2fs_free_mem(&fs->block_map);
400 fs->block_map = 0;
401 }
402 if (do_inode) {
403 ext2fs_free_mem(&fs->inode_map);
404 fs->inode_map = 0;
405 }
406 if (inode_bitmap)
407 ext2fs_free_mem(&inode_bitmap);
408 if (block_bitmap)
409 ext2fs_free_mem(&block_bitmap);
410 if (buf)
411 ext2fs_free_mem(&buf);
412 return retval;
413 }
414
ext2fs_read_inode_bitmap(ext2_filsys fs)415 errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
416 {
417 return read_bitmaps(fs, 1, 0);
418 }
419
ext2fs_read_block_bitmap(ext2_filsys fs)420 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
421 {
422 return read_bitmaps(fs, 0, 1);
423 }
424
ext2fs_write_inode_bitmap(ext2_filsys fs)425 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
426 {
427 return write_bitmaps(fs, 1, 0);
428 }
429
ext2fs_write_block_bitmap(ext2_filsys fs)430 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
431 {
432 return write_bitmaps(fs, 0, 1);
433 }
434
ext2fs_read_bitmaps(ext2_filsys fs)435 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
436 {
437 if (fs->inode_map && fs->block_map)
438 return 0;
439
440 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
441 }
442
ext2fs_write_bitmaps(ext2_filsys fs)443 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
444 {
445 int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
446 int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
447
448 if (!do_inode && !do_block)
449 return 0;
450
451 return write_bitmaps(fs, do_inode, do_block);
452 }
453