2 * dirinfo.c --- maintains the directory information table for e2fsck.
4 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
15 #include "uuid/uuid.h"
17 #include "ext2fs/ext2fs.h"
18 #include <ext2fs/tdb.h>
23 struct dir_info *array;
24 struct dir_info *last_lookup;
31 struct dir_info_iter {
39 ext2_ino_t dotdot; /* Parent according to '..' */
40 ext2_ino_t parent; /* Parent according to treewalk */
44 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
47 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
49 struct dir_info_db *db = ctx->dir_info;
53 char *tdb_dir, uuid[40];
56 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
58 profile_get_uint(ctx->profile, "scratch_files",
59 "numdirs_threshold", 0, 0, &threshold);
60 profile_get_boolean(ctx->profile, "scratch_files",
61 "dirinfo", 0, 1, &enable);
63 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
64 (threshold && num_dirs <= threshold))
67 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
71 uuid_unparse(ctx->fs->super->s_uuid, uuid);
72 sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
73 save_umask = umask(077);
74 fd = mkstemp(db->tdb_fn);
82 num_dirs = 99991; /* largest 5 digit prime */
84 db->tdb = tdb_open(db->tdb_fn, num_dirs, TDB_NOLOCK | TDB_NOSYNC,
85 O_RDWR | O_CREAT | O_TRUNC, 0600);
90 static void setup_db(e2fsck_t ctx)
92 struct dir_info_db *db;
96 db = (struct dir_info_db *)
97 e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
99 db->count = db->size = 0;
104 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
106 num_dirs = 1024; /* Guess */
109 setup_tdb(ctx, num_dirs);
113 printf("Note: using tdb!\n");
119 db->size = num_dirs + 10;
120 db->array = (struct dir_info *)
121 e2fsck_allocate_memory(ctx, db->size
122 * sizeof (struct dir_info),
127 * Return the min index that has ino larger or equal to @ino
128 * If not found, return -ENOENT
131 e2fsck_dir_info_min_larger_equal(struct dir_info_db *dir_info,
132 ext2_ino_t ino, ext2_ino_t *index)
135 ext2_ino_t mid, high;
139 if (dir_info->count == 0)
142 high = dir_info->count - 1;
143 while (low <= high) {
144 /* sum may overflow, but result will fit into mid again */
145 mid = (unsigned long long)(low + high) / 2;
146 tmp_ino = dir_info->array[mid].ino;
147 if (ino == tmp_ino) {
151 } else if (ino < tmp_ino) {
153 * The mid ino is larger than @ino, remember the index
154 * here so we won't miss this ino
173 * Merge two sorted dir info to @dest
175 void e2fsck_merge_dir_info(e2fsck_t ctx, struct dir_info_db *src,
176 struct dir_info_db *dest)
178 size_t size_dir_info = sizeof(struct dir_info);
179 ext2_ino_t size = dest->size;
180 struct dir_info *src_array = src->array;
181 struct dir_info *dest_array = dest->array;
182 ext2_ino_t src_count = src->count;
183 ext2_ino_t dest_count = dest->count;
184 ext2_ino_t total_count = src_count + dest_count;
185 struct dir_info *tmp_array;
186 struct dir_info *array_ptr;
187 ext2_ino_t src_index = 0;
188 ext2_ino_t dest_index = 0;
193 if (size < total_count)
196 if (size < src->size)
199 tmp_array = e2fsck_allocate_memory(ctx, size * size_dir_info,
201 array_ptr = tmp_array;
203 * This can be improved by binary search and memcpy, but codes
204 * would be more complex. And if the groups distributed to each
205 * thread are strided, this implementation won't be too bad
206 * comparing to the optimiztion.
208 while (src_index < src_count || dest_index < dest_count) {
209 if (src_index >= src_count) {
210 memcpy(array_ptr, &dest_array[dest_index],
211 (dest_count - dest_index) * size_dir_info);
214 if (dest_index >= dest_count) {
215 memcpy(array_ptr, &src_array[src_index],
216 (src_count - src_index) * size_dir_info);
219 if (src_array[src_index].ino < dest_array[dest_index].ino) {
220 *array_ptr = src_array[src_index];
223 assert(src_array[src_index].ino >
224 dest_array[dest_index].ino);
225 *array_ptr = dest_array[dest_index];
232 ext2fs_free_mem(&dest->array);
233 dest->array = tmp_array;
235 dest->count = total_count;
240 * Insert an inode into the sorted array. The array should have at least one
243 * Normally, add_dir_info is called with each inode in
244 * sequential order; but once in a while (like when pass 3
245 * needs to recreate the root directory or lost+found
246 * directory) it is called out of order. In those cases, we
247 * need to move the dir_info entries down to make room, since
248 * the dir_info array needs to be sorted by inode number for
249 * get_dir_info()'s sake.
251 static void e2fsck_insert_dir_info(struct dir_info_db *dir_info, ext2_ino_t ino, ext2_ino_t parent)
254 struct dir_info *dir;
255 size_t dir_size = sizeof(*dir);
256 struct dir_info *array = dir_info->array;
257 ext2_ino_t array_count = dir_info->count;
261 * Removing this check won't break anything. But since seqential ino
262 * inserting happens a lot, this check avoids binary search.
264 if (array_count == 0 || array[array_count - 1].ino < ino) {
265 dir = &array[array_count];
270 err = e2fsck_dir_info_min_larger_equal(dir_info, ino, &index);
271 if (err >= 0 && array[index].ino == ino) {
276 dir = &array[array_count];
282 memmove((char *)dir + dir_size, dir, dir_size * (array_count - index));
286 dir->dotdot = parent;
287 dir->parent = parent;
291 * This subroutine is called during pass1 to create a directory info
292 * entry. During pass1, the passed-in parent is 0; it will get filled
295 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
297 struct dir_info *dir, *old_array;
300 unsigned long old_size;
303 printf("add_dir_info for inode (%u, %u)...\n", ino, parent);
308 if (ctx->dir_info->count >= ctx->dir_info->size) {
309 old_size = ctx->dir_info->size * sizeof(struct dir_info);
310 ctx->dir_info->size += 10;
311 old_array = ctx->dir_info->array;
312 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
313 sizeof(struct dir_info),
314 &ctx->dir_info->array);
316 fprintf(stderr, "Couldn't reallocate dir_info "
317 "structure to %u entries\n",
318 ctx->dir_info->size);
320 ctx->dir_info->size -= 10;
323 if (old_array != ctx->dir_info->array)
324 ctx->dir_info->last_lookup = NULL;
328 if (ctx->dir_info->tdb) {
334 e2fsck_put_dir_info(ctx, &ent);
339 e2fsck_insert_dir_info(ctx->dir_info, ino, parent);
343 * get_dir_info() --- given an inode number, try to find the directory
344 * information entry for it.
346 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
348 struct dir_info_db *db = ctx->dir_info;
356 printf("e2fsck_get_dir_info %u...", ino);
361 static struct dir_info ret_dir_info;
363 struct dir_info_ent *buf;
365 key.dptr = (unsigned char *) &ino;
366 key.dsize = sizeof(ext2_ino_t);
368 data = tdb_fetch(db->tdb, key);
370 if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
371 printf("fetch failed: %s\n",
372 tdb_errorstr(db->tdb));
376 buf = (struct dir_info_ent *) data.dptr;
377 ret_dir_info.ino = ino;
378 ret_dir_info.dotdot = buf->dotdot;
379 ret_dir_info.parent = buf->parent;
381 printf("(%u,%u,%u)\n", ino, buf->dotdot, buf->parent);
384 return &ret_dir_info;
388 if (db->last_lookup && db->last_lookup->ino == ino)
389 return db->last_lookup;
391 err = e2fsck_dir_info_min_larger_equal(ctx->dir_info, ino, &index);
394 assert(ino <= ctx->dir_info->array[index].ino);
395 if (ino == ctx->dir_info->array[index].ino) {
397 printf("(%d,%d,%d)\n", ino,
398 ctx->dir_info->array[index].dotdot,
399 ctx->dir_info->array[index].parent);
401 return &ctx->dir_info->array[index];
406 static void e2fsck_put_dir_info(e2fsck_t ctx EXT2FS_NO_TDB_UNUSED,
407 struct dir_info *dir EXT2FS_NO_TDB_UNUSED)
410 struct dir_info_db *db = ctx->dir_info;
411 struct dir_info_ent buf;
416 printf("e2fsck_put_dir_info (%u, %u, %u)...", dir->ino, dir->dotdot,
424 buf.parent = dir->parent;
425 buf.dotdot = dir->dotdot;
427 key.dptr = (unsigned char *) &dir->ino;
428 key.dsize = sizeof(ext2_ino_t);
429 data.dptr = (unsigned char *) &buf;
430 data.dsize = sizeof(buf);
432 if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
433 printf("store failed: %s\n", tdb_errorstr(db->tdb));
439 * Free the dir_info structure when it isn't needed any more.
441 void e2fsck_free_dir_info(e2fsck_t ctx)
445 if (ctx->dir_info->tdb)
446 tdb_close(ctx->dir_info->tdb);
447 if (ctx->dir_info->tdb_fn) {
448 if (unlink(ctx->dir_info->tdb_fn) < 0)
449 com_err("e2fsck_free_dir_info", errno,
450 _("while freeing dir_info tdb file"));
451 ext2fs_free_mem(&ctx->dir_info->tdb_fn);
454 if (ctx->dir_info->array)
455 ext2fs_free_mem(&ctx->dir_info->array);
456 ctx->dir_info->array = 0;
457 ctx->dir_info->size = 0;
458 ctx->dir_info->count = 0;
459 ext2fs_free_mem(&ctx->dir_info);
465 * Return the count of number of directories in the dir_info structure
467 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
469 return ctx->dir_info ? ctx->dir_info->count : 0;
472 struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
474 struct dir_info_iter *iter;
476 iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
477 "dir_info iterator");
480 if (ctx->dir_info->tdb)
481 iter->tdb_iter = tdb_firstkey(ctx->dir_info->tdb);
487 void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
488 struct dir_info_iter *iter)
491 free(iter->tdb_iter.dptr);
493 ext2fs_free_mem(&iter);
497 * A simple interator function
499 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
501 if (!ctx->dir_info || !iter)
505 if (ctx->dir_info->tdb) {
506 static struct dir_info ret_dir_info;
507 struct dir_info_ent *buf;
510 if (iter->tdb_iter.dptr == 0)
512 key = iter->tdb_iter;
513 data = tdb_fetch(ctx->dir_info->tdb, key);
515 printf("iter fetch failed: %s\n",
516 tdb_errorstr(ctx->dir_info->tdb));
519 buf = (struct dir_info_ent *) data.dptr;
520 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
521 ret_dir_info.dotdot = buf->dotdot;
522 ret_dir_info.parent = buf->parent;
523 iter->tdb_iter = tdb_nextkey(ctx->dir_info->tdb, key);
526 return &ret_dir_info;
530 if (iter->i >= ctx->dir_info->count)
534 printf("iter(%u, %u, %u)...", ctx->dir_info->array[iter->i].ino,
535 ctx->dir_info->array[iter->i].dotdot,
536 ctx->dir_info->array[iter->i].parent);
538 ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
539 return(ctx->dir_info->last_lookup);
543 * This function only sets the parent pointer, and requires that
544 * dirinfo structure has already been created.
546 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
551 p = e2fsck_get_dir_info(ctx, ino);
555 e2fsck_put_dir_info(ctx, p);
560 * This function only sets the dot dot pointer, and requires that
561 * dirinfo structure has already been created.
563 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
568 p = e2fsck_get_dir_info(ctx, ino);
572 e2fsck_put_dir_info(ctx, p);
577 * This function only sets the parent pointer, and requires that
578 * dirinfo structure has already been created.
580 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
585 p = e2fsck_get_dir_info(ctx, ino);
593 * This function only sets the dot dot pointer, and requires that
594 * dirinfo structure has already been created.
596 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
601 p = e2fsck_get_dir_info(ctx, ino);