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;
52 retval = ext2fs_get_mem(sizeof(struct ea_refcount),
56 memset(refcount, 0, sizeof(struct ea_refcount));
60 refcount->size = size;
61 bytes = (size_t) (size * sizeof(struct ea_refcount_el));
63 printf("Refcount allocated %d entries, %d bytes.\n",
64 refcount->size, bytes);
66 retval = ext2fs_get_mem(bytes, (void **) &refcount->list);
69 memset(refcount->list, 0, bytes);
78 ea_refcount_free(refcount);
83 * collapse_refcount() --- go through the refcount array, and get rid
84 * of any count == zero entries
86 static void refcount_collapse(ext2_refcount_t refcount)
89 struct ea_refcount_el *list;
91 list = refcount->list;
92 for (i = 0, j = 0; i < refcount->count; i++) {
93 if (list[i].ea_count) {
99 #if defined(DEBUG) || defined(TEST_PROGRAM)
100 printf("Refcount_collapse: size was %d, now %d\n",
108 * insert_refcount_el() --- Insert a new entry into the sorted list at a
109 * specified position.
111 static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
114 struct ea_refcount_el *el;
119 if (refcount->count >= refcount->size) {
120 new_size = refcount->size + 100;
122 printf("Reallocating refcount %d entries...\n", new_size);
124 retval = ext2fs_resize_mem((size_t) refcount->size *
125 sizeof(struct ea_refcount_el),
127 sizeof(struct ea_refcount_el),
128 (void **) &refcount->list);
131 refcount->size = new_size;
133 num = (int) refcount->count - pos;
135 return 0; /* should never happen */
137 memmove(&refcount->list[pos+1], &refcount->list[pos],
138 sizeof(struct ea_refcount_el) * num);
141 el = &refcount->list[pos];
149 * get_refcount_el() --- given an block number, try to find refcount
150 * information in the sorted list. If the create flag is set,
151 * and we can't find an entry, create one in the sorted list.
153 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
154 blk_t blk, int create)
158 blk_t lowval, highval;
160 if (!refcount || !refcount->list)
164 high = (int) refcount->count-1;
165 if (create && ((refcount->count == 0) ||
166 (blk > refcount->list[high].ea_blk))) {
167 if (refcount->count >= refcount->size)
168 refcount_collapse(refcount);
170 return insert_refcount_el(refcount, blk,
171 (unsigned) refcount->count);
173 if (refcount->count == 0)
176 if (refcount->cursor >= refcount->count)
177 refcount->cursor = 0;
178 if (blk == refcount->list[refcount->cursor].ea_blk)
179 return &refcount->list[refcount->cursor++];
181 printf("Non-cursor get_refcount_el: %u\n", blk);
183 while (low <= high) {
190 /* Interpolate for efficiency */
191 lowval = refcount->list[low].ea_blk;
192 highval = refcount->list[high].ea_blk;
196 else if (blk > highval)
199 range = ((float) (blk - lowval)) /
201 mid = low + ((int) (range * (high-low)));
204 if (blk == refcount->list[mid].ea_blk) {
205 refcount->cursor = mid+1;
206 return &refcount->list[mid];
208 if (blk < refcount->list[mid].ea_blk)
214 * If we need to create a new entry, it should be right at
215 * low (where high will be left at low-1).
218 if (refcount->count >= refcount->size) {
219 refcount_collapse(refcount);
220 if (refcount->count < refcount->size)
223 return insert_refcount_el(refcount, blk, low);
228 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk_t blk,
231 struct ea_refcount_el *el;
233 el = get_refcount_el(refcount, blk, 0);
242 errcode_t ea_refcount_increment(ext2_refcount_t refcount, blk_t blk, int *ret)
244 struct ea_refcount_el *el;
246 el = get_refcount_el(refcount, blk, 1);
248 return EXT2_ET_NO_MEMORY;
256 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, blk_t blk, int *ret)
258 struct ea_refcount_el *el;
260 el = get_refcount_el(refcount, blk, 0);
261 if (!el || el->ea_count == 0)
262 return EXT2_ET_INVALID_ARGUMENT;
271 errcode_t ea_refcount_store(ext2_refcount_t refcount, blk_t blk, int count)
273 struct ea_refcount_el *el;
276 * Get the refcount element
278 el = get_refcount_el(refcount, blk, count ? 1 : 0);
280 return count ? EXT2_ET_NO_MEMORY : 0;
281 el->ea_count = count;
285 blk_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
290 return refcount->size;
293 void ea_refcount_intr_begin(ext2_refcount_t refcount)
295 refcount->cursor = 0;
299 blk_t ea_refcount_intr_next(ext2_refcount_t refcount,
302 struct ea_refcount_el *list;
305 if (refcount->cursor >= refcount->count)
307 list = refcount->list;
308 if (list[refcount->cursor].ea_count) {
310 *ret = list[refcount->cursor].ea_count;
311 return list[refcount->cursor++].ea_blk;
320 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
324 const char *bad = "bad refcount";
326 if (refcount->count > refcount->size) {
327 fprintf(out, "%s: count > size\n", bad);
328 return EXT2_ET_INVALID_ARGUMENT;
330 for (i=1; i < refcount->count; i++) {
331 if (refcount->list[i-1].ea_blk >= refcount->list[i].ea_blk) {
332 fprintf(out, "%s: list[%d].blk=%u, list[%d].blk=%u\n",
333 bad, i-1, refcount->list[i-1].ea_blk,
334 i, refcount->list[i].ea_blk);
335 ret = EXT2_ET_INVALID_ARGUMENT;
342 #define BCODE_CREATE 1
344 #define BCODE_STORE 3
347 #define BCODE_FETCH 6
348 #define BCODE_VALIDATE 7
350 #define BCODE_COLLAPSE 9
352 int bcode_program[] = {
387 int main(int argc, char **argv)
390 ext2_refcount_t refcount;
396 switch (bcode_program[i++]) {
400 size = bcode_program[i++];
401 retval = ea_refcount_create(size, &refcount);
403 com_err("ea_refcount_create",
407 printf("Creating refcount with size %d\n",
411 ea_refcount_free(refcount);
413 printf("Freeing refcount\n");
416 blk = (blk_t) bcode_program[i++];
417 arg = bcode_program[i++];
418 retval = ea_refcount_store(refcount, blk, arg);
419 printf("Storing blk %u with value %d\n", blk, arg);
421 com_err("ea_refcount_store", retval, "");
424 blk = (blk_t) bcode_program[i++];
425 retval = ea_refcount_fetch(refcount, blk, &arg);
427 com_err("ea_refcount_fetch", retval, "");
429 printf("bcode_fetch(%u) returns %d\n",
433 blk = (blk_t) bcode_program[i++];
434 retval = ea_refcount_increment(refcount, blk,
437 com_err("ea_refcount_increment", retval,
440 printf("bcode_increment(%u) returns %d\n",
444 blk = (blk_t) bcode_program[i++];
445 retval = ea_refcount_decrement(refcount, blk,
448 com_err("ea_refcount_decrement", retval,
449 "while decrementing blk %u", blk);
451 printf("bcode_decrement(%u) returns %d\n",
455 retval = ea_refcount_validate(refcount, stderr);
457 com_err("ea_refcount_validate",
460 printf("Refcount validation OK.\n");
463 ea_refcount_intr_begin(refcount);
465 blk = ea_refcount_intr_next(refcount,
469 printf("\tblk=%u, count=%d\n", blk,
474 refcount_collapse(refcount);