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);
196 save_umask = umask(077);
200 ext2fs_free_mem(&fn);
206 * This is an overestimate of the size that we will need; the
207 * ideal value is the number of used inodes with a count
208 * greater than 1. OTOH the times when we really need this is
209 * with the backup programs that use lots of hard links, in
210 * which case the number of inodes in use approaches the ideal
213 num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
215 icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
216 O_RDWR | O_CREAT | O_TRUNC, 0600);
218 if (icount->tdb == NULL) {
225 ext2fs_free_icount(icount);
229 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
230 ext2_icount_t hint, ext2_icount_t *ret)
232 ext2_icount_t icount;
238 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
239 if (hint->size > size)
240 size = (size_t) hint->size;
243 retval = alloc_icount(fs, flags, &icount);
251 * Figure out how many special case inode counts we will
252 * have. We know we will need one for each directory;
253 * we also need to reserve some extra room for file links
255 retval = ext2fs_get_num_dirs(fs, &icount->size);
258 icount->size += fs->super->s_inodes_count / 50;
261 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
263 printf("Icount allocated %u entries, %d bytes.\n",
264 icount->size, bytes);
266 retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
270 memset(icount->list, 0, bytes);
276 * Populate the sorted list with those entries which were
277 * found in the hint icount (since those are ones which will
278 * likely need to be in the sorted list this time around).
281 for (i=0; i < hint->count; i++)
282 icount->list[i].ino = hint->list[i].ino;
283 icount->count = hint->count;
290 ext2fs_free_icount(icount);
294 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
298 return ext2fs_create_icount2(fs, flags, size, 0, ret);
302 * insert_icount_el() --- Insert a new entry into the sorted list at a
303 * specified position.
305 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
306 ext2_ino_t ino, int pos)
308 struct ext2_icount_el *el;
310 ext2_ino_t new_size = 0;
313 if (icount->last_lookup && icount->last_lookup->ino == ino)
314 return icount->last_lookup;
316 if (icount->count >= icount->size) {
318 new_size = icount->list[(unsigned)icount->count-1].ino;
319 new_size = (ext2_ino_t) (icount->count *
320 ((float) icount->num_inodes / new_size));
322 if (new_size < (icount->size + 100))
323 new_size = icount->size + 100;
325 printf("Reallocating icount %u entries...\n", new_size);
327 retval = ext2fs_resize_mem((size_t) icount->size *
328 sizeof(struct ext2_icount_el),
330 sizeof(struct ext2_icount_el),
334 icount->size = new_size;
336 num = (int) icount->count - pos;
338 return 0; /* should never happen */
340 memmove(&icount->list[pos+1], &icount->list[pos],
341 sizeof(struct ext2_icount_el) * num);
344 el = &icount->list[pos];
347 icount->last_lookup = el;
352 * get_icount_el() --- given an inode number, try to find icount
353 * information in the sorted list. If the create flag is set,
354 * and we can't find an entry, create one in the sorted list.
356 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
357 ext2_ino_t ino, int create)
361 if (!icount || !icount->list)
364 if (create && ((icount->count == 0) ||
365 (ino > icount->list[(unsigned)icount->count-1].ino))) {
366 return insert_icount_el(icount, ino, (unsigned) icount->count);
368 if (icount->count == 0)
371 if (icount->cursor >= icount->count)
373 if (ino == icount->list[icount->cursor].ino)
374 return &icount->list[icount->cursor++];
376 printf("Non-cursor get_icount_el: %u\n", ino);
379 high = (int) icount->count-1;
380 while (low <= high) {
381 mid = ((unsigned)low + (unsigned)high) >> 1;
382 if (ino == icount->list[mid].ino) {
383 icount->cursor = mid+1;
384 return &icount->list[mid];
386 if (ino < icount->list[mid].ino)
392 * If we need to create a new entry, it should be right at
393 * low (where high will be left at low-1).
396 return insert_icount_el(icount, ino, low);
400 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
403 struct ext2_icount_el *el;
407 key.dptr = (unsigned char *) &ino;
408 key.dsize = sizeof(ext2_ino_t);
409 data.dptr = (unsigned char *) &count;
410 data.dsize = sizeof(__u32);
412 if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
413 return tdb_error(icount->tdb) +
416 if (tdb_delete(icount->tdb, key))
417 return tdb_error(icount->tdb) +
423 el = get_icount_el(icount, ino, 1);
425 return EXT2_ET_NO_MEMORY;
431 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
434 struct ext2_icount_el *el;
438 key.dptr = (unsigned char *) &ino;
439 key.dsize = sizeof(ext2_ino_t);
441 data = tdb_fetch(icount->tdb, key);
442 if (data.dptr == NULL) {
444 return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
447 *count = *((__u32 *) data.dptr);
451 el = get_icount_el(icount, ino, 0);
461 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
465 const char *bad = "bad icount";
467 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
469 if (icount->count > icount->size) {
470 fprintf(out, "%s: count > size\n", bad);
471 return EXT2_ET_INVALID_ARGUMENT;
473 for (i=1; i < icount->count; i++) {
474 if (icount->list[i-1].ino >= icount->list[i].ino) {
475 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
476 bad, i-1, icount->list[i-1].ino,
477 i, icount->list[i].ino);
478 ret = EXT2_ET_INVALID_ARGUMENT;
484 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
487 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
489 if (!ino || (ino > icount->num_inodes))
490 return EXT2_ET_INVALID_ARGUMENT;
492 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
496 if (icount->multiple &&
497 !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
501 get_inode_count(icount, ino, &val);
502 *ret = icount_16_xlate(val);
506 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
511 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
513 if (!ino || (ino > icount->num_inodes))
514 return EXT2_ET_INVALID_ARGUMENT;
516 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
518 * If the existing count is 1, then we know there is
519 * no entry in the list.
521 if (set_inode_count(icount, ino, 2))
522 return EXT2_ET_NO_MEMORY;
524 ext2fs_unmark_inode_bitmap2(icount->single, ino);
525 } else if (icount->multiple) {
527 * The count is either zero or greater than 1; if the
528 * inode is set in icount->multiple, then there should
529 * be an entry in the list, so we need to fix it.
531 if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
532 get_inode_count(icount, ino, &curr_value);
534 if (set_inode_count(icount, ino, curr_value))
535 return EXT2_ET_NO_MEMORY;
538 * The count was zero; mark the single bitmap
541 ext2fs_mark_inode_bitmap2(icount->single, ino);
548 * The count is either zero or greater than 1; try to
549 * find an entry in the list to determine which.
551 get_inode_count(icount, ino, &curr_value);
553 if (set_inode_count(icount, ino, curr_value))
554 return EXT2_ET_NO_MEMORY;
556 if (icount->multiple)
557 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
559 *ret = icount_16_xlate(curr_value);
563 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
568 if (!ino || (ino > icount->num_inodes))
569 return EXT2_ET_INVALID_ARGUMENT;
571 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
573 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
574 ext2fs_unmark_inode_bitmap2(icount->single, ino);
575 if (icount->multiple)
576 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
578 set_inode_count(icount, ino, 0);
585 if (icount->multiple &&
586 !ext2fs_test_inode_bitmap2(icount->multiple, ino))
587 return EXT2_ET_INVALID_ARGUMENT;
589 get_inode_count(icount, ino, &curr_value);
591 return EXT2_ET_INVALID_ARGUMENT;
593 if (set_inode_count(icount, ino, curr_value))
594 return EXT2_ET_NO_MEMORY;
597 ext2fs_mark_inode_bitmap2(icount->single, ino);
598 if ((curr_value == 0) && icount->multiple)
599 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
602 *ret = icount_16_xlate(curr_value);
606 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
609 if (!ino || (ino > icount->num_inodes))
610 return EXT2_ET_INVALID_ARGUMENT;
612 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
615 ext2fs_mark_inode_bitmap2(icount->single, ino);
616 if (icount->multiple)
617 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
621 ext2fs_unmark_inode_bitmap2(icount->single, ino);
622 if (icount->multiple) {
624 * If the icount->multiple bitmap is enabled,
625 * we can just clear both bitmaps and we're done
627 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
629 set_inode_count(icount, ino, 0);
633 if (set_inode_count(icount, ino, count))
634 return EXT2_ET_NO_MEMORY;
635 ext2fs_unmark_inode_bitmap2(icount->single, ino);
636 if (icount->multiple)
637 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
641 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
643 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
652 ext2_icount_t icount;
657 #define INCREMENT 0x03
658 #define DECREMENT 0x04
660 struct test_program {
667 struct test_program prog[] = {
668 { STORE, 42, 42, 42 },
674 { INCREMENT, 5, 0, 1 },
675 { INCREMENT, 5, 0, 2 },
676 { INCREMENT, 5, 0, 3 },
677 { INCREMENT, 5, 0, 4 },
678 { DECREMENT, 5, 0, 3 },
679 { DECREMENT, 5, 0, 2 },
680 { DECREMENT, 5, 0, 1 },
681 { DECREMENT, 5, 0, 0 },
686 { INCREMENT, 1, 0, 2 },
687 { DECREMENT, 2, 0, 1 },
688 { DECREMENT, 2, 0, 0 },
693 struct test_program extended[] = {
728 * Setup the variables for doing the inode scan test.
730 static void setup(void)
733 struct ext2_super_block param;
735 initialize_ext2_error_table();
737 memset(¶m, 0, sizeof(param));
738 ext2fs_blocks_count_set(¶m, 12000);
740 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m,
741 test_io_manager, &test_fs);
743 com_err("setup", retval,
744 "while initializing filesystem");
747 retval = ext2fs_allocate_tables(test_fs);
749 com_err("setup", retval,
750 "while allocating tables for test filesystem");
755 int run_test(int flags, int size, char *dir, struct test_program *prog)
758 ext2_icount_t icount;
759 struct test_program *pc;
764 retval = ext2fs_create_icount_tdb(test_fs, dir,
767 com_err("run_test", retval,
768 "while creating icount using tdb");
772 retval = ext2fs_create_icount2(test_fs, flags, size, 0,
775 com_err("run_test", retval, "while creating icount");
779 for (pc = prog; pc->cmd != EXIT; pc++) {
782 printf("icount_fetch(%u) = ", pc->ino);
785 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
787 com_err("run_test", retval,
788 "while calling icount_store");
791 printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
794 retval = ext2fs_icount_increment(icount, pc->ino, 0);
796 com_err("run_test", retval,
797 "while calling icount_increment");
800 printf("icount_increment(%u) = ", pc->ino);
803 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
805 com_err("run_test", retval,
806 "while calling icount_decrement");
809 printf("icount_decrement(%u) = ", pc->ino);
812 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
814 com_err("run_test", retval,
815 "while calling icount_fetch");
818 printf("%u (%s)\n", result, (result == pc->expected) ?
820 if (result != pc->expected)
823 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
824 retval = ext2fs_icount_validate(icount, stdout);
826 com_err("run_test", retval, "while calling icount_validate");
829 ext2fs_free_icount(icount);
834 int main(int argc, char **argv)
839 printf("Standard icount run:\n");
840 failed += run_test(0, 0, 0, prog);
841 printf("\nMultiple bitmap test:\n");
842 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, 0, prog);
843 printf("\nResizing icount:\n");
844 failed += run_test(0, 3, 0, extended);
845 printf("\nStandard icount run with tdb:\n");
846 failed += run_test(0, 0, ".", prog);
847 printf("\nMultiple bitmap test with tdb:\n");
848 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, ".", prog);