blob: 8712e2d580f5a752fd66ba34ba8b1d1783094408 [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>
17
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <linux/ext2_fs.h>
19
20#include "ext2fs.h"
21
Theodore Ts'of3db3561997-04-26 13:34:30 +000022errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
23 const char *descr,
24 ext2fs_inode_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000025{
Theodore Ts'of3db3561997-04-26 13:34:30 +000026 ext2fs_inode_bitmap bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000027 int size;
28
Theodore Ts'of3db3561997-04-26 13:34:30 +000029 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
30
Theodore Ts'o3839e651997-04-26 13:21:57 +000031 fs->write_bitmaps = ext2fs_write_bitmaps;
Theodore Ts'of3db3561997-04-26 13:34:30 +000032
33 bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
34 if (!bitmap)
Theodore Ts'o3839e651997-04-26 13:21:57 +000035 return ENOMEM;
Theodore Ts'of3db3561997-04-26 13:34:30 +000036
37 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
38 bitmap->fs = fs;
39 bitmap->start = 1;
40 bitmap->end = fs->super->s_inodes_count;
41 bitmap->real_end = (EXT2_INODES_PER_GROUP(fs->super)
42 * fs->group_desc_count);
43 if (descr) {
44 bitmap->description = malloc(strlen(descr)+1);
45 if (!bitmap->description) {
46 free(bitmap);
47 return ENOMEM;
48 }
49 strcpy(bitmap->description, descr);
50 } else
51 bitmap->description = 0;
52
53 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
54 bitmap->bitmap = malloc(size);
55 if (!bitmap->bitmap) {
56 free(bitmap->description);
57 free(bitmap);
58 return ENOMEM;
59 }
60
61 memset(bitmap->bitmap, 0, size);
62 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000063 return 0;
64}
65
Theodore Ts'of3db3561997-04-26 13:34:30 +000066errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
67 const char *descr,
68 ext2fs_block_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000069{
Theodore Ts'of3db3561997-04-26 13:34:30 +000070 ext2fs_block_bitmap bitmap;
71 int size;
72
73 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o3839e651997-04-26 13:21:57 +000074
75 fs->write_bitmaps = ext2fs_write_bitmaps;
76
Theodore Ts'of3db3561997-04-26 13:34:30 +000077 bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
78 if (!bitmap)
79 return ENOMEM;
80
81 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
82 bitmap->fs = fs;
83 bitmap->start = fs->super->s_first_data_block;
84 bitmap->end = fs->super->s_blocks_count-1;
85 bitmap->real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
86 * fs->group_desc_count)-1 + bitmap->start;
87 if (descr) {
88 bitmap->description = malloc(strlen(descr)+1);
89 if (!bitmap->description) {
90 free(bitmap);
91 return ENOMEM;
92 }
93 strcpy(bitmap->description, descr);
94 } else
95 bitmap->description = 0;
96
97 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
98 bitmap->bitmap = malloc(size);
99 if (!bitmap->bitmap) {
100 free(bitmap->description);
101 free(bitmap);
102 return ENOMEM;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000104
105 memset(bitmap->bitmap, 0, size);
106 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 return 0;
108}
109
Theodore Ts'of3db3561997-04-26 13:34:30 +0000110errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
111 ino_t end, ino_t *oend)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000113 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
114
115 if (end > bitmap->real_end)
116 return EXT2_ET_FUDGE_INODE_BITMAP_END;
117 if (oend)
118 *oend = bitmap->end;
119 bitmap->end = end;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122
Theodore Ts'of3db3561997-04-26 13:34:30 +0000123errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
124 blk_t end, blk_t *oend)
125{
126 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
127
128 if (end > bitmap->real_end)
129 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
130 if (oend)
131 *oend = bitmap->end;
132 bitmap->end = end;
133 return 0;
134}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135
Theodore Ts'of3db3561997-04-26 13:34:30 +0000136void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
137{
138 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
139 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140
Theodore Ts'of3db3561997-04-26 13:34:30 +0000141 memset(bitmap->bitmap, 0,
142 ((bitmap->real_end - bitmap->start) / 8) + 1);
143}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144
Theodore Ts'of3db3561997-04-26 13:34:30 +0000145void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
146{
147 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
148 return;
149
150 memset(bitmap->bitmap, 0,
151 ((bitmap->real_end - bitmap->start) / 8) + 1);
152}
153
154void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
155{
156 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
157 return;
158
159 bitmap->magic = 0;
160 if (bitmap->description) {
161 free(bitmap->description);
162 bitmap->description = 0;
163 }
164 if (bitmap->bitmap) {
165 free(bitmap->bitmap);
166 bitmap->bitmap = 0;
167 }
168 free(bitmap);
169}
170
171void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
172{
173 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
174 return;
175
176 bitmap->magic = 0;
177 if (bitmap->description) {
178 free(bitmap->description);
179 bitmap->description = 0;
180 }
181 if (bitmap->bitmap) {
182 free(bitmap->bitmap);
183 bitmap->bitmap = 0;
184 }
185 free(bitmap);
186}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187