]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - e2fsck/ea_refcount.c
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(&refcount
->list
);
42 ext2fs_free_mem(&refcount
);
45 errcode_t
ea_refcount_create(int size
, ext2_refcount_t
*ret
)
47 ext2_refcount_t refcount
;
51 retval
= ext2fs_get_mem(sizeof(struct ea_refcount
), &refcount
);
54 memset(refcount
, 0, sizeof(struct ea_refcount
));
58 refcount
->size
= size
;
59 bytes
= (size_t) (size
* sizeof(struct ea_refcount_el
));
61 printf("Refcount allocated %d entries, %d bytes.\n",
62 refcount
->size
, bytes
);
64 retval
= ext2fs_get_mem(bytes
, &refcount
->list
);
67 memset(refcount
->list
, 0, bytes
);
76 ea_refcount_free(refcount
);
81 * collapse_refcount() --- go through the refcount array, and get rid
82 * of any count == zero entries
84 static void refcount_collapse(ext2_refcount_t refcount
)
87 struct ea_refcount_el
*list
;
89 list
= refcount
->list
;
90 for (i
= 0, j
= 0; i
< refcount
->count
; i
++) {
91 if (list
[i
].ea_count
) {
97 #if defined(DEBUG) || defined(TEST_PROGRAM)
98 printf("Refcount_collapse: size was %d, now %d\n",
106 * insert_refcount_el() --- Insert a new entry into the sorted list at a
107 * specified position.
109 static struct ea_refcount_el
*insert_refcount_el(ext2_refcount_t refcount
,
112 struct ea_refcount_el
*el
;
117 if (refcount
->count
>= refcount
->size
) {
118 new_size
= refcount
->size
+ 100;
120 printf("Reallocating refcount %d entries...\n", new_size
);
122 retval
= ext2fs_resize_mem((size_t) refcount
->size
*
123 sizeof(struct ea_refcount_el
),
125 sizeof(struct ea_refcount_el
),
129 refcount
->size
= new_size
;
131 num
= (int) refcount
->count
- pos
;
133 return 0; /* should never happen */
135 memmove(&refcount
->list
[pos
+1], &refcount
->list
[pos
],
136 sizeof(struct ea_refcount_el
) * num
);
139 el
= &refcount
->list
[pos
];
147 * get_refcount_el() --- given an block number, try to find refcount
148 * information in the sorted list. If the create flag is set,
149 * and we can't find an entry, create one in the sorted list.
151 static struct ea_refcount_el
*get_refcount_el(ext2_refcount_t refcount
,
152 blk_t blk
, int create
)
156 blk_t lowval
, highval
;
158 if (!refcount
|| !refcount
->list
)
162 high
= (int) refcount
->count
-1;
163 if (create
&& ((refcount
->count
== 0) ||
164 (blk
> refcount
->list
[high
].ea_blk
))) {
165 if (refcount
->count
>= refcount
->size
)
166 refcount_collapse(refcount
);
168 return insert_refcount_el(refcount
, blk
,
169 (unsigned) refcount
->count
);
171 if (refcount
->count
== 0)
174 if (refcount
->cursor
>= refcount
->count
)
175 refcount
->cursor
= 0;
176 if (blk
== refcount
->list
[refcount
->cursor
].ea_blk
)
177 return &refcount
->list
[refcount
->cursor
++];
179 printf("Non-cursor get_refcount_el: %u\n", blk
);
181 while (low
<= high
) {
188 /* Interpolate for efficiency */
189 lowval
= refcount
->list
[low
].ea_blk
;
190 highval
= refcount
->list
[high
].ea_blk
;
194 else if (blk
> highval
)
197 range
= ((float) (blk
- lowval
)) /
199 mid
= low
+ ((int) (range
* (high
-low
)));
202 if (blk
== refcount
->list
[mid
].ea_blk
) {
203 refcount
->cursor
= mid
+1;
204 return &refcount
->list
[mid
];
206 if (blk
< refcount
->list
[mid
].ea_blk
)
212 * If we need to create a new entry, it should be right at
213 * low (where high will be left at low-1).
216 if (refcount
->count
>= refcount
->size
) {
217 refcount_collapse(refcount
);
218 if (refcount
->count
< refcount
->size
)
221 return insert_refcount_el(refcount
, blk
, low
);
226 errcode_t
ea_refcount_fetch(ext2_refcount_t refcount
, blk_t blk
,
229 struct ea_refcount_el
*el
;
231 el
= get_refcount_el(refcount
, blk
, 0);
240 errcode_t
ea_refcount_increment(ext2_refcount_t refcount
, blk_t blk
, int *ret
)
242 struct ea_refcount_el
*el
;
244 el
= get_refcount_el(refcount
, blk
, 1);
246 return EXT2_ET_NO_MEMORY
;
254 errcode_t
ea_refcount_decrement(ext2_refcount_t refcount
, blk_t blk
, int *ret
)
256 struct ea_refcount_el
*el
;
258 el
= get_refcount_el(refcount
, blk
, 0);
259 if (!el
|| el
->ea_count
== 0)
260 return EXT2_ET_INVALID_ARGUMENT
;
269 errcode_t
ea_refcount_store(ext2_refcount_t refcount
, blk_t blk
, int count
)
271 struct ea_refcount_el
*el
;
274 * Get the refcount element
276 el
= get_refcount_el(refcount
, blk
, count
? 1 : 0);
278 return count
? EXT2_ET_NO_MEMORY
: 0;
279 el
->ea_count
= count
;
283 blk_t
ext2fs_get_refcount_size(ext2_refcount_t refcount
)
288 return refcount
->size
;
291 void ea_refcount_intr_begin(ext2_refcount_t refcount
)
293 refcount
->cursor
= 0;
297 blk_t
ea_refcount_intr_next(ext2_refcount_t refcount
,
300 struct ea_refcount_el
*list
;
303 if (refcount
->cursor
>= refcount
->count
)
305 list
= refcount
->list
;
306 if (list
[refcount
->cursor
].ea_count
) {
308 *ret
= list
[refcount
->cursor
].ea_count
;
309 return list
[refcount
->cursor
++].ea_blk
;
318 errcode_t
ea_refcount_validate(ext2_refcount_t refcount
, FILE *out
)
322 const char *bad
= "bad refcount";
324 if (refcount
->count
> refcount
->size
) {
325 fprintf(out
, "%s: count > size\n", bad
);
326 return EXT2_ET_INVALID_ARGUMENT
;
328 for (i
=1; i
< refcount
->count
; i
++) {
329 if (refcount
->list
[i
-1].ea_blk
>= refcount
->list
[i
].ea_blk
) {
330 fprintf(out
, "%s: list[%d].blk=%u, list[%d].blk=%u\n",
331 bad
, i
-1, refcount
->list
[i
-1].ea_blk
,
332 i
, refcount
->list
[i
].ea_blk
);
333 ret
= EXT2_ET_INVALID_ARGUMENT
;
340 #define BCODE_CREATE 1
342 #define BCODE_STORE 3
345 #define BCODE_FETCH 6
346 #define BCODE_VALIDATE 7
348 #define BCODE_COLLAPSE 9
350 int bcode_program
[] = {
385 int main(int argc
, char **argv
)
388 ext2_refcount_t refcount
;
394 switch (bcode_program
[i
++]) {
398 size
= bcode_program
[i
++];
399 retval
= ea_refcount_create(size
, &refcount
);
401 com_err("ea_refcount_create",
405 printf("Creating refcount with size %d\n",
409 ea_refcount_free(refcount
);
411 printf("Freeing refcount\n");
414 blk
= (blk_t
) bcode_program
[i
++];
415 arg
= bcode_program
[i
++];
416 retval
= ea_refcount_store(refcount
, blk
, arg
);
417 printf("Storing blk %u with value %d\n", blk
, arg
);
419 com_err("ea_refcount_store", retval
, "");
422 blk
= (blk_t
) bcode_program
[i
++];
423 retval
= ea_refcount_fetch(refcount
, blk
, &arg
);
425 com_err("ea_refcount_fetch", retval
, "");
427 printf("bcode_fetch(%u) returns %d\n",
431 blk
= (blk_t
) bcode_program
[i
++];
432 retval
= ea_refcount_increment(refcount
, blk
,
435 com_err("ea_refcount_increment", retval
,
438 printf("bcode_increment(%u) returns %d\n",
442 blk
= (blk_t
) bcode_program
[i
++];
443 retval
= ea_refcount_decrement(refcount
, blk
,
446 com_err("ea_refcount_decrement", retval
,
447 "while decrementing blk %u", blk
);
449 printf("bcode_decrement(%u) returns %d\n",
453 retval
= ea_refcount_validate(refcount
, stderr
);
455 com_err("ea_refcount_validate",
458 printf("Refcount validation OK.\n");
461 ea_refcount_intr_begin(refcount
);
463 blk
= ea_refcount_intr_next(refcount
,
467 printf("\tblk=%u, count=%d\n", blk
,
472 refcount_collapse(refcount
);