]>
Commit | Line | Data |
---|---|---|
342d847d TT |
1 | /* |
2 | * ea_refcount.c | |
efc6f628 | 3 | * |
342d847d TT |
4 | * Copyright (C) 2001 Theodore Ts'o. This file may be |
5 | * redistributed under the terms of the GNU Public License. | |
6 | */ | |
7 | ||
d1154eb4 | 8 | #include "config.h" |
342d847d TT |
9 | #if HAVE_UNISTD_H |
10 | #include <unistd.h> | |
11 | #endif | |
12 | #include <string.h> | |
13 | #include <stdio.h> | |
14 | ||
0eeec8ac TT |
15 | #ifdef TEST_PROGRAM |
16 | #undef ENABLE_NLS | |
17 | #endif | |
342d847d TT |
18 | #include "e2fsck.h" |
19 | ||
20 | /* | |
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 | |
efc6f628 | 25 | * checked, its bit is set in the block_ea_map bitmap. |
342d847d TT |
26 | */ |
27 | struct ea_refcount_el { | |
e8b3cc90 TE |
28 | /* ea_key could either be an inode number or block number. */ |
29 | ea_key_t ea_key; | |
30 | ea_value_t ea_value; | |
342d847d TT |
31 | }; |
32 | ||
33 | struct ea_refcount { | |
e8b3cc90 TE |
34 | size_t count; |
35 | size_t size; | |
36 | size_t cursor; | |
342d847d TT |
37 | struct ea_refcount_el *list; |
38 | }; | |
39 | ||
40 | void ea_refcount_free(ext2_refcount_t refcount) | |
41 | { | |
42 | if (!refcount) | |
43 | return; | |
44 | ||
45 | if (refcount->list) | |
c4e3d3f3 TT |
46 | ext2fs_free_mem(&refcount->list); |
47 | ext2fs_free_mem(&refcount); | |
342d847d TT |
48 | } |
49 | ||
e8b3cc90 | 50 | errcode_t ea_refcount_create(size_t size, ext2_refcount_t *ret) |
342d847d TT |
51 | { |
52 | ext2_refcount_t refcount; | |
53 | errcode_t retval; | |
54 | size_t bytes; | |
342d847d | 55 | |
70303df1 | 56 | retval = ext2fs_get_memzero(sizeof(struct ea_refcount), &refcount); |
342d847d TT |
57 | if (retval) |
58 | return retval; | |
342d847d TT |
59 | |
60 | if (!size) | |
61 | size = 500; | |
62 | refcount->size = size; | |
e8b3cc90 | 63 | bytes = size * sizeof(struct ea_refcount_el); |
342d847d | 64 | #ifdef DEBUG |
e8b3cc90 | 65 | printf("Refcount allocated %zu entries, %zu bytes.\n", |
342d847d TT |
66 | refcount->size, bytes); |
67 | #endif | |
70303df1 | 68 | retval = ext2fs_get_memzero(bytes, &refcount->list); |
342d847d TT |
69 | if (retval) |
70 | goto errout; | |
342d847d TT |
71 | |
72 | refcount->count = 0; | |
73 | refcount->cursor = 0; | |
74 | ||
75 | *ret = refcount; | |
76 | return 0; | |
77 | ||
78 | errout: | |
79 | ea_refcount_free(refcount); | |
80 | return(retval); | |
81 | } | |
82 | ||
83 | /* | |
84 | * collapse_refcount() --- go through the refcount array, and get rid | |
85 | * of any count == zero entries | |
86 | */ | |
87 | static void refcount_collapse(ext2_refcount_t refcount) | |
88 | { | |
54434927 | 89 | unsigned int i, j; |
342d847d TT |
90 | struct ea_refcount_el *list; |
91 | ||
92 | list = refcount->list; | |
93 | for (i = 0, j = 0; i < refcount->count; i++) { | |
e8b3cc90 | 94 | if (list[i].ea_value) { |
342d847d TT |
95 | if (i != j) |
96 | list[j] = list[i]; | |
97 | j++; | |
98 | } | |
99 | } | |
100 | #if defined(DEBUG) || defined(TEST_PROGRAM) | |
e8b3cc90 | 101 | printf("Refcount_collapse: size was %zu, now %d\n", |
342d847d TT |
102 | refcount->count, j); |
103 | #endif | |
104 | refcount->count = j; | |
105 | } | |
106 | ||
107 | ||
108 | /* | |
109 | * insert_refcount_el() --- Insert a new entry into the sorted list at a | |
110 | * specified position. | |
111 | */ | |
112 | static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount, | |
e8b3cc90 | 113 | ea_key_t ea_key, int pos) |
342d847d TT |
114 | { |
115 | struct ea_refcount_el *el; | |
116 | errcode_t retval; | |
e8b3cc90 | 117 | size_t new_size = 0; |
342d847d TT |
118 | int num; |
119 | ||
120 | if (refcount->count >= refcount->size) { | |
121 | new_size = refcount->size + 100; | |
122 | #ifdef DEBUG | |
123 | printf("Reallocating refcount %d entries...\n", new_size); | |
efc6f628 | 124 | #endif |
342d847d TT |
125 | retval = ext2fs_resize_mem((size_t) refcount->size * |
126 | sizeof(struct ea_refcount_el), | |
127 | (size_t) new_size * | |
128 | sizeof(struct ea_refcount_el), | |
c4e3d3f3 | 129 | &refcount->list); |
342d847d TT |
130 | if (retval) |
131 | return 0; | |
132 | refcount->size = new_size; | |
133 | } | |
134 | num = (int) refcount->count - pos; | |
135 | if (num < 0) | |
136 | return 0; /* should never happen */ | |
137 | if (num) { | |
138 | memmove(&refcount->list[pos+1], &refcount->list[pos], | |
139 | sizeof(struct ea_refcount_el) * num); | |
140 | } | |
141 | refcount->count++; | |
142 | el = &refcount->list[pos]; | |
e8b3cc90 TE |
143 | el->ea_key = ea_key; |
144 | el->ea_value = 0; | |
342d847d TT |
145 | return el; |
146 | } | |
147 | ||
148 | ||
149 | /* | |
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. | |
153 | */ | |
154 | static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount, | |
e8b3cc90 | 155 | ea_key_t ea_key, int create) |
342d847d | 156 | { |
342d847d | 157 | int low, high, mid; |
342d847d TT |
158 | |
159 | if (!refcount || !refcount->list) | |
160 | return 0; | |
161 | retry: | |
162 | low = 0; | |
163 | high = (int) refcount->count-1; | |
164 | if (create && ((refcount->count == 0) || | |
e8b3cc90 | 165 | (ea_key > refcount->list[high].ea_key))) { |
342d847d TT |
166 | if (refcount->count >= refcount->size) |
167 | refcount_collapse(refcount); | |
168 | ||
e8b3cc90 | 169 | return insert_refcount_el(refcount, ea_key, |
342d847d TT |
170 | (unsigned) refcount->count); |
171 | } | |
172 | if (refcount->count == 0) | |
173 | return 0; | |
efc6f628 | 174 | |
342d847d TT |
175 | if (refcount->cursor >= refcount->count) |
176 | refcount->cursor = 0; | |
e8b3cc90 | 177 | if (ea_key == refcount->list[refcount->cursor].ea_key) |
342d847d TT |
178 | return &refcount->list[refcount->cursor++]; |
179 | #ifdef DEBUG | |
e8b3cc90 | 180 | printf("Non-cursor get_refcount_el: %u\n", ea_key); |
342d847d TT |
181 | #endif |
182 | while (low <= high) { | |
342d847d | 183 | mid = (low+high)/2; |
e8b3cc90 | 184 | if (ea_key == refcount->list[mid].ea_key) { |
342d847d TT |
185 | refcount->cursor = mid+1; |
186 | return &refcount->list[mid]; | |
187 | } | |
e8b3cc90 | 188 | if (ea_key < refcount->list[mid].ea_key) |
342d847d TT |
189 | high = mid-1; |
190 | else | |
191 | low = mid+1; | |
192 | } | |
193 | /* | |
194 | * If we need to create a new entry, it should be right at | |
195 | * low (where high will be left at low-1). | |
196 | */ | |
197 | if (create) { | |
198 | if (refcount->count >= refcount->size) { | |
199 | refcount_collapse(refcount); | |
200 | if (refcount->count < refcount->size) | |
201 | goto retry; | |
202 | } | |
e8b3cc90 | 203 | return insert_refcount_el(refcount, ea_key, low); |
342d847d TT |
204 | } |
205 | return 0; | |
206 | } | |
207 | ||
e8b3cc90 TE |
208 | errcode_t ea_refcount_fetch(ext2_refcount_t refcount, ea_key_t ea_key, |
209 | ea_value_t *ret) | |
342d847d TT |
210 | { |
211 | struct ea_refcount_el *el; | |
efc6f628 | 212 | |
e8b3cc90 | 213 | el = get_refcount_el(refcount, ea_key, 0); |
342d847d TT |
214 | if (!el) { |
215 | *ret = 0; | |
216 | return 0; | |
217 | } | |
e8b3cc90 | 218 | *ret = el->ea_value; |
342d847d TT |
219 | return 0; |
220 | } | |
221 | ||
e8b3cc90 TE |
222 | errcode_t ea_refcount_increment(ext2_refcount_t refcount, ea_key_t ea_key, |
223 | ea_value_t *ret) | |
342d847d TT |
224 | { |
225 | struct ea_refcount_el *el; | |
226 | ||
e8b3cc90 | 227 | el = get_refcount_el(refcount, ea_key, 1); |
342d847d TT |
228 | if (!el) |
229 | return EXT2_ET_NO_MEMORY; | |
e8b3cc90 | 230 | el->ea_value++; |
342d847d TT |
231 | |
232 | if (ret) | |
e8b3cc90 | 233 | *ret = el->ea_value; |
342d847d TT |
234 | return 0; |
235 | } | |
236 | ||
e8b3cc90 TE |
237 | errcode_t ea_refcount_decrement(ext2_refcount_t refcount, ea_key_t ea_key, |
238 | ea_value_t *ret) | |
342d847d TT |
239 | { |
240 | struct ea_refcount_el *el; | |
241 | ||
e8b3cc90 TE |
242 | el = get_refcount_el(refcount, ea_key, 0); |
243 | if (!el || el->ea_value == 0) | |
342d847d TT |
244 | return EXT2_ET_INVALID_ARGUMENT; |
245 | ||
e8b3cc90 | 246 | el->ea_value--; |
342d847d TT |
247 | |
248 | if (ret) | |
e8b3cc90 | 249 | *ret = el->ea_value; |
342d847d TT |
250 | return 0; |
251 | } | |
252 | ||
e8b3cc90 TE |
253 | errcode_t ea_refcount_store(ext2_refcount_t refcount, ea_key_t ea_key, |
254 | ea_value_t ea_value) | |
342d847d TT |
255 | { |
256 | struct ea_refcount_el *el; | |
257 | ||
258 | /* | |
259 | * Get the refcount element | |
260 | */ | |
e8b3cc90 | 261 | el = get_refcount_el(refcount, ea_key, ea_value ? 1 : 0); |
342d847d | 262 | if (!el) |
e8b3cc90 TE |
263 | return ea_value ? EXT2_ET_NO_MEMORY : 0; |
264 | el->ea_value = ea_value; | |
342d847d TT |
265 | return 0; |
266 | } | |
267 | ||
e8b3cc90 | 268 | size_t ext2fs_get_refcount_size(ext2_refcount_t refcount) |
342d847d TT |
269 | { |
270 | if (!refcount) | |
271 | return 0; | |
272 | ||
273 | return refcount->size; | |
274 | } | |
275 | ||
276 | void ea_refcount_intr_begin(ext2_refcount_t refcount) | |
277 | { | |
278 | refcount->cursor = 0; | |
279 | } | |
280 | ||
e8b3cc90 TE |
281 | ea_key_t ea_refcount_intr_next(ext2_refcount_t refcount, |
282 | ea_value_t *ret) | |
342d847d TT |
283 | { |
284 | struct ea_refcount_el *list; | |
efc6f628 | 285 | |
342d847d TT |
286 | while (1) { |
287 | if (refcount->cursor >= refcount->count) | |
288 | return 0; | |
289 | list = refcount->list; | |
e8b3cc90 | 290 | if (list[refcount->cursor].ea_value) { |
342d847d | 291 | if (ret) |
e8b3cc90 TE |
292 | *ret = list[refcount->cursor].ea_value; |
293 | return list[refcount->cursor++].ea_key; | |
342d847d TT |
294 | } |
295 | refcount->cursor++; | |
296 | } | |
297 | } | |
298 | ||
299 | ||
300 | #ifdef TEST_PROGRAM | |
301 | ||
302 | errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out) | |
303 | { | |
304 | errcode_t ret = 0; | |
305 | int i; | |
306 | const char *bad = "bad refcount"; | |
efc6f628 | 307 | |
342d847d TT |
308 | if (refcount->count > refcount->size) { |
309 | fprintf(out, "%s: count > size\n", bad); | |
310 | return EXT2_ET_INVALID_ARGUMENT; | |
311 | } | |
312 | for (i=1; i < refcount->count; i++) { | |
e8b3cc90 | 313 | if (refcount->list[i-1].ea_key >= refcount->list[i].ea_key) { |
a8263cab | 314 | fprintf(out, |
e8b3cc90 TE |
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); | |
342d847d TT |
318 | ret = EXT2_ET_INVALID_ARGUMENT; |
319 | } | |
320 | } | |
321 | return ret; | |
322 | } | |
323 | ||
324 | #define BCODE_END 0 | |
325 | #define BCODE_CREATE 1 | |
326 | #define BCODE_FREE 2 | |
327 | #define BCODE_STORE 3 | |
328 | #define BCODE_INCR 4 | |
329 | #define BCODE_DECR 5 | |
330 | #define BCODE_FETCH 6 | |
331 | #define BCODE_VALIDATE 7 | |
332 | #define BCODE_LIST 8 | |
333 | #define BCODE_COLLAPSE 9 | |
334 | ||
335 | int bcode_program[] = { | |
336 | BCODE_CREATE, 5, | |
337 | BCODE_STORE, 3, 3, | |
338 | BCODE_STORE, 4, 4, | |
339 | BCODE_STORE, 1, 1, | |
340 | BCODE_STORE, 8, 8, | |
341 | BCODE_STORE, 2, 2, | |
342 | BCODE_STORE, 4, 0, | |
343 | BCODE_STORE, 2, 0, | |
344 | BCODE_STORE, 6, 6, | |
345 | BCODE_VALIDATE, | |
346 | BCODE_STORE, 4, 4, | |
347 | BCODE_STORE, 2, 2, | |
348 | BCODE_FETCH, 1, | |
349 | BCODE_FETCH, 2, | |
350 | BCODE_INCR, 3, | |
351 | BCODE_INCR, 3, | |
352 | BCODE_DECR, 4, | |
353 | BCODE_STORE, 4, 4, | |
354 | BCODE_VALIDATE, | |
355 | BCODE_STORE, 20, 20, | |
356 | BCODE_STORE, 40, 40, | |
357 | BCODE_STORE, 30, 30, | |
358 | BCODE_STORE, 10, 10, | |
359 | BCODE_DECR, 30, | |
360 | BCODE_FETCH, 30, | |
361 | BCODE_DECR, 2, | |
362 | BCODE_DECR, 2, | |
363 | BCODE_COLLAPSE, | |
364 | BCODE_LIST, | |
365 | BCODE_VALIDATE, | |
366 | BCODE_FREE, | |
367 | BCODE_END | |
368 | }; | |
369 | ||
370 | int main(int argc, char **argv) | |
371 | { | |
372 | int i = 0; | |
373 | ext2_refcount_t refcount; | |
e8b3cc90 TE |
374 | size_t size; |
375 | ea_key_t ea_key; | |
376 | ea_value_t arg; | |
342d847d TT |
377 | errcode_t retval; |
378 | ||
379 | while (1) { | |
380 | switch (bcode_program[i++]) { | |
381 | case BCODE_END: | |
382 | exit(0); | |
383 | case BCODE_CREATE: | |
384 | size = bcode_program[i++]; | |
385 | retval = ea_refcount_create(size, &refcount); | |
386 | if (retval) { | |
6b56f3d9 | 387 | com_err("ea_refcount_create", retval, |
e8b3cc90 | 388 | "while creating size %zu", size); |
342d847d TT |
389 | exit(1); |
390 | } else | |
e8b3cc90 | 391 | printf("Creating refcount with size %zu\n", |
342d847d TT |
392 | size); |
393 | break; | |
394 | case BCODE_FREE: | |
395 | ea_refcount_free(refcount); | |
396 | refcount = 0; | |
397 | printf("Freeing refcount\n"); | |
398 | break; | |
399 | case BCODE_STORE: | |
e8b3cc90 | 400 | ea_key = (size_t) bcode_program[i++]; |
342d847d | 401 | arg = bcode_program[i++]; |
e8b3cc90 TE |
402 | printf("Storing ea_key %llu with value %llu\n", ea_key, |
403 | arg); | |
404 | retval = ea_refcount_store(refcount, ea_key, arg); | |
342d847d | 405 | if (retval) |
6b56f3d9 | 406 | com_err("ea_refcount_store", retval, |
e8b3cc90 | 407 | "while storing ea_key %llu", ea_key); |
342d847d TT |
408 | break; |
409 | case BCODE_FETCH: | |
e8b3cc90 TE |
410 | ea_key = (size_t) bcode_program[i++]; |
411 | retval = ea_refcount_fetch(refcount, ea_key, &arg); | |
342d847d | 412 | if (retval) |
6b56f3d9 | 413 | com_err("ea_refcount_fetch", retval, |
e8b3cc90 | 414 | "while fetching ea_key %llu", ea_key); |
342d847d | 415 | else |
e8b3cc90 TE |
416 | printf("bcode_fetch(%llu) returns %llu\n", |
417 | ea_key, arg); | |
342d847d TT |
418 | break; |
419 | case BCODE_INCR: | |
e8b3cc90 TE |
420 | ea_key = (size_t) bcode_program[i++]; |
421 | retval = ea_refcount_increment(refcount, ea_key, &arg); | |
342d847d TT |
422 | if (retval) |
423 | com_err("ea_refcount_increment", retval, | |
e8b3cc90 TE |
424 | "while incrementing ea_key %llu", |
425 | ea_key); | |
342d847d | 426 | else |
e8b3cc90 TE |
427 | printf("bcode_increment(%llu) returns %llu\n", |
428 | ea_key, arg); | |
342d847d TT |
429 | break; |
430 | case BCODE_DECR: | |
e8b3cc90 TE |
431 | ea_key = (size_t) bcode_program[i++]; |
432 | retval = ea_refcount_decrement(refcount, ea_key, &arg); | |
342d847d TT |
433 | if (retval) |
434 | com_err("ea_refcount_decrement", retval, | |
e8b3cc90 TE |
435 | "while decrementing ea_key %llu", |
436 | ea_key); | |
342d847d | 437 | else |
e8b3cc90 TE |
438 | printf("bcode_decrement(%llu) returns %llu\n", |
439 | ea_key, arg); | |
342d847d TT |
440 | break; |
441 | case BCODE_VALIDATE: | |
442 | retval = ea_refcount_validate(refcount, stderr); | |
443 | if (retval) | |
6b56f3d9 AD |
444 | com_err("ea_refcount_validate", retval, |
445 | "while validating"); | |
342d847d TT |
446 | else |
447 | printf("Refcount validation OK.\n"); | |
448 | break; | |
449 | case BCODE_LIST: | |
450 | ea_refcount_intr_begin(refcount); | |
451 | while (1) { | |
e8b3cc90 TE |
452 | ea_key = ea_refcount_intr_next(refcount, &arg); |
453 | if (!ea_key) | |
342d847d | 454 | break; |
e8b3cc90 TE |
455 | printf("\tea_key=%llu, count=%llu\n", ea_key, |
456 | arg); | |
342d847d TT |
457 | } |
458 | break; | |
459 | case BCODE_COLLAPSE: | |
460 | refcount_collapse(refcount); | |
461 | break; | |
462 | } | |
efc6f628 | 463 | |
342d847d TT |
464 | } |
465 | } | |
466 | ||
467 | #endif |