4 * Copyright (C) 2001 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
20 * The strategy we use for keeping track of EA refcounts is as
21 * follows. We keep a sorted array of first EA blocks and its
22 * reference counts. Once the refcount has dropped to zero, it is
23 * removed from the array to save memory space. Once the EA block is
24 * checked, its bit is set in the block_ea_map bitmap.
26 struct ea_refcount_el {
35 struct ea_refcount_el *list;
38 void ea_refcount_free(ext2_refcount_t refcount)
44 ext2fs_free_mem(&refcount->list);
45 ext2fs_free_mem(&refcount);
48 errcode_t ea_refcount_create(int size, ext2_refcount_t *ret)
50 ext2_refcount_t refcount;
54 retval = ext2fs_get_mem(sizeof(struct ea_refcount), &refcount);
57 memset(refcount, 0, sizeof(struct ea_refcount));
61 refcount->size = size;
62 bytes = (size_t) (size * sizeof(struct ea_refcount_el));
64 printf("Refcount allocated %d entries, %d bytes.\n",
65 refcount->size, bytes);
67 retval = ext2fs_get_mem(bytes, &refcount->list);
70 memset(refcount->list, 0, bytes);
79 ea_refcount_free(refcount);
84 * collapse_refcount() --- go through the refcount array, and get rid
85 * of any count == zero entries
87 static void refcount_collapse(ext2_refcount_t refcount)
90 struct ea_refcount_el *list;
92 list = refcount->list;
93 for (i = 0, j = 0; i < refcount->count; i++) {
94 if (list[i].ea_count) {
100 #if defined(DEBUG) || defined(TEST_PROGRAM)
101 printf("Refcount_collapse: size was %d, now %d\n",
109 * insert_refcount_el() --- Insert a new entry into the sorted list at a
110 * specified position.
112 static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
115 struct ea_refcount_el *el;
120 if (refcount->count >= refcount->size) {
121 new_size = refcount->size + 100;
123 printf("Reallocating refcount %d entries...\n", new_size);
125 retval = ext2fs_resize_mem((size_t) refcount->size *
126 sizeof(struct ea_refcount_el),
128 sizeof(struct ea_refcount_el),
132 refcount->size = new_size;
134 num = (int) refcount->count - pos;
136 return 0; /* should never happen */
138 memmove(&refcount->list[pos+1], &refcount->list[pos],
139 sizeof(struct ea_refcount_el) * num);
142 el = &refcount->list[pos];
150 * get_refcount_el() --- given an block number, try to find refcount
151 * information in the sorted list. If the create flag is set,
152 * and we can't find an entry, create one in the sorted list.
154 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
155 blk_t blk, int create)
159 blk_t lowval, highval;
161 if (!refcount || !refcount->list)
165 high = (int) refcount->count-1;
166 if (create && ((refcount->count == 0) ||
167 (blk > refcount->list[high].ea_blk))) {
168 if (refcount->count >= refcount->size)
169 refcount_collapse(refcount);
171 return insert_refcount_el(refcount, blk,
172 (unsigned) refcount->count);
174 if (refcount->count == 0)
177 if (refcount->cursor >= refcount->count)
178 refcount->cursor = 0;
179 if (blk == refcount->list[refcount->cursor].ea_blk)
180 return &refcount->list[refcount->cursor++];
182 printf("Non-cursor get_refcount_el: %u\n", blk);
184 while (low <= high) {
191 /* Interpolate for efficiency */
192 lowval = refcount->list[low].ea_blk;
193 highval = refcount->list[high].ea_blk;
197 else if (blk > highval)
200 range = ((float) (blk - lowval)) /
202 mid = low + ((int) (range * (high-low)));
205 if (blk == refcount->list[mid].ea_blk) {
206 refcount->cursor = mid+1;
207 return &refcount->list[mid];
209 if (blk < refcount->list[mid].ea_blk)
215 * If we need to create a new entry, it should be right at
216 * low (where high will be left at low-1).
219 if (refcount->count >= refcount->size) {
220 refcount_collapse(refcount);
221 if (refcount->count < refcount->size)
224 return insert_refcount_el(refcount, blk, low);
229 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk_t blk,
232 struct ea_refcount_el *el;
234 el = get_refcount_el(refcount, blk, 0);
243 errcode_t ea_refcount_increment(ext2_refcount_t refcount, blk_t blk, int *ret)
245 struct ea_refcount_el *el;
247 el = get_refcount_el(refcount, blk, 1);
249 return EXT2_ET_NO_MEMORY;
257 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, blk_t blk, int *ret)
259 struct ea_refcount_el *el;
261 el = get_refcount_el(refcount, blk, 0);
262 if (!el || el->ea_count == 0)
263 return EXT2_ET_INVALID_ARGUMENT;
272 errcode_t ea_refcount_store(ext2_refcount_t refcount, blk_t blk, int count)
274 struct ea_refcount_el *el;
277 * Get the refcount element
279 el = get_refcount_el(refcount, blk, count ? 1 : 0);
281 return count ? EXT2_ET_NO_MEMORY : 0;
282 el->ea_count = count;
286 blk_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
291 return refcount->size;
294 void ea_refcount_intr_begin(ext2_refcount_t refcount)
296 refcount->cursor = 0;
300 blk_t ea_refcount_intr_next(ext2_refcount_t refcount,
303 struct ea_refcount_el *list;
306 if (refcount->cursor >= refcount->count)
308 list = refcount->list;
309 if (list[refcount->cursor].ea_count) {
311 *ret = list[refcount->cursor].ea_count;
312 return list[refcount->cursor++].ea_blk;
321 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
325 const char *bad = "bad refcount";
327 if (refcount->count > refcount->size) {
328 fprintf(out, "%s: count > size\n", bad);
329 return EXT2_ET_INVALID_ARGUMENT;
331 for (i=1; i < refcount->count; i++) {
332 if (refcount->list[i-1].ea_blk >= refcount->list[i].ea_blk) {
333 fprintf(out, "%s: list[%d].blk=%u, list[%d].blk=%u\n",
334 bad, i-1, refcount->list[i-1].ea_blk,
335 i, refcount->list[i].ea_blk);
336 ret = EXT2_ET_INVALID_ARGUMENT;
343 #define BCODE_CREATE 1
345 #define BCODE_STORE 3
348 #define BCODE_FETCH 6
349 #define BCODE_VALIDATE 7
351 #define BCODE_COLLAPSE 9
353 int bcode_program[] = {
388 int main(int argc, char **argv)
391 ext2_refcount_t refcount;
397 switch (bcode_program[i++]) {
401 size = bcode_program[i++];
402 retval = ea_refcount_create(size, &refcount);
404 com_err("ea_refcount_create",
408 printf("Creating refcount with size %d\n",
412 ea_refcount_free(refcount);
414 printf("Freeing refcount\n");
417 blk = (blk_t) bcode_program[i++];
418 arg = bcode_program[i++];
419 retval = ea_refcount_store(refcount, blk, arg);
420 printf("Storing blk %u with value %d\n", blk, arg);
422 com_err("ea_refcount_store", retval, "");
425 blk = (blk_t) bcode_program[i++];
426 retval = ea_refcount_fetch(refcount, blk, &arg);
428 com_err("ea_refcount_fetch", retval, "");
430 printf("bcode_fetch(%u) returns %d\n",
434 blk = (blk_t) bcode_program[i++];
435 retval = ea_refcount_increment(refcount, blk,
438 com_err("ea_refcount_increment", retval,
441 printf("bcode_increment(%u) returns %d\n",
445 blk = (blk_t) bcode_program[i++];
446 retval = ea_refcount_decrement(refcount, blk,
449 com_err("ea_refcount_decrement", retval,
450 "while decrementing blk %u", blk);
452 printf("bcode_decrement(%u) returns %d\n",
456 retval = ea_refcount_validate(refcount, stderr);
458 com_err("ea_refcount_validate",
461 printf("Refcount validation OK.\n");
464 ea_refcount_intr_begin(refcount);
466 blk = ea_refcount_intr_next(refcount,
470 printf("\tblk=%u, count=%d\n", blk,
475 refcount_collapse(refcount);