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
174 * Insert an inode into the sorted array. The array should have at least one
177 * Normally, add_dir_info is called with each inode in
178 * sequential order; but once in a while (like when pass 3
179 * needs to recreate the root directory or lost+found
180 * directory) it is called out of order. In those cases, we
181 * need to move the dir_info entries down to make room, since
182 * the dir_info array needs to be sorted by inode number for
183 * get_dir_info()'s sake.
185 static void e2fsck_insert_dir_info(struct dir_info_db *dir_info, ext2_ino_t ino, ext2_ino_t parent)
188 struct dir_info *dir;
189 size_t dir_size = sizeof(*dir);
190 struct dir_info *array = dir_info->array;
191 ext2_ino_t array_count = dir_info->count;
195 * Removing this check won't break anything. But since seqential ino
196 * inserting happens a lot, this check avoids binary search.
198 if (array_count == 0 || array[array_count - 1].ino < ino) {
199 dir = &array[array_count];
204 err = e2fsck_dir_info_min_larger_equal(dir_info, ino, &index);
205 if (err >= 0 && array[index].ino == ino) {
210 dir = &array[array_count];
216 memmove((char *)dir + dir_size, dir, dir_size * (array_count - index));
220 dir->dotdot = parent;
221 dir->parent = parent;
225 * This subroutine is called during pass1 to create a directory info
226 * entry. During pass1, the passed-in parent is 0; it will get filled
229 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
231 struct dir_info *dir, *old_array;
234 unsigned long old_size;
237 printf("add_dir_info for inode (%u, %u)...\n", ino, parent);
242 if (ctx->dir_info->count >= ctx->dir_info->size) {
243 old_size = ctx->dir_info->size * sizeof(struct dir_info);
244 ctx->dir_info->size += 10;
245 old_array = ctx->dir_info->array;
246 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
247 sizeof(struct dir_info),
248 &ctx->dir_info->array);
250 fprintf(stderr, "Couldn't reallocate dir_info "
251 "structure to %u entries\n",
252 ctx->dir_info->size);
254 ctx->dir_info->size -= 10;
257 if (old_array != ctx->dir_info->array)
258 ctx->dir_info->last_lookup = NULL;
262 if (ctx->dir_info->tdb) {
268 e2fsck_put_dir_info(ctx, &ent);
273 e2fsck_insert_dir_info(ctx->dir_info, ino, parent);
277 * get_dir_info() --- given an inode number, try to find the directory
278 * information entry for it.
280 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
282 struct dir_info_db *db = ctx->dir_info;
290 printf("e2fsck_get_dir_info %u...", ino);
295 static struct dir_info ret_dir_info;
297 struct dir_info_ent *buf;
299 key.dptr = (unsigned char *) &ino;
300 key.dsize = sizeof(ext2_ino_t);
302 data = tdb_fetch(db->tdb, key);
304 if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
305 printf("fetch failed: %s\n",
306 tdb_errorstr(db->tdb));
310 buf = (struct dir_info_ent *) data.dptr;
311 ret_dir_info.ino = ino;
312 ret_dir_info.dotdot = buf->dotdot;
313 ret_dir_info.parent = buf->parent;
315 printf("(%u,%u,%u)\n", ino, buf->dotdot, buf->parent);
318 return &ret_dir_info;
322 if (db->last_lookup && db->last_lookup->ino == ino)
323 return db->last_lookup;
325 err = e2fsck_dir_info_min_larger_equal(ctx->dir_info, ino, &index);
328 assert(ino <= ctx->dir_info->array[index].ino);
329 if (ino == ctx->dir_info->array[index].ino) {
331 printf("(%d,%d,%d)\n", ino,
332 ctx->dir_info->array[index].dotdot,
333 ctx->dir_info->array[index].parent);
335 return &ctx->dir_info->array[index];
340 static void e2fsck_put_dir_info(e2fsck_t ctx EXT2FS_NO_TDB_UNUSED,
341 struct dir_info *dir EXT2FS_NO_TDB_UNUSED)
344 struct dir_info_db *db = ctx->dir_info;
345 struct dir_info_ent buf;
350 printf("e2fsck_put_dir_info (%u, %u, %u)...", dir->ino, dir->dotdot,
358 buf.parent = dir->parent;
359 buf.dotdot = dir->dotdot;
361 key.dptr = (unsigned char *) &dir->ino;
362 key.dsize = sizeof(ext2_ino_t);
363 data.dptr = (unsigned char *) &buf;
364 data.dsize = sizeof(buf);
366 if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
367 printf("store failed: %s\n", tdb_errorstr(db->tdb));
373 * Free the dir_info structure when it isn't needed any more.
375 void e2fsck_free_dir_info(e2fsck_t ctx)
379 if (ctx->dir_info->tdb)
380 tdb_close(ctx->dir_info->tdb);
381 if (ctx->dir_info->tdb_fn) {
382 if (unlink(ctx->dir_info->tdb_fn) < 0)
383 com_err("e2fsck_free_dir_info", errno,
384 _("while freeing dir_info tdb file"));
385 ext2fs_free_mem(&ctx->dir_info->tdb_fn);
388 if (ctx->dir_info->array)
389 ext2fs_free_mem(&ctx->dir_info->array);
390 ctx->dir_info->array = 0;
391 ctx->dir_info->size = 0;
392 ctx->dir_info->count = 0;
393 ext2fs_free_mem(&ctx->dir_info);
399 * Return the count of number of directories in the dir_info structure
401 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
403 return ctx->dir_info ? ctx->dir_info->count : 0;
406 struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
408 struct dir_info_iter *iter;
410 iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
411 "dir_info iterator");
414 if (ctx->dir_info->tdb)
415 iter->tdb_iter = tdb_firstkey(ctx->dir_info->tdb);
421 void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
422 struct dir_info_iter *iter)
425 free(iter->tdb_iter.dptr);
427 ext2fs_free_mem(&iter);
431 * A simple interator function
433 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
435 if (!ctx->dir_info || !iter)
439 if (ctx->dir_info->tdb) {
440 static struct dir_info ret_dir_info;
441 struct dir_info_ent *buf;
444 if (iter->tdb_iter.dptr == 0)
446 key = iter->tdb_iter;
447 data = tdb_fetch(ctx->dir_info->tdb, key);
449 printf("iter fetch failed: %s\n",
450 tdb_errorstr(ctx->dir_info->tdb));
453 buf = (struct dir_info_ent *) data.dptr;
454 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
455 ret_dir_info.dotdot = buf->dotdot;
456 ret_dir_info.parent = buf->parent;
457 iter->tdb_iter = tdb_nextkey(ctx->dir_info->tdb, key);
460 return &ret_dir_info;
464 if (iter->i >= ctx->dir_info->count)
468 printf("iter(%u, %u, %u)...", ctx->dir_info->array[iter->i].ino,
469 ctx->dir_info->array[iter->i].dotdot,
470 ctx->dir_info->array[iter->i].parent);
472 ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
473 return(ctx->dir_info->last_lookup);
477 * This function only sets the parent pointer, and requires that
478 * dirinfo structure has already been created.
480 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
485 p = e2fsck_get_dir_info(ctx, ino);
489 e2fsck_put_dir_info(ctx, p);
494 * This function only sets the dot dot pointer, and requires that
495 * dirinfo structure has already been created.
497 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
502 p = e2fsck_get_dir_info(ctx, ino);
506 e2fsck_put_dir_info(ctx, p);
511 * This function only sets the parent pointer, and requires that
512 * dirinfo structure has already been created.
514 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
519 p = e2fsck_get_dir_info(ctx, ino);
527 * This function only sets the dot dot pointer, and requires that
528 * dirinfo structure has already been created.
530 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
535 p = e2fsck_get_dir_info(ctx, ino);