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.
14 #include "uuid/uuid.h"
16 #include <ext2fs/tdb.h>
21 struct dir_info *array;
22 struct dir_info *last_lookup;
27 struct dir_info_iter {
33 ext2_ino_t dotdot; /* Parent according to '..' */
34 ext2_ino_t parent; /* Parent according to treewalk */
38 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
40 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
42 struct dir_info_db *db = ctx->dir_info;
43 unsigned int threshold;
46 char *tdb_dir, uuid[40];
49 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
51 profile_get_uint(ctx->profile, "scratch_files",
52 "numdirs_threshold", 0, 0, &threshold);
53 profile_get_boolean(ctx->profile, "scratch_files",
54 "dirinfo", 0, 1, &enable);
56 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
57 (threshold && num_dirs <= threshold))
60 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
64 uuid_unparse(ctx->fs->super->s_uuid, uuid);
65 sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
66 save_umask = umask(077);
67 fd = mkstemp(db->tdb_fn);
75 num_dirs = 99991; /* largest 5 digit prime */
77 db->tdb = tdb_open(db->tdb_fn, num_dirs, TDB_NOLOCK | TDB_NOSYNC,
78 O_RDWR | O_CREAT | O_TRUNC, 0600);
82 static void setup_db(e2fsck_t ctx)
84 struct dir_info_db *db;
88 db = (struct dir_info_db *)
89 e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
91 db->count = db->size = 0;
96 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
98 num_dirs = 1024; /* Guess */
100 setup_tdb(ctx, num_dirs);
104 printf("Note: using tdb!\n");
109 db->size = num_dirs + 10;
110 db->array = (struct dir_info *)
111 e2fsck_allocate_memory(ctx, db->size
112 * sizeof (struct dir_info),
117 * This subroutine is called during pass1 to create a directory info
118 * entry. During pass1, the passed-in parent is 0; it will get filled
121 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
123 struct dir_info_db *db;
124 struct dir_info *dir, ent, *old_array;
127 unsigned long old_size;
130 printf("add_dir_info for inode (%lu, %lu)...\n", ino, parent);
136 if (ctx->dir_info->count >= ctx->dir_info->size) {
137 old_size = ctx->dir_info->size * sizeof(struct dir_info);
138 ctx->dir_info->size += 10;
139 old_array = ctx->dir_info->array;
140 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
141 sizeof(struct dir_info),
142 &ctx->dir_info->array);
144 fprintf(stderr, "Couldn't reallocate dir_info "
145 "structure to %d entries\n",
146 ctx->dir_info->size);
148 ctx->dir_info->size -= 10;
151 if (old_array != ctx->dir_info->array)
152 ctx->dir_info->last_lookup = NULL;
160 e2fsck_put_dir_info(ctx, &ent);
165 * Normally, add_dir_info is called with each inode in
166 * sequential order; but once in a while (like when pass 3
167 * needs to recreate the root directory or lost+found
168 * directory) it is called out of order. In those cases, we
169 * need to move the dir_info entries down to make room, since
170 * the dir_info array needs to be sorted by inode number for
171 * get_dir_info()'s sake.
173 if (ctx->dir_info->count &&
174 ctx->dir_info->array[ctx->dir_info->count-1].ino >= ino) {
175 for (i = ctx->dir_info->count-1; i > 0; i--)
176 if (ctx->dir_info->array[i-1].ino < ino)
178 dir = &ctx->dir_info->array[i];
180 for (j = ctx->dir_info->count++; j > i; j--)
181 ctx->dir_info->array[j] = ctx->dir_info->array[j-1];
183 dir = &ctx->dir_info->array[ctx->dir_info->count++];
186 dir->dotdot = parent;
187 dir->parent = parent;
191 * get_dir_info() --- given an inode number, try to find the directory
192 * information entry for it.
194 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
196 struct dir_info_db *db = ctx->dir_info;
198 struct dir_info_ent *buf;
199 static struct dir_info ret_dir_info;
205 printf("e2fsck_get_dir_info %d...", ino);
211 key.dptr = (unsigned char *) &ino;
212 key.dsize = sizeof(ext2_ino_t);
214 data = tdb_fetch(db->tdb, key);
216 if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
217 printf("fetch failed: %s\n",
218 tdb_errorstr(db->tdb));
222 buf = (struct dir_info_ent *) data.dptr;
223 ret_dir_info.ino = ino;
224 ret_dir_info.dotdot = buf->dotdot;
225 ret_dir_info.parent = buf->parent;
227 printf("(%d,%d,%d)\n", ino, buf->dotdot, buf->parent);
230 return &ret_dir_info;
233 if (db->last_lookup && db->last_lookup->ino == ino)
234 return db->last_lookup;
237 high = ctx->dir_info->count-1;
238 if (ino == ctx->dir_info->array[low].ino) {
240 printf("(%d,%d,%d)\n", ino,
241 ctx->dir_info->array[low].dotdot,
242 ctx->dir_info->array[low].parent);
244 return &ctx->dir_info->array[low];
246 if (ino == ctx->dir_info->array[high].ino) {
248 printf("(%d,%d,%d)\n", ino,
249 ctx->dir_info->array[high].dotdot,
250 ctx->dir_info->array[high].parent);
252 return &ctx->dir_info->array[high];
257 if (mid == low || mid == high)
259 if (ino == ctx->dir_info->array[mid].ino) {
261 printf("(%d,%d,%d)\n", ino,
262 ctx->dir_info->array[mid].dotdot,
263 ctx->dir_info->array[mid].parent);
265 return &ctx->dir_info->array[mid];
267 if (ino < ctx->dir_info->array[mid].ino)
275 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
277 struct dir_info_db *db = ctx->dir_info;
278 struct dir_info_ent buf;
282 printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
289 buf.parent = dir->parent;
290 buf.dotdot = dir->dotdot;
292 key.dptr = (unsigned char *) &dir->ino;
293 key.dsize = sizeof(ext2_ino_t);
294 data.dptr = (unsigned char *) &buf;
295 data.dsize = sizeof(buf);
297 if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
298 printf("store failed: %s\n", tdb_errorstr(db->tdb));
304 * Free the dir_info structure when it isn't needed any more.
306 void e2fsck_free_dir_info(e2fsck_t ctx)
309 if (ctx->dir_info->tdb)
310 tdb_close(ctx->dir_info->tdb);
311 if (ctx->dir_info->tdb_fn) {
312 unlink(ctx->dir_info->tdb_fn);
313 free(ctx->dir_info->tdb_fn);
315 if (ctx->dir_info->array)
316 ext2fs_free_mem(&ctx->dir_info->array);
317 ctx->dir_info->array = 0;
318 ctx->dir_info->size = 0;
319 ctx->dir_info->count = 0;
320 ext2fs_free_mem(&ctx->dir_info);
326 * Return the count of number of directories in the dir_info structure
328 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
330 return ctx->dir_info ? ctx->dir_info->count : 0;
333 struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
335 struct dir_info_iter *iter;
336 struct dir_info_db *db = ctx->dir_info;
338 iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
339 "dir_info iterator");
342 iter->tdb_iter = tdb_firstkey(db->tdb);
347 void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
348 struct dir_info_iter *iter)
350 free(iter->tdb_iter.dptr);
351 ext2fs_free_mem(&iter);
355 * A simple interator function
357 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
360 struct dir_info_db *db = ctx->dir_info;
361 struct dir_info_ent *buf;
362 static struct dir_info ret_dir_info;
364 if (!ctx->dir_info || !iter)
368 if (iter->tdb_iter.dptr == 0)
370 key = iter->tdb_iter;
371 data = tdb_fetch(db->tdb, key);
373 printf("iter fetch failed: %s\n",
374 tdb_errorstr(db->tdb));
377 buf = (struct dir_info_ent *) data.dptr;
378 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
379 ret_dir_info.dotdot = buf->dotdot;
380 ret_dir_info.parent = buf->parent;
381 iter->tdb_iter = tdb_nextkey(db->tdb, key);
384 return &ret_dir_info;
387 if (iter->i >= ctx->dir_info->count)
391 printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
392 ctx->dir_info->array[iter->i].dotdot,
393 ctx->dir_info->array[iter->i].parent);
395 ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
396 return(ctx->dir_info->last_lookup);
400 * This function only sets the parent pointer, and requires that
401 * dirinfo structure has already been created.
403 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
408 p = e2fsck_get_dir_info(ctx, ino);
412 e2fsck_put_dir_info(ctx, p);
417 * This function only sets the dot dot pointer, and requires that
418 * dirinfo structure has already been created.
420 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
425 p = e2fsck_get_dir_info(ctx, ino);
429 e2fsck_put_dir_info(ctx, p);
434 * This function only sets the parent pointer, and requires that
435 * dirinfo structure has already been created.
437 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
442 p = e2fsck_get_dir_info(ctx, ino);
450 * This function only sets the dot dot pointer, and requires that
451 * dirinfo structure has already been created.
453 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
458 p = e2fsck_get_dir_info(ctx, ino);