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 {
28 /* ea_key could either be an inode number or block number. */
37 struct ea_refcount_el *list;
40 void ea_refcount_free(ext2_refcount_t refcount)
46 ext2fs_free_mem(&refcount->list);
47 ext2fs_free_mem(&refcount);
50 errcode_t ea_refcount_create(size_t size, ext2_refcount_t *ret)
52 ext2_refcount_t refcount;
56 retval = ext2fs_get_mem(sizeof(struct ea_refcount), &refcount);
59 memset(refcount, 0, sizeof(struct ea_refcount));
63 refcount->size = size;
64 bytes = size * sizeof(struct ea_refcount_el);
66 printf("Refcount allocated %zu entries, %zu bytes.\n",
67 refcount->size, bytes);
69 retval = ext2fs_get_mem(bytes, &refcount->list);
72 memset(refcount->list, 0, bytes);
81 ea_refcount_free(refcount);
86 * collapse_refcount() --- go through the refcount array, and get rid
87 * of any count == zero entries
89 static void refcount_collapse(ext2_refcount_t refcount)
92 struct ea_refcount_el *list;
94 list = refcount->list;
95 for (i = 0, j = 0; i < refcount->count; i++) {
96 if (list[i].ea_value) {
102 #if defined(DEBUG) || defined(TEST_PROGRAM)
103 printf("Refcount_collapse: size was %zu, now %d\n",
111 * insert_refcount_el() --- Insert a new entry into the sorted list at a
112 * specified position.
114 static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
115 ea_key_t ea_key, int pos)
117 struct ea_refcount_el *el;
122 if (refcount->count >= refcount->size) {
123 new_size = refcount->size + 100;
125 printf("Reallocating refcount %d entries...\n", new_size);
127 retval = ext2fs_resize_mem((size_t) refcount->size *
128 sizeof(struct ea_refcount_el),
130 sizeof(struct ea_refcount_el),
134 refcount->size = new_size;
136 num = (int) refcount->count - pos;
138 return 0; /* should never happen */
140 memmove(&refcount->list[pos+1], &refcount->list[pos],
141 sizeof(struct ea_refcount_el) * num);
144 el = &refcount->list[pos];
152 * get_refcount_el() --- given an block number, try to find refcount
153 * information in the sorted list. If the create flag is set,
154 * and we can't find an entry, create one in the sorted list.
156 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
157 ea_key_t ea_key, int create)
161 if (!refcount || !refcount->list)
165 high = (int) refcount->count-1;
166 if (create && ((refcount->count == 0) ||
167 (ea_key > refcount->list[high].ea_key))) {
168 if (refcount->count >= refcount->size)
169 refcount_collapse(refcount);
171 return insert_refcount_el(refcount, ea_key,
172 (unsigned) refcount->count);
174 if (refcount->count == 0)
177 if (refcount->cursor >= refcount->count)
178 refcount->cursor = 0;
179 if (ea_key == refcount->list[refcount->cursor].ea_key)
180 return &refcount->list[refcount->cursor++];
182 printf("Non-cursor get_refcount_el: %u\n", ea_key);
184 while (low <= high) {
186 if (ea_key == refcount->list[mid].ea_key) {
187 refcount->cursor = mid+1;
188 return &refcount->list[mid];
190 if (ea_key < refcount->list[mid].ea_key)
196 * If we need to create a new entry, it should be right at
197 * low (where high will be left at low-1).
200 if (refcount->count >= refcount->size) {
201 refcount_collapse(refcount);
202 if (refcount->count < refcount->size)
205 return insert_refcount_el(refcount, ea_key, low);
210 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, ea_key_t ea_key,
213 struct ea_refcount_el *el;
215 el = get_refcount_el(refcount, ea_key, 0);
224 errcode_t ea_refcount_increment(ext2_refcount_t refcount, ea_key_t ea_key,
227 struct ea_refcount_el *el;
229 el = get_refcount_el(refcount, ea_key, 1);
231 return EXT2_ET_NO_MEMORY;
239 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, ea_key_t ea_key,
242 struct ea_refcount_el *el;
244 el = get_refcount_el(refcount, ea_key, 0);
245 if (!el || el->ea_value == 0)
246 return EXT2_ET_INVALID_ARGUMENT;
255 errcode_t ea_refcount_store(ext2_refcount_t refcount, ea_key_t ea_key,
258 struct ea_refcount_el *el;
261 * Get the refcount element
263 el = get_refcount_el(refcount, ea_key, ea_value ? 1 : 0);
265 return ea_value ? EXT2_ET_NO_MEMORY : 0;
266 el->ea_value = ea_value;
270 size_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
275 return refcount->size;
278 void ea_refcount_intr_begin(ext2_refcount_t refcount)
280 refcount->cursor = 0;
283 ea_key_t ea_refcount_intr_next(ext2_refcount_t refcount,
286 struct ea_refcount_el *list;
289 if (refcount->cursor >= refcount->count)
291 list = refcount->list;
292 if (list[refcount->cursor].ea_value) {
294 *ret = list[refcount->cursor].ea_value;
295 return list[refcount->cursor++].ea_key;
304 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
308 const char *bad = "bad refcount";
310 if (refcount->count > refcount->size) {
311 fprintf(out, "%s: count > size\n", bad);
312 return EXT2_ET_INVALID_ARGUMENT;
314 for (i=1; i < refcount->count; i++) {
315 if (refcount->list[i-1].ea_key >= refcount->list[i].ea_key) {
317 "%s: list[%d].ea_key=%llu, list[%d].ea_key=%llu\n",
318 bad, i-1, refcount->list[i-1].ea_key, i,
319 refcount->list[i].ea_key);
320 ret = EXT2_ET_INVALID_ARGUMENT;
327 #define BCODE_CREATE 1
329 #define BCODE_STORE 3
332 #define BCODE_FETCH 6
333 #define BCODE_VALIDATE 7
335 #define BCODE_COLLAPSE 9
337 int bcode_program[] = {
372 int main(int argc, char **argv)
375 ext2_refcount_t refcount;
382 switch (bcode_program[i++]) {
386 size = bcode_program[i++];
387 retval = ea_refcount_create(size, &refcount);
389 com_err("ea_refcount_create", retval,
390 "while creating size %zu", size);
393 printf("Creating refcount with size %zu\n",
397 ea_refcount_free(refcount);
399 printf("Freeing refcount\n");
402 ea_key = (size_t) bcode_program[i++];
403 arg = bcode_program[i++];
404 printf("Storing ea_key %llu with value %llu\n", ea_key,
406 retval = ea_refcount_store(refcount, ea_key, arg);
408 com_err("ea_refcount_store", retval,
409 "while storing ea_key %llu", ea_key);
412 ea_key = (size_t) bcode_program[i++];
413 retval = ea_refcount_fetch(refcount, ea_key, &arg);
415 com_err("ea_refcount_fetch", retval,
416 "while fetching ea_key %llu", ea_key);
418 printf("bcode_fetch(%llu) returns %llu\n",
422 ea_key = (size_t) bcode_program[i++];
423 retval = ea_refcount_increment(refcount, ea_key, &arg);
425 com_err("ea_refcount_increment", retval,
426 "while incrementing ea_key %llu",
429 printf("bcode_increment(%llu) returns %llu\n",
433 ea_key = (size_t) bcode_program[i++];
434 retval = ea_refcount_decrement(refcount, ea_key, &arg);
436 com_err("ea_refcount_decrement", retval,
437 "while decrementing ea_key %llu",
440 printf("bcode_decrement(%llu) returns %llu\n",
444 retval = ea_refcount_validate(refcount, stderr);
446 com_err("ea_refcount_validate", retval,
449 printf("Refcount validation OK.\n");
452 ea_refcount_intr_begin(refcount);
454 ea_key = ea_refcount_intr_next(refcount, &arg);
457 printf("\tea_key=%llu, count=%llu\n", ea_key,
462 refcount_collapse(refcount);