blob: 696baad3df718bcac3fc5a95fa2643ab5c3a1c29 [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
Eric Sandeen5830d6b2006-08-30 02:16:55 -0400105 /* Protect loop from wrap-around if map->real_end is maxed */
106 for (i=map->end+1, j = i - map->start;
107 i <= map->real_end && i > map->end;
108 i++, j++)
Theodore Ts'offf876b1997-09-13 00:32:29 +0000109 ext2fs_set_bit(j, map->bitmap);
110
111 return;
112}
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000113
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000114errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000115 const char *descr,
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000116 ext2fs_inode_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000118 ext2fs_inode_bitmap bitmap;
119 errcode_t retval;
120 __u32 start, end, real_end;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121
122 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123
124 fs->write_bitmaps = ext2fs_write_bitmaps;
125
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000126 start = 1;
127 end = fs->super->s_inodes_count;
128 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
129
130 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
131 descr, &bitmap);
132 if (retval)
133 return retval;
134
135 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
136 bitmap->fs = fs;
137 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
138
139 *ret = bitmap;
140 return 0;
141}
142
143errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
144 const char *descr,
145 ext2fs_block_bitmap *ret)
146{
147 ext2fs_block_bitmap bitmap;
148 errcode_t retval;
149 __u32 start, end, real_end;
150
151 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
152
153 fs->write_bitmaps = ext2fs_write_bitmaps;
154
155 start = fs->super->s_first_data_block;
156 end = fs->super->s_blocks_count-1;
157 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
158 * fs->group_desc_count)-1 + start;
159
160 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
161 descr, &bitmap);
162 if (retval)
163 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000164
165 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
166 bitmap->fs = fs;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000167 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
168
Theodore Ts'of3db3561997-04-26 13:34:30 +0000169 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 return 0;
171}
172
Theodore Ts'of3db3561997-04-26 13:34:30 +0000173errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000174 ext2_ino_t end, ext2_ino_t *oend)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000176 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
177
178 if (end > bitmap->real_end)
179 return EXT2_ET_FUDGE_INODE_BITMAP_END;
180 if (oend)
181 *oend = bitmap->end;
182 bitmap->end = end;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000184}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185
Theodore Ts'of3db3561997-04-26 13:34:30 +0000186errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
187 blk_t end, blk_t *oend)
188{
189 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
190
191 if (end > bitmap->real_end)
192 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
193 if (oend)
194 *oend = bitmap->end;
195 bitmap->end = end;
196 return 0;
197}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198
Theodore Ts'of3db3561997-04-26 13:34:30 +0000199void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
200{
201 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
202 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203
Theodore Ts'of3db3561997-04-26 13:34:30 +0000204 memset(bitmap->bitmap, 0,
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000205 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000206}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207
Theodore Ts'of3db3561997-04-26 13:34:30 +0000208void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
209{
210 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
211 return;
212
213 memset(bitmap->bitmap, 0,
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +0000214 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000215}