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, 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 %d 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, int size,
154 return ext2fs_create_icount2(fs, flags, size, 0, ret);
158 * insert_icount_el() --- Insert a new entry into the sorted list at a
159 * specified position.
161 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
162 ext2_ino_t ino, int pos)
164 struct ext2_icount_el *el;
166 ext2_ino_t new_size = 0;
169 if (icount->count >= icount->size) {
171 new_size = icount->list[(unsigned)icount->count-1].ino;
172 new_size = (ext2_ino_t) (icount->count *
173 ((float) icount->num_inodes / new_size));
175 if (new_size < (icount->size + 100))
176 new_size = icount->size + 100;
178 printf("Reallocating icount %d entries...\n", new_size);
180 retval = ext2fs_resize_mem((size_t) icount->size *
181 sizeof(struct ext2_icount_el),
183 sizeof(struct ext2_icount_el),
187 icount->size = new_size;
189 num = (int) icount->count - pos;
191 return 0; /* should never happen */
193 memmove(&icount->list[pos+1], &icount->list[pos],
194 sizeof(struct ext2_icount_el) * num);
197 el = &icount->list[pos];
204 * get_icount_el() --- given an inode number, try to find icount
205 * information in the sorted list. If the create flag is set,
206 * and we can't find an entry, create one in the sorted list.
208 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
209 ext2_ino_t ino, int create)
213 ext2_ino_t lowval, highval;
215 if (!icount || !icount->list)
218 if (create && ((icount->count == 0) ||
219 (ino > icount->list[(unsigned)icount->count-1].ino))) {
220 return insert_icount_el(icount, ino, (unsigned) icount->count);
222 if (icount->count == 0)
225 if (icount->cursor >= icount->count)
227 if (ino == icount->list[icount->cursor].ino)
228 return &icount->list[icount->cursor++];
230 printf("Non-cursor get_icount_el: %u\n", ino);
233 high = (int) icount->count-1;
234 while (low <= high) {
241 /* Interpolate for efficiency */
242 lowval = icount->list[low].ino;
243 highval = icount->list[high].ino;
247 else if (ino > highval)
250 range = ((float) (ino - lowval)) /
252 mid = low + ((int) (range * (high-low)));
255 if (ino == icount->list[mid].ino) {
256 icount->cursor = mid+1;
257 return &icount->list[mid];
259 if (ino < icount->list[mid].ino)
265 * If we need to create a new entry, it should be right at
266 * low (where high will be left at low-1).
269 return insert_icount_el(icount, ino, low);
273 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
277 const char *bad = "bad icount";
279 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
281 if (icount->count > icount->size) {
282 fprintf(out, "%s: count > size\n", bad);
283 return EXT2_ET_INVALID_ARGUMENT;
285 for (i=1; i < icount->count; i++) {
286 if (icount->list[i-1].ino >= icount->list[i].ino) {
287 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
288 bad, i-1, icount->list[i-1].ino,
289 i, icount->list[i].ino);
290 ret = EXT2_ET_INVALID_ARGUMENT;
296 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
298 struct ext2_icount_el *el;
300 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
302 if (!ino || (ino > icount->num_inodes))
303 return EXT2_ET_INVALID_ARGUMENT;
305 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
309 if (icount->multiple &&
310 !ext2fs_test_inode_bitmap(icount->multiple, ino)) {
314 el = get_icount_el(icount, ino, 0);
323 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
326 struct ext2_icount_el *el;
328 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
330 if (!ino || (ino > icount->num_inodes))
331 return EXT2_ET_INVALID_ARGUMENT;
333 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
335 * If the existing count is 1, then we know there is
336 * no entry in the list.
338 el = get_icount_el(icount, ino, 1);
340 return EXT2_ET_NO_MEMORY;
341 ext2fs_unmark_inode_bitmap(icount->single, ino);
343 } else if (icount->multiple) {
345 * The count is either zero or greater than 1; if the
346 * inode is set in icount->multiple, then there should
347 * be an entry in the list, so find it using
350 if (ext2fs_test_inode_bitmap(icount->multiple, ino)) {
351 el = get_icount_el(icount, ino, 1);
353 return EXT2_ET_NO_MEMORY;
357 * The count was zero; mark the single bitmap
361 ext2fs_mark_inode_bitmap(icount->single, ino);
368 * The count is either zero or greater than 1; try to
369 * find an entry in the list to determine which.
371 el = get_icount_el(icount, ino, 0);
373 /* No entry means the count was zero */
376 el = get_icount_el(icount, ino, 1);
378 return EXT2_ET_NO_MEMORY;
381 if (icount->multiple)
382 ext2fs_mark_inode_bitmap(icount->multiple, ino);
388 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
391 struct ext2_icount_el *el;
393 if (!ino || (ino > icount->num_inodes))
394 return EXT2_ET_INVALID_ARGUMENT;
396 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
398 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
399 ext2fs_unmark_inode_bitmap(icount->single, ino);
400 if (icount->multiple)
401 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
403 el = get_icount_el(icount, ino, 0);
412 if (icount->multiple &&
413 !ext2fs_test_inode_bitmap(icount->multiple, ino))
414 return EXT2_ET_INVALID_ARGUMENT;
416 el = get_icount_el(icount, ino, 0);
417 if (!el || el->count == 0)
418 return EXT2_ET_INVALID_ARGUMENT;
422 ext2fs_mark_inode_bitmap(icount->single, ino);
423 if ((el->count == 0) && icount->multiple)
424 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
431 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
434 struct ext2_icount_el *el;
436 if (!ino || (ino > icount->num_inodes))
437 return EXT2_ET_INVALID_ARGUMENT;
439 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
442 ext2fs_mark_inode_bitmap(icount->single, ino);
443 if (icount->multiple)
444 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
448 ext2fs_unmark_inode_bitmap(icount->single, ino);
449 if (icount->multiple) {
451 * If the icount->multiple bitmap is enabled,
452 * we can just clear both bitmaps and we're done
454 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
456 el = get_icount_el(icount, ino, 0);
464 * Get the icount element
466 el = get_icount_el(icount, ino, 1);
468 return EXT2_ET_NO_MEMORY;
470 ext2fs_unmark_inode_bitmap(icount->single, ino);
471 if (icount->multiple)
472 ext2fs_mark_inode_bitmap(icount->multiple, ino);
476 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
478 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)