]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ext2fs/icount.c
9223f662e2a0c752ab1d12281ac293938e7db80d
[thirdparty/e2fsprogs.git] / lib / ext2fs / icount.c
1 /*
2 * icount.c --- an efficient inode count abstraction
3 *
4 * Copyright (C) 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #if HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include <string.h>
16 #include <stdio.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <errno.h>
20
21 #include "ext2_fs.h"
22 #include "ext2fs.h"
23 #include "tdb.h"
24
25 /*
26 * The data storage strategy used by icount relies on the observation
27 * that most inode counts are either zero (for non-allocated inodes),
28 * one (for most files), and only a few that are two or more
29 * (directories and files that are linked to more than one directory).
30 *
31 * Also, e2fsck tends to load the icount data sequentially.
32 *
33 * So, we use an inode bitmap to indicate which inodes have a count of
34 * one, and then use a sorted list to store the counts for inodes
35 * which are greater than one.
36 *
37 * We also use an optional bitmap to indicate which inodes are already
38 * in the sorted list, to speed up the use of this abstraction by
39 * e2fsck's pass 2. Pass 2 increments inode counts as it finds them,
40 * so this extra bitmap avoids searching the sorted list to see if a
41 * particular inode is on the sorted list already.
42 */
43
44 struct ext2_icount_el {
45 ext2_ino_t ino;
46 __u32 count;
47 };
48
49 struct ext2_icount {
50 errcode_t magic;
51 ext2fs_inode_bitmap single;
52 ext2fs_inode_bitmap multiple;
53 ext2_ino_t count;
54 ext2_ino_t size;
55 ext2_ino_t num_inodes;
56 ext2_ino_t cursor;
57 struct ext2_icount_el *list;
58 struct ext2_icount_el *last_lookup;
59 char *tdb_fn;
60 TDB_CONTEXT *tdb;
61 };
62
63 /*
64 * We now use a 32-bit counter field because it doesn't cost us
65 * anything extra for the in-memory data structure, due to alignment
66 * padding. But there's no point changing the interface if most of
67 * the time we only care if the number is bigger than 65,000 or not.
68 * So use the following translation function to return a 16-bit count.
69 */
70 #define icount_16_xlate(x) (((x) > 65500) ? 65500 : (x))
71
72 void ext2fs_free_icount(ext2_icount_t icount)
73 {
74 if (!icount)
75 return;
76
77 icount->magic = 0;
78 if (icount->list)
79 ext2fs_free_mem(&icount->list);
80 if (icount->single)
81 ext2fs_free_inode_bitmap(icount->single);
82 if (icount->multiple)
83 ext2fs_free_inode_bitmap(icount->multiple);
84 if (icount->tdb)
85 tdb_close(icount->tdb);
86 if (icount->tdb_fn) {
87 unlink(icount->tdb_fn);
88 free(icount->tdb_fn);
89 }
90
91 ext2fs_free_mem(&icount);
92 }
93
94 static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
95 {
96 ext2_icount_t icount;
97 errcode_t retval;
98
99 *ret = 0;
100
101 retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
102 if (retval)
103 return retval;
104 memset(icount, 0, sizeof(struct ext2_icount));
105
106 retval = ext2fs_allocate_inode_bitmap(fs, 0, &icount->single);
107 if (retval)
108 goto errout;
109
110 if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
111 retval = ext2fs_allocate_inode_bitmap(fs, 0,
112 &icount->multiple);
113 if (retval)
114 goto errout;
115 } else
116 icount->multiple = 0;
117
118 icount->magic = EXT2_ET_MAGIC_ICOUNT;
119 icount->num_inodes = fs->super->s_inodes_count;
120
121 *ret = icount;
122 return 0;
123
124 errout:
125 ext2fs_free_icount(icount);
126 return(retval);
127 }
128
129 struct uuid {
130 __u32 time_low;
131 __u16 time_mid;
132 __u16 time_hi_and_version;
133 __u16 clock_seq;
134 __u8 node[6];
135 };
136
137 static void unpack_uuid(void *in, struct uuid *uu)
138 {
139 __u8 *ptr = in;
140 __u32 tmp;
141
142 tmp = *ptr++;
143 tmp = (tmp << 8) | *ptr++;
144 tmp = (tmp << 8) | *ptr++;
145 tmp = (tmp << 8) | *ptr++;
146 uu->time_low = tmp;
147
148 tmp = *ptr++;
149 tmp = (tmp << 8) | *ptr++;
150 uu->time_mid = tmp;
151
152 tmp = *ptr++;
153 tmp = (tmp << 8) | *ptr++;
154 uu->time_hi_and_version = tmp;
155
156 tmp = *ptr++;
157 tmp = (tmp << 8) | *ptr++;
158 uu->clock_seq = tmp;
159
160 memcpy(uu->node, ptr, 6);
161 }
162
163 static void uuid_unparse(void *uu, char *out)
164 {
165 struct uuid uuid;
166
167 unpack_uuid(uu, &uuid);
168 sprintf(out,
169 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
170 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
171 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
172 uuid.node[0], uuid.node[1], uuid.node[2],
173 uuid.node[3], uuid.node[4], uuid.node[5]);
174 }
175
176 errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
177 int flags, ext2_icount_t *ret)
178 {
179 ext2_icount_t icount;
180 errcode_t retval;
181 char *fn, uuid[40];
182 int fd;
183
184 retval = alloc_icount(fs, flags, &icount);
185 if (retval)
186 return retval;
187
188 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn);
189 if (retval)
190 goto errout;
191 uuid_unparse(fs->super->s_uuid, uuid);
192 sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
193 fd = mkstemp(fn);
194
195 icount->tdb_fn = fn;
196 icount->tdb = tdb_open(fn, 0, TDB_CLEAR_IF_FIRST,
197 O_RDWR | O_CREAT | O_TRUNC, 0600);
198 if (icount->tdb) {
199 close(fd);
200 *ret = icount;
201 return 0;
202 }
203
204 retval = errno;
205 close(fd);
206
207 errout:
208 ext2fs_free_icount(icount);
209 return(retval);
210 }
211
212 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
213 ext2_icount_t hint, ext2_icount_t *ret)
214 {
215 ext2_icount_t icount;
216 errcode_t retval;
217 size_t bytes;
218 ext2_ino_t i;
219
220 if (hint) {
221 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
222 if (hint->size > size)
223 size = (size_t) hint->size;
224 }
225
226 retval = alloc_icount(fs, flags, &icount);
227 if (retval)
228 return retval;
229
230 if (size) {
231 icount->size = size;
232 } else {
233 /*
234 * Figure out how many special case inode counts we will
235 * have. We know we will need one for each directory;
236 * we also need to reserve some extra room for file links
237 */
238 retval = ext2fs_get_num_dirs(fs, &icount->size);
239 if (retval)
240 goto errout;
241 icount->size += fs->super->s_inodes_count / 50;
242 }
243
244 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
245 #if 0
246 printf("Icount allocated %u entries, %d bytes.\n",
247 icount->size, bytes);
248 #endif
249 retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
250 &icount->list);
251 if (retval)
252 goto errout;
253 memset(icount->list, 0, bytes);
254
255 icount->count = 0;
256 icount->cursor = 0;
257
258 /*
259 * Populate the sorted list with those entries which were
260 * found in the hint icount (since those are ones which will
261 * likely need to be in the sorted list this time around).
262 */
263 if (hint) {
264 for (i=0; i < hint->count; i++)
265 icount->list[i].ino = hint->list[i].ino;
266 icount->count = hint->count;
267 }
268
269 *ret = icount;
270 return 0;
271
272 errout:
273 ext2fs_free_icount(icount);
274 return(retval);
275 }
276
277 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
278 unsigned int size,
279 ext2_icount_t *ret)
280 {
281 return ext2fs_create_icount2(fs, flags, size, 0, ret);
282 }
283
284 /*
285 * insert_icount_el() --- Insert a new entry into the sorted list at a
286 * specified position.
287 */
288 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
289 ext2_ino_t ino, int pos)
290 {
291 struct ext2_icount_el *el;
292 errcode_t retval;
293 ext2_ino_t new_size = 0;
294 int num;
295
296 if (icount->last_lookup && icount->last_lookup->ino == ino)
297 return icount->last_lookup;
298
299 if (icount->count >= icount->size) {
300 if (icount->count) {
301 new_size = icount->list[(unsigned)icount->count-1].ino;
302 new_size = (ext2_ino_t) (icount->count *
303 ((float) icount->num_inodes / new_size));
304 }
305 if (new_size < (icount->size + 100))
306 new_size = icount->size + 100;
307 #if 0
308 printf("Reallocating icount %u entries...\n", new_size);
309 #endif
310 retval = ext2fs_resize_mem((size_t) icount->size *
311 sizeof(struct ext2_icount_el),
312 (size_t) new_size *
313 sizeof(struct ext2_icount_el),
314 &icount->list);
315 if (retval)
316 return 0;
317 icount->size = new_size;
318 }
319 num = (int) icount->count - pos;
320 if (num < 0)
321 return 0; /* should never happen */
322 if (num) {
323 memmove(&icount->list[pos+1], &icount->list[pos],
324 sizeof(struct ext2_icount_el) * num);
325 }
326 icount->count++;
327 el = &icount->list[pos];
328 el->count = 0;
329 el->ino = ino;
330 icount->last_lookup = el;
331 return el;
332 }
333
334 /*
335 * get_icount_el() --- given an inode number, try to find icount
336 * information in the sorted list. If the create flag is set,
337 * and we can't find an entry, create one in the sorted list.
338 */
339 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
340 ext2_ino_t ino, int create)
341 {
342 float range;
343 int low, high, mid;
344 ext2_ino_t lowval, highval;
345
346 if (!icount || !icount->list)
347 return 0;
348
349 if (create && ((icount->count == 0) ||
350 (ino > icount->list[(unsigned)icount->count-1].ino))) {
351 return insert_icount_el(icount, ino, (unsigned) icount->count);
352 }
353 if (icount->count == 0)
354 return 0;
355
356 if (icount->cursor >= icount->count)
357 icount->cursor = 0;
358 if (ino == icount->list[icount->cursor].ino)
359 return &icount->list[icount->cursor++];
360 #if 0
361 printf("Non-cursor get_icount_el: %u\n", ino);
362 #endif
363 low = 0;
364 high = (int) icount->count-1;
365 while (low <= high) {
366 mid = ((unsigned)low + (unsigned)high) >> 1;
367 if (ino == icount->list[mid].ino) {
368 icount->cursor = mid+1;
369 return &icount->list[mid];
370 }
371 if (ino < icount->list[mid].ino)
372 high = mid-1;
373 else
374 low = mid+1;
375 }
376 /*
377 * If we need to create a new entry, it should be right at
378 * low (where high will be left at low-1).
379 */
380 if (create)
381 return insert_icount_el(icount, ino, low);
382 return 0;
383 }
384
385 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
386 __u32 count)
387 {
388 struct ext2_icount_el *el;
389 TDB_DATA key, data;
390
391 if (icount->tdb) {
392 key.dptr = (unsigned char *) &ino;
393 key.dsize = sizeof(ext2_ino_t);
394 data.dptr = (unsigned char *) &count;
395 data.dsize = sizeof(__u32);
396 if (count) {
397 if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
398 return tdb_error(icount->tdb) +
399 EXT2_ET_TDB_SUCCESS;
400 } else {
401 if (tdb_delete(icount->tdb, key))
402 return tdb_error(icount->tdb) +
403 EXT2_ET_TDB_SUCCESS;
404 }
405 return 0;
406 }
407
408 el = get_icount_el(icount, ino, 1);
409 if (!el)
410 return EXT2_ET_NO_MEMORY;
411
412 el->count = count;
413 return 0;
414 }
415
416 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
417 __u32 *count)
418 {
419 struct ext2_icount_el *el;
420 TDB_DATA key, data;
421
422 if (icount->tdb) {
423 key.dptr = (unsigned char *) &ino;
424 key.dsize = sizeof(ext2_ino_t);
425
426 data = tdb_fetch(icount->tdb, key);
427 if (data.dptr == NULL) {
428 *count = 0;
429 return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
430 }
431
432 *count = *((__u32 *) data.dptr);
433 free(data.dptr);
434 return 0;
435 }
436 el = get_icount_el(icount, ino, 0);
437 if (!el) {
438 *count = 0;
439 return ENOENT;
440 }
441
442 *count = el->count;
443 return 0;
444 }
445
446 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
447 {
448 errcode_t ret = 0;
449 unsigned int i;
450 const char *bad = "bad icount";
451
452 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
453
454 if (icount->count > icount->size) {
455 fprintf(out, "%s: count > size\n", bad);
456 return EXT2_ET_INVALID_ARGUMENT;
457 }
458 for (i=1; i < icount->count; i++) {
459 if (icount->list[i-1].ino >= icount->list[i].ino) {
460 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
461 bad, i-1, icount->list[i-1].ino,
462 i, icount->list[i].ino);
463 ret = EXT2_ET_INVALID_ARGUMENT;
464 }
465 }
466 return ret;
467 }
468
469 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
470 {
471 __u32 val;
472 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
473
474 if (!ino || (ino > icount->num_inodes))
475 return EXT2_ET_INVALID_ARGUMENT;
476
477 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
478 *ret = 1;
479 return 0;
480 }
481 if (icount->multiple &&
482 !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
483 *ret = 0;
484 return 0;
485 }
486 get_inode_count(icount, ino, &val);
487 *ret = icount_16_xlate(val);
488 return 0;
489 }
490
491 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
492 __u16 *ret)
493 {
494 __u32 curr_value;
495
496 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
497
498 if (!ino || (ino > icount->num_inodes))
499 return EXT2_ET_INVALID_ARGUMENT;
500
501 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
502 /*
503 * If the existing count is 1, then we know there is
504 * no entry in the list.
505 */
506 if (set_inode_count(icount, ino, 2))
507 return EXT2_ET_NO_MEMORY;
508 curr_value = 2;
509 ext2fs_unmark_inode_bitmap2(icount->single, ino);
510 } else if (icount->multiple) {
511 /*
512 * The count is either zero or greater than 1; if the
513 * inode is set in icount->multiple, then there should
514 * be an entry in the list, so we need to fix it.
515 */
516 if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
517 get_inode_count(icount, ino, &curr_value);
518 curr_value++;
519 if (set_inode_count(icount, ino, curr_value))
520 return EXT2_ET_NO_MEMORY;
521 } else {
522 /*
523 * The count was zero; mark the single bitmap
524 * and return.
525 */
526 ext2fs_mark_inode_bitmap2(icount->single, ino);
527 if (ret)
528 *ret = 1;
529 return 0;
530 }
531 } else {
532 /*
533 * The count is either zero or greater than 1; try to
534 * find an entry in the list to determine which.
535 */
536 get_inode_count(icount, ino, &curr_value);
537 curr_value++;
538 if (set_inode_count(icount, ino, curr_value))
539 return EXT2_ET_NO_MEMORY;
540 }
541 if (icount->multiple)
542 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
543 if (ret)
544 *ret = icount_16_xlate(curr_value);
545 return 0;
546 }
547
548 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
549 __u16 *ret)
550 {
551 __u32 curr_value;
552
553 if (!ino || (ino > icount->num_inodes))
554 return EXT2_ET_INVALID_ARGUMENT;
555
556 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
557
558 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
559 ext2fs_unmark_inode_bitmap2(icount->single, ino);
560 if (icount->multiple)
561 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
562 else {
563 set_inode_count(icount, ino, 0);
564 }
565 if (ret)
566 *ret = 0;
567 return 0;
568 }
569
570 if (icount->multiple &&
571 !ext2fs_test_inode_bitmap2(icount->multiple, ino))
572 return EXT2_ET_INVALID_ARGUMENT;
573
574 get_inode_count(icount, ino, &curr_value);
575 if (!curr_value)
576 return EXT2_ET_INVALID_ARGUMENT;
577 curr_value--;
578 if (set_inode_count(icount, ino, curr_value))
579 return EXT2_ET_NO_MEMORY;
580
581 if (curr_value == 1)
582 ext2fs_mark_inode_bitmap2(icount->single, ino);
583 if ((curr_value == 0) && icount->multiple)
584 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
585
586 if (ret)
587 *ret = icount_16_xlate(curr_value);
588 return 0;
589 }
590
591 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
592 __u16 count)
593 {
594 if (!ino || (ino > icount->num_inodes))
595 return EXT2_ET_INVALID_ARGUMENT;
596
597 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
598
599 if (count == 1) {
600 ext2fs_mark_inode_bitmap2(icount->single, ino);
601 if (icount->multiple)
602 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
603 return 0;
604 }
605 if (count == 0) {
606 ext2fs_unmark_inode_bitmap2(icount->single, ino);
607 if (icount->multiple) {
608 /*
609 * If the icount->multiple bitmap is enabled,
610 * we can just clear both bitmaps and we're done
611 */
612 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
613 } else
614 set_inode_count(icount, ino, 0);
615 return 0;
616 }
617
618 if (set_inode_count(icount, ino, count))
619 return EXT2_ET_NO_MEMORY;
620 ext2fs_unmark_inode_bitmap2(icount->single, ino);
621 if (icount->multiple)
622 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
623 return 0;
624 }
625
626 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
627 {
628 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
629 return 0;
630
631 return icount->size;
632 }
633
634 #ifdef DEBUG
635
636 ext2_filsys test_fs;
637 ext2_icount_t icount;
638
639 #define EXIT 0x00
640 #define FETCH 0x01
641 #define STORE 0x02
642 #define INCREMENT 0x03
643 #define DECREMENT 0x04
644
645 struct test_program {
646 int cmd;
647 ext2_ino_t ino;
648 __u16 arg;
649 __u16 expected;
650 };
651
652 struct test_program prog[] = {
653 { STORE, 42, 42, 42 },
654 { STORE, 1, 1, 1 },
655 { STORE, 2, 2, 2 },
656 { STORE, 3, 3, 3 },
657 { STORE, 10, 1, 1 },
658 { STORE, 42, 0, 0 },
659 { INCREMENT, 5, 0, 1 },
660 { INCREMENT, 5, 0, 2 },
661 { INCREMENT, 5, 0, 3 },
662 { INCREMENT, 5, 0, 4 },
663 { DECREMENT, 5, 0, 3 },
664 { DECREMENT, 5, 0, 2 },
665 { DECREMENT, 5, 0, 1 },
666 { DECREMENT, 5, 0, 0 },
667 { FETCH, 10, 0, 1 },
668 { FETCH, 1, 0, 1 },
669 { FETCH, 2, 0, 2 },
670 { FETCH, 3, 0, 3 },
671 { INCREMENT, 1, 0, 2 },
672 { DECREMENT, 2, 0, 1 },
673 { DECREMENT, 2, 0, 0 },
674 { FETCH, 12, 0, 0 },
675 { EXIT, 0, 0, 0 }
676 };
677
678 struct test_program extended[] = {
679 { STORE, 1, 1, 1 },
680 { STORE, 2, 2, 2 },
681 { STORE, 3, 3, 3 },
682 { STORE, 4, 4, 4 },
683 { STORE, 5, 5, 5 },
684 { STORE, 6, 1, 1 },
685 { STORE, 7, 2, 2 },
686 { STORE, 8, 3, 3 },
687 { STORE, 9, 4, 4 },
688 { STORE, 10, 5, 5 },
689 { STORE, 11, 1, 1 },
690 { STORE, 12, 2, 2 },
691 { STORE, 13, 3, 3 },
692 { STORE, 14, 4, 4 },
693 { STORE, 15, 5, 5 },
694 { STORE, 16, 1, 1 },
695 { STORE, 17, 2, 2 },
696 { STORE, 18, 3, 3 },
697 { STORE, 19, 4, 4 },
698 { STORE, 20, 5, 5 },
699 { STORE, 21, 1, 1 },
700 { STORE, 22, 2, 2 },
701 { STORE, 23, 3, 3 },
702 { STORE, 24, 4, 4 },
703 { STORE, 25, 5, 5 },
704 { STORE, 26, 1, 1 },
705 { STORE, 27, 2, 2 },
706 { STORE, 28, 3, 3 },
707 { STORE, 29, 4, 4 },
708 { STORE, 30, 5, 5 },
709 { EXIT, 0, 0, 0 }
710 };
711
712 /*
713 * Setup the variables for doing the inode scan test.
714 */
715 static void setup(void)
716 {
717 errcode_t retval;
718 struct ext2_super_block param;
719
720 initialize_ext2_error_table();
721
722 memset(&param, 0, sizeof(param));
723 ext2fs_blocks_count_set(&param, 12000);
724
725 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
726 test_io_manager, &test_fs);
727 if (retval) {
728 com_err("setup", retval,
729 "while initializing filesystem");
730 exit(1);
731 }
732 retval = ext2fs_allocate_tables(test_fs);
733 if (retval) {
734 com_err("setup", retval,
735 "while allocating tables for test filesystem");
736 exit(1);
737 }
738 }
739
740 int run_test(int flags, int size, char *dir, struct test_program *prog)
741 {
742 errcode_t retval;
743 ext2_icount_t icount;
744 struct test_program *pc;
745 __u16 result;
746 int problem = 0;
747
748 if (dir) {
749 retval = ext2fs_create_icount_tdb(test_fs, dir,
750 flags, &icount);
751 if (retval) {
752 com_err("run_test", retval,
753 "while creating icount using tdb");
754 exit(1);
755 }
756 } else {
757 retval = ext2fs_create_icount2(test_fs, flags, size, 0,
758 &icount);
759 if (retval) {
760 com_err("run_test", retval, "while creating icount");
761 exit(1);
762 }
763 }
764 for (pc = prog; pc->cmd != EXIT; pc++) {
765 switch (pc->cmd) {
766 case FETCH:
767 printf("icount_fetch(%u) = ", pc->ino);
768 break;
769 case STORE:
770 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
771 if (retval) {
772 com_err("run_test", retval,
773 "while calling icount_store");
774 exit(1);
775 }
776 printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
777 break;
778 case INCREMENT:
779 retval = ext2fs_icount_increment(icount, pc->ino, 0);
780 if (retval) {
781 com_err("run_test", retval,
782 "while calling icount_increment");
783 exit(1);
784 }
785 printf("icount_increment(%u) = ", pc->ino);
786 break;
787 case DECREMENT:
788 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
789 if (retval) {
790 com_err("run_test", retval,
791 "while calling icount_decrement");
792 exit(1);
793 }
794 printf("icount_decrement(%u) = ", pc->ino);
795 break;
796 }
797 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
798 if (retval) {
799 com_err("run_test", retval,
800 "while calling icount_fetch");
801 exit(1);
802 }
803 printf("%u (%s)\n", result, (result == pc->expected) ?
804 "OK" : "NOT OK");
805 if (result != pc->expected)
806 problem++;
807 }
808 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
809 retval = ext2fs_icount_validate(icount, stdout);
810 if (retval) {
811 com_err("run_test", retval, "while calling icount_validate");
812 exit(1);
813 }
814 ext2fs_free_icount(icount);
815 return problem;
816 }
817
818
819 int main(int argc, char **argv)
820 {
821 int failed = 0;
822
823 setup();
824 printf("Standard icount run:\n");
825 failed += run_test(0, 0, 0, prog);
826 printf("\nMultiple bitmap test:\n");
827 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, 0, prog);
828 printf("\nResizing icount:\n");
829 failed += run_test(0, 3, 0, extended);
830 printf("\nStandard icount run with tdb:\n");
831 failed += run_test(0, 0, ".", prog);
832 printf("\nMultiple bitmap test with tdb:\n");
833 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, ".", prog);
834 if (failed)
835 printf("FAILED!\n");
836 return failed;
837 }
838 #endif