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)
62 ext2fs_free_mem(&icount->list);
63 ext2fs_free_inode_bitmap(icount->single);
64 ext2fs_free_inode_bitmap(icount->multiple);
65 ext2fs_free_mem(&icount);
68 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
69 ext2_icount_t hint, ext2_icount_t *ret)
77 EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT);
78 if (hint->size > size)
79 size = (size_t) hint->size;
82 retval = ext2fs_get_mem(sizeof(struct ext2_icount), &icount);
85 memset(icount, 0, sizeof(struct ext2_icount));
87 retval = ext2fs_allocate_inode_bitmap(fs, 0,
92 if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
93 retval = ext2fs_allocate_inode_bitmap(fs, 0,
104 * Figure out how many special case inode counts we will
105 * have. We know we will need one for each directory;
106 * we also need to reserve some extra room for file links
108 retval = ext2fs_get_num_dirs(fs, &icount->size);
111 icount->size += fs->super->s_inodes_count / 50;
114 bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el));
116 printf("Icount allocated %d entries, %d bytes.\n",
117 icount->size, bytes);
119 retval = ext2fs_get_mem(bytes, &icount->list);
122 memset(icount->list, 0, bytes);
124 icount->magic = EXT2_ET_MAGIC_ICOUNT;
127 icount->num_inodes = fs->super->s_inodes_count;
130 * Populate the sorted list with those entries which were
131 * found in the hint icount (since those are ones which will
132 * likely need to be in the sorted list this time around).
135 for (i=0; i < hint->count; i++)
136 icount->list[i].ino = hint->list[i].ino;
137 icount->count = hint->count;
144 ext2fs_free_icount(icount);
148 errcode_t ext2fs_create_icount(ext2_filsys fs, int flags,
152 return ext2fs_create_icount2(fs, flags, size, 0, ret);
156 * insert_icount_el() --- Insert a new entry into the sorted list at a
157 * specified position.
159 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
160 ext2_ino_t ino, int pos)
162 struct ext2_icount_el *el;
164 ext2_ino_t new_size = 0;
167 if (icount->count >= icount->size) {
169 new_size = icount->list[(unsigned)icount->count-1].ino;
170 new_size = (ext2_ino_t) (icount->count *
171 ((float) icount->num_inodes / new_size));
173 if (new_size < (icount->size + 100))
174 new_size = icount->size + 100;
176 printf("Reallocating icount %d entries...\n", new_size);
178 retval = ext2fs_resize_mem((size_t) icount->size *
179 sizeof(struct ext2_icount_el),
181 sizeof(struct ext2_icount_el),
185 icount->size = new_size;
187 num = (int) icount->count - pos;
189 return 0; /* should never happen */
191 memmove(&icount->list[pos+1], &icount->list[pos],
192 sizeof(struct ext2_icount_el) * num);
195 el = &icount->list[pos];
202 * get_icount_el() --- given an inode number, try to find icount
203 * information in the sorted list. If the create flag is set,
204 * and we can't find an entry, create one in the sorted list.
206 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
207 ext2_ino_t ino, int create)
211 ext2_ino_t lowval, highval;
213 if (!icount || !icount->list)
216 if (create && ((icount->count == 0) ||
217 (ino > icount->list[(unsigned)icount->count-1].ino))) {
218 return insert_icount_el(icount, ino, (unsigned) icount->count);
220 if (icount->count == 0)
223 if (icount->cursor >= icount->count)
225 if (ino == icount->list[icount->cursor].ino)
226 return &icount->list[icount->cursor++];
228 printf("Non-cursor get_icount_el: %u\n", ino);
231 high = (int) icount->count-1;
232 while (low <= high) {
239 /* Interpolate for efficiency */
240 lowval = icount->list[low].ino;
241 highval = icount->list[high].ino;
245 else if (ino > highval)
248 range = ((float) (ino - lowval)) /
250 mid = low + ((int) (range * (high-low)));
253 if (ino == icount->list[mid].ino) {
254 icount->cursor = mid+1;
255 return &icount->list[mid];
257 if (ino < icount->list[mid].ino)
263 * If we need to create a new entry, it should be right at
264 * low (where high will be left at low-1).
267 return insert_icount_el(icount, ino, low);
271 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
275 const char *bad = "bad icount";
277 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
279 if (icount->count > icount->size) {
280 fprintf(out, "%s: count > size\n", bad);
281 return EXT2_ET_INVALID_ARGUMENT;
283 for (i=1; i < icount->count; i++) {
284 if (icount->list[i-1].ino >= icount->list[i].ino) {
285 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
286 bad, i-1, icount->list[i-1].ino,
287 i, icount->list[i].ino);
288 ret = EXT2_ET_INVALID_ARGUMENT;
294 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
296 struct ext2_icount_el *el;
298 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
300 if (!ino || (ino > icount->num_inodes))
301 return EXT2_ET_INVALID_ARGUMENT;
303 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
307 if (icount->multiple &&
308 !ext2fs_test_inode_bitmap(icount->multiple, ino)) {
312 el = get_icount_el(icount, ino, 0);
321 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
324 struct ext2_icount_el *el;
326 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
328 if (!ino || (ino > icount->num_inodes))
329 return EXT2_ET_INVALID_ARGUMENT;
331 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
333 * If the existing count is 1, then we know there is
334 * no entry in the list.
336 el = get_icount_el(icount, ino, 1);
338 return EXT2_ET_NO_MEMORY;
339 ext2fs_unmark_inode_bitmap(icount->single, ino);
341 } else if (icount->multiple) {
343 * The count is either zero or greater than 1; if the
344 * inode is set in icount->multiple, then there should
345 * be an entry in the list, so find it using
348 if (ext2fs_test_inode_bitmap(icount->multiple, ino)) {
349 el = get_icount_el(icount, ino, 1);
351 return EXT2_ET_NO_MEMORY;
355 * The count was zero; mark the single bitmap
359 ext2fs_mark_inode_bitmap(icount->single, ino);
366 * The count is either zero or greater than 1; try to
367 * find an entry in the list to determine which.
369 el = get_icount_el(icount, ino, 0);
371 /* No entry means the count was zero */
374 el = get_icount_el(icount, ino, 1);
376 return EXT2_ET_NO_MEMORY;
379 if (icount->multiple)
380 ext2fs_mark_inode_bitmap(icount->multiple, ino);
386 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
389 struct ext2_icount_el *el;
391 if (!ino || (ino > icount->num_inodes))
392 return EXT2_ET_INVALID_ARGUMENT;
394 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
396 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
397 ext2fs_unmark_inode_bitmap(icount->single, ino);
398 if (icount->multiple)
399 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
401 el = get_icount_el(icount, ino, 0);
410 if (icount->multiple &&
411 !ext2fs_test_inode_bitmap(icount->multiple, ino))
412 return EXT2_ET_INVALID_ARGUMENT;
414 el = get_icount_el(icount, ino, 0);
415 if (!el || el->count == 0)
416 return EXT2_ET_INVALID_ARGUMENT;
420 ext2fs_mark_inode_bitmap(icount->single, ino);
421 if ((el->count == 0) && icount->multiple)
422 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
429 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
432 struct ext2_icount_el *el;
434 if (!ino || (ino > icount->num_inodes))
435 return EXT2_ET_INVALID_ARGUMENT;
437 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
440 ext2fs_mark_inode_bitmap(icount->single, ino);
441 if (icount->multiple)
442 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
446 ext2fs_unmark_inode_bitmap(icount->single, ino);
447 if (icount->multiple) {
449 * If the icount->multiple bitmap is enabled,
450 * we can just clear both bitmaps and we're done
452 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
454 el = get_icount_el(icount, ino, 0);
462 * Get the icount element
464 el = get_icount_el(icount, ino, 1);
466 return EXT2_ET_NO_MEMORY;
468 ext2fs_unmark_inode_bitmap(icount->single, ino);
469 if (icount->multiple)
470 ext2fs_mark_inode_bitmap(icount->multiple, ino);
474 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
476 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)