blob: 093dddf4288b5c888ffa89dcff4db7ca3d7ba7b5 [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%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'of3db3561997-04-26 13:34:30 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000017#include <fcntl.h>
18#include <time.h>
Theodore Ts'o519ee041997-10-20 00:52:43 +000019#ifdef HAVE_SYS_STAT_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000020#include <sys/stat.h>
Theodore Ts'o519ee041997-10-20 00:52:43 +000021#endif
22#ifdef HAVE_SYS_TYPES_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000023#include <sys/types.h>
Theodore Ts'o519ee041997-10-20 00:52:43 +000024#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#if EXT2_FLAT_INCLUDES
27#include "ext2_fs.h"
28#else
Theodore Ts'of3db3561997-04-26 13:34:30 +000029#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000030#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000031
32#include "ext2fs.h"
Theodore Ts'oa78926e2001-05-03 04:02:29 +000033#include "e2image.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000034
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000035#ifdef __powerpc__
36/*
37 * On the PowerPC, the big-endian variant of the ext2 filesystem
38 * has its bitmaps stored as 32-bit words with bit 0 as the LSB
39 * of each word. Thus a bitmap with only bit 0 set would be, as
40 * a string of bytes, 00 00 00 01 00 ...
41 * To cope with this, we byte-reverse each word of a bitmap if
42 * we have a big-endian filesystem, that is, if we are *not*
43 * byte-swapping other word-sized numbers.
44 */
45#define EXT2_BIG_ENDIAN_BITMAPS
46#endif
47
48#ifdef EXT2_BIG_ENDIAN_BITMAPS
49void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes)
50{
51 __u32 *p = (__u32 *) bitmap;
52 int n;
53
54 for (n = nbytes / sizeof(__u32); n > 0; --n, ++p)
55 *p = ext2fs_swab32(*p);
56}
57#endif
58
Theodore Ts'of3db3561997-04-26 13:34:30 +000059errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
60{
Theodore Ts'o2eb374c1998-09-03 01:22:57 +000061 dgrp_t i;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000062 size_t nbytes;
Theodore Ts'of3db3561997-04-26 13:34:30 +000063 errcode_t retval;
64 char * inode_bitmap = fs->inode_map->bitmap;
65 char * bitmap_block = NULL;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000066 blk_t blk;
Theodore Ts'of3db3561997-04-26 13:34:30 +000067
68 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
69
70 if (!(fs->flags & EXT2_FLAG_RW))
71 return EXT2_ET_RO_FILSYS;
72 if (!inode_bitmap)
73 return 0;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000074 nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
Theodore Ts'o7f88b041997-04-26 14:48:50 +000075
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000076 retval = ext2fs_get_mem(fs->blocksize, (void **) &bitmap_block);
77 if (retval)
78 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +000079 memset(bitmap_block, 0xff, fs->blocksize);
80 for (i = 0; i < fs->group_desc_count; i++) {
81 memcpy(bitmap_block, inode_bitmap, nbytes);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000082 blk = fs->group_desc[i].bg_inode_bitmap;
83 if (blk) {
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000084#ifdef EXT2_BIG_ENDIAN_BITMAPS
85 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
86 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
87 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
88#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000089 retval = io_channel_write_blk(fs->io, blk, 1,
90 bitmap_block);
91 if (retval)
92 return EXT2_ET_INODE_BITMAP_WRITE;
93 }
Theodore Ts'of3db3561997-04-26 13:34:30 +000094 inode_bitmap += nbytes;
95 }
96 fs->flags |= EXT2_FLAG_CHANGED;
97 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000098 ext2fs_free_mem((void **) &bitmap_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +000099 return 0;
100}
101
102errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
103{
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000104 dgrp_t i;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000105 int j;
106 int nbytes;
107 int nbits;
108 errcode_t retval;
109 char * block_bitmap = fs->block_map->bitmap;
110 char * bitmap_block = NULL;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000111 blk_t blk;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000112
113 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
114
115 if (!(fs->flags & EXT2_FLAG_RW))
116 return EXT2_ET_RO_FILSYS;
117 if (!block_bitmap)
118 return 0;
119 nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000120 retval = ext2fs_get_mem(fs->blocksize, (void **) &bitmap_block);
121 if (retval)
122 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000123 memset(bitmap_block, 0xff, fs->blocksize);
124 for (i = 0; i < fs->group_desc_count; i++) {
125 memcpy(bitmap_block, block_bitmap, nbytes);
126 if (i == fs->group_desc_count - 1) {
127 /* Force bitmap padding for the last group */
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000128 nbits = (int) ((fs->super->s_blocks_count
129 - fs->super->s_first_data_block)
130 % EXT2_BLOCKS_PER_GROUP(fs->super));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000131 if (nbits)
132 for (j = nbits; j < fs->blocksize * 8; j++)
Theodore Ts'o74becf31997-04-26 14:37:06 +0000133 ext2fs_set_bit(j, bitmap_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000134 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000135 blk = fs->group_desc[i].bg_block_bitmap;
136 if (blk) {
Theodore Ts'o1c27cac1997-08-14 17:20:42 +0000137#ifdef EXT2_BIG_ENDIAN_BITMAPS
138 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
139 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
140 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
141#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000142 retval = io_channel_write_blk(fs->io, blk, 1,
143 bitmap_block);
144 if (retval)
145 return EXT2_ET_BLOCK_BITMAP_WRITE;
146 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000147 block_bitmap += nbytes;
148 }
149 fs->flags |= EXT2_FLAG_CHANGED;
150 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000151 ext2fs_free_mem((void **) &bitmap_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000152 return 0;
153}
154
155static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
156{
Theodore Ts'o2eb374c1998-09-03 01:22:57 +0000157 dgrp_t i;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000158 char *block_bitmap = 0, *inode_bitmap = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000159 char *buf;
160 errcode_t retval;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000161 int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
162 int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000163 blk_t blk;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000164
165 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
166
167 fs->write_bitmaps = ext2fs_write_bitmaps;
168
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000169 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, (void **) &buf);
170 if (retval)
171 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000172 if (do_block) {
173 if (fs->block_map)
174 ext2fs_free_block_bitmap(fs->block_map);
175 sprintf(buf, "block bitmap for %s", fs->device_name);
176 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
177 if (retval)
178 goto cleanup;
179 block_bitmap = fs->block_map->bitmap;
180 }
181 if (do_inode) {
182 if (fs->inode_map)
183 ext2fs_free_inode_bitmap(fs->inode_map);
184 sprintf(buf, "inode bitmap for %s", fs->device_name);
185 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
186 if (retval)
187 goto cleanup;
188 inode_bitmap = fs->inode_map->bitmap;
189 }
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000190 ext2fs_free_mem((void **) &buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000191
Theodore Ts'oa78926e2001-05-03 04:02:29 +0000192 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
193 if (inode_bitmap) {
194 blk = (fs->image_header->offset_inodemap /
195 fs->blocksize);
196 retval = io_channel_read_blk(fs->io, blk,
197 -(inode_nbytes * fs->group_desc_count),
198 inode_bitmap);
199 if (retval)
200 goto cleanup;
201 }
202 if (block_bitmap) {
203 blk = (fs->image_header->offset_blockmap /
204 fs->blocksize);
205 retval = io_channel_read_blk(fs->io, blk,
206 -(block_nbytes * fs->group_desc_count),
207 block_bitmap);
208 if (retval)
209 goto cleanup;
210 }
211 return 0;
212 }
213
Theodore Ts'of3db3561997-04-26 13:34:30 +0000214 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000215 if (block_bitmap) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000216 blk = fs->group_desc[i].bg_block_bitmap;
217 if (blk) {
218 retval = io_channel_read_blk(fs->io, blk,
219 -block_nbytes, block_bitmap);
220 if (retval) {
221 retval = EXT2_ET_BLOCK_BITMAP_READ;
222 goto cleanup;
223 }
Theodore Ts'o1c27cac1997-08-14 17:20:42 +0000224#ifdef EXT2_BIG_ENDIAN_BITMAPS
225 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
226 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
227 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
228#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000229 } else
230 memset(block_bitmap, 0, block_nbytes);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000231 block_bitmap += block_nbytes;
232 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000233 if (inode_bitmap) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000234 blk = fs->group_desc[i].bg_inode_bitmap;
235 if (blk) {
236 retval = io_channel_read_blk(fs->io, blk,
237 -inode_nbytes, inode_bitmap);
238 if (retval) {
239 retval = EXT2_ET_INODE_BITMAP_READ;
240 goto cleanup;
241 }
Theodore Ts'o1c27cac1997-08-14 17:20:42 +0000242#ifdef EXT2_BIG_ENDIAN_BITMAPS
243 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
244 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
245 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
246#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000247 } else
248 memset(inode_bitmap, 0, inode_nbytes);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000249 inode_bitmap += inode_nbytes;
250 }
251 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000252 return 0;
253
254cleanup:
255 if (do_block) {
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000256 ext2fs_free_mem((void **) &fs->block_map);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000257 fs->block_map = 0;
258 }
259 if (do_inode) {
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000260 ext2fs_free_mem((void **) &fs->inode_map);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000261 fs->inode_map = 0;
262 }
263 if (buf)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000264 ext2fs_free_mem((void **) &buf);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000265 return retval;
266}
267
268errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
269{
270 return read_bitmaps(fs, 1, 0);
271}
272
273errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
274{
275 return read_bitmaps(fs, 0, 1);
276}
277
278errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
279{
280
281 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
282
Theodore Ts'o9abd2ce1998-02-16 22:00:37 +0000283 if (fs->inode_map && fs->block_map)
284 return 0;
285
Theodore Ts'of3db3561997-04-26 13:34:30 +0000286 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
287}
288
289errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
290{
291 errcode_t retval;
292
293 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
294
295 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
296 retval = ext2fs_write_block_bitmap(fs);
297 if (retval)
298 return retval;
299 }
300 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
301 retval = ext2fs_write_inode_bitmap(fs);
302 if (retval)
303 return retval;
304 }
305 return 0;
306}
307