blob: 3ef5666cfb6885d680d99b538d2e109e53911268 [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>
15#include <unistd.h>
16#include <stdlib.h>
17#include <fcntl.h>
18#include <time.h>
19#include <sys/stat.h>
20#include <sys/types.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000021#if HAVE_ERRNO_H
22#include <errno.h>
23#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000024
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <linux/ext2_fs.h>
26
27#include "ext2fs.h"
28
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000029errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
30 __u32 end,
31 __u32 real_end,
32 const char *descr,
33 ext2fs_generic_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000034{
Theodore Ts'o5c576471997-04-29 15:29:49 +000035 ext2fs_generic_bitmap bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000036 int size;
37
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000038 bitmap = malloc(sizeof(struct ext2fs_struct_generic_bitmap));
Theodore Ts'of3db3561997-04-26 13:34:30 +000039 if (!bitmap)
Theodore Ts'o3839e651997-04-26 13:21:57 +000040 return ENOMEM;
Theodore Ts'of3db3561997-04-26 13:34:30 +000041
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000042 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
43 bitmap->fs = NULL;
44 bitmap->start = start;
45 bitmap->end = end;
46 bitmap->real_end = real_end;
47 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
Theodore Ts'of3db3561997-04-26 13:34:30 +000048 if (descr) {
49 bitmap->description = malloc(strlen(descr)+1);
50 if (!bitmap->description) {
51 free(bitmap);
52 return ENOMEM;
53 }
54 strcpy(bitmap->description, descr);
55 } else
56 bitmap->description = 0;
57
58 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
59 bitmap->bitmap = malloc(size);
60 if (!bitmap->bitmap) {
61 free(bitmap->description);
62 free(bitmap);
63 return ENOMEM;
64 }
65
66 memset(bitmap->bitmap, 0, size);
67 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068 return 0;
69}
70
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000071errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
Theodore Ts'of3db3561997-04-26 13:34:30 +000072 const char *descr,
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000073 ext2fs_inode_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000074{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000075 ext2fs_inode_bitmap bitmap;
76 errcode_t retval;
77 __u32 start, end, real_end;
Theodore Ts'of3db3561997-04-26 13:34:30 +000078
79 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o3839e651997-04-26 13:21:57 +000080
81 fs->write_bitmaps = ext2fs_write_bitmaps;
82
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000083 start = 1;
84 end = fs->super->s_inodes_count;
85 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
86
87 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
88 descr, &bitmap);
89 if (retval)
90 return retval;
91
92 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
93 bitmap->fs = fs;
94 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
95
96 *ret = bitmap;
97 return 0;
98}
99
100errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
101 const char *descr,
102 ext2fs_block_bitmap *ret)
103{
104 ext2fs_block_bitmap bitmap;
105 errcode_t retval;
106 __u32 start, end, real_end;
107
108 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
109
110 fs->write_bitmaps = ext2fs_write_bitmaps;
111
112 start = fs->super->s_first_data_block;
113 end = fs->super->s_blocks_count-1;
114 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
115 * fs->group_desc_count)-1 + start;
116
117 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
118 descr, &bitmap);
119 if (retval)
120 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121
122 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
123 bitmap->fs = fs;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000124 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
125
Theodore Ts'of3db3561997-04-26 13:34:30 +0000126 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000127 return 0;
128}
129
Theodore Ts'of3db3561997-04-26 13:34:30 +0000130errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
131 ino_t end, ino_t *oend)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000133 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
134
135 if (end > bitmap->real_end)
136 return EXT2_ET_FUDGE_INODE_BITMAP_END;
137 if (oend)
138 *oend = bitmap->end;
139 bitmap->end = end;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000141}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142
Theodore Ts'of3db3561997-04-26 13:34:30 +0000143errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
144 blk_t end, blk_t *oend)
145{
146 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
147
148 if (end > bitmap->real_end)
149 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
150 if (oend)
151 *oend = bitmap->end;
152 bitmap->end = end;
153 return 0;
154}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155
Theodore Ts'of3db3561997-04-26 13:34:30 +0000156void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
157{
158 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
159 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160
Theodore Ts'of3db3561997-04-26 13:34:30 +0000161 memset(bitmap->bitmap, 0,
162 ((bitmap->real_end - bitmap->start) / 8) + 1);
163}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164
Theodore Ts'of3db3561997-04-26 13:34:30 +0000165void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
166{
167 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
168 return;
169
170 memset(bitmap->bitmap, 0,
171 ((bitmap->real_end - bitmap->start) / 8) + 1);
172}