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_memzero(sizeof(struct ea_refcount), &refcount);
62 refcount->size = size;
63 bytes = size * sizeof(struct ea_refcount_el);
65 printf("Refcount allocated %zu entries, %zu bytes.\n",
66 refcount->size, bytes);
68 retval = ext2fs_get_memzero(bytes, &refcount->list);
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_value) {
100 #if defined(DEBUG) || defined(TEST_PROGRAM)
101 printf("Refcount_collapse: size was %zu, 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 ea_key_t ea_key, 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 ea_key_t ea_key, int create)
159 if (!refcount || !refcount->list)
163 high = (int) refcount->count-1;
164 if (create && ((refcount->count == 0) ||
165 (ea_key > refcount->list[high].ea_key))) {
166 if (refcount->count >= refcount->size)
167 refcount_collapse(refcount);
169 return insert_refcount_el(refcount, ea_key,
170 (unsigned) refcount->count);
172 if (refcount->count == 0)
175 if (refcount->cursor >= refcount->count)
176 refcount->cursor = 0;
177 if (ea_key == refcount->list[refcount->cursor].ea_key)
178 return &refcount->list[refcount->cursor++];
180 printf("Non-cursor get_refcount_el: %u\n", ea_key);
182 while (low <= high) {
184 if (ea_key == refcount->list[mid].ea_key) {
185 refcount->cursor = mid+1;
186 return &refcount->list[mid];
188 if (ea_key < refcount->list[mid].ea_key)
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, ea_key, low);
208 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, ea_key_t ea_key,
211 struct ea_refcount_el *el;
213 el = get_refcount_el(refcount, ea_key, 0);
222 errcode_t ea_refcount_increment(ext2_refcount_t refcount, ea_key_t ea_key,
225 struct ea_refcount_el *el;
227 el = get_refcount_el(refcount, ea_key, 1);
229 return EXT2_ET_NO_MEMORY;
237 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, ea_key_t ea_key,
240 struct ea_refcount_el *el;
242 el = get_refcount_el(refcount, ea_key, 0);
243 if (!el || el->ea_value == 0)
244 return EXT2_ET_INVALID_ARGUMENT;
253 errcode_t ea_refcount_store(ext2_refcount_t refcount, ea_key_t ea_key,
256 struct ea_refcount_el *el;
259 * Get the refcount element
261 el = get_refcount_el(refcount, ea_key, ea_value ? 1 : 0);
263 return ea_value ? EXT2_ET_NO_MEMORY : 0;
264 el->ea_value = ea_value;
268 size_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
273 return refcount->size;
276 void ea_refcount_intr_begin(ext2_refcount_t refcount)
278 refcount->cursor = 0;
281 ea_key_t ea_refcount_intr_next(ext2_refcount_t refcount,
284 struct ea_refcount_el *list;
287 if (refcount->cursor >= refcount->count)
289 list = refcount->list;
290 if (list[refcount->cursor].ea_value) {
292 *ret = list[refcount->cursor].ea_value;
293 return list[refcount->cursor++].ea_key;
302 errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
306 const char *bad = "bad refcount";
308 if (refcount->count > refcount->size) {
309 fprintf(out, "%s: count > size\n", bad);
310 return EXT2_ET_INVALID_ARGUMENT;
312 for (i=1; i < refcount->count; i++) {
313 if (refcount->list[i-1].ea_key >= refcount->list[i].ea_key) {
315 "%s: list[%d].ea_key=%llu, list[%d].ea_key=%llu\n",
316 bad, i-1, refcount->list[i-1].ea_key, i,
317 refcount->list[i].ea_key);
318 ret = EXT2_ET_INVALID_ARGUMENT;
325 #define BCODE_CREATE 1
327 #define BCODE_STORE 3
330 #define BCODE_FETCH 6
331 #define BCODE_VALIDATE 7
333 #define BCODE_COLLAPSE 9
335 int bcode_program[] = {
370 int main(int argc, char **argv)
373 ext2_refcount_t refcount;
380 switch (bcode_program[i++]) {
384 size = bcode_program[i++];
385 retval = ea_refcount_create(size, &refcount);
387 com_err("ea_refcount_create", retval,
388 "while creating size %zu", size);
391 printf("Creating refcount with size %zu\n",
395 ea_refcount_free(refcount);
397 printf("Freeing refcount\n");
400 ea_key = (size_t) bcode_program[i++];
401 arg = bcode_program[i++];
402 printf("Storing ea_key %llu with value %llu\n", ea_key,
404 retval = ea_refcount_store(refcount, ea_key, arg);
406 com_err("ea_refcount_store", retval,
407 "while storing ea_key %llu", ea_key);
410 ea_key = (size_t) bcode_program[i++];
411 retval = ea_refcount_fetch(refcount, ea_key, &arg);
413 com_err("ea_refcount_fetch", retval,
414 "while fetching ea_key %llu", ea_key);
416 printf("bcode_fetch(%llu) returns %llu\n",
420 ea_key = (size_t) bcode_program[i++];
421 retval = ea_refcount_increment(refcount, ea_key, &arg);
423 com_err("ea_refcount_increment", retval,
424 "while incrementing ea_key %llu",
427 printf("bcode_increment(%llu) returns %llu\n",
431 ea_key = (size_t) bcode_program[i++];
432 retval = ea_refcount_decrement(refcount, ea_key, &arg);
434 com_err("ea_refcount_decrement", retval,
435 "while decrementing ea_key %llu",
438 printf("bcode_decrement(%llu) returns %llu\n",
442 retval = ea_refcount_validate(refcount, stderr);
444 com_err("ea_refcount_validate", retval,
447 printf("Refcount validation OK.\n");
450 ea_refcount_intr_begin(refcount);
452 ea_key = ea_refcount_intr_next(refcount, &arg);
455 printf("\tea_key=%llu, count=%llu\n", ea_key,
460 refcount_collapse(refcount);