2 * icount.c --- an efficient inode count abstraction
4 * Copyright (C) 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
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).
31 * Also, e2fsck tends to load the icount data sequentially.
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.
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.
44 struct ext2_icount_el {
51 ext2fs_inode_bitmap single;
52 ext2fs_inode_bitmap multiple;
55 ext2_ino_t num_inodes;
57 struct ext2_icount_el *list;
58 struct ext2_icount_el *last_lookup;
63 void ext2fs_free_icount(ext2_icount_t icount)
70 ext2fs_free_mem(&icount->list);
72 ext2fs_free_inode_bitmap(icount->single);
74 ext2fs_free_inode_bitmap(icount->multiple);
76 tdb_close(icount->tdb);
78 unlink(icount->tdb_fn);
82 ext2fs_free_mem(&icount);
85 static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
92 retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
95 memset(icount, 0, sizeof(struct ext2_icount));
97 retval = ext2fs_allocate_inode_bitmap(fs, 0, &icount->single);
101 if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
102 retval = ext2fs_allocate_inode_bitmap(fs, 0,
107 icount->multiple = 0;
109 icount->magic = EXT2_ET_MAGIC_ICOUNT;
110 icount->num_inodes = fs->super->s_inodes_count;
116 ext2fs_free_icount(icount);
123 __u16 time_hi_and_version;
128 static void unpack_uuid(void *in, struct uuid *uu)
134 tmp = (tmp << 8) | *ptr++;
135 tmp = (tmp << 8) | *ptr++;
136 tmp = (tmp << 8) | *ptr++;
140 tmp = (tmp << 8) | *ptr++;
144 tmp = (tmp << 8) | *ptr++;
145 uu->time_hi_and_version = tmp;
148 tmp = (tmp << 8) | *ptr++;
151 memcpy(uu->node, ptr, 6);
154 static void uuid_unparse(void *uu, char *out)
158 unpack_uuid(uu, &uuid);
160 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
161 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
162 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
163 uuid.node[0], uuid.node[1], uuid.node[2],
164 uuid.node[3], uuid.node[4], uuid.node[5]);
167 errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
168 int flags, ext2_icount_t *ret)
170 ext2_icount_t icount;
175 retval = alloc_icount(fs, flags, &icount);
179 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn);
182 uuid_unparse(fs->super->s_uuid, uuid);
183 sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
187 icount->tdb = tdb_open(fn, 0, TDB_CLEAR_IF_FIRST,
188 O_RDWR | O_CREAT | O_TRUNC, 0600);
199 ext2fs_free_icount(icount);
203 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
204 ext2_icount_t hint, ext2_icount_t *ret)
206 ext2_icount_t icount;
212 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
213 if (hint->size > size)
214 size = (size_t) hint->size;
217 retval = alloc_icount(fs, flags, &icount);
225 * Figure out how many special case inode counts we will
226 * have. We know we will need one for each directory;
227 * we also need to reserve some extra room for file links
229 retval = ext2fs_get_num_dirs(fs, &icount->size);
232 icount->size += fs->super->s_inodes_count / 50;
235 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
237 printf("Icount allocated %u entries, %d bytes.\n",
238 icount->size, bytes);
240 retval = ext2fs_get_mem(bytes, &icount->list);
243 memset(icount->list, 0, bytes);
249 * Populate the sorted list with those entries which were
250 * found in the hint icount (since those are ones which will
251 * likely need to be in the sorted list this time around).
254 for (i=0; i < hint->count; i++)
255 icount->list[i].ino = hint->list[i].ino;
256 icount->count = hint->count;
263 ext2fs_free_icount(icount);
267 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
271 return ext2fs_create_icount2(fs, flags, size, 0, ret);
275 * insert_icount_el() --- Insert a new entry into the sorted list at a
276 * specified position.
278 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
279 ext2_ino_t ino, int pos)
281 struct ext2_icount_el *el;
283 ext2_ino_t new_size = 0;
286 if (icount->last_lookup && icount->last_lookup->ino == ino)
287 return icount->last_lookup;
289 if (icount->count >= icount->size) {
291 new_size = icount->list[(unsigned)icount->count-1].ino;
292 new_size = (ext2_ino_t) (icount->count *
293 ((float) icount->num_inodes / new_size));
295 if (new_size < (icount->size + 100))
296 new_size = icount->size + 100;
298 printf("Reallocating icount %u entries...\n", new_size);
300 retval = ext2fs_resize_mem((size_t) icount->size *
301 sizeof(struct ext2_icount_el),
303 sizeof(struct ext2_icount_el),
307 icount->size = new_size;
309 num = (int) icount->count - pos;
311 return 0; /* should never happen */
313 memmove(&icount->list[pos+1], &icount->list[pos],
314 sizeof(struct ext2_icount_el) * num);
317 el = &icount->list[pos];
320 icount->last_lookup = el;
325 * get_icount_el() --- given an inode number, try to find icount
326 * information in the sorted list. If the create flag is set,
327 * and we can't find an entry, create one in the sorted list.
329 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
330 ext2_ino_t ino, int create)
334 ext2_ino_t lowval, highval;
336 if (!icount || !icount->list)
339 if (create && ((icount->count == 0) ||
340 (ino > icount->list[(unsigned)icount->count-1].ino))) {
341 return insert_icount_el(icount, ino, (unsigned) icount->count);
343 if (icount->count == 0)
346 if (icount->cursor >= icount->count)
348 if (ino == icount->list[icount->cursor].ino)
349 return &icount->list[icount->cursor++];
351 printf("Non-cursor get_icount_el: %u\n", ino);
354 high = (int) icount->count-1;
355 while (low <= high) {
362 /* Interpolate for efficiency */
363 lowval = icount->list[low].ino;
364 highval = icount->list[high].ino;
368 else if (ino > highval)
371 range = ((float) (ino - lowval)) /
378 mid = low + ((int) (range * (high-low)));
381 if (ino == icount->list[mid].ino) {
382 icount->cursor = mid+1;
383 return &icount->list[mid];
385 if (ino < icount->list[mid].ino)
391 * If we need to create a new entry, it should be right at
392 * low (where high will be left at low-1).
395 return insert_icount_el(icount, ino, low);
399 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
402 struct ext2_icount_el *el;
406 key.dptr = (unsigned char *) &ino;
407 key.dsize = sizeof(ext2_ino_t);
408 data.dptr = (unsigned char *) &count;
409 data.dsize = sizeof(__u16);
411 if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
412 return tdb_error(icount->tdb) +
415 if (tdb_delete(icount->tdb, key))
416 return tdb_error(icount->tdb) +
422 el = get_icount_el(icount, ino, 1);
424 return EXT2_ET_NO_MEMORY;
430 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
433 struct ext2_icount_el *el;
437 key.dptr = (unsigned char *) &ino;
438 key.dsize = sizeof(ext2_ino_t);
440 data = tdb_fetch(icount->tdb, key);
441 if (data.dptr == NULL) {
443 return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
446 *count = *((__u16 *) data.dptr);
450 el = get_icount_el(icount, ino, 0);
460 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
464 const char *bad = "bad icount";
466 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
468 if (icount->count > icount->size) {
469 fprintf(out, "%s: count > size\n", bad);
470 return EXT2_ET_INVALID_ARGUMENT;
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;
483 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
485 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
487 if (!ino || (ino > icount->num_inodes))
488 return EXT2_ET_INVALID_ARGUMENT;
490 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
494 if (icount->multiple &&
495 !ext2fs_test_inode_bitmap(icount->multiple, ino)) {
499 get_inode_count(icount, ino, ret);
503 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
508 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
510 if (!ino || (ino > icount->num_inodes))
511 return EXT2_ET_INVALID_ARGUMENT;
513 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
515 * If the existing count is 1, then we know there is
516 * no entry in the list.
518 if (set_inode_count(icount, ino, 2))
519 return EXT2_ET_NO_MEMORY;
521 ext2fs_unmark_inode_bitmap(icount->single, ino);
522 } else if (icount->multiple) {
524 * The count is either zero or greater than 1; if the
525 * inode is set in icount->multiple, then there should
526 * be an entry in the list, so we need to fix it.
528 if (ext2fs_test_inode_bitmap(icount->multiple, ino)) {
529 get_inode_count(icount, ino, &curr_value);
531 if (set_inode_count(icount, ino, curr_value))
532 return EXT2_ET_NO_MEMORY;
535 * The count was zero; mark the single bitmap
538 ext2fs_mark_inode_bitmap(icount->single, ino);
545 * The count is either zero or greater than 1; try to
546 * find an entry in the list to determine which.
548 get_inode_count(icount, ino, &curr_value);
550 if (set_inode_count(icount, ino, curr_value))
551 return EXT2_ET_NO_MEMORY;
553 if (icount->multiple)
554 ext2fs_mark_inode_bitmap(icount->multiple, ino);
560 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
565 if (!ino || (ino > icount->num_inodes))
566 return EXT2_ET_INVALID_ARGUMENT;
568 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
570 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
571 ext2fs_unmark_inode_bitmap(icount->single, ino);
572 if (icount->multiple)
573 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
575 set_inode_count(icount, ino, 0);
582 if (icount->multiple &&
583 !ext2fs_test_inode_bitmap(icount->multiple, ino))
584 return EXT2_ET_INVALID_ARGUMENT;
586 get_inode_count(icount, ino, &curr_value);
588 return EXT2_ET_INVALID_ARGUMENT;
590 if (set_inode_count(icount, ino, curr_value))
591 return EXT2_ET_NO_MEMORY;
594 ext2fs_mark_inode_bitmap(icount->single, ino);
595 if ((curr_value == 0) && icount->multiple)
596 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
603 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
606 if (!ino || (ino > icount->num_inodes))
607 return EXT2_ET_INVALID_ARGUMENT;
609 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
612 ext2fs_mark_inode_bitmap(icount->single, ino);
613 if (icount->multiple)
614 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
618 ext2fs_unmark_inode_bitmap(icount->single, ino);
619 if (icount->multiple) {
621 * If the icount->multiple bitmap is enabled,
622 * we can just clear both bitmaps and we're done
624 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
626 set_inode_count(icount, ino, 0);
630 if (set_inode_count(icount, ino, count))
631 return EXT2_ET_NO_MEMORY;
632 ext2fs_unmark_inode_bitmap(icount->single, ino);
633 if (icount->multiple)
634 ext2fs_mark_inode_bitmap(icount->multiple, ino);
638 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
640 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
649 ext2_icount_t icount;
654 #define INCREMENT 0x03
655 #define DECREMENT 0x04
657 struct test_program {
664 struct test_program prog[] = {
665 { STORE, 42, 42, 42 },
671 { INCREMENT, 5, 0, 1 },
672 { INCREMENT, 5, 0, 2 },
673 { INCREMENT, 5, 0, 3 },
674 { INCREMENT, 5, 0, 4 },
675 { DECREMENT, 5, 0, 3 },
676 { DECREMENT, 5, 0, 2 },
677 { DECREMENT, 5, 0, 1 },
678 { DECREMENT, 5, 0, 0 },
683 { INCREMENT, 1, 0, 2 },
684 { DECREMENT, 2, 0, 1 },
685 { DECREMENT, 2, 0, 0 },
690 struct test_program extended[] = {
725 * Setup the variables for doing the inode scan test.
727 static void setup(void)
730 struct ext2_super_block param;
732 initialize_ext2_error_table();
734 memset(¶m, 0, sizeof(param));
735 param.s_blocks_count = 12000;
737 retval = ext2fs_initialize("test fs", 0, ¶m,
738 test_io_manager, &test_fs);
740 com_err("setup", retval,
741 "while initializing filesystem");
744 retval = ext2fs_allocate_tables(test_fs);
746 com_err("setup", retval,
747 "while allocating tables for test filesystem");
752 int run_test(int flags, int size, char *dir, struct test_program *prog)
755 ext2_icount_t icount;
756 struct test_program *pc;
761 retval = ext2fs_create_icount_tdb(test_fs, dir,
764 com_err("run_test", retval,
765 "while creating icount using tdb");
769 retval = ext2fs_create_icount2(test_fs, flags, size, 0,
772 com_err("run_test", retval, "while creating icount");
776 for (pc = prog; pc->cmd != EXIT; pc++) {
779 printf("icount_fetch(%u) = ", pc->ino);
782 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
784 com_err("run_test", retval,
785 "while calling icount_store");
788 printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
791 retval = ext2fs_icount_increment(icount, pc->ino, 0);
793 com_err("run_test", retval,
794 "while calling icount_increment");
797 printf("icount_increment(%u) = ", pc->ino);
800 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
802 com_err("run_test", retval,
803 "while calling icount_decrement");
806 printf("icount_decrement(%u) = ", pc->ino);
809 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
811 com_err("run_test", retval,
812 "while calling icount_fetch");
815 printf("%u (%s)\n", result, (result == pc->expected) ?
817 if (result != pc->expected)
820 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
821 retval = ext2fs_icount_validate(icount, stdout);
823 com_err("run_test", retval, "while calling icount_validate");
826 ext2fs_free_icount(icount);
831 int main(int argc, char **argv)
836 printf("Standard icount run:\n");
837 failed += run_test(0, 0, 0, prog);
838 printf("\nMultiple bitmap test:\n");
839 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, 0, prog);
840 printf("\nResizing icount:\n");
841 failed += run_test(0, 3, 0, extended);
842 printf("\nStandard icount run with tdb:\n");
843 failed += run_test(0, 0, ".", prog);
844 printf("\nMultiple bitmap test with tdb:\n");
845 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, ".", prog);