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
read_bitmaps(ext2_filsys fs,int do_inode,int do_block)198 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
199 {
200 dgrp_t i;
201 char *block_bitmap = 0, *inode_bitmap = 0;
202 char *buf;
203 errcode_t retval;
204 int block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
205 int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
206 int csum_flag;
207 unsigned int cnt;
208 blk64_t blk;
209 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
210 blk64_t blk_cnt;
211 ext2_ino_t ino_itr = 1;
212 ext2_ino_t ino_cnt;
213
214 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
215
216 if ((block_nbytes > (int) fs->blocksize) ||
217 (inode_nbytes > (int) fs->blocksize))
218 return EXT2_ET_CORRUPT_SUPERBLOCK;
219
220 fs->write_bitmaps = ext2fs_write_bitmaps;
221
222 csum_flag = ext2fs_has_group_desc_csum(fs);
223
224 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
225 if (retval)
226 return retval;
227 if (do_block) {
228 if (fs->block_map)
229 ext2fs_free_block_bitmap(fs->block_map);
230 strcpy(buf, "block bitmap for ");
231 strcat(buf, fs->device_name);
232 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
233 if (retval)
234 goto cleanup;
235 retval = io_channel_alloc_buf(fs->io, 0, &block_bitmap);
236 if (retval)
237 goto cleanup;
238 } else
239 block_nbytes = 0;
240 if (do_inode) {
241 if (fs->inode_map)
242 ext2fs_free_inode_bitmap(fs->inode_map);
243 strcpy(buf, "inode bitmap for ");
244 strcat(buf, fs->device_name);
245 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
246 if (retval)
247 goto cleanup;
248 retval = io_channel_alloc_buf(fs->io, 0, &inode_bitmap);
249 if (retval)
250 goto cleanup;
251 } else
252 inode_nbytes = 0;
253 ext2fs_free_mem(&buf);
254
255 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
256 blk = (ext2fs_le32_to_cpu(fs->image_header->offset_inodemap) / fs->blocksize);
257 ino_cnt = fs->super->s_inodes_count;
258 while (inode_bitmap && ino_cnt > 0) {
259 retval = io_channel_read_blk64(fs->image_io, blk++,
260 1, inode_bitmap);
261 if (retval)
262 goto cleanup;
263 cnt = fs->blocksize << 3;
264 if (cnt > ino_cnt)
265 cnt = ino_cnt;
266 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
267 ino_itr, cnt, inode_bitmap);
268 if (retval)
269 goto cleanup;
270 ino_itr += cnt;
271 ino_cnt -= cnt;
272 }
273 blk = (ext2fs_le32_to_cpu(fs->image_header->offset_blockmap) /
274 fs->blocksize);
275 blk_cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super,
276 fs->group_desc_count);
277 while (block_bitmap && blk_cnt > 0) {
278 retval = io_channel_read_blk64(fs->image_io, blk++,
279 1, block_bitmap);
280 if (retval)
281 goto cleanup;
282 cnt = fs->blocksize << 3;
283 if (cnt > blk_cnt)
284 cnt = blk_cnt;
285 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
286 blk_itr, cnt, block_bitmap);
287 if (retval)
288 goto cleanup;
289 blk_itr += cnt;
290 blk_cnt -= cnt;
291 }
292 goto success_cleanup;
293 }
294
295 for (i = 0; i < fs->group_desc_count; i++) {
296 if (block_bitmap) {
297 blk = ext2fs_block_bitmap_loc(fs, i);
298 if (csum_flag &&
299 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
300 ext2fs_group_desc_csum_verify(fs, i))
301 blk = 0;
302 if (blk) {
303 retval = io_channel_read_blk64(fs->io, blk,
304 1, block_bitmap);
305 if (retval) {
306 retval = EXT2_ET_BLOCK_BITMAP_READ;
307 goto cleanup;
308 }
309 /* verify block bitmap checksum */
310 if (!(fs->flags &
311 EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
312 !ext2fs_block_bitmap_csum_verify(fs, i,
313 block_bitmap, block_nbytes)) {
314 retval =
315 EXT2_ET_BLOCK_BITMAP_CSUM_INVALID;
316 goto cleanup;
317 }
318 } else
319 memset(block_bitmap, 0, block_nbytes);
320 cnt = block_nbytes << 3;
321 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
322 blk_itr, cnt, block_bitmap);
323 if (retval)
324 goto cleanup;
325 blk_itr += block_nbytes << 3;
326 }
327 if (inode_bitmap) {
328 blk = ext2fs_inode_bitmap_loc(fs, i);
329 if (csum_flag &&
330 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) &&
331 ext2fs_group_desc_csum_verify(fs, i))
332 blk = 0;
333 if (blk) {
334 retval = io_channel_read_blk64(fs->io, blk,
335 1, inode_bitmap);
336 if (retval) {
337 retval = EXT2_ET_INODE_BITMAP_READ;
338 goto cleanup;
339 }
340
341 /* verify inode bitmap checksum */
342 if (!(fs->flags &
343 EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
344 !ext2fs_inode_bitmap_csum_verify(fs, i,
345 inode_bitmap, inode_nbytes)) {
346 retval =
347 EXT2_ET_INODE_BITMAP_CSUM_INVALID;
348 goto cleanup;
349 }
350 } else
351 memset(inode_bitmap, 0, inode_nbytes);
352 cnt = inode_nbytes << 3;
353 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
354 ino_itr, cnt, inode_bitmap);
355 if (retval)
356 goto cleanup;
357 ino_itr += inode_nbytes << 3;
358 }
359 }
360
361 /* Mark group blocks for any BLOCK_UNINIT groups */
362 if (do_block) {
363 retval = mark_uninit_bg_group_blocks(fs);
364 if (retval)
365 goto cleanup;
366 }
367
368 success_cleanup:
369 if (inode_bitmap)
370 ext2fs_free_mem(&inode_bitmap);
371 if (block_bitmap)
372 ext2fs_free_mem(&block_bitmap);
373 return 0;
374
375 cleanup:
376 if (do_block) {
377 ext2fs_free_mem(&fs->block_map);
378 fs->block_map = 0;
379 }
380 if (do_inode) {
381 ext2fs_free_mem(&fs->inode_map);
382 fs->inode_map = 0;
383 }
384 if (inode_bitmap)
385 ext2fs_free_mem(&inode_bitmap);
386 if (block_bitmap)
387 ext2fs_free_mem(&block_bitmap);
388 if (buf)
389 ext2fs_free_mem(&buf);
390 return retval;
391 }
392
ext2fs_read_inode_bitmap(ext2_filsys fs)393 errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
394 {
395 return read_bitmaps(fs, 1, 0);
396 }
397
ext2fs_read_block_bitmap(ext2_filsys fs)398 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
399 {
400 return read_bitmaps(fs, 0, 1);
401 }
402
ext2fs_write_inode_bitmap(ext2_filsys fs)403 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
404 {
405 return write_bitmaps(fs, 1, 0);
406 }
407
ext2fs_write_block_bitmap(ext2_filsys fs)408 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
409 {
410 return write_bitmaps(fs, 0, 1);
411 }
412
ext2fs_read_bitmaps(ext2_filsys fs)413 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
414 {
415 if (fs->inode_map && fs->block_map)
416 return 0;
417
418 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
419 }
420
ext2fs_write_bitmaps(ext2_filsys fs)421 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
422 {
423 int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
424 int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
425
426 if (!do_inode && !do_block)
427 return 0;
428
429 return write_bitmaps(fs, do_inode, do_block);
430 }
431