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,
113 blk64_t blk, int pos)
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 blk64_t blk, int create)
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) {
184 if (blk == refcount->list[mid].ea_blk) {
185 refcount->cursor = mid+1;
186 return &refcount->list[mid];
188 if (blk < refcount->list[mid].ea_blk)
194 * If we need to create a new entry, it should be right at
195 * low (where high will be left at low-1).
198 if (refcount->count >= refcount->size) {
199 refcount_collapse(refcount);
200 if (refcount->count < refcount->size)
203 return insert_refcount_el(refcount, blk, low);
208 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk64_t blk,
211 struct ea_refcount_el *el;
213 el = get_refcount_el(refcount, blk, 0);
222 errcode_t ea_refcount_increment(ext2_refcount_t refcount, blk64_t blk, int *ret)
224 struct ea_refcount_el *el;
226 el = get_refcount_el(refcount, blk, 1);
228 return EXT2_ET_NO_MEMORY;
236 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, blk64_t blk, int *ret)
238 struct ea_refcount_el *el;
240 el = get_refcount_el(refcount, blk, 0);
241 if (!el || el->ea_count == 0)
242 return EXT2_ET_INVALID_ARGUMENT;
251 errcode_t ea_refcount_store(ext2_refcount_t refcount, blk64_t blk, int count)
253 struct ea_refcount_el *el;
256 * Get the refcount element
258 el = get_refcount_el(refcount, blk, count ? 1 : 0);
260 return count ? EXT2_ET_NO_MEMORY : 0;
261 el->ea_count = count;
265 blk_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
270 return refcount->size;
273 void ea_refcount_intr_begin(ext2_refcount_t refcount)
275 refcount->cursor = 0;
279 blk64_t ea_refcount_intr_next(ext2_refcount_t refcount,
282 struct ea_refcount_el *list;
285 if (refcount->cursor >= refcount->count)
287 list = refcount->list;
288 if (list[refcount->cursor].ea_count) {
290 *ret = list[refcount->cursor].ea_count;
291 return list[refcount->cursor++].ea_blk;
300 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
304 const char *bad = "bad refcount";
306 if (refcount->count > refcount->size) {
307 fprintf(out, "%s: count > size\n", bad);
308 return EXT2_ET_INVALID_ARGUMENT;
310 for (i=1; i < refcount->count; i++) {
311 if (refcount->list[i-1].ea_blk >= refcount->list[i].ea_blk) {
313 "%s: list[%d].blk=%llu, list[%d].blk=%llu\n",
314 bad, i-1, refcount->list[i-1].ea_blk,
315 i, refcount->list[i].ea_blk);
316 ret = EXT2_ET_INVALID_ARGUMENT;
323 #define BCODE_CREATE 1
325 #define BCODE_STORE 3
328 #define BCODE_FETCH 6
329 #define BCODE_VALIDATE 7
331 #define BCODE_COLLAPSE 9
333 int bcode_program[] = {
368 int main(int argc, char **argv)
371 ext2_refcount_t refcount;
377 switch (bcode_program[i++]) {
381 size = bcode_program[i++];
382 retval = ea_refcount_create(size, &refcount);
384 com_err("ea_refcount_create", retval,
385 "while creating size %d", size);
388 printf("Creating refcount with size %d\n",
392 ea_refcount_free(refcount);
394 printf("Freeing refcount\n");
397 blk = (blk_t) bcode_program[i++];
398 arg = bcode_program[i++];
399 printf("Storing blk %llu with value %d\n", blk, arg);
400 retval = ea_refcount_store(refcount, blk, arg);
402 com_err("ea_refcount_store", retval,
403 "while storing blk %llu", blk);
406 blk = (blk_t) bcode_program[i++];
407 retval = ea_refcount_fetch(refcount, blk, &arg);
409 com_err("ea_refcount_fetch", retval,
410 "while fetching blk %llu", blk);
412 printf("bcode_fetch(%llu) returns %d\n",
416 blk = (blk_t) bcode_program[i++];
417 retval = ea_refcount_increment(refcount, blk, &arg);
419 com_err("ea_refcount_increment", retval,
420 "while incrementing blk %llu", blk);
422 printf("bcode_increment(%llu) returns %d\n",
426 blk = (blk_t) bcode_program[i++];
427 retval = ea_refcount_decrement(refcount, blk, &arg);
429 com_err("ea_refcount_decrement", retval,
430 "while decrementing blk %llu", blk);
432 printf("bcode_decrement(%llu) returns %d\n",
436 retval = ea_refcount_validate(refcount, stderr);
438 com_err("ea_refcount_validate", retval,
441 printf("Refcount validation OK.\n");
444 ea_refcount_intr_begin(refcount);
446 blk = ea_refcount_intr_next(refcount, &arg);
449 printf("\tblk=%llu, count=%d\n", blk, arg);
453 refcount_collapse(refcount);