blob: 7aaf54967116fdefc7701006acabf05542caf3a4 [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 *
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
12#include <stdlib.h>
13#include <fcntl.h>
14#include <time.h>
15#include <sys/stat.h>
16#include <sys/types.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#if HAVE_ERRNO_H
18#include <errno.h>
19#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000020
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <linux/ext2_fs.h>
22
23#include "ext2fs.h"
24
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000025errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
26 __u32 end,
27 __u32 real_end,
28 const char *descr,
29 ext2fs_generic_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000030{
Theodore Ts'of3db3561997-04-26 13:34:30 +000031 ext2fs_inode_bitmap bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000032 int size;
33
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000034 bitmap = malloc(sizeof(struct ext2fs_struct_generic_bitmap));
Theodore Ts'of3db3561997-04-26 13:34:30 +000035 if (!bitmap)
Theodore Ts'o3839e651997-04-26 13:21:57 +000036 return ENOMEM;
Theodore Ts'of3db3561997-04-26 13:34:30 +000037
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000038 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
39 bitmap->fs = NULL;
40 bitmap->start = start;
41 bitmap->end = end;
42 bitmap->real_end = real_end;
43 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
Theodore Ts'of3db3561997-04-26 13:34:30 +000044 if (descr) {
45 bitmap->description = malloc(strlen(descr)+1);
46 if (!bitmap->description) {
47 free(bitmap);
48 return ENOMEM;
49 }
50 strcpy(bitmap->description, descr);
51 } else
52 bitmap->description = 0;
53
54 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
55 bitmap->bitmap = malloc(size);
56 if (!bitmap->bitmap) {
57 free(bitmap->description);
58 free(bitmap);
59 return ENOMEM;
60 }
61
62 memset(bitmap->bitmap, 0, size);
63 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000064 return 0;
65}
66
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000067errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
Theodore Ts'of3db3561997-04-26 13:34:30 +000068 const char *descr,
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000069 ext2fs_inode_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000070{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000071 ext2fs_inode_bitmap bitmap;
72 errcode_t retval;
73 __u32 start, end, real_end;
Theodore Ts'of3db3561997-04-26 13:34:30 +000074
75 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o3839e651997-04-26 13:21:57 +000076
77 fs->write_bitmaps = ext2fs_write_bitmaps;
78
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000079 start = 1;
80 end = fs->super->s_inodes_count;
81 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
82
83 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
84 descr, &bitmap);
85 if (retval)
86 return retval;
87
88 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
89 bitmap->fs = fs;
90 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
91
92 *ret = bitmap;
93 return 0;
94}
95
96errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
97 const char *descr,
98 ext2fs_block_bitmap *ret)
99{
100 ext2fs_block_bitmap bitmap;
101 errcode_t retval;
102 __u32 start, end, real_end;
103
104 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
105
106 fs->write_bitmaps = ext2fs_write_bitmaps;
107
108 start = fs->super->s_first_data_block;
109 end = fs->super->s_blocks_count-1;
110 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
111 * fs->group_desc_count)-1 + start;
112
113 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
114 descr, &bitmap);
115 if (retval)
116 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000117
118 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
119 bitmap->fs = fs;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000120 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
121
Theodore Ts'of3db3561997-04-26 13:34:30 +0000122 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123 return 0;
124}
125
Theodore Ts'of3db3561997-04-26 13:34:30 +0000126errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
127 ino_t end, ino_t *oend)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000129 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
130
131 if (end > bitmap->real_end)
132 return EXT2_ET_FUDGE_INODE_BITMAP_END;
133 if (oend)
134 *oend = bitmap->end;
135 bitmap->end = end;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000137}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138
Theodore Ts'of3db3561997-04-26 13:34:30 +0000139errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
140 blk_t end, blk_t *oend)
141{
142 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
143
144 if (end > bitmap->real_end)
145 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
146 if (oend)
147 *oend = bitmap->end;
148 bitmap->end = end;
149 return 0;
150}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151
Theodore Ts'of3db3561997-04-26 13:34:30 +0000152void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
153{
154 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
155 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156
Theodore Ts'of3db3561997-04-26 13:34:30 +0000157 memset(bitmap->bitmap, 0,
158 ((bitmap->real_end - bitmap->start) / 8) + 1);
159}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160
Theodore Ts'of3db3561997-04-26 13:34:30 +0000161void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
162{
163 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
164 return;
165
166 memset(bitmap->bitmap, 0,
167 ((bitmap->real_end - bitmap->start) / 8) + 1);
168}
169