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
22 * The data storage strategy used by icount relies on the observation
23 * that most inode counts are either zero (for non-allocated inodes),
24 * one (for most files), and only a few that are two or more
25 * (directories and files that are linked to more than one directory).
27 * Also, e2fsck tends to load the icount data sequentially.
29 * So, we use an inode bitmap to indicate which inodes have a count of
30 * one, and then use a sorted list to store the counts for inodes
31 * which are greater than one.
33 * We also use an optional bitmap to indicate which inodes are already
34 * in the sorted list, to speed up the use of this abstraction by
35 * e2fsck's pass 2. Pass 2 increments inode counts as it finds them,
36 * so this extra bitmap avoids searching the sorted list to see if a
37 * particular inode is on the sorted list already.
40 struct ext2_icount_el {
47 ext2fs_inode_bitmap single;
48 ext2fs_inode_bitmap multiple;
51 ext2_ino_t num_inodes;
53 struct ext2_icount_el *list;
56 void ext2fs_free_icount(ext2_icount_t icount)
63 ext2fs_free_mem(&icount->list);
65 ext2fs_free_inode_bitmap(icount->single);
67 ext2fs_free_inode_bitmap(icount->multiple);
68 ext2fs_free_mem(&icount);
71 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
72 ext2_icount_t hint, ext2_icount_t *ret)
80 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
81 if (hint->size > size)
82 size = (size_t) hint->size;
85 retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
88 memset(icount, 0, sizeof(struct ext2_icount));
90 retval = ext2fs_allocate_inode_bitmap(fs, 0,
95 if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
96 retval = ext2fs_allocate_inode_bitmap(fs, 0,
101 icount->multiple = 0;
107 * Figure out how many special case inode counts we will
108 * have. We know we will need one for each directory;
109 * we also need to reserve some extra room for file links
111 retval = ext2fs_get_num_dirs(fs, &icount->size);
114 icount->size += fs->super->s_inodes_count / 50;
117 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
119 printf("Icount allocated %u entries, %d bytes.\n",
120 icount->size, bytes);
122 retval = ext2fs_get_mem(bytes, &icount->list);
125 memset(icount->list, 0, bytes);
127 icount->magic = EXT2_ET_MAGIC_ICOUNT;
130 icount->num_inodes = fs->super->s_inodes_count;
133 * Populate the sorted list with those entries which were
134 * found in the hint icount (since those are ones which will
135 * likely need to be in the sorted list this time around).
138 for (i=0; i < hint->count; i++)
139 icount->list[i].ino = hint->list[i].ino;
140 icount->count = hint->count;
147 ext2fs_free_icount(icount);
151 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
155 return ext2fs_create_icount2(fs, flags, size, 0, ret);
159 * insert_icount_el() --- Insert a new entry into the sorted list at a
160 * specified position.
162 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
163 ext2_ino_t ino, int pos)
165 struct ext2_icount_el *el;
167 ext2_ino_t new_size = 0;
170 if (icount->count >= icount->size) {
172 new_size = icount->list[(unsigned)icount->count-1].ino;
173 new_size = (ext2_ino_t) (icount->count *
174 ((float) icount->num_inodes / new_size));
176 if (new_size < (icount->size + 100))
177 new_size = icount->size + 100;
179 printf("Reallocating icount %u entries...\n", new_size);
181 retval = ext2fs_resize_mem((size_t) icount->size *
182 sizeof(struct ext2_icount_el),
184 sizeof(struct ext2_icount_el),
188 icount->size = new_size;
190 num = (int) icount->count - pos;
192 return 0; /* should never happen */
194 memmove(&icount->list[pos+1], &icount->list[pos],
195 sizeof(struct ext2_icount_el) * num);
198 el = &icount->list[pos];
205 * get_icount_el() --- given an inode number, try to find icount
206 * information in the sorted list. If the create flag is set,
207 * and we can't find an entry, create one in the sorted list.
209 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
210 ext2_ino_t ino, int create)
214 ext2_ino_t lowval, highval;
216 if (!icount || !icount->list)
219 if (create && ((icount->count == 0) ||
220 (ino > icount->list[(unsigned)icount->count-1].ino))) {
221 return insert_icount_el(icount, ino, (unsigned) icount->count);
223 if (icount->count == 0)
226 if (icount->cursor >= icount->count)
228 if (ino == icount->list[icount->cursor].ino)
229 return &icount->list[icount->cursor++];
231 printf("Non-cursor get_icount_el: %u\n", ino);
234 high = (int) icount->count-1;
235 while (low <= high) {
242 /* Interpolate for efficiency */
243 lowval = icount->list[low].ino;
244 highval = icount->list[high].ino;
248 else if (ino > highval)
251 range = ((float) (ino - lowval)) /
258 mid = low + ((int) (range * (high-low)));
261 if (ino == icount->list[mid].ino) {
262 icount->cursor = mid+1;
263 return &icount->list[mid];
265 if (ino < icount->list[mid].ino)
271 * If we need to create a new entry, it should be right at
272 * low (where high will be left at low-1).
275 return insert_icount_el(icount, ino, low);
279 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
283 const char *bad = "bad icount";
285 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
287 if (icount->count > icount->size) {
288 fprintf(out, "%s: count > size\n", bad);
289 return EXT2_ET_INVALID_ARGUMENT;
291 for (i=1; i < icount->count; i++) {
292 if (icount->list[i-1].ino >= icount->list[i].ino) {
293 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
294 bad, i-1, icount->list[i-1].ino,
295 i, icount->list[i].ino);
296 ret = EXT2_ET_INVALID_ARGUMENT;
302 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
304 struct ext2_icount_el *el;
306 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
308 if (!ino || (ino > icount->num_inodes))
309 return EXT2_ET_INVALID_ARGUMENT;
311 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
315 if (icount->multiple &&
316 !ext2fs_test_inode_bitmap(icount->multiple, ino)) {
320 el = get_icount_el(icount, ino, 0);
329 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
332 struct ext2_icount_el *el;
334 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
336 if (!ino || (ino > icount->num_inodes))
337 return EXT2_ET_INVALID_ARGUMENT;
339 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
341 * If the existing count is 1, then we know there is
342 * no entry in the list.
344 el = get_icount_el(icount, ino, 1);
346 return EXT2_ET_NO_MEMORY;
347 ext2fs_unmark_inode_bitmap(icount->single, ino);
349 } else if (icount->multiple) {
351 * The count is either zero or greater than 1; if the
352 * inode is set in icount->multiple, then there should
353 * be an entry in the list, so find it using
356 if (ext2fs_test_inode_bitmap(icount->multiple, ino)) {
357 el = get_icount_el(icount, ino, 1);
359 return EXT2_ET_NO_MEMORY;
363 * The count was zero; mark the single bitmap
367 ext2fs_mark_inode_bitmap(icount->single, ino);
374 * The count is either zero or greater than 1; try to
375 * find an entry in the list to determine which.
377 el = get_icount_el(icount, ino, 0);
379 /* No entry means the count was zero */
382 el = get_icount_el(icount, ino, 1);
384 return EXT2_ET_NO_MEMORY;
387 if (icount->multiple)
388 ext2fs_mark_inode_bitmap(icount->multiple, ino);
394 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
397 struct ext2_icount_el *el;
399 if (!ino || (ino > icount->num_inodes))
400 return EXT2_ET_INVALID_ARGUMENT;
402 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
404 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
405 ext2fs_unmark_inode_bitmap(icount->single, ino);
406 if (icount->multiple)
407 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
409 el = get_icount_el(icount, ino, 0);
418 if (icount->multiple &&
419 !ext2fs_test_inode_bitmap(icount->multiple, ino))
420 return EXT2_ET_INVALID_ARGUMENT;
422 el = get_icount_el(icount, ino, 0);
423 if (!el || el->count == 0)
424 return EXT2_ET_INVALID_ARGUMENT;
428 ext2fs_mark_inode_bitmap(icount->single, ino);
429 if ((el->count == 0) && icount->multiple)
430 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
437 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
440 struct ext2_icount_el *el;
442 if (!ino || (ino > icount->num_inodes))
443 return EXT2_ET_INVALID_ARGUMENT;
445 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
448 ext2fs_mark_inode_bitmap(icount->single, ino);
449 if (icount->multiple)
450 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
454 ext2fs_unmark_inode_bitmap(icount->single, ino);
455 if (icount->multiple) {
457 * If the icount->multiple bitmap is enabled,
458 * we can just clear both bitmaps and we're done
460 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
462 el = get_icount_el(icount, ino, 0);
470 * Get the icount element
472 el = get_icount_el(icount, ino, 1);
474 return EXT2_ET_NO_MEMORY;
476 ext2fs_unmark_inode_bitmap(icount->single, ino);
477 if (icount->multiple)
478 ext2fs_mark_inode_bitmap(icount->multiple, ino);
482 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
484 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
493 ext2_icount_t icount;
498 #define INCREMENT 0x03
499 #define DECREMENT 0x04
501 struct test_program {
508 struct test_program prog[] = {
509 { STORE, 42, 42, 42 },
515 { INCREMENT, 5, 0, 1 },
516 { INCREMENT, 5, 0, 2 },
517 { INCREMENT, 5, 0, 3 },
518 { INCREMENT, 5, 0, 4 },
519 { DECREMENT, 5, 0, 3 },
520 { DECREMENT, 5, 0, 2 },
521 { DECREMENT, 5, 0, 1 },
522 { DECREMENT, 5, 0, 0 },
527 { INCREMENT, 1, 0, 2 },
528 { DECREMENT, 2, 0, 1 },
529 { DECREMENT, 2, 0, 0 },
534 struct test_program extended[] = {
569 * Setup the variables for doing the inode scan test.
571 static void setup(void)
575 struct ext2_super_block param;
577 initialize_ext2_error_table();
579 memset(¶m, 0, sizeof(param));
580 param.s_blocks_count = 12000;
582 retval = ext2fs_initialize("test fs", 0, ¶m,
583 test_io_manager, &test_fs);
585 com_err("setup", retval,
586 "while initializing filesystem");
589 retval = ext2fs_allocate_tables(test_fs);
591 com_err("setup", retval,
592 "while allocating tables for test filesystem");
597 int run_test(int flags, int size, struct test_program *prog)
600 ext2_icount_t icount;
601 struct test_program *pc;
605 retval = ext2fs_create_icount2(test_fs, flags, size, 0, &icount);
607 com_err("run_test", retval,
608 "While creating icount");
611 for (pc = prog; pc->cmd != EXIT; pc++) {
614 printf("icount_fetch(%u) = ", pc->ino);
617 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
619 com_err("run_test", retval,
620 "while calling icount_store");
623 printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
626 retval = ext2fs_icount_increment(icount, pc->ino, 0);
628 com_err("run_test", retval,
629 "while calling icount_increment");
632 printf("icount_increment(%u) = ", pc->ino);
635 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
637 com_err("run_test", retval,
638 "while calling icount_decrement");
641 printf("icount_decrement(%u) = ", pc->ino);
644 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
646 com_err("run_test", retval,
647 "while calling icount_fetch");
650 printf("%u (%s)\n", result, (result == pc->expected) ?
652 if (result != pc->expected)
655 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
656 retval = ext2fs_icount_validate(icount, stdout);
658 com_err("run_test", retval, "while calling icount_validate");
661 ext2fs_free_icount(icount);
666 int main(int argc, char **argv)
671 printf("Standard icount run:\n");
672 failed += run_test(0, 0, prog);
673 printf("\nMultiple bitmap test:\n");
674 failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, prog);
675 printf("\nResizing icount:\n");
676 failed += run_test(0, 3, extended);