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/ext2fs.h"
17 #include <ext2fs/tdb.h>
22 struct dir_info *array;
23 struct dir_info *last_lookup;
28 struct dir_info_iter {
34 ext2_ino_t dotdot; /* Parent according to '..' */
35 ext2_ino_t parent; /* Parent according to treewalk */
39 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
41 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
43 struct dir_info_db *db = ctx->dir_info;
44 unsigned int threshold;
47 char *tdb_dir, uuid[40];
50 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
52 profile_get_uint(ctx->profile, "scratch_files",
53 "numdirs_threshold", 0, 0, &threshold);
54 profile_get_boolean(ctx->profile, "scratch_files",
55 "dirinfo", 0, 1, &enable);
57 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
58 (threshold && num_dirs <= threshold))
61 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
65 uuid_unparse(ctx->fs->super->s_uuid, uuid);
66 sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
67 save_umask = umask(077);
68 fd = mkstemp(db->tdb_fn);
76 num_dirs = 99991; /* largest 5 digit prime */
78 db->tdb = tdb_open(db->tdb_fn, num_dirs, TDB_NOLOCK | TDB_NOSYNC,
79 O_RDWR | O_CREAT | O_TRUNC, 0600);
83 static void setup_db(e2fsck_t ctx)
85 struct dir_info_db *db;
89 db = (struct dir_info_db *)
90 e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
92 db->count = db->size = 0;
97 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
99 num_dirs = 1024; /* Guess */
101 setup_tdb(ctx, num_dirs);
105 printf("Note: using tdb!\n");
110 db->size = num_dirs + 10;
111 db->array = (struct dir_info *)
112 e2fsck_allocate_memory(ctx, db->size
113 * sizeof (struct dir_info),
118 * This subroutine is called during pass1 to create a directory info
119 * entry. During pass1, the passed-in parent is 0; it will get filled
122 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
124 struct dir_info_db *db;
125 struct dir_info *dir, ent, *old_array;
128 unsigned long old_size;
131 printf("add_dir_info for inode (%lu, %lu)...\n", ino, parent);
137 if (ctx->dir_info->count >= ctx->dir_info->size) {
138 old_size = ctx->dir_info->size * sizeof(struct dir_info);
139 ctx->dir_info->size += 10;
140 old_array = ctx->dir_info->array;
141 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
142 sizeof(struct dir_info),
143 &ctx->dir_info->array);
145 fprintf(stderr, "Couldn't reallocate dir_info "
146 "structure to %d entries\n",
147 ctx->dir_info->size);
149 ctx->dir_info->size -= 10;
152 if (old_array != ctx->dir_info->array)
153 ctx->dir_info->last_lookup = NULL;
161 e2fsck_put_dir_info(ctx, &ent);
166 * Normally, add_dir_info is called with each inode in
167 * sequential order; but once in a while (like when pass 3
168 * needs to recreate the root directory or lost+found
169 * directory) it is called out of order. In those cases, we
170 * need to move the dir_info entries down to make room, since
171 * the dir_info array needs to be sorted by inode number for
172 * get_dir_info()'s sake.
174 if (ctx->dir_info->count &&
175 ctx->dir_info->array[ctx->dir_info->count-1].ino >= ino) {
176 for (i = ctx->dir_info->count-1; i > 0; i--)
177 if (ctx->dir_info->array[i-1].ino < ino)
179 dir = &ctx->dir_info->array[i];
181 for (j = ctx->dir_info->count++; j > i; j--)
182 ctx->dir_info->array[j] = ctx->dir_info->array[j-1];
184 dir = &ctx->dir_info->array[ctx->dir_info->count++];
187 dir->dotdot = parent;
188 dir->parent = parent;
192 * get_dir_info() --- given an inode number, try to find the directory
193 * information entry for it.
195 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
197 struct dir_info_db *db = ctx->dir_info;
199 struct dir_info_ent *buf;
200 static struct dir_info ret_dir_info;
206 printf("e2fsck_get_dir_info %d...", ino);
212 key.dptr = (unsigned char *) &ino;
213 key.dsize = sizeof(ext2_ino_t);
215 data = tdb_fetch(db->tdb, key);
217 if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
218 printf("fetch failed: %s\n",
219 tdb_errorstr(db->tdb));
223 buf = (struct dir_info_ent *) data.dptr;
224 ret_dir_info.ino = ino;
225 ret_dir_info.dotdot = buf->dotdot;
226 ret_dir_info.parent = buf->parent;
228 printf("(%d,%d,%d)\n", ino, buf->dotdot, buf->parent);
231 return &ret_dir_info;
234 if (db->last_lookup && db->last_lookup->ino == ino)
235 return db->last_lookup;
238 high = ctx->dir_info->count-1;
239 if (ino == ctx->dir_info->array[low].ino) {
241 printf("(%d,%d,%d)\n", ino,
242 ctx->dir_info->array[low].dotdot,
243 ctx->dir_info->array[low].parent);
245 return &ctx->dir_info->array[low];
247 if (ino == ctx->dir_info->array[high].ino) {
249 printf("(%d,%d,%d)\n", ino,
250 ctx->dir_info->array[high].dotdot,
251 ctx->dir_info->array[high].parent);
253 return &ctx->dir_info->array[high];
258 if (mid == low || mid == high)
260 if (ino == ctx->dir_info->array[mid].ino) {
262 printf("(%d,%d,%d)\n", ino,
263 ctx->dir_info->array[mid].dotdot,
264 ctx->dir_info->array[mid].parent);
266 return &ctx->dir_info->array[mid];
268 if (ino < ctx->dir_info->array[mid].ino)
276 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
278 struct dir_info_db *db = ctx->dir_info;
279 struct dir_info_ent buf;
283 printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
290 buf.parent = dir->parent;
291 buf.dotdot = dir->dotdot;
293 key.dptr = (unsigned char *) &dir->ino;
294 key.dsize = sizeof(ext2_ino_t);
295 data.dptr = (unsigned char *) &buf;
296 data.dsize = sizeof(buf);
298 if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
299 printf("store failed: %s\n", tdb_errorstr(db->tdb));
305 * Free the dir_info structure when it isn't needed any more.
307 void e2fsck_free_dir_info(e2fsck_t ctx)
310 if (ctx->dir_info->tdb)
311 tdb_close(ctx->dir_info->tdb);
312 if (ctx->dir_info->tdb_fn) {
313 unlink(ctx->dir_info->tdb_fn);
314 free(ctx->dir_info->tdb_fn);
316 if (ctx->dir_info->array)
317 ext2fs_free_mem(&ctx->dir_info->array);
318 ctx->dir_info->array = 0;
319 ctx->dir_info->size = 0;
320 ctx->dir_info->count = 0;
321 ext2fs_free_mem(&ctx->dir_info);
327 * Return the count of number of directories in the dir_info structure
329 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
331 return ctx->dir_info ? ctx->dir_info->count : 0;
334 struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
336 struct dir_info_iter *iter;
337 struct dir_info_db *db = ctx->dir_info;
339 iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
340 "dir_info iterator");
343 iter->tdb_iter = tdb_firstkey(db->tdb);
348 void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
349 struct dir_info_iter *iter)
351 free(iter->tdb_iter.dptr);
352 ext2fs_free_mem(&iter);
356 * A simple interator function
358 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
361 struct dir_info_db *db = ctx->dir_info;
362 struct dir_info_ent *buf;
363 static struct dir_info ret_dir_info;
365 if (!ctx->dir_info || !iter)
369 if (iter->tdb_iter.dptr == 0)
371 key = iter->tdb_iter;
372 data = tdb_fetch(db->tdb, key);
374 printf("iter fetch failed: %s\n",
375 tdb_errorstr(db->tdb));
378 buf = (struct dir_info_ent *) data.dptr;
379 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
380 ret_dir_info.dotdot = buf->dotdot;
381 ret_dir_info.parent = buf->parent;
382 iter->tdb_iter = tdb_nextkey(db->tdb, key);
385 return &ret_dir_info;
388 if (iter->i >= ctx->dir_info->count)
392 printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
393 ctx->dir_info->array[iter->i].dotdot,
394 ctx->dir_info->array[iter->i].parent);
396 ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
397 return(ctx->dir_info->last_lookup);
401 * This function only sets the parent pointer, and requires that
402 * dirinfo structure has already been created.
404 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
409 p = e2fsck_get_dir_info(ctx, ino);
413 e2fsck_put_dir_info(ctx, p);
418 * This function only sets the dot dot pointer, and requires that
419 * dirinfo structure has already been created.
421 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
426 p = e2fsck_get_dir_info(ctx, ino);
430 e2fsck_put_dir_info(ctx, p);
435 * This function only sets the parent pointer, and requires that
436 * dirinfo structure has already been created.
438 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
443 p = e2fsck_get_dir_info(ctx, ino);
451 * This function only sets the dot dot pointer, and requires that
452 * dirinfo structure has already been created.
454 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
459 p = e2fsck_get_dir_info(ctx, ino);