4 * Copyright (C) 2001 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
21 * The strategy we use for keeping track of EA refcounts is as
22 * follows. We keep a sorted array of first EA blocks and its
23 * reference counts. Once the refcount has dropped to zero, it is
24 * removed from the array to save memory space. Once the EA block is
25 * checked, its bit is set in the block_ea_map bitmap.
27 struct ea_refcount_el {
36 struct ea_refcount_el *list;
39 void ea_refcount_free(ext2_refcount_t refcount)
45 ext2fs_free_mem(&refcount->list);
46 ext2fs_free_mem(&refcount);
49 errcode_t ea_refcount_create(int size, ext2_refcount_t *ret)
51 ext2_refcount_t refcount;
55 retval = ext2fs_get_mem(sizeof(struct ea_refcount), &refcount);
58 memset(refcount, 0, sizeof(struct ea_refcount));
62 refcount->size = size;
63 bytes = (size_t) (size * sizeof(struct ea_refcount_el));
65 printf("Refcount allocated %d entries, %d bytes.\n",
66 refcount->size, bytes);
68 retval = ext2fs_get_mem(bytes, &refcount->list);
71 memset(refcount->list, 0, bytes);
80 ea_refcount_free(refcount);
85 * collapse_refcount() --- go through the refcount array, and get rid
86 * of any count == zero entries
88 static void refcount_collapse(ext2_refcount_t refcount)
91 struct ea_refcount_el *list;
93 list = refcount->list;
94 for (i = 0, j = 0; i < refcount->count; i++) {
95 if (list[i].ea_count) {
101 #if defined(DEBUG) || defined(TEST_PROGRAM)
102 printf("Refcount_collapse: size was %d, now %d\n",
110 * insert_refcount_el() --- Insert a new entry into the sorted list at a
111 * specified position.
113 static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
114 blk64_t blk, int pos)
116 struct ea_refcount_el *el;
121 if (refcount->count >= refcount->size) {
122 new_size = refcount->size + 100;
124 printf("Reallocating refcount %d entries...\n", new_size);
126 retval = ext2fs_resize_mem((size_t) refcount->size *
127 sizeof(struct ea_refcount_el),
129 sizeof(struct ea_refcount_el),
133 refcount->size = new_size;
135 num = (int) refcount->count - pos;
137 return 0; /* should never happen */
139 memmove(&refcount->list[pos+1], &refcount->list[pos],
140 sizeof(struct ea_refcount_el) * num);
143 el = &refcount->list[pos];
151 * get_refcount_el() --- given an block number, try to find refcount
152 * information in the sorted list. If the create flag is set,
153 * and we can't find an entry, create one in the sorted list.
155 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
156 blk64_t blk, int create)
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) {
185 if (blk == refcount->list[mid].ea_blk) {
186 refcount->cursor = mid+1;
187 return &refcount->list[mid];
189 if (blk < refcount->list[mid].ea_blk)
195 * If we need to create a new entry, it should be right at
196 * low (where high will be left at low-1).
199 if (refcount->count >= refcount->size) {
200 refcount_collapse(refcount);
201 if (refcount->count < refcount->size)
204 return insert_refcount_el(refcount, blk, low);
209 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk64_t blk,
212 struct ea_refcount_el *el;
214 el = get_refcount_el(refcount, blk, 0);
223 errcode_t ea_refcount_increment(ext2_refcount_t refcount, blk64_t blk, int *ret)
225 struct ea_refcount_el *el;
227 el = get_refcount_el(refcount, blk, 1);
229 return EXT2_ET_NO_MEMORY;
237 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, blk64_t blk, int *ret)
239 struct ea_refcount_el *el;
241 el = get_refcount_el(refcount, blk, 0);
242 if (!el || el->ea_count == 0)
243 return EXT2_ET_INVALID_ARGUMENT;
252 errcode_t ea_refcount_store(ext2_refcount_t refcount, blk64_t blk, int count)
254 struct ea_refcount_el *el;
257 * Get the refcount element
259 el = get_refcount_el(refcount, blk, count ? 1 : 0);
261 return count ? EXT2_ET_NO_MEMORY : 0;
262 el->ea_count = count;
266 blk_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
271 return refcount->size;
274 void ea_refcount_intr_begin(ext2_refcount_t refcount)
276 refcount->cursor = 0;
280 blk64_t ea_refcount_intr_next(ext2_refcount_t refcount,
283 struct ea_refcount_el *list;
286 if (refcount->cursor >= refcount->count)
288 list = refcount->list;
289 if (list[refcount->cursor].ea_count) {
291 *ret = list[refcount->cursor].ea_count;
292 return list[refcount->cursor++].ea_blk;
301 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
305 const char *bad = "bad refcount";
307 if (refcount->count > refcount->size) {
308 fprintf(out, "%s: count > size\n", bad);
309 return EXT2_ET_INVALID_ARGUMENT;
311 for (i=1; i < refcount->count; i++) {
312 if (refcount->list[i-1].ea_blk >= refcount->list[i].ea_blk) {
314 "%s: list[%d].blk=%llu, list[%d].blk=%llu\n",
315 bad, i-1, refcount->list[i-1].ea_blk,
316 i, refcount->list[i].ea_blk);
317 ret = EXT2_ET_INVALID_ARGUMENT;
324 #define BCODE_CREATE 1
326 #define BCODE_STORE 3
329 #define BCODE_FETCH 6
330 #define BCODE_VALIDATE 7
332 #define BCODE_COLLAPSE 9
334 int bcode_program[] = {
369 int main(int argc, char **argv)
372 ext2_refcount_t refcount;
378 switch (bcode_program[i++]) {
382 size = bcode_program[i++];
383 retval = ea_refcount_create(size, &refcount);
385 com_err("ea_refcount_create", retval,
386 "while creating size %d", size);
389 printf("Creating refcount with size %d\n",
393 ea_refcount_free(refcount);
395 printf("Freeing refcount\n");
398 blk = (blk_t) bcode_program[i++];
399 arg = bcode_program[i++];
400 printf("Storing blk %llu with value %d\n", blk, arg);
401 retval = ea_refcount_store(refcount, blk, arg);
403 com_err("ea_refcount_store", retval,
404 "while storing blk %llu", blk);
407 blk = (blk_t) bcode_program[i++];
408 retval = ea_refcount_fetch(refcount, blk, &arg);
410 com_err("ea_refcount_fetch", retval,
411 "while fetching blk %llu", blk);
413 printf("bcode_fetch(%llu) returns %d\n",
417 blk = (blk_t) bcode_program[i++];
418 retval = ea_refcount_increment(refcount, blk, &arg);
420 com_err("ea_refcount_increment", retval,
421 "while incrementing blk %llu", blk);
423 printf("bcode_increment(%llu) returns %d\n",
427 blk = (blk_t) bcode_program[i++];
428 retval = ea_refcount_decrement(refcount, blk, &arg);
430 com_err("ea_refcount_decrement", retval,
431 "while decrementing blk %llu", blk);
433 printf("bcode_decrement(%llu) returns %d\n",
437 retval = ea_refcount_validate(refcount, stderr);
439 com_err("ea_refcount_validate", retval,
442 printf("Refcount validation OK.\n");
445 ea_refcount_intr_begin(refcount);
447 blk = ea_refcount_intr_next(refcount, &arg);
450 printf("\tblk=%llu, count=%d\n", blk, arg);
454 refcount_collapse(refcount);