blob: d5ef0ec7e3aad04443bf374882ea7c954d4a350b [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'of3db3561997-04-26 13:34:30 +000025errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
26 const char *descr,
27 ext2fs_inode_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000028{
Theodore Ts'of3db3561997-04-26 13:34:30 +000029 ext2fs_inode_bitmap bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000030 int size;
31
Theodore Ts'of3db3561997-04-26 13:34:30 +000032 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
33
Theodore Ts'o3839e651997-04-26 13:21:57 +000034 fs->write_bitmaps = ext2fs_write_bitmaps;
Theodore Ts'of3db3561997-04-26 13:34:30 +000035
36 bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
37 if (!bitmap)
Theodore Ts'o3839e651997-04-26 13:21:57 +000038 return ENOMEM;
Theodore Ts'of3db3561997-04-26 13:34:30 +000039
40 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
41 bitmap->fs = fs;
42 bitmap->start = 1;
43 bitmap->end = fs->super->s_inodes_count;
44 bitmap->real_end = (EXT2_INODES_PER_GROUP(fs->super)
45 * fs->group_desc_count);
46 if (descr) {
47 bitmap->description = malloc(strlen(descr)+1);
48 if (!bitmap->description) {
49 free(bitmap);
50 return ENOMEM;
51 }
52 strcpy(bitmap->description, descr);
53 } else
54 bitmap->description = 0;
55
56 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
57 bitmap->bitmap = malloc(size);
58 if (!bitmap->bitmap) {
59 free(bitmap->description);
60 free(bitmap);
61 return ENOMEM;
62 }
63
64 memset(bitmap->bitmap, 0, size);
65 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +000066 return 0;
67}
68
Theodore Ts'of3db3561997-04-26 13:34:30 +000069errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
70 const char *descr,
71 ext2fs_block_bitmap *ret)
Theodore Ts'o3839e651997-04-26 13:21:57 +000072{
Theodore Ts'of3db3561997-04-26 13:34:30 +000073 ext2fs_block_bitmap bitmap;
74 int size;
75
76 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
Theodore Ts'o3839e651997-04-26 13:21:57 +000077
78 fs->write_bitmaps = ext2fs_write_bitmaps;
79
Theodore Ts'of3db3561997-04-26 13:34:30 +000080 bitmap = malloc(sizeof(struct ext2fs_struct_inode_bitmap));
81 if (!bitmap)
82 return ENOMEM;
83
84 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
85 bitmap->fs = fs;
86 bitmap->start = fs->super->s_first_data_block;
87 bitmap->end = fs->super->s_blocks_count-1;
88 bitmap->real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
89 * fs->group_desc_count)-1 + bitmap->start;
90 if (descr) {
91 bitmap->description = malloc(strlen(descr)+1);
92 if (!bitmap->description) {
93 free(bitmap);
94 return ENOMEM;
95 }
96 strcpy(bitmap->description, descr);
97 } else
98 bitmap->description = 0;
99
100 size = ((bitmap->real_end - bitmap->start) / 8) + 1;
101 bitmap->bitmap = malloc(size);
102 if (!bitmap->bitmap) {
103 free(bitmap->description);
104 free(bitmap);
105 return ENOMEM;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000107
108 memset(bitmap->bitmap, 0, size);
109 *ret = bitmap;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 return 0;
111}
112
Theodore Ts'of3db3561997-04-26 13:34:30 +0000113errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
114 ino_t end, ino_t *oend)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000116 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
117
118 if (end > bitmap->real_end)
119 return EXT2_ET_FUDGE_INODE_BITMAP_END;
120 if (oend)
121 *oend = bitmap->end;
122 bitmap->end = end;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000124}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125
Theodore Ts'of3db3561997-04-26 13:34:30 +0000126errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
127 blk_t end, blk_t *oend)
128{
129 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
130
131 if (end > bitmap->real_end)
132 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
133 if (oend)
134 *oend = bitmap->end;
135 bitmap->end = end;
136 return 0;
137}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138
Theodore Ts'of3db3561997-04-26 13:34:30 +0000139void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
140{
141 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
142 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143
Theodore Ts'of3db3561997-04-26 13:34:30 +0000144 memset(bitmap->bitmap, 0,
145 ((bitmap->real_end - bitmap->start) / 8) + 1);
146}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147
Theodore Ts'of3db3561997-04-26 13:34:30 +0000148void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
149{
150 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
151 return;
152
153 memset(bitmap->bitmap, 0,
154 ((bitmap->real_end - bitmap->start) / 8) + 1);
155}
156