4 * Copyright (C) 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
23 #include <linux/ext2_fs.h>
28 static errcode_t ima_put(ext2_irel irel, ino_t old,
29 struct ext2_inode_relocate_entry *ent);
30 static errcode_t ima_get(ext2_irel irel, ino_t old,
31 struct ext2_inode_relocate_entry *ent);
32 static errcode_t ima_get_by_orig(ext2_irel irel, ino_t orig, ino_t *old,
33 struct ext2_inode_relocate_entry *ent);
34 static errcode_t ima_start_iter(ext2_irel irel);
35 static errcode_t ima_next(ext2_irel irel, ino_t *old,
36 struct ext2_inode_relocate_entry *ent);
37 static errcode_t ima_add_ref(ext2_irel irel, ino_t ino,
38 struct ext2_inode_reference *ref);
39 static errcode_t ima_start_iter_ref(ext2_irel irel, ino_t ino);
40 static errcode_t ima_next_ref(ext2_irel irel, struct ext2_inode_reference *ref);
41 static errcode_t ima_move(ext2_irel irel, ino_t old, ino_t new);
42 static errcode_t ima_delete(ext2_irel irel, ino_t old);
43 static errcode_t ima_free(ext2_irel irel);
46 * This data structure stores the array of inode references; there is
47 * a structure for each inode.
49 struct inode_reference_entry {
51 struct ext2_inode_reference *refs;
60 struct ext2_inode_relocate_entry *entries;
61 struct inode_reference_entry *ref_entries;
64 errcode_t ext2fs_irel_memarray_create(char *name, ino_t max_inode,
69 struct irel_ma *ma = 0;
75 * Allocate memory structures
78 irel = malloc(sizeof(struct ext2_inode_relocation_table));
81 memset(irel, 0, sizeof(struct ext2_inode_relocation_table));
83 irel->name = malloc(strlen(name)+1);
86 strcpy(irel->name, name);
88 ma = malloc(sizeof(struct irel_ma));
91 memset(ma, 0, sizeof(struct irel_ma));
94 size = (size_t) (sizeof(ino_t) * (max_inode+1));
95 ma->orig_map = malloc(size);
98 memset(ma->orig_map, 0, size);
100 size = (size_t) (sizeof(struct ext2_inode_relocate_entry) *
102 ma->entries = malloc(size);
105 memset(ma->entries, 0, size);
107 size = (size_t) (sizeof(struct inode_reference_entry) *
109 ma->ref_entries = malloc(size);
110 if (!ma->ref_entries)
112 memset(ma->ref_entries, 0, size);
113 ma->max_inode = max_inode;
116 * Fill in the irel data structure
120 irel->get_by_orig = ima_get_by_orig;
121 irel->start_iter = ima_start_iter;
122 irel->next = ima_next;
123 irel->add_ref = ima_add_ref;
124 irel->start_iter_ref = ima_start_iter_ref;
125 irel->next_ref = ima_next_ref;
126 irel->move = ima_move;
127 irel->delete = ima_delete;
128 irel->free = ima_free;
138 static errcode_t ima_put(ext2_irel irel, ino_t old,
139 struct ext2_inode_relocate_entry *ent)
142 struct inode_reference_entry *ref_ent;
143 struct ext2_inode_reference *new_refs;
147 if (old > ma->max_inode)
151 * Force the orig field to the correct value; the application
152 * program shouldn't be messing with this field.
154 if (ma->entries[(unsigned) old].new == 0)
157 ent->orig = ma->entries[(unsigned) old].orig;
160 * If max_refs has changed, reallocate the refs array
162 ref_ent = ma->ref_entries + (unsigned) old;
163 if (ref_ent->refs && ent->max_refs !=
164 ma->entries[(unsigned) old].max_refs) {
165 size = (sizeof(struct ext2_inode_reference) * ent->max_refs);
166 new_refs = realloc(ref_ent->refs, size);
169 ref_ent->refs = new_refs;
172 ma->entries[(unsigned) old] = *ent;
173 ma->orig_map[(unsigned) ent->orig] = old;
177 static errcode_t ima_get(ext2_irel irel, ino_t old,
178 struct ext2_inode_relocate_entry *ent)
183 if (old > ma->max_inode)
185 if (ma->entries[(unsigned) old].new == 0)
187 *ent = ma->entries[(unsigned) old];
191 static errcode_t ima_get_by_orig(ext2_irel irel, ino_t orig, ino_t *old,
192 struct ext2_inode_relocate_entry *ent)
198 if (orig > ma->max_inode)
200 ino = ma->orig_map[(unsigned) orig];
204 *ent = ma->entries[(unsigned) ino];
208 static errcode_t ima_start_iter(ext2_irel irel)
214 static errcode_t ima_next(ext2_irel irel, ino_t *old,
215 struct ext2_inode_relocate_entry *ent)
220 while (++irel->current < ma->max_inode) {
221 if (ma->entries[(unsigned) irel->current].new == 0)
223 *old = irel->current;
224 *ent = ma->entries[(unsigned) irel->current];
231 static errcode_t ima_add_ref(ext2_irel irel, ino_t ino,
232 struct ext2_inode_reference *ref)
236 struct inode_reference_entry *ref_ent;
237 struct ext2_inode_relocate_entry *ent;
240 if (ino > ma->max_inode)
243 ref_ent = ma->ref_entries + (unsigned) ino;
244 ent = ma->entries + (unsigned) ino;
247 * If the inode reference array doesn't exist, create it.
249 if (ref_ent->refs == 0) {
250 size = (size_t) ((sizeof(struct ext2_inode_reference) *
252 ref_ent->refs = malloc(size);
253 if (ref_ent->refs == 0)
255 memset(ref_ent->refs, 0, size);
259 if (ref_ent->num >= ent->max_refs)
262 ref_ent->refs[(unsigned) ref_ent->num++] = *ref;
266 static errcode_t ima_start_iter_ref(ext2_irel irel, ino_t ino)
271 if (ino > ma->max_inode)
273 if (ma->entries[(unsigned) ino].new == 0)
275 ma->ref_current = ino;
280 static errcode_t ima_next_ref(ext2_irel irel,
281 struct ext2_inode_reference *ref)
284 struct inode_reference_entry *ref_ent;
288 ref_ent = ma->ref_entries + ma->ref_current;
290 if ((ref_ent->refs == NULL) ||
291 (ma->ref_iter >= ref_ent->num)) {
296 *ref = ref_ent->refs[ma->ref_iter++];
301 static errcode_t ima_move(ext2_irel irel, ino_t old, ino_t new)
306 if ((old > ma->max_inode) || (new > ma->max_inode))
308 if (ma->entries[(unsigned) old].new == 0)
311 ma->entries[(unsigned) new] = ma->entries[(unsigned) old];
312 if (ma->ref_entries[(unsigned) new].refs)
313 free(ma->ref_entries[(unsigned) new].refs);
314 ma->ref_entries[(unsigned) new] = ma->ref_entries[(unsigned) old];
316 ma->entries[(unsigned) old].new = 0;
317 ma->ref_entries[(unsigned) old].num = 0;
318 ma->ref_entries[(unsigned) old].refs = 0;
320 ma->orig_map[ma->entries[new].orig] = new;
324 static errcode_t ima_delete(ext2_irel irel, ino_t old)
329 if (old > ma->max_inode)
331 if (ma->entries[(unsigned) old].new == 0)
334 ma->entries[old].new = 0;
335 if (ma->ref_entries[(unsigned) old].refs)
336 free(ma->ref_entries[(unsigned) old].refs);
337 ma->orig_map[ma->entries[(unsigned) old].orig] = 0;
339 ma->ref_entries[(unsigned) old].num = 0;
340 ma->ref_entries[(unsigned) old].refs = 0;
344 static errcode_t ima_free(ext2_irel irel)
359 if (ma->ref_entries) {
360 for (ino = 0; ino <= ma->max_inode; ino++) {
361 if (ma->ref_entries[(unsigned) ino].refs)
362 free(ma->ref_entries[(unsigned) ino].refs);
364 free(ma->ref_entries);