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