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, unsigned 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,
155 return ext2fs_create_icount2(fs, flags, size, 0, ret);
159 * insert_icount_el() --- Insert a new entry into the sorted list at a
160 * specified position.
162 static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
163 ext2_ino_t ino, int pos)
165 struct ext2_icount_el *el;
167 ext2_ino_t new_size = 0;
170 if (icount->count >= icount->size) {
172 new_size = icount->list[(unsigned)icount->count-1].ino;
173 new_size = (ext2_ino_t) (icount->count *
174 ((float) icount->num_inodes / new_size));
176 if (new_size < (icount->size + 100))
177 new_size = icount->size + 100;
179 printf("Reallocating icount %d entries...\n", new_size);
181 retval = ext2fs_resize_mem((size_t) icount->size *
182 sizeof(struct ext2_icount_el),
184 sizeof(struct ext2_icount_el),
188 icount->size = new_size;
190 num = (int) icount->count - pos;
192 return 0; /* should never happen */
194 memmove(&icount->list[pos+1], &icount->list[pos],
195 sizeof(struct ext2_icount_el) * num);
198 el = &icount->list[pos];
205 * get_icount_el() --- given an inode number, try to find icount
206 * information in the sorted list. If the create flag is set,
207 * and we can't find an entry, create one in the sorted list.
209 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
210 ext2_ino_t ino, int create)
214 ext2_ino_t lowval, highval;
216 if (!icount || !icount->list)
219 if (create && ((icount->count == 0) ||
220 (ino > icount->list[(unsigned)icount->count-1].ino))) {
221 return insert_icount_el(icount, ino, (unsigned) icount->count);
223 if (icount->count == 0)
226 if (icount->cursor >= icount->count)
228 if (ino == icount->list[icount->cursor].ino)
229 return &icount->list[icount->cursor++];
231 printf("Non-cursor get_icount_el: %u\n", ino);
234 high = (int) icount->count-1;
235 while (low <= high) {
242 /* Interpolate for efficiency */
243 lowval = icount->list[low].ino;
244 highval = icount->list[high].ino;
248 else if (ino > highval)
251 range = ((float) (ino - lowval)) /
253 mid = low + ((int) (range * (high-low)));
256 if (ino == icount->list[mid].ino) {
257 icount->cursor = mid+1;
258 return &icount->list[mid];
260 if (ino < icount->list[mid].ino)
266 * If we need to create a new entry, it should be right at
267 * low (where high will be left at low-1).
270 return insert_icount_el(icount, ino, low);
274 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
278 const char *bad = "bad icount";
280 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
282 if (icount->count > icount->size) {
283 fprintf(out, "%s: count > size\n", bad);
284 return EXT2_ET_INVALID_ARGUMENT;
286 for (i=1; i < icount->count; i++) {
287 if (icount->list[i-1].ino >= icount->list[i].ino) {
288 fprintf(out, "%s: list[%d].ino=%u, list[%d].ino=%u\n",
289 bad, i-1, icount->list[i-1].ino,
290 i, icount->list[i].ino);
291 ret = EXT2_ET_INVALID_ARGUMENT;
297 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
299 struct ext2_icount_el *el;
301 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
303 if (!ino || (ino > icount->num_inodes))
304 return EXT2_ET_INVALID_ARGUMENT;
306 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
310 if (icount->multiple &&
311 !ext2fs_test_inode_bitmap(icount->multiple, ino)) {
315 el = get_icount_el(icount, ino, 0);
324 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
327 struct ext2_icount_el *el;
329 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
331 if (!ino || (ino > icount->num_inodes))
332 return EXT2_ET_INVALID_ARGUMENT;
334 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
336 * If the existing count is 1, then we know there is
337 * no entry in the list.
339 el = get_icount_el(icount, ino, 1);
341 return EXT2_ET_NO_MEMORY;
342 ext2fs_unmark_inode_bitmap(icount->single, ino);
344 } else if (icount->multiple) {
346 * The count is either zero or greater than 1; if the
347 * inode is set in icount->multiple, then there should
348 * be an entry in the list, so find it using
351 if (ext2fs_test_inode_bitmap(icount->multiple, ino)) {
352 el = get_icount_el(icount, ino, 1);
354 return EXT2_ET_NO_MEMORY;
358 * The count was zero; mark the single bitmap
362 ext2fs_mark_inode_bitmap(icount->single, ino);
369 * The count is either zero or greater than 1; try to
370 * find an entry in the list to determine which.
372 el = get_icount_el(icount, ino, 0);
374 /* No entry means the count was zero */
377 el = get_icount_el(icount, ino, 1);
379 return EXT2_ET_NO_MEMORY;
382 if (icount->multiple)
383 ext2fs_mark_inode_bitmap(icount->multiple, ino);
389 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
392 struct ext2_icount_el *el;
394 if (!ino || (ino > icount->num_inodes))
395 return EXT2_ET_INVALID_ARGUMENT;
397 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
399 if (ext2fs_test_inode_bitmap(icount->single, ino)) {
400 ext2fs_unmark_inode_bitmap(icount->single, ino);
401 if (icount->multiple)
402 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
404 el = get_icount_el(icount, ino, 0);
413 if (icount->multiple &&
414 !ext2fs_test_inode_bitmap(icount->multiple, ino))
415 return EXT2_ET_INVALID_ARGUMENT;
417 el = get_icount_el(icount, ino, 0);
418 if (!el || el->count == 0)
419 return EXT2_ET_INVALID_ARGUMENT;
423 ext2fs_mark_inode_bitmap(icount->single, ino);
424 if ((el->count == 0) && icount->multiple)
425 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
432 errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
435 struct ext2_icount_el *el;
437 if (!ino || (ino > icount->num_inodes))
438 return EXT2_ET_INVALID_ARGUMENT;
440 EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
443 ext2fs_mark_inode_bitmap(icount->single, ino);
444 if (icount->multiple)
445 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
449 ext2fs_unmark_inode_bitmap(icount->single, ino);
450 if (icount->multiple) {
452 * If the icount->multiple bitmap is enabled,
453 * we can just clear both bitmaps and we're done
455 ext2fs_unmark_inode_bitmap(icount->multiple, ino);
457 el = get_icount_el(icount, ino, 0);
465 * Get the icount element
467 el = get_icount_el(icount, ino, 1);
469 return EXT2_ET_NO_MEMORY;
471 ext2fs_unmark_inode_bitmap(icount->single, ino);
472 if (icount->multiple)
473 ext2fs_mark_inode_bitmap(icount->multiple, ino);
477 ext2_ino_t ext2fs_get_icount_size(ext2_icount_t icount)
479 if (!icount || icount->magic != EXT2_ET_MAGIC_ICOUNT)