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