1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1996, 1997 Theodore Ts'o.
7 * TODO: rewrite to not use a direct array!!! (Fortunately this
8 * module isn't really used yet.)
11 * This file may be redistributed under the terms of the GNU Public
30 static errcode_t
bma_put(ext2_brel brel
, blk_t old
,
31 struct ext2_block_relocate_entry
*ent
);
32 static errcode_t
bma_get(ext2_brel brel
, blk_t old
,
33 struct ext2_block_relocate_entry
*ent
);
34 static errcode_t
bma_start_iter(ext2_brel brel
);
35 static errcode_t
bma_next(ext2_brel brel
, blk_t
*old
,
36 struct ext2_block_relocate_entry
*ent
);
37 static errcode_t
bma_move(ext2_brel brel
, blk_t old
, blk_t
new);
38 static errcode_t
bma_delete(ext2_brel brel
, blk_t old
);
39 static errcode_t
bma_free(ext2_brel brel
);
44 struct ext2_block_relocate_entry
*entries
;
47 errcode_t
ext2fs_brel_memarray_create(char *name
, blk_t max_block
,
52 struct brel_ma
*ma
= 0;
58 * Allocate memory structures
60 retval
= ext2fs_get_mem(sizeof(struct ext2_block_relocation_table
),
64 memset(brel
, 0, sizeof(struct ext2_block_relocation_table
));
66 retval
= ext2fs_get_mem(strlen(name
)+1, &brel
->name
);
69 strcpy(brel
->name
, name
);
71 retval
= ext2fs_get_mem(sizeof(struct brel_ma
), &ma
);
74 memset(ma
, 0, sizeof(struct brel_ma
));
77 size
= (size_t) (sizeof(struct ext2_block_relocate_entry
) *
79 retval
= ext2fs_get_mem(size
, &ma
->entries
);
82 memset(ma
->entries
, 0, size
);
83 ma
->max_block
= max_block
;
86 * Fill in the brel data structure
90 brel
->start_iter
= bma_start_iter
;
91 brel
->next
= bma_next
;
92 brel
->move
= bma_move
;
93 brel
->delete = bma_delete
;
94 brel
->free
= bma_free
;
104 static errcode_t
bma_put(ext2_brel brel
, blk_t old
,
105 struct ext2_block_relocate_entry
*ent
)
109 ma
= brel
->priv_data
;
110 if (old
> ma
->max_block
)
111 return EXT2_ET_INVALID_ARGUMENT
;
112 ma
->entries
[(unsigned)old
] = *ent
;
116 static errcode_t
bma_get(ext2_brel brel
, blk_t old
,
117 struct ext2_block_relocate_entry
*ent
)
121 ma
= brel
->priv_data
;
122 if (old
> ma
->max_block
)
123 return EXT2_ET_INVALID_ARGUMENT
;
124 if (ma
->entries
[(unsigned)old
].new == 0)
126 *ent
= ma
->entries
[old
];
130 static errcode_t
bma_start_iter(ext2_brel brel
)
136 static errcode_t
bma_next(ext2_brel brel
, blk_t
*old
,
137 struct ext2_block_relocate_entry
*ent
)
141 ma
= brel
->priv_data
;
142 while (++brel
->current
< ma
->max_block
) {
143 if (ma
->entries
[(unsigned)brel
->current
].new == 0)
145 *old
= brel
->current
;
146 *ent
= ma
->entries
[(unsigned)brel
->current
];
153 static errcode_t
bma_move(ext2_brel brel
, blk_t old
, blk_t
new)
157 ma
= brel
->priv_data
;
158 if ((old
> ma
->max_block
) || (new > ma
->max_block
))
159 return EXT2_ET_INVALID_ARGUMENT
;
160 if (ma
->entries
[(unsigned)old
].new == 0)
162 ma
->entries
[(unsigned)new] = ma
->entries
[old
];
163 ma
->entries
[(unsigned)old
].new = 0;
167 static errcode_t
bma_delete(ext2_brel brel
, blk_t old
)
171 ma
= brel
->priv_data
;
172 if (old
> ma
->max_block
)
173 return EXT2_ET_INVALID_ARGUMENT
;
174 if (ma
->entries
[(unsigned)old
].new == 0)
176 ma
->entries
[(unsigned)old
].new = 0;
180 static errcode_t
bma_free(ext2_brel brel
)
187 ma
= brel
->priv_data
;
190 ext2fs_free_mem(&ma
->entries
);
191 ext2fs_free_mem(&ma
);
193 ext2fs_free_mem(&brel
->name
);
194 ext2fs_free_mem(&brel
);