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 Library
8 * General Public License, version 2.
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).
32 * Also, e2fsck tends to load the icount data sequentially.
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.
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.
45 struct ext2_icount_el {
52 ext2fs_inode_bitmap single;
53 ext2fs_inode_bitmap multiple;
56 ext2_ino_t num_inodes;
58 struct ext2_icount_el *list;
59 struct ext2_icount_el *last_lookup;
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.
71 #define icount_16_xlate(x) (((x) > 65500) ? 65500 : (x))
73 void ext2fs_free_icount(ext2_icount_t icount)
80 ext2fs_free_mem(&icount->list);
82 ext2fs_free_inode_bitmap(icount->single);
84 ext2fs_free_inode_bitmap(icount->multiple);
86 tdb_close(icount->tdb);
88 unlink(icount->tdb_fn);
92 ext2fs_free_mem(&icount);
95 static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
102 retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
105 memset(icount, 0, sizeof(struct ext2_icount));
107 retval = ext2fs_allocate_inode_bitmap(fs, "icount", &icount->single);
111 if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
112 retval = ext2fs_allocate_inode_bitmap(fs, "icount_inc",
117 icount->multiple = 0;
119 icount->magic = EXT2_ET_MAGIC_ICOUNT;
120 icount->num_inodes = fs->super->s_inodes_count;
126 ext2fs_free_icount(icount);
133 __u16 time_hi_and_version;
138 static void unpack_uuid(void *in, struct uuid *uu)
144 tmp = (tmp << 8) | *ptr++;
145 tmp = (tmp << 8) | *ptr++;
146 tmp = (tmp << 8) | *ptr++;
150 tmp = (tmp << 8) | *ptr++;
154 tmp = (tmp << 8) | *ptr++;
155 uu->time_hi_and_version = tmp;
158 tmp = (tmp << 8) | *ptr++;
161 memcpy(uu->node, ptr, 6);
164 static void uuid_unparse(void *uu, char *out)
168 unpack_uuid(uu, &uuid);
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]);
177 errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
178 int flags, ext2_icount_t *ret)
180 ext2_icount_t icount;
183 ext2_ino_t num_inodes;
186 retval = alloc_icount(fs, flags, &icount);
190 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn);
193 uuid_unparse(fs->super->s_uuid, uuid);
194 sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
198 * This is an overestimate of the size that we will need; the
199 * ideal value is the number of used inodes with a count
200 * greater than 1. OTOH the times when we really need this is
201 * with the backup programs that use lots of hard links, in
202 * which case the number of inodes in use approaches the ideal
205 num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
208 icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
209 O_RDWR | O_CREAT | O_TRUNC, 0600);
220 ext2fs_free_icount(icount);
224 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
225 ext2_icount_t hint, ext2_icount_t *ret)
227 ext2_icount_t icount;
233 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
234 if (hint->size > size)
235 size = (size_t) hint->size;
238 retval = alloc_icount(fs, flags, &icount);
246 * Figure out how many special case inode counts we will
247 * have. We know we will need one for each directory;
248 * we also need to reserve some extra room for file links
250 retval = ext2fs_get_num_dirs(fs, &icount->size);
253 icount->size += fs->super->s_inodes_count / 50;
256 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
258 printf("Icount allocated %u entries, %d bytes.\n",
259 icount->size, bytes);
261 retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
265 memset(icount->list, 0, bytes);
271 * Populate the sorted list with those entries which were
272 * found in the hint icount (since those are ones which will
273 * likely need to be in the sorted list this time around).
276 for (i=0; i < hint->count; i++)
277 icount->list[i].ino = hint->list[i].ino;
278 icount->count = hint->count;
285 ext2fs_free_icount(icount);
289 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
293 return ext2fs_create_icount2(fs, flags, size, 0, ret);
297 * insert_icount_el() --- Insert a new entry into the sorted list at a
298 * specified position.
300 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
301 ext2_ino_t ino, int pos)
303 struct ext2_icount_el *el;
305 ext2_ino_t new_size = 0;
308 if (icount->last_lookup && icount->last_lookup->ino == ino)
309 return icount->last_lookup;
311 if (icount->count >= icount->size) {
313 new_size = icount->list[(unsigned)icount->count-1].ino;
314 new_size = (ext2_ino_t) (icount->count *
315 ((float) icount->num_inodes / new_size));
317 if (new_size < (icount->size + 100))
318 new_size = icount->size + 100;
320 printf("Reallocating icount %u entries...\n", new_size);
322 retval = ext2fs_resize_mem((size_t) icount->size *
323 sizeof(struct ext2_icount_el),
325 sizeof(struct ext2_icount_el),
329 icount->size = new_size;
331 num = (int) icount->count - pos;
333 return 0; /* should never happen */
335 memmove(&icount->list[pos+1], &icount->list[pos],
336 sizeof(struct ext2_icount_el) * num);
339 el = &icount->list[pos];
342 icount->last_lookup = el;
347 * get_icount_el() --- given an inode number, try to find icount
348 * information in the sorted list. If the create flag is set,
349 * and we can't find an entry, create one in the sorted list.
351 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
352 ext2_ino_t ino, int create)
356 if (!icount || !icount->list)
359 if (create && ((icount->count == 0) ||
360 (ino > icount->list[(unsigned)icount->count-1].ino))) {
361 return insert_icount_el(icount, ino, (unsigned) icount->count);
363 if (icount->count == 0)
366 if (icount->cursor >= icount->count)
368 if (ino == icount->list[icount->cursor].ino)
369 return &icount->list[icount->cursor++];
371 printf("Non-cursor get_icount_el: %u\n", ino);
374 high = (int) icount->count-1;
375 while (low <= high) {
376 mid = ((unsigned)low + (unsigned)high) >> 1;
377 if (ino == icount->list[mid].ino) {
378 icount->cursor = mid+1;
379 return &icount->list[mid];
381 if (ino < icount->list[mid].ino)
387 * If we need to create a new entry, it should be right at
388 * low (where high will be left at low-1).
391 return insert_icount_el(icount, ino, low);
395 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
398 struct ext2_icount_el *el;
402 key.dptr = (unsigned char *) &ino;
403 key.dsize = sizeof(ext2_ino_t);
404 data.dptr = (unsigned char *) &count;
405 data.dsize = sizeof(__u32);
407 if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
408 return tdb_error(icount->tdb) +
411 if (tdb_delete(icount->tdb, key))
412 return tdb_error(icount->tdb) +
418 el = get_icount_el(icount, ino, 1);
420 return EXT2_ET_NO_MEMORY;
426 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
429 struct ext2_icount_el *el;
433 key.dptr = (unsigned char *) &ino;
434 key.dsize = sizeof(ext2_ino_t);
436 data = tdb_fetch(icount->tdb, key);
437 if (data.dptr == NULL) {
439 return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
442 *count = *((__u32 *) data.dptr);
446 el = get_icount_el(icount, ino, 0);
456 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
460 const char *bad = "bad icount";
462 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
464 if (icount->count > icount->size) {
465 fprintf(out, "%s: count > size\n", bad);
466 return EXT2_ET_INVALID_ARGUMENT;
468 for (i=1; i < icount->count; i++) {
469 if (icount->list[i-1].ino >= icount->list[i].ino) {
470 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
471 bad, i-1, icount->list[i-1].ino,
472 i, icount->list[i].ino);
473 ret = EXT2_ET_INVALID_ARGUMENT;
479 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
482 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
484 if (!ino || (ino > icount->num_inodes))
485 return EXT2_ET_INVALID_ARGUMENT;
487 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
491 if (icount->multiple &&
492 !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
496 get_inode_count(icount, ino, &val);
497 *ret = icount_16_xlate(val);
501 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
506 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
508 if (!ino || (ino > icount->num_inodes))
509 return EXT2_ET_INVALID_ARGUMENT;
511 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
513 * If the existing count is 1, then we know there is
514 * no entry in the list.
516 if (set_inode_count(icount, ino, 2))
517 return EXT2_ET_NO_MEMORY;
519 ext2fs_unmark_inode_bitmap2(icount->single, ino);
520 } else if (icount->multiple) {
522 * The count is either zero or greater than 1; if the
523 * inode is set in icount->multiple, then there should
524 * be an entry in the list, so we need to fix it.
526 if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
527 get_inode_count(icount, ino, &curr_value);
529 if (set_inode_count(icount, ino, curr_value))
530 return EXT2_ET_NO_MEMORY;
533 * The count was zero; mark the single bitmap
536 ext2fs_mark_inode_bitmap2(icount->single, ino);
543 * The count is either zero or greater than 1; try to
544 * find an entry in the list to determine which.
546 get_inode_count(icount, ino, &curr_value);
548 if (set_inode_count(icount, ino, curr_value))
549 return EXT2_ET_NO_MEMORY;
551 if (icount->multiple)
552 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
554 *ret = icount_16_xlate(curr_value);
558 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
563 if (!ino || (ino > icount->num_inodes))
564 return EXT2_ET_INVALID_ARGUMENT;
566 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
568 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
569 ext2fs_unmark_inode_bitmap2(icount->single, ino);
570 if (icount->multiple)
571 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
573 set_inode_count(icount, ino, 0);
580 if (icount->multiple &&
581 !ext2fs_test_inode_bitmap2(icount->multiple, ino))
582 return EXT2_ET_INVALID_ARGUMENT;
584 get_inode_count(icount, ino, &curr_value);
586 return EXT2_ET_INVALID_ARGUMENT;
588 if (set_inode_count(icount, ino, curr_value))
589 return EXT2_ET_NO_MEMORY;
592 ext2fs_mark_inode_bitmap2(icount->single, ino);
593 if ((curr_value == 0) && icount->multiple)
594 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
597 *ret = icount_16_xlate(curr_value);
601 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
604 if (!ino || (ino > icount->num_inodes))
605 return EXT2_ET_INVALID_ARGUMENT;
607 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
610 ext2fs_mark_inode_bitmap2(icount->single, ino);
611 if (icount->multiple)
612 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
616 ext2fs_unmark_inode_bitmap2(icount->single, ino);
617 if (icount->multiple) {
619 * If the icount->multiple bitmap is enabled,
620 * we can just clear both bitmaps and we're done
622 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
624 set_inode_count(icount, ino, 0);
628 if (set_inode_count(icount, ino, count))
629 return EXT2_ET_NO_MEMORY;
630 ext2fs_unmark_inode_bitmap2(icount->single, ino);
631 if (icount->multiple)
632 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
636 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
638 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
647 ext2_icount_t icount;
652 #define INCREMENT 0x03
653 #define DECREMENT 0x04
655 struct test_program {
662 struct test_program prog[] = {
663 { STORE, 42, 42, 42 },
669 { INCREMENT, 5, 0, 1 },
670 { INCREMENT, 5, 0, 2 },
671 { INCREMENT, 5, 0, 3 },
672 { INCREMENT, 5, 0, 4 },
673 { DECREMENT, 5, 0, 3 },
674 { DECREMENT, 5, 0, 2 },
675 { DECREMENT, 5, 0, 1 },
676 { DECREMENT, 5, 0, 0 },
681 { INCREMENT, 1, 0, 2 },
682 { DECREMENT, 2, 0, 1 },
683 { DECREMENT, 2, 0, 0 },
688 struct test_program extended[] = {
723 * Setup the variables for doing the inode scan test.
725 static void setup(void)
728 struct ext2_super_block param;
730 initialize_ext2_error_table();
732 memset(¶m, 0, sizeof(param));
733 ext2fs_blocks_count_set(¶m, 12000);
735 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m,
736 test_io_manager, &test_fs);
738 com_err("setup", retval,
739 "while initializing filesystem");
742 retval = ext2fs_allocate_tables(test_fs);
744 com_err("setup", retval,
745 "while allocating tables for test filesystem");
750 int run_test(int flags, int size, char *dir, struct test_program *prog)
753 ext2_icount_t icount;
754 struct test_program *pc;
759 retval = ext2fs_create_icount_tdb(test_fs, dir,
762 com_err("run_test", retval,
763 "while creating icount using tdb");
767 retval = ext2fs_create_icount2(test_fs, flags, size, 0,
770 com_err("run_test", retval, "while creating icount");
774 for (pc = prog; pc->cmd != EXIT; pc++) {
777 printf("icount_fetch(%u) = ", pc->ino);
780 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
782 com_err("run_test", retval,
783 "while calling icount_store");
786 printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
789 retval = ext2fs_icount_increment(icount, pc->ino, 0);
791 com_err("run_test", retval,
792 "while calling icount_increment");
795 printf("icount_increment(%u) = ", pc->ino);
798 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
800 com_err("run_test", retval,
801 "while calling icount_decrement");
804 printf("icount_decrement(%u) = ", pc->ino);
807 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
809 com_err("run_test", retval,
810 "while calling icount_fetch");
813 printf("%u (%s)\n", result, (result == pc->expected) ?
815 if (result != pc->expected)
818 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
819 retval = ext2fs_icount_validate(icount, stdout);
821 com_err("run_test", retval, "while calling icount_validate");
824 ext2fs_free_icount(icount);
829 int main(int argc, char **argv)
834 printf("Standard icount run:\n");
835 failed += run_test(0, 0, 0, prog);
836 printf("\nMultiple bitmap test:\n");
837 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, 0, prog);
838 printf("\nResizing icount:\n");
839 failed += run_test(0, 3, 0, extended);
840 printf("\nStandard icount run with tdb:\n");
841 failed += run_test(0, 0, ".", prog);
842 printf("\nMultiple bitmap test with tdb:\n");
843 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, ".", prog);