blob: 7edd28d7b9e78e409e2a368d5c8b7e7e358a70e5 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * bitmaps.c --- routines to read, write, and manipulate the inode and
3 * block bitmaps.
4 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00005 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 */
12
13#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <fcntl.h>
19#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000020#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#endif
23#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000026
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000027#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include "ext2fs.h"
29
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000030static errcode_t make_bitmap(__u32 start, __u32 end, __u32 real_end,
31 const char *descr, char *init_map,
32 ext2fs_generic_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000033{
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000034 ext2fs_generic_bitmap bitmap;
35 errcode_t retval;
36 size_t size;
Theodore Ts'o3839e651997-04-26 13:21:57 +000037
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040038 retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
39 &bitmap);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000040 if (retval)
41 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +000042
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000043 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
44 bitmap->fs = NULL;
45 bitmap->start = start;
46 bitmap->end = end;
47 bitmap->real_end = real_end;
48 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
Theodore Ts'of3db3561997-04-26 13:34:30 +000049 if (descr) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040050 retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000051 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040052 ext2fs_free_mem(&bitmap);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000053 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +000054 }
55 strcpy(bitmap->description, descr);
56 } else
57 bitmap->description = 0;
58
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000059 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040060 retval = ext2fs_get_mem(size, &bitmap->bitmap);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000061 if (retval) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040062 ext2fs_free_mem(&bitmap->description);
63 ext2fs_free_mem(&bitmap);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000064 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +000065 }
66
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000067 if (init_map)
68 memcpy(bitmap->bitmap, init_map, size);
69 else
70 memset(bitmap->bitmap, 0, size);
Theodore Ts'of3db3561997-04-26 13:34:30 +000071 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000072 return 0;
73}
74
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000075errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
76 __u32 end,
77 __u32 real_end,
78 const char *descr,
79 ext2fs_generic_bitmap *ret)
80{
81 return make_bitmap(start, end, real_end, descr, 0, ret);
82}
83
84errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
85 ext2fs_generic_bitmap *dest)
86{
87 errcode_t retval;
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000088 ext2fs_generic_bitmap new_map;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000089
90 retval = make_bitmap(src->start, src->end, src->real_end,
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000091 src->description, src->bitmap, &new_map);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000092 if (retval)
93 return retval;
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000094 new_map->magic = src->magic;
95 new_map->fs = src->fs;
96 new_map->base_error_code = src->base_error_code;
97 *dest = new_map;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000098 return 0;
99}
100
Theodore Ts'offf876b1997-09-13 00:32:29 +0000101void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
102{
103 __u32 i, j;
104
105 for (i=map->end+1, j = i - map->start; i <= map->real_end; i++, j++)
106 ext2fs_set_bit(j, map->bitmap);
107
108 return;
109}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000110
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000111errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000112 const char *descr,
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000113 ext2fs_inode_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000115 ext2fs_inode_bitmap bitmap;
116 errcode_t retval;
117 __u32 start, end, real_end;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000118
119 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120
121 fs->write_bitmaps = ext2fs_write_bitmaps;
122
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000123 start = 1;
124 end = fs->super->s_inodes_count;
125 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
126
127 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
128 descr, &bitmap);
129 if (retval)
130 return retval;
131
132 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
133 bitmap->fs = fs;
134 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
135
136 *ret = bitmap;
137 return 0;
138}
139
140errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
141 const char *descr,
142 ext2fs_block_bitmap *ret)
143{
144 ext2fs_block_bitmap bitmap;
145 errcode_t retval;
146 __u32 start, end, real_end;
147
148 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
149
150 fs->write_bitmaps = ext2fs_write_bitmaps;
151
152 start = fs->super->s_first_data_block;
153 end = fs->super->s_blocks_count-1;
154 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
155 * fs->group_desc_count)-1 + start;
156
157 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
158 descr, &bitmap);
159 if (retval)
160 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000161
162 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
163 bitmap->fs = fs;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000164 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
165
Theodore Ts'of3db3561997-04-26 13:34:30 +0000166 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 return 0;
168}
169
Theodore Ts'of3db3561997-04-26 13:34:30 +0000170errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000171 ext2_ino_t end, ext2_ino_t *oend)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000173 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
174
175 if (end > bitmap->real_end)
176 return EXT2_ET_FUDGE_INODE_BITMAP_END;
177 if (oend)
178 *oend = bitmap->end;
179 bitmap->end = end;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000181}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182
Theodore Ts'of3db3561997-04-26 13:34:30 +0000183errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
184 blk_t end, blk_t *oend)
185{
186 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
187
188 if (end > bitmap->real_end)
189 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
190 if (oend)
191 *oend = bitmap->end;
192 bitmap->end = end;
193 return 0;
194}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195
Theodore Ts'of3db3561997-04-26 13:34:30 +0000196void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
197{
198 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
199 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200
Theodore Ts'of3db3561997-04-26 13:34:30 +0000201 memset(bitmap->bitmap, 0,
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000202 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000203}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204
Theodore Ts'of3db3561997-04-26 13:34:30 +0000205void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
206{
207 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
208 return;
209
210 memset(bitmap->bitmap, 0,
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000211 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000212}