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
29 static errcode_t bma_put(ext2_brel brel, blk_t old,
30 struct ext2_block_relocate_entry *ent);
31 static errcode_t bma_get(ext2_brel brel, blk_t old,
32 struct ext2_block_relocate_entry *ent);
33 static errcode_t bma_start_iter(ext2_brel brel);
34 static errcode_t bma_next(ext2_brel brel, blk_t *old,
35 struct ext2_block_relocate_entry *ent);
36 static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new);
37 static errcode_t bma_delete(ext2_brel brel, blk_t old);
38 static errcode_t bma_free(ext2_brel brel);
43 struct ext2_block_relocate_entry *entries;
46 errcode_t ext2fs_brel_memarray_create(char *name, blk_t max_block,
51 struct brel_ma *ma = 0;
57 * Allocate memory structures
59 retval = ext2fs_get_mem(sizeof(struct ext2_block_relocation_table),
63 memset(brel, 0, sizeof(struct ext2_block_relocation_table));
65 retval = ext2fs_get_mem(strlen(name)+1, &brel->name);
68 strcpy(brel->name, name);
70 retval = ext2fs_get_mem(sizeof(struct brel_ma), &ma);
73 memset(ma, 0, sizeof(struct brel_ma));
76 size = (size_t) (sizeof(struct ext2_block_relocate_entry) *
78 retval = ext2fs_get_mem(size, &ma->entries);
81 memset(ma->entries, 0, size);
82 ma->max_block = max_block;
85 * Fill in the brel data structure
89 brel->start_iter = bma_start_iter;
90 brel->next = bma_next;
91 brel->move = bma_move;
92 brel->delete = bma_delete;
93 brel->free = bma_free;
103 static errcode_t bma_put(ext2_brel brel, blk_t old,
104 struct ext2_block_relocate_entry *ent)
108 ma = brel->priv_data;
109 if (old > ma->max_block)
110 return EXT2_ET_INVALID_ARGUMENT;
111 ma->entries[(unsigned)old] = *ent;
115 static errcode_t bma_get(ext2_brel brel, blk_t old,
116 struct ext2_block_relocate_entry *ent)
120 ma = brel->priv_data;
121 if (old > ma->max_block)
122 return EXT2_ET_INVALID_ARGUMENT;
123 if (ma->entries[(unsigned)old].new == 0)
125 *ent = ma->entries[old];
129 static errcode_t bma_start_iter(ext2_brel brel)
135 static errcode_t bma_next(ext2_brel brel, blk_t *old,
136 struct ext2_block_relocate_entry *ent)
140 ma = brel->priv_data;
141 while (++brel->current < ma->max_block) {
142 if (ma->entries[(unsigned)brel->current].new == 0)
144 *old = brel->current;
145 *ent = ma->entries[(unsigned)brel->current];
152 static errcode_t bma_move(ext2_brel brel, blk_t old, blk_t new)
156 ma = brel->priv_data;
157 if ((old > ma->max_block) || (new > ma->max_block))
158 return EXT2_ET_INVALID_ARGUMENT;
159 if (ma->entries[(unsigned)old].new == 0)
161 ma->entries[(unsigned)new] = ma->entries[old];
162 ma->entries[(unsigned)old].new = 0;
166 static errcode_t bma_delete(ext2_brel brel, blk_t old)
170 ma = brel->priv_data;
171 if (old > ma->max_block)
172 return EXT2_ET_INVALID_ARGUMENT;
173 if (ma->entries[(unsigned)old].new == 0)
175 ma->entries[(unsigned)old].new = 0;
179 static errcode_t bma_free(ext2_brel brel)
186 ma = brel->priv_data;
190 ext2fs_free_mem(&ma->entries);
191 ext2fs_free_mem(&ma);
194 ext2fs_free_mem(&brel->name);
195 ext2fs_free_mem(&brel);