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 Library
11 * General Public License, version 2.
30 static errcode_t bma_put(ext2_brel brel, blk64_t old,
31 struct ext2_block_relocate_entry *ent);
32 static errcode_t bma_get(ext2_brel brel, blk64_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, blk64_t *old,
36 struct ext2_block_relocate_entry *ent);
37 static errcode_t bma_move(ext2_brel brel, blk64_t old, blk64_t new);
38 static errcode_t bma_delete(ext2_brel brel, blk64_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, blk64_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_array(max_block+1,
80 sizeof(struct ext2_block_relocate_entry), &ma->entries);
83 memset(ma->entries, 0, size);
84 ma->max_block = max_block;
87 * Fill in the brel data structure
91 brel->start_iter = bma_start_iter;
92 brel->next = bma_next;
93 brel->move = bma_move;
94 brel->delete = bma_delete;
95 brel->free = bma_free;
105 static errcode_t bma_put(ext2_brel brel, blk64_t old,
106 struct ext2_block_relocate_entry *ent)
110 ma = brel->priv_data;
111 if (old > ma->max_block)
112 return EXT2_ET_INVALID_ARGUMENT;
113 ma->entries[(unsigned)old] = *ent;
117 static errcode_t bma_get(ext2_brel brel, blk64_t old,
118 struct ext2_block_relocate_entry *ent)
122 ma = brel->priv_data;
123 if (old > ma->max_block)
124 return EXT2_ET_INVALID_ARGUMENT;
125 if (ma->entries[(unsigned)old].new == 0)
127 *ent = ma->entries[old];
131 static errcode_t bma_start_iter(ext2_brel brel)
137 static errcode_t bma_next(ext2_brel brel, blk64_t *old,
138 struct ext2_block_relocate_entry *ent)
142 ma = brel->priv_data;
143 while (++brel->current < ma->max_block) {
144 if (ma->entries[(unsigned)brel->current].new == 0)
146 *old = brel->current;
147 *ent = ma->entries[(unsigned)brel->current];
154 static errcode_t bma_move(ext2_brel brel, blk64_t old, blk64_t new)
158 ma = brel->priv_data;
159 if ((old > ma->max_block) || (new > ma->max_block))
160 return EXT2_ET_INVALID_ARGUMENT;
161 if (ma->entries[(unsigned)old].new == 0)
163 ma->entries[(unsigned)new] = ma->entries[old];
164 ma->entries[(unsigned)old].new = 0;
168 static errcode_t bma_delete(ext2_brel brel, blk64_t old)
172 ma = brel->priv_data;
173 if (old > ma->max_block)
174 return EXT2_ET_INVALID_ARGUMENT;
175 if (ma->entries[(unsigned)old].new == 0)
177 ma->entries[(unsigned)old].new = 0;
181 static errcode_t bma_free(ext2_brel brel)
188 ma = brel->priv_data;
192 ext2fs_free_mem(&ma->entries);
193 ext2fs_free_mem(&ma);
196 ext2fs_free_mem(&brel->name);
197 ext2fs_free_mem(&brel);