Lines Matching defs: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, "icount", &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;
185 retval = alloc_icount(fs, flags, &icount);
193 sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
208 icount->tdb_fn = fn;
209 icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
211 if (icount->tdb) {
213 *ret = icount;
221 ext2fs_free_icount(icount);
228 ext2_icount_t icount;
239 retval = alloc_icount(fs, flags, &icount);
244 icount->size = size;
251 retval = ext2fs_get_num_dirs(fs, &icount->size);
254 icount->size += fs->super->s_inodes_count / 50;
257 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
259 printf("Icount allocated %u entries, %d bytes.\n",
260 icount->size, bytes);
262 retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
263 &icount->list);
266 memset(icount->list, 0, bytes);
268 icount->count = 0;
269 icount->cursor = 0;
273 * found in the hint icount (since those are ones which will
278 icount->list[i].ino = hint->list[i].ino;
279 icount->count = hint->count;
282 *ret = icount;
286 ext2fs_free_icount(icount);
301 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
309 if (icount->last_lookup && icount->last_lookup->ino == ino)
310 return icount->last_lookup;
312 if (icount->count >= icount->size) {
313 if (icount->count) {
314 new_size = icount->list[(unsigned)icount->count-1].ino;
315 new_size = (ext2_ino_t) (icount->count *
316 ((float) icount->num_inodes / new_size));
318 if (new_size < (icount->size + 100))
319 new_size = icount->size + 100;
321 printf("Reallocating icount %u entries...\n", new_size);
323 retval = ext2fs_resize_mem((size_t) icount->size *
327 &icount->list);
330 icount->size = new_size;
332 num = (int) icount->count - pos;
336 memmove(&icount->list[pos+1], &icount->list[pos],
339 icount->count++;
340 el = &icount->list[pos];
343 icount->last_lookup = el;
348 * get_icount_el() --- given an inode number, try to find icount
352 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
357 if (!icount || !icount->list)
360 if (create && ((icount->count == 0) ||
361 (ino > icount->list[(unsigned)icount->count-1].ino))) {
362 return insert_icount_el(icount, ino, (unsigned) icount->count);
364 if (icount->count == 0)
367 if (icount->cursor >= icount->count)
368 icount->cursor = 0;
369 if (ino == icount->list[icount->cursor].ino)
370 return &icount->list[icount->cursor++];
375 high = (int) icount->count-1;
378 if (ino == icount->list[mid].ino) {
379 icount->cursor = mid+1;
380 return &icount->list[mid];
382 if (ino < icount->list[mid].ino)
392 return insert_icount_el(icount, ino, low);
396 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
402 if (icount->tdb) {
408 if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
409 return tdb_error(icount->tdb) +
412 if (tdb_delete(icount->tdb, key))
413 return tdb_error(icount->tdb) +
419 el = get_icount_el(icount, ino, 1);
427 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
433 if (icount->tdb) {
437 data = tdb_fetch(icount->tdb, key);
440 return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
447 el = get_icount_el(icount, ino, 0);
457 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
461 const char *bad = "bad icount";
463 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
465 if (icount->count > icount->size) {
469 for (i=1; i < icount->count; i++) {
470 if (icount->list[i-1].ino >= icount->list[i].ino) {
472 bad, i-1, icount->list[i-1].ino,
473 i, icount->list[i].ino);
480 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
483 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
485 if (!ino || (ino > icount->num_inodes))
488 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
492 if (icount->multiple &&
493 !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
497 get_inode_count(icount, ino, &val);
502 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
507 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
509 if (!ino || (ino > icount->num_inodes))
512 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
517 if (set_inode_count(icount, ino, 2))
520 ext2fs_unmark_inode_bitmap2(icount->single, ino);
521 } else if (icount->multiple) {
524 * inode is set in icount->multiple, then there should
527 if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
528 get_inode_count(icount, ino, &curr_value);
530 if (set_inode_count(icount, ino, curr_value))
537 ext2fs_mark_inode_bitmap2(icount->single, ino);
547 get_inode_count(icount, ino, &curr_value);
549 if (set_inode_count(icount, ino, curr_value))
552 if (icount->multiple)
553 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
559 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
564 if (!ino || (ino > icount->num_inodes))
567 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
569 if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
570 ext2fs_unmark_inode_bitmap2(icount->single, ino);
571 if (icount->multiple)
572 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
574 set_inode_count(icount, ino, 0);
581 if (icount->multiple &&
582 !ext2fs_test_inode_bitmap2(icount->multiple, ino))
585 get_inode_count(icount, ino, &curr_value);
589 if (set_inode_count(icount, ino, curr_value))
593 ext2fs_mark_inode_bitmap2(icount->single, ino);
594 if ((curr_value == 0) && icount->multiple)
595 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
602 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
605 if (!ino || (ino > icount->num_inodes))
608 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
611 ext2fs_mark_inode_bitmap2(icount->single, ino);
612 if (icount->multiple)
613 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
617 ext2fs_unmark_inode_bitmap2(icount->single, ino);
618 if (icount->multiple) {
620 * If the icount->multiple bitmap is enabled,
623 ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
625 set_inode_count(icount, ino, 0);
629 if (set_inode_count(icount, ino, count))
631 ext2fs_unmark_inode_bitmap2(icount->single, ino);
632 if (icount->multiple)
633 ext2fs_mark_inode_bitmap2(icount->multiple, ino);
637 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
639 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
642 return icount->size;
648 ext2_icount_t icount;
754 ext2_icount_t icount;
761 flags, &icount);
764 "while creating icount using tdb");
769 &icount);
771 com_err("run_test", retval, "while creating icount");
781 retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
790 retval = ext2fs_icount_increment(icount, pc->ino, 0);
799 retval = ext2fs_icount_decrement(icount, pc->ino, 0);
808 retval = ext2fs_icount_fetch(icount, pc->ino, &result);
819 printf("icount size is %u\n", ext2fs_get_icount_size(icount));
820 retval = ext2fs_icount_validate(icount, stdout);
825 ext2fs_free_icount(icount);
835 printf("Standard icount run:\n");
839 printf("\nResizing icount:\n");
841 printf("\nStandard icount run with tdb:\n");