Lines Matching refs:icount
2 * icount.c --- an efficient inode count abstraction
26 * The data storage strategy used by icount relies on the observation
31 * Also, e2fsck tends to load the icount data sequentially.
72 void ext2fs_free_icount(ext2_icount_t icount)
74 if (!icount)
77 icount->magic = 0;
78 if (icount->list)
79 ext2fs_free_mem(&icount->list);
80 if (icount->single)
81 ext2fs_free_inode_bitmap(icount->single);
82 if (icount->multiple)
83 ext2fs_free_inode_bitmap(icount->multiple);
84 if (icount->tdb)
85 tdb_close(icount->tdb);
86 if (icount->tdb_fn) {
87 unlink(icount->tdb_fn);
88 free(icount->tdb_fn);
91 ext2fs_free_mem(&icount);
96 ext2_icount_t icount;
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);
112 &icount->multiple);
116 icount->multiple = 0;
118 icount->magic = EXT2_ET_MAGIC_ICOUNT;
119 icount->num_inodes = fs->super->s_inodes_count;
121 *ret = icount;
125 ext2fs_free_icount(icount);
179 ext2_icount_t icount;
184 retval = alloc_icount(fs, flags, &icount);
192 sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
195 icount->tdb_fn = fn;
196 icount->tdb = tdb_open(fn, 0, TDB_CLEAR_IF_FIRST,
198 if (icount->tdb) {
200 *ret = icount;
208 ext2fs_free_icount(icount);
215 ext2_icount_t icount;
226 retval = alloc_icount(fs, flags, &icount);
231 icount->size = size;
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),
250 &icount->list);
253 memset(icount->list, 0, bytes);
255 icount->count = 0;
256 icount->cursor = 0;
260 * found in the hint icount (since those are ones which will
265 icount->list[i].ino = hint->list[i].ino;
266 icount->count = hint->count;
269 *ret = icount;
273 ext2fs_free_icount(icount);
288 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
296 if (icount->last_lookup && icount->last_lookup->ino == ino)
297 return icount->last_lookup;
299 if (icount->count >= icount->size) {
300 if (icount->count) {
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 *
314 &icount->list);
317 icount->size = new_size;
319 num = (int) icount->count - pos;
323 memmove(&icount->list[pos+1], &icount->list[pos],
326 icount->count++;
327 el = &icount->list[pos];
330 icount->last_lookup = el;
335 * get_icount_el() --- given an inode number, try to find icount
339 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
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)
357 icount->cursor = 0;
358 if (ino == icount->list[icount->cursor].ino)
359 return &icount->list[icount->cursor++];
364 high = (int) icount->count-1;
373 lowval = icount->list[low].ino;
374 highval = icount->list[high].ino;
391 if (ino == icount->list[mid].ino) {
392 icount->cursor = mid+1;
393 return &icount->list[mid];
395 if (ino < icount->list[mid].ino)
405 return insert_icount_el(icount, ino, low);
409 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
415 if (icount->tdb) {
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);
440 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
446 if (icount->tdb) {
450 data = tdb_fetch(icount->tdb, key);
453 return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
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) {
482 for (i=1; i < icount->count; i++) {
483 if (icount->list[i-1].ino >= icount->list[i].ino) {
485 bad, i-1, icount->list[i-1].ino,
486 i, icount->list[i].ino);
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))
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);
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))
525 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
530 if (set_inode_count(icount, ino, 2))
533 ext2fs_unmark_inode_bitmap(icount->single, ino);
534 } else if (icount->multiple) {
537 * inode is set in icount->multiple, then there should
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))
550 ext2fs_mark_inode_bitmap(icount->single, ino);
560 get_inode_count(icount, ino, &curr_value);
562 if (set_inode_count(icount, ino, curr_value))
565 if (icount->multiple)
566 ext2fs_mark_inode_bitmap(icount->multiple, ino);
572 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
577 if (!ino || (ino > icount->num_inodes))
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))
598 get_inode_count(icount, ino, &curr_value);
602 if (set_inode_count(icount, ino, curr_value))
606 ext2fs_mark_inode_bitmap(icount->single, ino);
607 if ((curr_value == 0) && icount->multiple)
608 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
615 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
618 if (!ino || (ino > icount->num_inodes))
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,
636 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
638 set_inode_count(icount, ino, 0);
642 if (set_inode_count(icount, ino, count))
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)
655 return icount->size;
661 ext2_icount_t icount;
767 ext2_icount_t icount;
774 flags, &icount);
777 "while creating icount using tdb");
782 &icount);
784 com_err("run_test", retval, "while creating icount");
794 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
803 retval = ext2fs_icount_increment(icount, pc->ino, 0);
812 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
821 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
832 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
833 retval = ext2fs_icount_validate(icount, stdout);
838 ext2fs_free_icount(icount);
848 printf("Standard icount run:\n");
852 printf("\nResizing icount:\n");
854 printf("\nStandard icount run with tdb:\n");