1 /*
2 * ea_refcount.c
3 *
4 * Copyright (C) 2001 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
6 */
7
8 #include "config.h"
9 #if HAVE_UNISTD_H
10 #include <unistd.h>
11 #endif
12 #include <string.h>
13 #include <stdio.h>
14
15 #ifdef TEST_PROGRAM
16 #undef ENABLE_NLS
17 #endif
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
25 * checked, its bit is set in the block_ea_map bitmap.
26 */
27 struct ea_refcount_el {
28 /* ea_key could either be an inode number or block number. */
29 ea_key_t ea_key;
30 ea_value_t ea_value;
31 };
32
33 struct ea_refcount {
34 size_t count;
35 size_t size;
36 size_t cursor;
37 struct ea_refcount_el *list;
38 };
39
ea_refcount_free(ext2_refcount_t refcount)40 void ea_refcount_free(ext2_refcount_t refcount)
41 {
42 if (!refcount)
43 return;
44
45 if (refcount->list)
46 ext2fs_free_mem(&refcount->list);
47 ext2fs_free_mem(&refcount);
48 }
49
ea_refcount_create(size_t size,ext2_refcount_t * ret)50 errcode_t ea_refcount_create(size_t size, ext2_refcount_t *ret)
51 {
52 ext2_refcount_t refcount;
53 errcode_t retval;
54 size_t bytes;
55
56 retval = ext2fs_get_memzero(sizeof(struct ea_refcount), &refcount);
57 if (retval)
58 return retval;
59
60 if (!size)
61 size = 500;
62 refcount->size = size;
63 bytes = size * sizeof(struct ea_refcount_el);
64 #ifdef DEBUG
65 printf("Refcount allocated %zu entries, %zu bytes.\n",
66 refcount->size, bytes);
67 #endif
68 retval = ext2fs_get_memzero(bytes, &refcount->list);
69 if (retval)
70 goto errout;
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 */
refcount_collapse(ext2_refcount_t refcount)87 static void refcount_collapse(ext2_refcount_t refcount)
88 {
89 unsigned int i, j;
90 struct ea_refcount_el *list;
91
92 list = refcount->list;
93 for (i = 0, j = 0; i < refcount->count; i++) {
94 if (list[i].ea_value) {
95 if (i != j)
96 list[j] = list[i];
97 j++;
98 }
99 }
100 #if defined(DEBUG) || defined(TEST_PROGRAM)
101 printf("Refcount_collapse: size was %zu, now %d\n",
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 */
insert_refcount_el(ext2_refcount_t refcount,ea_key_t ea_key,int pos)112 static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
113 ea_key_t ea_key, int pos)
114 {
115 struct ea_refcount_el *el;
116 errcode_t retval;
117 size_t new_size = 0;
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);
124 #endif
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),
129 &refcount->list);
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];
143 el->ea_key = ea_key;
144 el->ea_value = 0;
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 */
get_refcount_el(ext2_refcount_t refcount,ea_key_t ea_key,int create)154 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
155 ea_key_t ea_key, int create)
156 {
157 int low, high, mid;
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) ||
165 (ea_key > refcount->list[high].ea_key))) {
166 if (refcount->count >= refcount->size)
167 refcount_collapse(refcount);
168
169 return insert_refcount_el(refcount, ea_key,
170 (unsigned) refcount->count);
171 }
172 if (refcount->count == 0)
173 return 0;
174
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++];
179 #ifdef DEBUG
180 printf("Non-cursor get_refcount_el: %u\n", ea_key);
181 #endif
182 while (low <= high) {
183 mid = (low+high)/2;
184 if (ea_key == refcount->list[mid].ea_key) {
185 refcount->cursor = mid+1;
186 return &refcount->list[mid];
187 }
188 if (ea_key < refcount->list[mid].ea_key)
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 }
203 return insert_refcount_el(refcount, ea_key, low);
204 }
205 return 0;
206 }
207
ea_refcount_fetch(ext2_refcount_t refcount,ea_key_t ea_key,ea_value_t * ret)208 errcode_t ea_refcount_fetch(ext2_refcount_t refcount, ea_key_t ea_key,
209 ea_value_t *ret)
210 {
211 struct ea_refcount_el *el;
212
213 el = get_refcount_el(refcount, ea_key, 0);
214 if (!el) {
215 *ret = 0;
216 return 0;
217 }
218 *ret = el->ea_value;
219 return 0;
220 }
221
ea_refcount_increment(ext2_refcount_t refcount,ea_key_t ea_key,ea_value_t * ret)222 errcode_t ea_refcount_increment(ext2_refcount_t refcount, ea_key_t ea_key,
223 ea_value_t *ret)
224 {
225 struct ea_refcount_el *el;
226
227 el = get_refcount_el(refcount, ea_key, 1);
228 if (!el)
229 return EXT2_ET_NO_MEMORY;
230 el->ea_value++;
231
232 if (ret)
233 *ret = el->ea_value;
234 return 0;
235 }
236
ea_refcount_decrement(ext2_refcount_t refcount,ea_key_t ea_key,ea_value_t * ret)237 errcode_t ea_refcount_decrement(ext2_refcount_t refcount, ea_key_t ea_key,
238 ea_value_t *ret)
239 {
240 struct ea_refcount_el *el;
241
242 el = get_refcount_el(refcount, ea_key, 0);
243 if (!el || el->ea_value == 0)
244 return EXT2_ET_INVALID_ARGUMENT;
245
246 el->ea_value--;
247
248 if (ret)
249 *ret = el->ea_value;
250 return 0;
251 }
252
ea_refcount_store(ext2_refcount_t refcount,ea_key_t ea_key,ea_value_t ea_value)253 errcode_t ea_refcount_store(ext2_refcount_t refcount, ea_key_t ea_key,
254 ea_value_t ea_value)
255 {
256 struct ea_refcount_el *el;
257
258 /*
259 * Get the refcount element
260 */
261 el = get_refcount_el(refcount, ea_key, ea_value ? 1 : 0);
262 if (!el)
263 return ea_value ? EXT2_ET_NO_MEMORY : 0;
264 el->ea_value = ea_value;
265 return 0;
266 }
267
ext2fs_get_refcount_size(ext2_refcount_t refcount)268 size_t ext2fs_get_refcount_size(ext2_refcount_t refcount)
269 {
270 if (!refcount)
271 return 0;
272
273 return refcount->size;
274 }
275
ea_refcount_intr_begin(ext2_refcount_t refcount)276 void ea_refcount_intr_begin(ext2_refcount_t refcount)
277 {
278 refcount->cursor = 0;
279 }
280
ea_refcount_intr_next(ext2_refcount_t refcount,ea_value_t * ret)281 ea_key_t ea_refcount_intr_next(ext2_refcount_t refcount,
282 ea_value_t *ret)
283 {
284 struct ea_refcount_el *list;
285
286 while (1) {
287 if (refcount->cursor >= refcount->count)
288 return 0;
289 list = refcount->list;
290 if (list[refcount->cursor].ea_value) {
291 if (ret)
292 *ret = list[refcount->cursor].ea_value;
293 return list[refcount->cursor++].ea_key;
294 }
295 refcount->cursor++;
296 }
297 }
298
299
300 #ifdef TEST_PROGRAM
301
ea_refcount_validate(ext2_refcount_t refcount,FILE * out)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";
307
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++) {
313 if (refcount->list[i-1].ea_key >= refcount->list[i].ea_key) {
314 fprintf(out,
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;
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
main(int argc,char ** argv)370 int main(int argc, char **argv)
371 {
372 int i = 0;
373 ext2_refcount_t refcount;
374 size_t size;
375 ea_key_t ea_key;
376 ea_value_t arg;
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) {
387 com_err("ea_refcount_create", retval,
388 "while creating size %zu", size);
389 exit(1);
390 } else
391 printf("Creating refcount with size %zu\n",
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:
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,
403 arg);
404 retval = ea_refcount_store(refcount, ea_key, arg);
405 if (retval)
406 com_err("ea_refcount_store", retval,
407 "while storing ea_key %llu", ea_key);
408 break;
409 case BCODE_FETCH:
410 ea_key = (size_t) bcode_program[i++];
411 retval = ea_refcount_fetch(refcount, ea_key, &arg);
412 if (retval)
413 com_err("ea_refcount_fetch", retval,
414 "while fetching ea_key %llu", ea_key);
415 else
416 printf("bcode_fetch(%llu) returns %llu\n",
417 ea_key, arg);
418 break;
419 case BCODE_INCR:
420 ea_key = (size_t) bcode_program[i++];
421 retval = ea_refcount_increment(refcount, ea_key, &arg);
422 if (retval)
423 com_err("ea_refcount_increment", retval,
424 "while incrementing ea_key %llu",
425 ea_key);
426 else
427 printf("bcode_increment(%llu) returns %llu\n",
428 ea_key, arg);
429 break;
430 case BCODE_DECR:
431 ea_key = (size_t) bcode_program[i++];
432 retval = ea_refcount_decrement(refcount, ea_key, &arg);
433 if (retval)
434 com_err("ea_refcount_decrement", retval,
435 "while decrementing ea_key %llu",
436 ea_key);
437 else
438 printf("bcode_decrement(%llu) returns %llu\n",
439 ea_key, arg);
440 break;
441 case BCODE_VALIDATE:
442 retval = ea_refcount_validate(refcount, stderr);
443 if (retval)
444 com_err("ea_refcount_validate", retval,
445 "while validating");
446 else
447 printf("Refcount validation OK.\n");
448 break;
449 case BCODE_LIST:
450 ea_refcount_intr_begin(refcount);
451 while (1) {
452 ea_key = ea_refcount_intr_next(refcount, &arg);
453 if (!ea_key)
454 break;
455 printf("\tea_key=%llu, count=%llu\n", ea_key,
456 arg);
457 }
458 break;
459 case BCODE_COLLAPSE:
460 refcount_collapse(refcount);
461 break;
462 }
463
464 }
465 }
466
467 #endif
468