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;
187 retval = alloc_icount(fs, flags, &icount);
191 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn);
194 uuid_unparse(fs->super->s_uuid, uuid);
195 sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
197 save_umask = umask(077);
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
212 num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
214 icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
215 O_RDWR | O_CREAT | O_TRUNC, 0600);
217 if (icount->tdb == NULL) {
224 ext2fs_free_icount(icount);
228 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
229 ext2_icount_t hint, ext2_icount_t *ret)
231 ext2_icount_t icount;
237 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
238 if (hint->size > size)
239 size = (size_t) hint->size;
242 retval = alloc_icount(fs, flags, &icount);
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
254 retval = ext2fs_get_num_dirs(fs, &icount->size);
257 icount->size += fs->super->s_inodes_count / 50;
260 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
262 printf("Icount allocated %u entries, %d bytes.\n",
263 icount->size, bytes);
265 retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
269 memset(icount->list, 0, bytes);
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).
280 for (i=0; i < hint->count; i++)
281 icount->list[i].ino = hint->list[i].ino;
282 icount->count = hint->count;
289 ext2fs_free_icount(icount);
293 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
297 return ext2fs_create_icount2(fs, flags, size, 0, ret);
301 * insert_icount_el() --- Insert a new entry into the sorted list at a
302 * specified position.
304 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
305 ext2_ino_t ino, int pos)
307 struct ext2_icount_el *el;
309 ext2_ino_t new_size = 0;
312 if (icount->last_lookup && icount->last_lookup->ino == ino)
313 return icount->last_lookup;
315 if (icount->count >= icount->size) {
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));
321 if (new_size < (icount->size + 100))
322 new_size = icount->size + 100;
324 printf("Reallocating icount %u entries...\n", new_size);
326 retval = ext2fs_resize_mem((size_t) icount->size *
327 sizeof(struct ext2_icount_el),
329 sizeof(struct ext2_icount_el),
333 icount->size = new_size;
335 num = (int) icount->count - pos;
337 return 0; /* should never happen */
339 memmove(&icount->list[pos+1], &icount->list[pos],
340 sizeof(struct ext2_icount_el) * num);
343 el = &icount->list[pos];
346 icount->last_lookup = el;
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.
355 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
356 ext2_ino_t ino, int create)
360 if (!icount || !icount->list)
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);
367 if (icount->count == 0)
370 if (icount->cursor >= icount->count)
372 if (ino == icount->list[icount->cursor].ino)
373 return &icount->list[icount->cursor++];
375 printf("Non-cursor get_icount_el: %u\n", ino);
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];
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(__u32);
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 = *((__u32 *) 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)
486 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
488 if (!ino || (ino > icount->num_inodes))
489 return EXT2_ET_INVALID_ARGUMENT;
491 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
495 if (icount->multiple &&
496 !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
500 get_inode_count(icount, ino, &val);
501 *ret = icount_16_xlate(val);
505 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
510 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
512 if (!ino || (ino > icount->num_inodes))
513 return EXT2_ET_INVALID_ARGUMENT;
515 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
517 * If the existing count is 1, then we know there is
518 * no entry in the list.
520 if (set_inode_count(icount, ino, 2))
521 return EXT2_ET_NO_MEMORY;
523 ext2fs_unmark_inode_bitmap2(icount->single, ino);
524 } else if (icount->multiple) {
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.
530 if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
531 get_inode_count(icount, ino, &curr_value);
533 if (set_inode_count(icount, ino, curr_value))
534 return EXT2_ET_NO_MEMORY;
537 * The count was zero; mark the single bitmap
540 ext2fs_mark_inode_bitmap2(icount->single, ino);
547 * The count is either zero or greater than 1; try to
548 * find an entry in the list to determine which.
550 get_inode_count(icount, ino, &curr_value);
552 if (set_inode_count(icount, ino, curr_value))
553 return EXT2_ET_NO_MEMORY;
555 if (icount->multiple)
556 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
558 *ret = icount_16_xlate(curr_value);
562 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
567 if (!ino || (ino > icount->num_inodes))
568 return EXT2_ET_INVALID_ARGUMENT;
570 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
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);
577 set_inode_count(icount, ino, 0);
584 if (icount->multiple &&
585 !ext2fs_test_inode_bitmap2(icount->multiple, ino))
586 return EXT2_ET_INVALID_ARGUMENT;
588 get_inode_count(icount, ino, &curr_value);
590 return EXT2_ET_INVALID_ARGUMENT;
592 if (set_inode_count(icount, ino, curr_value))
593 return EXT2_ET_NO_MEMORY;
596 ext2fs_mark_inode_bitmap2(icount->single, ino);
597 if ((curr_value == 0) && icount->multiple)
598 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
601 *ret = icount_16_xlate(curr_value);
605 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
608 if (!ino || (ino > icount->num_inodes))
609 return EXT2_ET_INVALID_ARGUMENT;
611 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
614 ext2fs_mark_inode_bitmap2(icount->single, ino);
615 if (icount->multiple)
616 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
620 ext2fs_unmark_inode_bitmap2(icount->single, ino);
621 if (icount->multiple) {
623 * If the icount->multiple bitmap is enabled,
624 * we can just clear both bitmaps and we're done
626 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
628 set_inode_count(icount, ino, 0);
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);
640 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
642 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
651 ext2_icount_t icount;
656 #define INCREMENT 0x03
657 #define DECREMENT 0x04
659 struct test_program {
666 struct test_program prog[] = {
667 { STORE, 42, 42, 42 },
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 },
685 { INCREMENT, 1, 0, 2 },
686 { DECREMENT, 2, 0, 1 },
687 { DECREMENT, 2, 0, 0 },
692 struct test_program extended[] = {
727 * Setup the variables for doing the inode scan test.
729 static void setup(void)
732 struct ext2_super_block param;
734 initialize_ext2_error_table();
736 memset(¶m, 0, sizeof(param));
737 ext2fs_blocks_count_set(¶m, 12000);
739 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m,
740 test_io_manager, &test_fs);
742 com_err("setup", retval,
743 "while initializing filesystem");
746 retval = ext2fs_allocate_tables(test_fs);
748 com_err("setup", retval,
749 "while allocating tables for test filesystem");
754 int run_test(int flags, int size, char *dir, struct test_program *prog)
757 ext2_icount_t icount;
758 struct test_program *pc;
763 retval = ext2fs_create_icount_tdb(test_fs, dir,
766 com_err("run_test", retval,
767 "while creating icount using tdb");
771 retval = ext2fs_create_icount2(test_fs, flags, size, 0,
774 com_err("run_test", retval, "while creating icount");
778 for (pc = prog; pc->cmd != EXIT; pc++) {
781 printf("icount_fetch(%u) = ", pc->ino);
784 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
786 com_err("run_test", retval,
787 "while calling icount_store");
790 printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
793 retval = ext2fs_icount_increment(icount, pc->ino, 0);
795 com_err("run_test", retval,
796 "while calling icount_increment");
799 printf("icount_increment(%u) = ", pc->ino);
802 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
804 com_err("run_test", retval,
805 "while calling icount_decrement");
808 printf("icount_decrement(%u) = ", pc->ino);
811 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
813 com_err("run_test", retval,
814 "while calling icount_fetch");
817 printf("%u (%s)\n", result, (result == pc->expected) ?
819 if (result != pc->expected)
822 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
823 retval = ext2fs_icount_validate(icount, stdout);
825 com_err("run_test", retval, "while calling icount_validate");
828 ext2fs_free_icount(icount);
833 int main(int argc, char **argv)
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);