blob: b7d65a9d15b720c5318d6faaab856271c051faac [file] [log] [blame]
Theodore Ts'of3db3561997-04-26 13:34:30 +00001/*
2 * rw_bitmaps.c --- routines to read and write the inode and block bitmaps.
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'of3db3561997-04-26 13:34:30 +000010 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000018#include <fcntl.h>
19#include <time.h>
Theodore Ts'o519ee041997-10-20 00:52:43 +000020#ifdef HAVE_SYS_STAT_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000021#include <sys/stat.h>
Theodore Ts'o519ee041997-10-20 00:52:43 +000022#endif
23#ifdef HAVE_SYS_TYPES_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000024#include <sys/types.h>
Theodore Ts'o519ee041997-10-20 00:52:43 +000025#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000026
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000027#include "ext2_fs.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000028#include "ext2fs.h"
Theodore Ts'oa78926e2001-05-03 04:02:29 +000029#include "e2image.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000030
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040031static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)
Theodore Ts'of3db3561997-04-26 13:34:30 +000032{
Theodore Ts'o2eb374c1998-09-03 01:22:57 +000033 dgrp_t i;
Theodore Ts'o54434922003-12-07 01:28:50 -050034 unsigned int j;
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040035 int block_nbytes, inode_nbytes;
Theodore Ts'o54434922003-12-07 01:28:50 -050036 unsigned int nbits;
Theodore Ts'of3db3561997-04-26 13:34:30 +000037 errcode_t retval;
Eric Sandeen5ea3c822011-09-16 15:49:34 -050038 char *block_buf = NULL, *inode_buf = NULL;
Theodore Ts'o16b851c2008-04-20 23:33:34 -040039 int csum_flag = 0;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040040 blk64_t blk;
Theodore Ts'o6a6337c2011-06-04 20:24:36 -040041 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
Theodore Ts'of1f115a2007-07-23 04:32:48 -040042 ext2_ino_t ino_itr = 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +000043
44 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
45
46 if (!(fs->flags & EXT2_FLAG_RW))
47 return EXT2_ET_RO_FILSYS;
Theodore Ts'o16b851c2008-04-20 23:33:34 -040048
49 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
50 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
51 csum_flag = 1;
52
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040053 inode_nbytes = block_nbytes = 0;
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040054 if (do_block) {
Theodore Ts'ob92fbed2011-06-04 20:05:22 -040055 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
Theodore Ts'ofd1c5a02012-05-07 14:41:49 -040056 retval = io_channel_alloc_buf(fs->io, 0, &block_buf);
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040057 if (retval)
Eric Sandeen5ea3c822011-09-16 15:49:34 -050058 goto errout;
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040059 memset(block_buf, 0xff, fs->blocksize);
60 }
61 if (do_inode) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -040062 inode_nbytes = (size_t)
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040063 ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
Theodore Ts'ofd1c5a02012-05-07 14:41:49 -040064 retval = io_channel_alloc_buf(fs->io, 0, &inode_buf);
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040065 if (retval)
Eric Sandeen5ea3c822011-09-16 15:49:34 -050066 goto errout;
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040067 memset(inode_buf, 0xff, fs->blocksize);
68 }
69
Theodore Ts'of3db3561997-04-26 13:34:30 +000070 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'of1f115a2007-07-23 04:32:48 -040071 if (!do_block)
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040072 goto skip_block_bitmap;
Theodore Ts'of5fa2002006-05-08 20:17:26 -040073
Theodore Ts'ocd65a242009-10-25 21:42:12 -040074 if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)
Theodore Ts'o732c8cd2009-09-07 21:15:12 -040075 )
Theodore Ts'of5fa2002006-05-08 20:17:26 -040076 goto skip_this_block_bitmap;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040077
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -040078 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -040079 blk_itr, block_nbytes << 3, block_buf);
80 if (retval)
Eric Sandeen5ea3c822011-09-16 15:49:34 -050081 goto errout;
Theodore Ts'of1f115a2007-07-23 04:32:48 -040082
Theodore Ts'of3db3561997-04-26 13:34:30 +000083 if (i == fs->group_desc_count - 1) {
84 /* Force bitmap padding for the last group */
Theodore Ts'ob92fbed2011-06-04 20:05:22 -040085 nbits = EXT2FS_NUM_B2C(fs,
Theodore Ts'o6a6337c2011-06-04 20:24:36 -040086 ((ext2fs_blocks_count(fs->super)
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -040087 - (__u64) fs->super->s_first_data_block)
Theodore Ts'o6a6337c2011-06-04 20:24:36 -040088 % (__u64) EXT2_BLOCKS_PER_GROUP(fs->super)));
Theodore Ts'of3db3561997-04-26 13:34:30 +000089 if (nbits)
90 for (j = nbits; j < fs->blocksize * 8; j++)
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -040091 ext2fs_set_bit(j, block_buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +000092 }
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040093 blk = ext2fs_block_bitmap_loc(fs, i);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000094 if (blk) {
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -040095 retval = io_channel_write_blk64(fs->io, blk, 1,
96 block_buf);
Eric Sandeen5ea3c822011-09-16 15:49:34 -050097 if (retval) {
98 retval = EXT2_ET_BLOCK_BITMAP_WRITE;
99 goto errout;
100 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000101 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400102 skip_this_block_bitmap:
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400103 blk_itr += block_nbytes << 3;
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400104 skip_block_bitmap:
105
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400106 if (!do_inode)
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400107 continue;
108
Theodore Ts'ocd65a242009-10-25 21:42:12 -0400109 if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400110 )
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400111 goto skip_this_inode_bitmap;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400112
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400113 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400114 ino_itr, inode_nbytes << 3, inode_buf);
115 if (retval)
Eric Sandeen5ea3c822011-09-16 15:49:34 -0500116 goto errout;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400117
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400118 blk = ext2fs_inode_bitmap_loc(fs, i);
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400119 if (blk) {
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400120 retval = io_channel_write_blk64(fs->io, blk, 1,
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400121 inode_buf);
Eric Sandeen5ea3c822011-09-16 15:49:34 -0500122 if (retval) {
123 retval = EXT2_ET_INODE_BITMAP_WRITE;
124 goto errout;
125 }
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400126 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400127 skip_this_inode_bitmap:
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400128 ino_itr += inode_nbytes << 3;
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400129
Theodore Ts'of3db3561997-04-26 13:34:30 +0000130 }
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400131 if (do_block) {
132 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
133 ext2fs_free_mem(&block_buf);
134 }
135 if (do_inode) {
136 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
137 ext2fs_free_mem(&inode_buf);
138 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000139 return 0;
Eric Sandeen5ea3c822011-09-16 15:49:34 -0500140errout:
141 if (inode_buf)
142 ext2fs_free_mem(&inode_buf);
143 if (block_buf)
144 ext2fs_free_mem(&block_buf);
145 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000146}
147
148static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
149{
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000150 dgrp_t i;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000151 char *block_bitmap = 0, *inode_bitmap = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000152 char *buf;
153 errcode_t retval;
Theodore Ts'ob92fbed2011-06-04 20:05:22 -0400154 int block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
Theodore Ts'o57fd39e2008-08-21 17:56:44 -0400155 int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400156 int csum_flag = 0;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400157 unsigned int cnt;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400158 blk64_t blk;
Theodore Ts'o6a6337c2011-06-04 20:24:36 -0400159 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400160 blk64_t blk_cnt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400161 ext2_ino_t ino_itr = 1;
162 ext2_ino_t ino_cnt;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000163
164 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
165
Theodore Ts'oe48bf252013-05-19 18:50:10 -0400166 if ((block_nbytes > (int) fs->blocksize) ||
167 (inode_nbytes > (int) fs->blocksize))
Theodore Ts'o07d12082012-05-07 14:30:31 -0400168 return EXT2_ET_CORRUPT_SUPERBLOCK;
169
Theodore Ts'of3db3561997-04-26 13:34:30 +0000170 fs->write_bitmaps = ext2fs_write_bitmaps;
171
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400172 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500173 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400174 csum_flag = 1;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400175
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400176 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000177 if (retval)
178 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000179 if (do_block) {
180 if (fs->block_map)
181 ext2fs_free_block_bitmap(fs->block_map);
Theodore Ts'o2d40a912008-08-22 10:37:18 -0400182 strcpy(buf, "block bitmap for ");
183 strcat(buf, fs->device_name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000184 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
185 if (retval)
186 goto cleanup;
Theodore Ts'ofd1c5a02012-05-07 14:41:49 -0400187 retval = io_channel_alloc_buf(fs->io, 0, &block_bitmap);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400188 if (retval)
189 goto cleanup;
190 } else
191 block_nbytes = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000192 if (do_inode) {
193 if (fs->inode_map)
194 ext2fs_free_inode_bitmap(fs->inode_map);
Theodore Ts'o2d40a912008-08-22 10:37:18 -0400195 strcpy(buf, "inode bitmap for ");
196 strcat(buf, fs->device_name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000197 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
198 if (retval)
199 goto cleanup;
Theodore Ts'ofd1c5a02012-05-07 14:41:49 -0400200 retval = io_channel_alloc_buf(fs->io, 0, &inode_bitmap);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400201 if (retval)
202 goto cleanup;
203 } else
204 inode_nbytes = 0;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400205 ext2fs_free_mem(&buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000206
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000207 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400208 blk = (fs->image_header->offset_inodemap / fs->blocksize);
209 ino_cnt = fs->super->s_inodes_count;
210 while (inode_nbytes > 0) {
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400211 retval = io_channel_read_blk64(fs->image_io, blk++,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400212 1, inode_bitmap);
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000213 if (retval)
214 goto cleanup;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400215 cnt = fs->blocksize << 3;
216 if (cnt > ino_cnt)
217 cnt = ino_cnt;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400218 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400219 ino_itr, cnt, inode_bitmap);
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000220 if (retval)
221 goto cleanup;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400222 ino_itr += fs->blocksize << 3;
223 ino_cnt -= fs->blocksize << 3;
224 inode_nbytes -= fs->blocksize;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000225 }
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400226 blk = (fs->image_header->offset_blockmap /
227 fs->blocksize);
Theodore Ts'o6a6337c2011-06-04 20:24:36 -0400228 blk_cnt = (blk64_t)EXT2_CLUSTERS_PER_GROUP(fs->super) *
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400229 fs->group_desc_count;
230 while (block_nbytes > 0) {
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400231 retval = io_channel_read_blk64(fs->image_io, blk++,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400232 1, block_bitmap);
233 if (retval)
234 goto cleanup;
235 cnt = fs->blocksize << 3;
236 if (cnt > blk_cnt)
237 cnt = blk_cnt;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400238 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400239 blk_itr, cnt, block_bitmap);
240 if (retval)
241 goto cleanup;
242 blk_itr += fs->blocksize << 3;
243 blk_cnt -= fs->blocksize << 3;
244 block_nbytes -= fs->blocksize;
245 }
246 goto success_cleanup;
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000247 }
248
Theodore Ts'of3db3561997-04-26 13:34:30 +0000249 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000250 if (block_bitmap) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400251 blk = ext2fs_block_bitmap_loc(fs, i);
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400252 if (csum_flag &&
Theodore Ts'ocd65a242009-10-25 21:42:12 -0400253 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500254 ext2fs_group_desc_csum_verify(fs, i))
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400255 blk = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000256 if (blk) {
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400257 retval = io_channel_read_blk64(fs->io, blk,
Theodore Ts'o07d12082012-05-07 14:30:31 -0400258 1, block_bitmap);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000259 if (retval) {
260 retval = EXT2_ET_BLOCK_BITMAP_READ;
261 goto cleanup;
262 }
263 } else
Theodore Ts'of4240c82009-08-09 22:55:55 -0400264 memset(block_bitmap, 0, block_nbytes);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400265 cnt = block_nbytes << 3;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400266 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400267 blk_itr, cnt, block_bitmap);
268 if (retval)
269 goto cleanup;
270 blk_itr += block_nbytes << 3;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000271 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000272 if (inode_bitmap) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400273 blk = ext2fs_inode_bitmap_loc(fs, i);
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400274 if (csum_flag &&
Theodore Ts'ocd65a242009-10-25 21:42:12 -0400275 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) &&
Jose R. Santosd4f34d42007-10-21 21:03:25 -0500276 ext2fs_group_desc_csum_verify(fs, i))
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400277 blk = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000278 if (blk) {
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400279 retval = io_channel_read_blk64(fs->io, blk,
Theodore Ts'o07d12082012-05-07 14:30:31 -0400280 1, inode_bitmap);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000281 if (retval) {
282 retval = EXT2_ET_INODE_BITMAP_READ;
283 goto cleanup;
284 }
285 } else
Theodore Ts'of4240c82009-08-09 22:55:55 -0400286 memset(inode_bitmap, 0, inode_nbytes);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400287 cnt = inode_nbytes << 3;
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400288 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400289 ino_itr, cnt, inode_bitmap);
290 if (retval)
291 goto cleanup;
292 ino_itr += inode_nbytes << 3;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000293 }
294 }
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400295success_cleanup:
296 if (inode_bitmap)
297 ext2fs_free_mem(&inode_bitmap);
298 if (block_bitmap)
299 ext2fs_free_mem(&block_bitmap);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000300 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400301
Theodore Ts'of3db3561997-04-26 13:34:30 +0000302cleanup:
303 if (do_block) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400304 ext2fs_free_mem(&fs->block_map);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000305 fs->block_map = 0;
306 }
307 if (do_inode) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400308 ext2fs_free_mem(&fs->inode_map);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000309 fs->inode_map = 0;
310 }
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400311 if (inode_bitmap)
312 ext2fs_free_mem(&inode_bitmap);
313 if (block_bitmap)
314 ext2fs_free_mem(&block_bitmap);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000315 if (buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400316 ext2fs_free_mem(&buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000317 return retval;
318}
319
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400320errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000321{
322 return read_bitmaps(fs, 1, 0);
323}
324
325errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
326{
327 return read_bitmaps(fs, 0, 1);
328}
329
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400330errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
331{
332 return write_bitmaps(fs, 1, 0);
333}
334
335errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
336{
337 return write_bitmaps(fs, 0, 1);
338}
339
Theodore Ts'of3db3561997-04-26 13:34:30 +0000340errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
341{
Theodore Ts'o9abd2ce1998-02-16 22:00:37 +0000342 if (fs->inode_map && fs->block_map)
343 return 0;
344
Theodore Ts'of3db3561997-04-26 13:34:30 +0000345 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
346}
347
348errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
349{
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400350 int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
351 int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000352
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400353 if (!do_inode && !do_block)
354 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000355
Theodore Ts'o1f2da2c2006-04-23 12:43:40 -0400356 return write_bitmaps(fs, do_inode, do_block);
357}