Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * icount.c --- an efficient inode count abstraction
      3  *
      4  * Copyright (C) 1997 Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Library
      8  * General Public License, version 2.
      9  * %End-Header%
     10  */
     11 
     12 #include "config.h"
     13 #if HAVE_UNISTD_H
     14 #include <unistd.h>
     15 #endif
     16 #include <string.h>
     17 #include <stdio.h>
     18 #include <sys/stat.h>
     19 #include <fcntl.h>
     20 #include <errno.h>
     21 
     22 #include "ext2_fs.h"
     23 #include "ext2fs.h"
     24 #include "tdb.h"
     25 
     26 /*
     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).
     31  *
     32  * Also, e2fsck tends to load the icount data sequentially.
     33  *
     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.
     37  *
     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.
     43  */
     44 
     45 struct ext2_icount_el {
     46 	ext2_ino_t	ino;
     47 	__u32		count;
     48 };
     49 
     50 struct ext2_icount {
     51 	errcode_t		magic;
     52 	ext2fs_inode_bitmap	single;
     53 	ext2fs_inode_bitmap	multiple;
     54 	ext2_ino_t		count;
     55 	ext2_ino_t		size;
     56 	ext2_ino_t		num_inodes;
     57 	ext2_ino_t		cursor;
     58 	struct ext2_icount_el	*list;
     59 	struct ext2_icount_el	*last_lookup;
     60 #ifdef CONFIG_TDB
     61 	char			*tdb_fn;
     62 	TDB_CONTEXT		*tdb;
     63 #endif
     64 };
     65 
     66 /*
     67  * We now use a 32-bit counter field because it doesn't cost us
     68  * anything extra for the in-memory data structure, due to alignment
     69  * padding.  But there's no point changing the interface if most of
     70  * the time we only care if the number is bigger than 65,000 or not.
     71  * So use the following translation function to return a 16-bit count.
     72  */
     73 #define icount_16_xlate(x) (((x) > 65500) ? 65500 : (x))
     74 
     75 void ext2fs_free_icount(ext2_icount_t icount)
     76 {
     77 	if (!icount)
     78 		return;
     79 
     80 	icount->magic = 0;
     81 	if (icount->list)
     82 		ext2fs_free_mem(&icount->list);
     83 	if (icount->single)
     84 		ext2fs_free_inode_bitmap(icount->single);
     85 	if (icount->multiple)
     86 		ext2fs_free_inode_bitmap(icount->multiple);
     87 #ifdef CONFIG_TDB
     88 	if (icount->tdb)
     89 		tdb_close(icount->tdb);
     90 	if (icount->tdb_fn) {
     91 		unlink(icount->tdb_fn);
     92 		free(icount->tdb_fn);
     93 	}
     94 #endif
     95 
     96 	ext2fs_free_mem(&icount);
     97 }
     98 
     99 static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
    100 {
    101 	ext2_icount_t	icount;
    102 	errcode_t	retval;
    103 
    104 	*ret = 0;
    105 
    106 	retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
    107 	if (retval)
    108 		return retval;
    109 	memset(icount, 0, sizeof(struct ext2_icount));
    110 
    111 	retval = ext2fs_allocate_inode_bitmap(fs, "icount", &icount->single);
    112 	if (retval)
    113 		goto errout;
    114 
    115 	if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
    116 		retval = ext2fs_allocate_inode_bitmap(fs, "icount_inc",
    117 						      &icount->multiple);
    118 		if (retval)
    119 			goto errout;
    120 	} else
    121 		icount->multiple = 0;
    122 
    123 	icount->magic = EXT2_ET_MAGIC_ICOUNT;
    124 	icount->num_inodes = fs->super->s_inodes_count;
    125 
    126 	*ret = icount;
    127 	return 0;
    128 
    129 errout:
    130 	ext2fs_free_icount(icount);
    131 	return(retval);
    132 }
    133 
    134 #ifdef CONFIG_TDB
    135 struct uuid {
    136 	__u32	time_low;
    137 	__u16	time_mid;
    138 	__u16	time_hi_and_version;
    139 	__u16	clock_seq;
    140 	__u8	node[6];
    141 };
    142 
    143 static void unpack_uuid(void *in, struct uuid *uu)
    144 {
    145 	__u8	*ptr = in;
    146 	__u32	tmp;
    147 
    148 	tmp = *ptr++;
    149 	tmp = (tmp << 8) | *ptr++;
    150 	tmp = (tmp << 8) | *ptr++;
    151 	tmp = (tmp << 8) | *ptr++;
    152 	uu->time_low = tmp;
    153 
    154 	tmp = *ptr++;
    155 	tmp = (tmp << 8) | *ptr++;
    156 	uu->time_mid = tmp;
    157 
    158 	tmp = *ptr++;
    159 	tmp = (tmp << 8) | *ptr++;
    160 	uu->time_hi_and_version = tmp;
    161 
    162 	tmp = *ptr++;
    163 	tmp = (tmp << 8) | *ptr++;
    164 	uu->clock_seq = tmp;
    165 
    166 	memcpy(uu->node, ptr, 6);
    167 }
    168 
    169 static void uuid_unparse(void *uu, char *out)
    170 {
    171 	struct uuid uuid;
    172 
    173 	unpack_uuid(uu, &uuid);
    174 	sprintf(out,
    175 		"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
    176 		uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
    177 		uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
    178 		uuid.node[0], uuid.node[1], uuid.node[2],
    179 		uuid.node[3], uuid.node[4], uuid.node[5]);
    180 }
    181 #endif
    182 
    183 errcode_t ext2fs_create_icount_tdb(ext2_filsys fs EXT2FS_NO_TDB_UNUSED,
    184 				   char *tdb_dir EXT2FS_NO_TDB_UNUSED,
    185 				   int flags EXT2FS_NO_TDB_UNUSED,
    186 				   ext2_icount_t *ret EXT2FS_NO_TDB_UNUSED)
    187 {
    188 #ifdef CONFIG_TDB
    189 	ext2_icount_t	icount;
    190 	errcode_t	retval;
    191 	char 		*fn, uuid[40];
    192 	ext2_ino_t	num_inodes;
    193 	mode_t		save_umask;
    194 	int		fd;
    195 
    196 	retval = alloc_icount(fs, flags,  &icount);
    197 	if (retval)
    198 		return retval;
    199 
    200 	retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn);
    201 	if (retval)
    202 		goto errout;
    203 	uuid_unparse(fs->super->s_uuid, uuid);
    204 	sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
    205 	save_umask = umask(077);
    206 	fd = mkstemp(fn);
    207 	if (fd < 0) {
    208 		retval = errno;
    209 		ext2fs_free_mem(&fn);
    210 		goto errout;
    211 	}
    212 	icount->tdb_fn = fn;
    213 	umask(save_umask);
    214 	/*
    215 	 * This is an overestimate of the size that we will need; the
    216 	 * ideal value is the number of used inodes with a count
    217 	 * greater than 1.  OTOH the times when we really need this is
    218 	 * with the backup programs that use lots of hard links, in
    219 	 * which case the number of inodes in use approaches the ideal
    220 	 * value.
    221 	 */
    222 	num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
    223 
    224 	icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
    225 			       O_RDWR | O_CREAT | O_TRUNC, 0600);
    226 	close(fd);
    227 	if (icount->tdb == NULL) {
    228 		retval = errno;
    229 		goto errout;
    230 	}
    231 	*ret = icount;
    232 	return 0;
    233 errout:
    234 	ext2fs_free_icount(icount);
    235 	return(retval);
    236 #else
    237 	return EXT2_ET_UNIMPLEMENTED;
    238 #endif
    239 }
    240 
    241 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
    242 				ext2_icount_t hint, ext2_icount_t *ret)
    243 {
    244 	ext2_icount_t	icount;
    245 	errcode_t	retval;
    246 	size_t		bytes;
    247 	ext2_ino_t	i;
    248 
    249 	if (hint) {
    250 		EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
    251 		if (hint->size > size)
    252 			size = (size_t) hint->size;
    253 	}
    254 
    255 	retval = alloc_icount(fs, flags, &icount);
    256 	if (retval)
    257 		return retval;
    258 
    259 	if (size) {
    260 		icount->size = size;
    261 	} else {
    262 		/*
    263 		 * Figure out how many special case inode counts we will
    264 		 * have.  We know we will need one for each directory;
    265 		 * we also need to reserve some extra room for file links
    266 		 */
    267 		retval = ext2fs_get_num_dirs(fs, &icount->size);
    268 		if (retval)
    269 			goto errout;
    270 		icount->size += fs->super->s_inodes_count / 50;
    271 	}
    272 
    273 	bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
    274 #if 0
    275 	printf("Icount allocated %u entries, %d bytes.\n",
    276 	       icount->size, bytes);
    277 #endif
    278 	retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
    279 			 &icount->list);
    280 	if (retval)
    281 		goto errout;
    282 	memset(icount->list, 0, bytes);
    283 
    284 	icount->count = 0;
    285 	icount->cursor = 0;
    286 
    287 	/*
    288 	 * Populate the sorted list with those entries which were
    289 	 * found in the hint icount (since those are ones which will
    290 	 * likely need to be in the sorted list this time around).
    291 	 */
    292 	if (hint) {
    293 		for (i=0; i < hint->count; i++)
    294 			icount->list[i].ino = hint->list[i].ino;
    295 		icount->count = hint->count;
    296 	}
    297 
    298 	*ret = icount;
    299 	return 0;
    300 
    301 errout:
    302 	ext2fs_free_icount(icount);
    303 	return(retval);
    304 }
    305 
    306 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
    307 			       unsigned int size,
    308 			       ext2_icount_t *ret)
    309 {
    310 	return ext2fs_create_icount2(fs, flags, size, 0, ret);
    311 }
    312 
    313 /*
    314  * insert_icount_el() --- Insert a new entry into the sorted list at a
    315  * 	specified position.
    316  */
    317 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
    318 					    ext2_ino_t ino, int pos)
    319 {
    320 	struct ext2_icount_el 	*el;
    321 	errcode_t		retval;
    322 	ext2_ino_t		new_size = 0;
    323 	int			num;
    324 
    325 	if (icount->last_lookup && icount->last_lookup->ino == ino)
    326 		return icount->last_lookup;
    327 
    328 	if (icount->count >= icount->size) {
    329 		if (icount->count) {
    330 			new_size = icount->list[(unsigned)icount->count-1].ino;
    331 			new_size = (ext2_ino_t) (icount->count *
    332 				((float) icount->num_inodes / new_size));
    333 		}
    334 		if (new_size < (icount->size + 100))
    335 			new_size = icount->size + 100;
    336 #if 0
    337 		printf("Reallocating icount %u entries...\n", new_size);
    338 #endif
    339 		retval = ext2fs_resize_mem((size_t) icount->size *
    340 					   sizeof(struct ext2_icount_el),
    341 					   (size_t) new_size *
    342 					   sizeof(struct ext2_icount_el),
    343 					   &icount->list);
    344 		if (retval)
    345 			return 0;
    346 		icount->size = new_size;
    347 	}
    348 	num = (int) icount->count - pos;
    349 	if (num < 0)
    350 		return 0;	/* should never happen */
    351 	if (num) {
    352 		memmove(&icount->list[pos+1], &icount->list[pos],
    353 			sizeof(struct ext2_icount_el) * num);
    354 	}
    355 	icount->count++;
    356 	el = &icount->list[pos];
    357 	el->count = 0;
    358 	el->ino = ino;
    359 	icount->last_lookup = el;
    360 	return el;
    361 }
    362 
    363 /*
    364  * get_icount_el() --- given an inode number, try to find icount
    365  * 	information in the sorted list.  If the create flag is set,
    366  * 	and we can't find an entry, create one in the sorted list.
    367  */
    368 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
    369 					    ext2_ino_t ino, int create)
    370 {
    371 	int	low, high, mid;
    372 
    373 	if (!icount || !icount->list)
    374 		return 0;
    375 
    376 	if (create && ((icount->count == 0) ||
    377 		       (ino > icount->list[(unsigned)icount->count-1].ino))) {
    378 		return insert_icount_el(icount, ino, (unsigned) icount->count);
    379 	}
    380 	if (icount->count == 0)
    381 		return 0;
    382 
    383 	if (icount->cursor >= icount->count)
    384 		icount->cursor = 0;
    385 	if (ino == icount->list[icount->cursor].ino)
    386 		return &icount->list[icount->cursor++];
    387 #if 0
    388 	printf("Non-cursor get_icount_el: %u\n", ino);
    389 #endif
    390 	low = 0;
    391 	high = (int) icount->count-1;
    392 	while (low <= high) {
    393 		mid = ((unsigned)low + (unsigned)high) >> 1;
    394 		if (ino == icount->list[mid].ino) {
    395 			icount->cursor = mid+1;
    396 			return &icount->list[mid];
    397 		}
    398 		if (ino < icount->list[mid].ino)
    399 			high = mid-1;
    400 		else
    401 			low = mid+1;
    402 	}
    403 	/*
    404 	 * If we need to create a new entry, it should be right at
    405 	 * low (where high will be left at low-1).
    406 	 */
    407 	if (create)
    408 		return insert_icount_el(icount, ino, low);
    409 	return 0;
    410 }
    411 
    412 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
    413 				 __u32 count)
    414 {
    415 	struct ext2_icount_el 	*el;
    416 #ifdef CONFIG_TDB
    417 	TDB_DATA key, data;
    418 
    419 	if (icount->tdb) {
    420 		key.dptr = (unsigned char *) &ino;
    421 		key.dsize = sizeof(ext2_ino_t);
    422 		data.dptr = (unsigned char *) &count;
    423 		data.dsize = sizeof(__u32);
    424 		if (count) {
    425 			if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
    426 				return tdb_error(icount->tdb) +
    427 					EXT2_ET_TDB_SUCCESS;
    428 		} else {
    429 			if (tdb_delete(icount->tdb, key))
    430 				return tdb_error(icount->tdb) +
    431 					EXT2_ET_TDB_SUCCESS;
    432 		}
    433 		return 0;
    434 	}
    435 #endif
    436 	el = get_icount_el(icount, ino, 1);
    437 	if (!el)
    438 		return EXT2_ET_NO_MEMORY;
    439 
    440 	el->count = count;
    441 	return 0;
    442 }
    443 
    444 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
    445 				 __u32 *count)
    446 {
    447 	struct ext2_icount_el 	*el;
    448 #ifdef CONFIG_TDB
    449 	TDB_DATA key, data;
    450 
    451 	if (icount->tdb) {
    452 		key.dptr = (unsigned char *) &ino;
    453 		key.dsize = sizeof(ext2_ino_t);
    454 
    455 		data = tdb_fetch(icount->tdb, key);
    456 		if (data.dptr == NULL) {
    457 			*count = 0;
    458 			return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
    459 		}
    460 
    461 		*count = *((__u32 *) data.dptr);
    462 		free(data.dptr);
    463 		return 0;
    464 	}
    465 #endif
    466 	el = get_icount_el(icount, ino, 0);
    467 	if (!el) {
    468 		*count = 0;
    469 		return ENOENT;
    470 	}
    471 
    472 	*count = el->count;
    473 	return 0;
    474 }
    475 
    476 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
    477 {
    478 	errcode_t	ret = 0;
    479 	unsigned int	i;
    480 	const char *bad = "bad icount";
    481 
    482 	EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
    483 
    484 	if (icount->count > icount->size) {
    485 		fprintf(out, "%s: count > size\n", bad);
    486 		return EXT2_ET_INVALID_ARGUMENT;
    487 	}
    488 	for (i=1; i < icount->count; i++) {
    489 		if (icount->list[i-1].ino >= icount->list[i].ino) {
    490 			fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
    491 				bad, i-1, icount->list[i-1].ino,
    492 				i, icount->list[i].ino);
    493 			ret = EXT2_ET_INVALID_ARGUMENT;
    494 		}
    495 	}
    496 	return ret;
    497 }
    498 
    499 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
    500 {
    501 	__u32	val;
    502 	EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
    503 
    504 	if (!ino || (ino > icount->num_inodes))
    505 		return EXT2_ET_INVALID_ARGUMENT;
    506 
    507 	if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
    508 		*ret = 1;
    509 		return 0;
    510 	}
    511 	if (icount->multiple &&
    512 	    !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
    513 		*ret = 0;
    514 		return 0;
    515 	}
    516 	get_inode_count(icount, ino, &val);
    517 	*ret = icount_16_xlate(val);
    518 	return 0;
    519 }
    520 
    521 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
    522 				  __u16 *ret)
    523 {
    524 	__u32			curr_value;
    525 
    526 	EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
    527 
    528 	if (!ino || (ino > icount->num_inodes))
    529 		return EXT2_ET_INVALID_ARGUMENT;
    530 
    531 	if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
    532 		/*
    533 		 * If the existing count is 1, then we know there is
    534 		 * no entry in the list.
    535 		 */
    536 		if (set_inode_count(icount, ino, 2))
    537 			return EXT2_ET_NO_MEMORY;
    538 		curr_value = 2;
    539 		ext2fs_unmark_inode_bitmap2(icount->single, ino);
    540 	} else if (icount->multiple) {
    541 		/*
    542 		 * The count is either zero or greater than 1; if the
    543 		 * inode is set in icount->multiple, then there should
    544 		 * be an entry in the list, so we need to fix it.
    545 		 */
    546 		if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
    547 			get_inode_count(icount, ino, &curr_value);
    548 			curr_value++;
    549 			if (set_inode_count(icount, ino, curr_value))
    550 				return EXT2_ET_NO_MEMORY;
    551 		} else {
    552 			/*
    553 			 * The count was zero; mark the single bitmap
    554 			 * and return.
    555 			 */
    556 			ext2fs_mark_inode_bitmap2(icount->single, ino);
    557 			if (ret)
    558 				*ret = 1;
    559 			return 0;
    560 		}
    561 	} else {
    562 		/*
    563 		 * The count is either zero or greater than 1; try to
    564 		 * find an entry in the list to determine which.
    565 		 */
    566 		get_inode_count(icount, ino, &curr_value);
    567 		curr_value++;
    568 		if (set_inode_count(icount, ino, curr_value))
    569 			return EXT2_ET_NO_MEMORY;
    570 	}
    571 	if (icount->multiple)
    572 		ext2fs_mark_inode_bitmap2(icount->multiple, ino);
    573 	if (ret)
    574 		*ret = icount_16_xlate(curr_value);
    575 	return 0;
    576 }
    577 
    578 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
    579 				  __u16 *ret)
    580 {
    581 	__u32			curr_value;
    582 
    583 	if (!ino || (ino > icount->num_inodes))
    584 		return EXT2_ET_INVALID_ARGUMENT;
    585 
    586 	EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
    587 
    588 	if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
    589 		ext2fs_unmark_inode_bitmap2(icount->single, ino);
    590 		if (icount->multiple)
    591 			ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
    592 		else {
    593 			set_inode_count(icount, ino, 0);
    594 		}
    595 		if (ret)
    596 			*ret = 0;
    597 		return 0;
    598 	}
    599 
    600 	if (icount->multiple &&
    601 	    !ext2fs_test_inode_bitmap2(icount->multiple, ino))
    602 		return EXT2_ET_INVALID_ARGUMENT;
    603 
    604 	get_inode_count(icount, ino, &curr_value);
    605 	if (!curr_value)
    606 		return EXT2_ET_INVALID_ARGUMENT;
    607 	curr_value--;
    608 	if (set_inode_count(icount, ino, curr_value))
    609 		return EXT2_ET_NO_MEMORY;
    610 
    611 	if (curr_value == 1)
    612 		ext2fs_mark_inode_bitmap2(icount->single, ino);
    613 	if ((curr_value == 0) && icount->multiple)
    614 		ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
    615 
    616 	if (ret)
    617 		*ret = icount_16_xlate(curr_value);
    618 	return 0;
    619 }
    620 
    621 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
    622 			      __u16 count)
    623 {
    624 	if (!ino || (ino > icount->num_inodes))
    625 		return EXT2_ET_INVALID_ARGUMENT;
    626 
    627 	EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
    628 
    629 	if (count == 1) {
    630 		ext2fs_mark_inode_bitmap2(icount->single, ino);
    631 		if (icount->multiple)
    632 			ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
    633 		return 0;
    634 	}
    635 	if (count == 0) {
    636 		ext2fs_unmark_inode_bitmap2(icount->single, ino);
    637 		if (icount->multiple) {
    638 			/*
    639 			 * If the icount->multiple bitmap is enabled,
    640 			 * we can just clear both bitmaps and we're done
    641 			 */
    642 			ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
    643 		} else
    644 			set_inode_count(icount, ino, 0);
    645 		return 0;
    646 	}
    647 
    648 	if (set_inode_count(icount, ino, count))
    649 		return EXT2_ET_NO_MEMORY;
    650 	ext2fs_unmark_inode_bitmap2(icount->single, ino);
    651 	if (icount->multiple)
    652 		ext2fs_mark_inode_bitmap2(icount->multiple, ino);
    653 	return 0;
    654 }
    655 
    656 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
    657 {
    658 	if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)
    659 		return 0;
    660 
    661 	return icount->size;
    662 }
    663 
    664 #ifdef DEBUG
    665 
    666 ext2_filsys	test_fs;
    667 ext2_icount_t	icount;
    668 
    669 #define EXIT		0x00
    670 #define FETCH		0x01
    671 #define STORE		0x02
    672 #define INCREMENT	0x03
    673 #define DECREMENT	0x04
    674 
    675 struct test_program {
    676 	int		cmd;
    677 	ext2_ino_t	ino;
    678 	__u16		arg;
    679 	__u16		expected;
    680 };
    681 
    682 struct test_program prog[] = {
    683 	{ STORE, 42, 42, 42 },
    684 	{ STORE, 1,  1, 1 },
    685 	{ STORE, 2,  2, 2 },
    686 	{ STORE, 3,  3, 3 },
    687 	{ STORE, 10, 1, 1 },
    688 	{ STORE, 42, 0, 0 },
    689 	{ INCREMENT, 5, 0, 1 },
    690 	{ INCREMENT, 5, 0, 2 },
    691 	{ INCREMENT, 5, 0, 3 },
    692 	{ INCREMENT, 5, 0, 4 },
    693 	{ DECREMENT, 5, 0, 3 },
    694 	{ DECREMENT, 5, 0, 2 },
    695 	{ DECREMENT, 5, 0, 1 },
    696 	{ DECREMENT, 5, 0, 0 },
    697 	{ FETCH, 10, 0, 1 },
    698 	{ FETCH, 1, 0, 1 },
    699 	{ FETCH, 2, 0, 2 },
    700 	{ FETCH, 3, 0, 3 },
    701 	{ INCREMENT, 1, 0, 2 },
    702 	{ DECREMENT, 2, 0, 1 },
    703 	{ DECREMENT, 2, 0, 0 },
    704 	{ FETCH, 12, 0, 0 },
    705 	{ EXIT, 0, 0, 0 }
    706 };
    707 
    708 struct test_program extended[] = {
    709 	{ STORE, 1,  1, 1 },
    710 	{ STORE, 2,  2, 2 },
    711 	{ STORE, 3,  3, 3 },
    712 	{ STORE, 4,  4, 4 },
    713 	{ STORE, 5,  5, 5 },
    714 	{ STORE, 6,  1, 1 },
    715 	{ STORE, 7,  2, 2 },
    716 	{ STORE, 8,  3, 3 },
    717 	{ STORE, 9,  4, 4 },
    718 	{ STORE, 10, 5, 5 },
    719 	{ STORE, 11, 1, 1 },
    720 	{ STORE, 12, 2, 2 },
    721 	{ STORE, 13, 3, 3 },
    722 	{ STORE, 14, 4, 4 },
    723 	{ STORE, 15, 5, 5 },
    724 	{ STORE, 16, 1, 1 },
    725 	{ STORE, 17, 2, 2 },
    726 	{ STORE, 18, 3, 3 },
    727 	{ STORE, 19, 4, 4 },
    728 	{ STORE, 20, 5, 5 },
    729 	{ STORE, 21, 1, 1 },
    730 	{ STORE, 22, 2, 2 },
    731 	{ STORE, 23, 3, 3 },
    732 	{ STORE, 24, 4, 4 },
    733 	{ STORE, 25, 5, 5 },
    734 	{ STORE, 26, 1, 1 },
    735 	{ STORE, 27, 2, 2 },
    736 	{ STORE, 28, 3, 3 },
    737 	{ STORE, 29, 4, 4 },
    738 	{ STORE, 30, 5, 5 },
    739 	{ EXIT, 0, 0, 0 }
    740 };
    741 
    742 /*
    743  * Setup the variables for doing the inode scan test.
    744  */
    745 static void setup(void)
    746 {
    747 	errcode_t	retval;
    748 	struct ext2_super_block param;
    749 
    750 	initialize_ext2_error_table();
    751 
    752 	memset(&param, 0, sizeof(param));
    753 	ext2fs_blocks_count_set(&param, 12000);
    754 
    755 	retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
    756 				   test_io_manager, &test_fs);
    757 	if (retval) {
    758 		com_err("setup", retval,
    759 			"while initializing filesystem");
    760 		exit(1);
    761 	}
    762 	retval = ext2fs_allocate_tables(test_fs);
    763 	if (retval) {
    764 		com_err("setup", retval,
    765 			"while allocating tables for test filesystem");
    766 		exit(1);
    767 	}
    768 }
    769 
    770 int run_test(int flags, int size, char *dir, struct test_program *prog)
    771 {
    772 	errcode_t	retval;
    773 	ext2_icount_t	icount;
    774 	struct test_program *pc;
    775 	__u16		result;
    776 	int		problem = 0;
    777 
    778 	if (dir) {
    779 #ifdef CONFIG_TDB
    780 		retval = ext2fs_create_icount_tdb(test_fs, dir,
    781 						  flags, &icount);
    782 		if (retval) {
    783 			com_err("run_test", retval,
    784 				"while creating icount using tdb");
    785 			exit(1);
    786 		}
    787 #else
    788 		printf("Skipped\n");
    789 		return 0;
    790 #endif
    791 	} else {
    792 		retval = ext2fs_create_icount2(test_fs, flags, size, 0,
    793 					       &icount);
    794 		if (retval) {
    795 			com_err("run_test", retval, "while creating icount");
    796 			exit(1);
    797 		}
    798 	}
    799 	for (pc = prog; pc->cmd != EXIT; pc++) {
    800 		switch (pc->cmd) {
    801 		case FETCH:
    802 			printf("icount_fetch(%u) = ", pc->ino);
    803 			break;
    804 		case STORE:
    805 			retval = ext2fs_icount_store(icount, pc->ino, pc->arg);
    806 			if (retval) {
    807 				com_err("run_test", retval,
    808 					"while calling icount_store");
    809 				exit(1);
    810 			}
    811 			printf("icount_store(%u, %u) = ", pc->ino, pc->arg);
    812 			break;
    813 		case INCREMENT:
    814 			retval = ext2fs_icount_increment(icount, pc->ino, 0);
    815 			if (retval) {
    816 				com_err("run_test", retval,
    817 					"while calling icount_increment");
    818 				exit(1);
    819 			}
    820 			printf("icount_increment(%u) = ", pc->ino);
    821 			break;
    822 		case DECREMENT:
    823 			retval = ext2fs_icount_decrement(icount, pc->ino, 0);
    824 			if (retval) {
    825 				com_err("run_test", retval,
    826 					"while calling icount_decrement");
    827 				exit(1);
    828 			}
    829 			printf("icount_decrement(%u) = ", pc->ino);
    830 			break;
    831 		}
    832 		retval = ext2fs_icount_fetch(icount, pc->ino, &result);
    833 		if (retval) {
    834 			com_err("run_test", retval,
    835 				"while calling icount_fetch");
    836 			exit(1);
    837 		}
    838 		printf("%u (%s)\n", result, (result == pc->expected) ?
    839 		       "OK" : "NOT OK");
    840 		if (result != pc->expected)
    841 			problem++;
    842 	}
    843 	printf("icount size is %u\n", ext2fs_get_icount_size(icount));
    844 	retval = ext2fs_icount_validate(icount, stdout);
    845 	if (retval) {
    846 		com_err("run_test", retval, "while calling icount_validate");
    847 		exit(1);
    848 	}
    849 	ext2fs_free_icount(icount);
    850 	return problem;
    851 }
    852 
    853 
    854 int main(int argc, char **argv)
    855 {
    856 	int failed = 0;
    857 
    858 	setup();
    859 	printf("Standard icount run:\n");
    860 	failed += run_test(0, 0, 0, prog);
    861 	printf("\nMultiple bitmap test:\n");
    862 	failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, 0, prog);
    863 	printf("\nResizing icount:\n");
    864 	failed += run_test(0, 3, 0, extended);
    865 	printf("\nStandard icount run with tdb:\n");
    866 	failed += run_test(0, 0, ".", prog);
    867 	printf("\nMultiple bitmap test with tdb:\n");
    868 	failed += run_test(EXT2_ICOUNT_OPT_INCREMENT, 0, ".", prog);
    869 	if (failed)
    870 		printf("FAILED!\n");
    871 	return failed;
    872 }
    873 #endif
    874