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;
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.
70 #define icount_16_xlate(x) (((x) > 65500) ? 65500 : (x))
72 void ext2fs_free_icount(ext2_icount_t icount)
79 ext2fs_free_mem(&icount->list);
81 ext2fs_free_inode_bitmap(icount->single);
83 ext2fs_free_inode_bitmap(icount->multiple);
85 tdb_close(icount->tdb);
87 unlink(icount->tdb_fn);
91 ext2fs_free_mem(&icount);
94 static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
101 retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
104 memset(icount, 0, sizeof(struct ext2_icount));
106 retval = ext2fs_allocate_inode_bitmap(fs, 0, &icount->single);
110 if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
111 retval = ext2fs_allocate_inode_bitmap(fs, 0,
116 icount->multiple = 0;
118 icount->magic = EXT2_ET_MAGIC_ICOUNT;
119 icount->num_inodes = fs->super->s_inodes_count;
125 ext2fs_free_icount(icount);
132 __u16 time_hi_and_version;
137 static void unpack_uuid(void *in, struct uuid *uu)
143 tmp = (tmp << 8) | *ptr++;
144 tmp = (tmp << 8) | *ptr++;
145 tmp = (tmp << 8) | *ptr++;
149 tmp = (tmp << 8) | *ptr++;
153 tmp = (tmp << 8) | *ptr++;
154 uu->time_hi_and_version = tmp;
157 tmp = (tmp << 8) | *ptr++;
160 memcpy(uu->node, ptr, 6);
163 static void uuid_unparse(void *uu, char *out)
167 unpack_uuid(uu, &uuid);
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]);
176 errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
177 int flags, ext2_icount_t *ret)
179 ext2_icount_t icount;
184 retval = alloc_icount(fs, flags, &icount);
188 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn);
191 uuid_unparse(fs->super->s_uuid, uuid);
192 sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
196 icount->tdb = tdb_open(fn, 0, TDB_CLEAR_IF_FIRST,
197 O_RDWR | O_CREAT | O_TRUNC, 0600);
208 ext2fs_free_icount(icount);
212 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
213 ext2_icount_t hint, ext2_icount_t *ret)
215 ext2_icount_t icount;
221 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
222 if (hint->size > size)
223 size = (size_t) hint->size;
226 retval = alloc_icount(fs, flags, &icount);
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
238 retval = ext2fs_get_num_dirs(fs, &icount->size);
241 icount->size += fs->super->s_inodes_count / 50;
244 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
246 printf("Icount allocated %u entries, %d bytes.\n",
247 icount->size, bytes);
249 retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
253 memset(icount->list, 0, bytes);
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).
264 for (i=0; i < hint->count; i++)
265 icount->list[i].ino = hint->list[i].ino;
266 icount->count = hint->count;
273 ext2fs_free_icount(icount);
277 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
281 return ext2fs_create_icount2(fs, flags, size, 0, ret);
285 * insert_icount_el() --- Insert a new entry into the sorted list at a
286 * specified position.
288 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
289 ext2_ino_t ino, int pos)
291 struct ext2_icount_el *el;
293 ext2_ino_t new_size = 0;
296 if (icount->last_lookup && icount->last_lookup->ino == ino)
297 return icount->last_lookup;
299 if (icount->count >= icount->size) {
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));
305 if (new_size < (icount->size + 100))
306 new_size = icount->size + 100;
308 printf("Reallocating icount %u entries...\n", new_size);
310 retval = ext2fs_resize_mem((size_t) icount->size *
311 sizeof(struct ext2_icount_el),
313 sizeof(struct ext2_icount_el),
317 icount->size = new_size;
319 num = (int) icount->count - pos;
321 return 0; /* should never happen */
323 memmove(&icount->list[pos+1], &icount->list[pos],
324 sizeof(struct ext2_icount_el) * num);
327 el = &icount->list[pos];
330 icount->last_lookup = el;
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.
339 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
340 ext2_ino_t ino, int create)
344 ext2_ino_t lowval, highval;
346 if (!icount || !icount->list)
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);
353 if (icount->count == 0)
356 if (icount->cursor >= icount->count)
358 if (ino == icount->list[icount->cursor].ino)
359 return &icount->list[icount->cursor++];
361 printf("Non-cursor get_icount_el: %u\n", ino);
364 high = (int) icount->count-1;
365 while (low <= high) {
372 /* Interpolate for efficiency */
373 lowval = icount->list[low].ino;
374 highval = icount->list[high].ino;
378 else if (ino > highval)
381 range = ((float) (ino - lowval)) /
388 mid = low + ((int) (range * (high-low)));
391 if (ino == icount->list[mid].ino) {
392 icount->cursor = mid+1;
393 return &icount->list[mid];
395 if (ino < icount->list[mid].ino)
401 * If we need to create a new entry, it should be right at
402 * low (where high will be left at low-1).
405 return insert_icount_el(icount, ino, low);
409 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
412 struct ext2_icount_el *el;
416 key.dptr = (unsigned char *) &ino;
417 key.dsize = sizeof(ext2_ino_t);
418 data.dptr = (unsigned char *) &count;
419 data.dsize = sizeof(__u32);
421 if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
422 return tdb_error(icount->tdb) +
425 if (tdb_delete(icount->tdb, key))
426 return tdb_error(icount->tdb) +
432 el = get_icount_el(icount, ino, 1);
434 return EXT2_ET_NO_MEMORY;
440 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
443 struct ext2_icount_el *el;
447 key.dptr = (unsigned char *) &ino;
448 key.dsize = sizeof(ext2_ino_t);
450 data = tdb_fetch(icount->tdb, key);
451 if (data.dptr == NULL) {
453 return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
456 *count = *((__u32 *) data.dptr);
460 el = get_icount_el(icount, ino, 0);
470 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
474 const char *bad = "bad icount";
476 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
478 if (icount->count > icount->size) {
479 fprintf(out, "%s: count > size\n", bad);
480 return EXT2_ET_INVALID_ARGUMENT;
482 for (i=1; i < icount->count; i++) {
483 if (icount->list[i-1].ino >= icount->list[i].ino) {
484 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
485 bad, i-1, icount->list[i-1].ino,
486 i, icount->list[i].ino);
487 ret = EXT2_ET_INVALID_ARGUMENT;
493 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
496 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
498 if (!ino || (ino > icount->num_inodes))
499 return EXT2_ET_INVALID_ARGUMENT;
501 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
505 if (icount->multiple &&
506 !ext2fs_test_inode_bitmap(icount->multiple, ino)) {
510 get_inode_count(icount, ino, &val);
511 *ret = icount_16_xlate(val);
515 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
520 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
522 if (!ino || (ino > icount->num_inodes))
523 return EXT2_ET_INVALID_ARGUMENT;
525 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
527 * If the existing count is 1, then we know there is
528 * no entry in the list.
530 if (set_inode_count(icount, ino, 2))
531 return EXT2_ET_NO_MEMORY;
533 ext2fs_unmark_inode_bitmap(icount->single, ino);
534 } else if (icount->multiple) {
536 * The count is either zero or greater than 1; if the
537 * inode is set in icount->multiple, then there should
538 * be an entry in the list, so we need to fix it.
540 if (ext2fs_test_inode_bitmap(icount->multiple, ino)) {
541 get_inode_count(icount, ino, &curr_value);
543 if (set_inode_count(icount, ino, curr_value))
544 return EXT2_ET_NO_MEMORY;
547 * The count was zero; mark the single bitmap
550 ext2fs_mark_inode_bitmap(icount->single, ino);
557 * The count is either zero or greater than 1; try to
558 * find an entry in the list to determine which.
560 get_inode_count(icount, ino, &curr_value);
562 if (set_inode_count(icount, ino, curr_value))
563 return EXT2_ET_NO_MEMORY;
565 if (icount->multiple)
566 ext2fs_mark_inode_bitmap(icount->multiple, ino);
568 *ret = icount_16_xlate(curr_value);
572 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
577 if (!ino || (ino > icount->num_inodes))
578 return EXT2_ET_INVALID_ARGUMENT;
580 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
582 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
583 ext2fs_unmark_inode_bitmap(icount->single, ino);
584 if (icount->multiple)
585 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
587 set_inode_count(icount, ino, 0);
594 if (icount->multiple &&
595 !ext2fs_test_inode_bitmap(icount->multiple, ino))
596 return EXT2_ET_INVALID_ARGUMENT;
598 get_inode_count(icount, ino, &curr_value);
600 return EXT2_ET_INVALID_ARGUMENT;
602 if (set_inode_count(icount, ino, curr_value))
603 return EXT2_ET_NO_MEMORY;
606 ext2fs_mark_inode_bitmap(icount->single, ino);
607 if ((curr_value == 0) && icount->multiple)
608 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
611 *ret = icount_16_xlate(curr_value);
615 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
618 if (!ino || (ino > icount->num_inodes))
619 return EXT2_ET_INVALID_ARGUMENT;
621 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
624 ext2fs_mark_inode_bitmap(icount->single, ino);
625 if (icount->multiple)
626 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
630 ext2fs_unmark_inode_bitmap(icount->single, ino);
631 if (icount->multiple) {
633 * If the icount->multiple bitmap is enabled,
634 * we can just clear both bitmaps and we're done
636 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
638 set_inode_count(icount, ino, 0);
642 if (set_inode_count(icount, ino, count))
643 return EXT2_ET_NO_MEMORY;
644 ext2fs_unmark_inode_bitmap(icount->single, ino);
645 if (icount->multiple)
646 ext2fs_mark_inode_bitmap(icount->multiple, ino);
650 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
652 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
661 ext2_icount_t icount;
666 #define INCREMENT 0x03
667 #define DECREMENT 0x04
669 struct test_program {
676 struct test_program prog[] = {
677 { STORE, 42, 42, 42 },
683 { INCREMENT, 5, 0, 1 },
684 { INCREMENT, 5, 0, 2 },
685 { INCREMENT, 5, 0, 3 },
686 { INCREMENT, 5, 0, 4 },
687 { DECREMENT, 5, 0, 3 },
688 { DECREMENT, 5, 0, 2 },
689 { DECREMENT, 5, 0, 1 },
690 { DECREMENT, 5, 0, 0 },
695 { INCREMENT, 1, 0, 2 },
696 { DECREMENT, 2, 0, 1 },
697 { DECREMENT, 2, 0, 0 },
702 struct test_program extended[] = {
737 * Setup the variables for doing the inode scan test.
739 static void setup(void)
742 struct ext2_super_block param;
744 initialize_ext2_error_table();
746 memset(¶m, 0, sizeof(param));
747 param.s_blocks_count = 12000;
749 retval = ext2fs_initialize("test fs", 0, ¶m,
750 test_io_manager, &test_fs);
752 com_err("setup", retval,
753 "while initializing filesystem");
756 retval = ext2fs_allocate_tables(test_fs);
758 com_err("setup", retval,
759 "while allocating tables for test filesystem");
764 int run_test(int flags, int size, char *dir, struct test_program *prog)
767 ext2_icount_t icount;
768 struct test_program *pc;
773 retval = ext2fs_create_icount_tdb(test_fs, dir,
776 com_err("run_test", retval,
777 "while creating icount using tdb");
781 retval = ext2fs_create_icount2(test_fs, flags, size, 0,
784 com_err("run_test", retval, "while creating icount");
788 for (pc = prog; pc->cmd != EXIT; pc++) {
791 printf("icount_fetch(%u) = ", pc->ino);
794 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
796 com_err("run_test", retval,
797 "while calling icount_store");
800 printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
803 retval = ext2fs_icount_increment(icount, pc->ino, 0);
805 com_err("run_test", retval,
806 "while calling icount_increment");
809 printf("icount_increment(%u) = ", pc->ino);
812 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
814 com_err("run_test", retval,
815 "while calling icount_decrement");
818 printf("icount_decrement(%u) = ", pc->ino);
821 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
823 com_err("run_test", retval,
824 "while calling icount_fetch");
827 printf("%u (%s)\n", result, (result == pc->expected) ?
829 if (result != pc->expected)
832 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
833 retval = ext2fs_icount_validate(icount, stdout);
835 com_err("run_test", retval, "while calling icount_validate");
838 ext2fs_free_icount(icount);
843 int main(int argc, char **argv)
848 printf("Standard icount run:\n");
849 failed += run_test(0, 0, 0, prog);
850 printf("\nMultiple bitmap test:\n");
851 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, 0, prog);
852 printf("\nResizing icount:\n");
853 failed += run_test(0, 3, 0, extended);
854 printf("\nStandard icount run with tdb:\n");
855 failed += run_test(0, 0, ".", prog);
856 printf("\nMultiple bitmap test with tdb:\n");
857 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, ".", prog);