4 * Copyright (C) 1996, 1997 Theodore Ts'o.
6 * TODO: rewrite to not use a direct array!!! (Fortunately this
7 * module isn't really used yet.)
10 * This file may be redistributed under the terms of the GNU Public
25 #if EXT2_FLAT_INCLUDES
28 #include <linux/ext2_fs.h>
34 static errcode_t bma_put(ext2_brel brel, blk_t old,
35 struct ext2_block_relocate_entry *ent);
36 static errcode_t bma_get(ext2_brel brel, blk_t old,
37 struct ext2_block_relocate_entry *ent);
38 static errcode_t bma_start_iter(ext2_brel brel);
39 static errcode_t bma_next(ext2_brel brel, blk_t *old,
40 struct ext2_block_relocate_entry *ent);
41 static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new);
42 static errcode_t bma_delete(ext2_brel brel, blk_t old);
43 static errcode_t bma_free(ext2_brel brel);
48 struct ext2_block_relocate_entry *entries;
51 errcode_t ext2fs_brel_memarray_create(char *name, blk_t max_block,
56 struct brel_ma *ma = 0;
62 * Allocate memory structures
64 retval = ext2fs_get_mem(sizeof(struct ext2_block_relocation_table),
68 memset(brel, 0, sizeof(struct ext2_block_relocation_table));
70 retval = ext2fs_get_mem(strlen(name)+1, (void **) &brel->name);
73 strcpy(brel->name, name);
75 retval = ext2fs_get_mem(sizeof(struct brel_ma), (void **) &ma);
78 memset(ma, 0, sizeof(struct brel_ma));
81 size = (size_t) (sizeof(struct ext2_block_relocate_entry) *
83 retval = ext2fs_get_mem(size, (void **) &ma->entries);
86 memset(ma->entries, 0, size);
87 ma->max_block = max_block;
90 * Fill in the brel data structure
94 brel->start_iter = bma_start_iter;
95 brel->next = bma_next;
96 brel->move = bma_move;
97 brel->delete = bma_delete;
98 brel->free = bma_free;
108 static errcode_t bma_put(ext2_brel brel, blk_t old,
109 struct ext2_block_relocate_entry *ent)
113 ma = brel->priv_data;
114 if (old > ma->max_block)
115 return EXT2_ET_INVALID_ARGUMENT;
116 ma->entries[(unsigned)old] = *ent;
120 static errcode_t bma_get(ext2_brel brel, blk_t old,
121 struct ext2_block_relocate_entry *ent)
125 ma = brel->priv_data;
126 if (old > ma->max_block)
127 return EXT2_ET_INVALID_ARGUMENT;
128 if (ma->entries[(unsigned)old].new == 0)
130 *ent = ma->entries[old];
134 static errcode_t bma_start_iter(ext2_brel brel)
140 static errcode_t bma_next(ext2_brel brel, blk_t *old,
141 struct ext2_block_relocate_entry *ent)
145 ma = brel->priv_data;
146 while (++brel->current < ma->max_block) {
147 if (ma->entries[(unsigned)brel->current].new == 0)
149 *old = brel->current;
150 *ent = ma->entries[(unsigned)brel->current];
157 static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new)
161 ma = brel->priv_data;
162 if ((old > ma->max_block) || (new > ma->max_block))
163 return EXT2_ET_INVALID_ARGUMENT;
164 if (ma->entries[(unsigned)old].new == 0)
166 ma->entries[(unsigned)new] = ma->entries[old];
167 ma->entries[(unsigned)old].new = 0;
171 static errcode_t bma_delete(ext2_brel brel, blk_t old)
175 ma = brel->priv_data;
176 if (old > ma->max_block)
177 return EXT2_ET_INVALID_ARGUMENT;
178 if (ma->entries[(unsigned)old].new == 0)
180 ma->entries[(unsigned)old].new = 0;
184 static errcode_t bma_free(ext2_brel brel)
191 ma = brel->priv_data;
195 ext2fs_free_mem((void **) &ma->entries);
196 ext2fs_free_mem((void **) &ma);
199 ext2fs_free_mem((void **) &brel->name);
200 ext2fs_free_mem((void **) &brel);