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