4 * Copyright (C) 2001 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
17 * The strategy we use for keeping track of EA refcounts is as
18 * follows. We keep a sorted array of first EA blocks and its
19 * reference counts. Once the refcount has dropped to zero, it is
20 * removed from the array to save memory space. Once the EA block is
21 * checked, its bit is set in the block_ea_map bitmap.
23 struct ea_refcount_el {
32 struct ea_refcount_el *list;
35 void ea_refcount_free(ext2_refcount_t refcount)
41 ext2fs_free_mem((void **) &refcount->list);
42 ext2fs_free_mem((void **) &refcount);
45 errcode_t ea_refcount_create(int size, ext2_refcount_t *ret)
47 ext2_refcount_t refcount;
51 retval = ext2fs_get_mem(sizeof(struct ea_refcount),
55 memset(refcount, 0, sizeof(struct ea_refcount));
59 refcount->size = size;
60 bytes = (size_t) (size * sizeof(struct ea_refcount_el));
62 printf("Refcount allocated %d entries, %d bytes.\n",
63 refcount->size, bytes);
65 retval = ext2fs_get_mem(bytes, (void **) &refcount->list);
68 memset(refcount->list, 0, bytes);
77 ea_refcount_free(refcount);
82 * collapse_refcount() --- go through the refcount array, and get rid
83 * of any count == zero entries
85 static void refcount_collapse(ext2_refcount_t refcount)
88 struct ea_refcount_el *list;
90 list = refcount->list;
91 for (i = 0, j = 0; i < refcount->count; i++) {
92 if (list[i].ea_count) {
98 #if defined(DEBUG) || defined(TEST_PROGRAM)
99 printf("Refcount_collapse: size was %d, now %d\n",
107 * insert_refcount_el() --- Insert a new entry into the sorted list at a
108 * specified position.
110 static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
113 struct ea_refcount_el *el;
118 if (refcount->count >= refcount->size) {
119 new_size = refcount->size + 100;
121 printf("Reallocating refcount %d entries...\n", new_size);
123 retval = ext2fs_resize_mem((size_t) refcount->size *
124 sizeof(struct ea_refcount_el),
126 sizeof(struct ea_refcount_el),
127 (void **) &refcount->list);
130 refcount->size = new_size;
132 num = (int) refcount->count - pos;
134 return 0; /* should never happen */
136 memmove(&refcount->list[pos+1], &refcount->list[pos],
137 sizeof(struct ea_refcount_el) * num);
140 el = &refcount->list[pos];
148 * get_refcount_el() --- given an block number, try to find refcount
149 * information in the sorted list. If the create flag is set,
150 * and we can't find an entry, create one in the sorted list.
152 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
153 blk_t blk, int create)
157 blk_t lowval, highval;
159 if (!refcount || !refcount->list)
163 high = (int) refcount->count-1;
164 if (create && ((refcount->count == 0) ||
165 (blk > refcount->list[high].ea_blk))) {
166 if (refcount->count >= refcount->size)
167 refcount_collapse(refcount);
169 return insert_refcount_el(refcount, blk,
170 (unsigned) refcount->count);
172 if (refcount->count == 0)
175 if (refcount->cursor >= refcount->count)
176 refcount->cursor = 0;
177 if (blk == refcount->list[refcount->cursor].ea_blk)
178 return &refcount->list[refcount->cursor++];
180 printf("Non-cursor get_refcount_el: %u\n", blk);
182 while (low <= high) {
189 /* Interpolate for efficiency */
190 lowval = refcount->list[low].ea_blk;
191 highval = refcount->list[high].ea_blk;
195 else if (blk > highval)
198 range = ((float) (blk - lowval)) /
200 mid = low + ((int) (range * (high-low)));
203 if (blk == refcount->list[mid].ea_blk) {
204 refcount->cursor = mid+1;
205 return &refcount->list[mid];
207 if (blk < refcount->list[mid].ea_blk)
213 * If we need to create a new entry, it should be right at
214 * low (where high will be left at low-1).
217 if (refcount->count >= refcount->size) {
218 refcount_collapse(refcount);
219 if (refcount->count < refcount->size)
222 return insert_refcount_el(refcount, blk, low);
227 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk_t blk,
230 struct ea_refcount_el *el;
232 el = get_refcount_el(refcount, blk, 0);
241 errcode_t ea_refcount_increment(ext2_refcount_t refcount, blk_t blk, int *ret)
243 struct ea_refcount_el *el;
245 el = get_refcount_el(refcount, blk, 1);
247 return EXT2_ET_NO_MEMORY;
255 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, blk_t blk, int *ret)
257 struct ea_refcount_el *el;
259 el = get_refcount_el(refcount, blk, 0);
260 if (!el || el->ea_count == 0)
261 return EXT2_ET_INVALID_ARGUMENT;
270 errcode_t ea_refcount_store(ext2_refcount_t refcount, blk_t blk, int count)
272 struct ea_refcount_el *el;
275 * Get the refcount element
277 el = get_refcount_el(refcount, blk, count ? 1 : 0);
279 return count ? EXT2_ET_NO_MEMORY : 0;
280 el->ea_count = count;
284 blk_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
289 return refcount->size;
292 void ea_refcount_intr_begin(ext2_refcount_t refcount)
294 refcount->cursor = 0;
298 blk_t ea_refcount_intr_next(ext2_refcount_t refcount,
301 struct ea_refcount_el *list;
304 if (refcount->cursor >= refcount->count)
306 list = refcount->list;
307 if (list[refcount->cursor].ea_count) {
309 *ret = list[refcount->cursor].ea_count;
310 return list[refcount->cursor++].ea_blk;
319 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
323 const char *bad = "bad refcount";
325 if (refcount->count > refcount->size) {
326 fprintf(out, "%s: count > size\n", bad);
327 return EXT2_ET_INVALID_ARGUMENT;
329 for (i=1; i < refcount->count; i++) {
330 if (refcount->list[i-1].ea_blk >= refcount->list[i].ea_blk) {
331 fprintf(out, "%s: list[%d].blk=%u, list[%d].blk=%u\n",
332 bad, i-1, refcount->list[i-1].ea_blk,
333 i, refcount->list[i].ea_blk);
334 ret = EXT2_ET_INVALID_ARGUMENT;
341 #define BCODE_CREATE 1
343 #define BCODE_STORE 3
346 #define BCODE_FETCH 6
347 #define BCODE_VALIDATE 7
349 #define BCODE_COLLAPSE 9
351 int bcode_program[] = {
386 int main(int argc, char **argv)
389 ext2_refcount_t refcount;
395 switch (bcode_program[i++]) {
399 size = bcode_program[i++];
400 retval = ea_refcount_create(size, &refcount);
402 com_err("ea_refcount_create",
406 printf("Creating refcount with size %d\n",
410 ea_refcount_free(refcount);
412 printf("Freeing refcount\n");
415 blk = (blk_t) bcode_program[i++];
416 arg = bcode_program[i++];
417 retval = ea_refcount_store(refcount, blk, arg);
418 printf("Storing blk %u with value %d\n", blk, arg);
420 com_err("ea_refcount_store", retval, "");
423 blk = (blk_t) bcode_program[i++];
424 retval = ea_refcount_fetch(refcount, blk, &arg);
426 com_err("ea_refcount_fetch", retval, "");
428 printf("bcode_fetch(%u) returns %d\n",
432 blk = (blk_t) bcode_program[i++];
433 retval = ea_refcount_increment(refcount, blk,
436 com_err("ea_refcount_increment", retval,
439 printf("bcode_increment(%u) returns %d\n",
443 blk = (blk_t) bcode_program[i++];
444 retval = ea_refcount_decrement(refcount, blk,
447 com_err("ea_refcount_decrement", retval,
448 "while decrementing blk %u", blk);
450 printf("bcode_decrement(%u) returns %d\n",
454 retval = ea_refcount_validate(refcount, stderr);
456 com_err("ea_refcount_validate",
459 printf("Refcount validation OK.\n");
462 ea_refcount_intr_begin(refcount);
464 blk = ea_refcount_intr_next(refcount,
468 printf("\tblk=%u, count=%d\n", blk,
473 refcount_collapse(refcount);