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.
13 #include "uuid/uuid.h"
15 #include <ext2fs/tdb.h>
20 struct dir_info *array;
21 struct dir_info *last_lookup;
26 struct dir_info_iter {
32 ext2_ino_t dotdot; /* Parent according to '..' */
33 ext2_ino_t parent; /* Parent according to treewalk */
37 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
39 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
41 struct dir_info_db *db = ctx->dir_info;
42 unsigned int threshold;
44 char *tdb_dir, uuid[40];
47 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
49 profile_get_uint(ctx->profile, "scratch_files",
50 "numdirs_threshold", 0, 0, &threshold);
51 profile_get_boolean(ctx->profile, "scratch_files",
52 "dirinfo", 0, 1, &enable);
54 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
55 (threshold && num_dirs <= threshold))
58 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
62 uuid_unparse(ctx->fs->super->s_uuid, uuid);
63 sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
64 fd = mkstemp(db->tdb_fn);
65 db->tdb = tdb_open(db->tdb_fn, 0, TDB_CLEAR_IF_FIRST,
66 O_RDWR | O_CREAT | O_TRUNC, 0600);
70 static void setup_db(e2fsck_t ctx)
72 struct dir_info_db *db;
76 db = (struct dir_info_db *)
77 e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
79 db->count = db->size = 0;
84 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
86 num_dirs = 1024; /* Guess */
88 setup_tdb(ctx, num_dirs);
92 printf("Note: using tdb!\n");
97 db->size = num_dirs + 10;
98 db->array = (struct dir_info *)
99 e2fsck_allocate_memory(ctx, db->size
100 * sizeof (struct dir_info),
105 * This subroutine is called during pass1 to create a directory info
106 * entry. During pass1, the passed-in parent is 0; it will get filled
109 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
111 struct dir_info_db *db;
112 struct dir_info *dir, ent;
115 unsigned long old_size;
118 printf("add_dir_info for inode (%lu, %lu)...\n", ino, parent);
124 if (ctx->dir_info->count >= ctx->dir_info->size) {
125 old_size = ctx->dir_info->size * sizeof(struct dir_info);
126 ctx->dir_info->size += 10;
127 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
128 sizeof(struct dir_info),
129 &ctx->dir_info->array);
131 ctx->dir_info->size -= 10;
141 e2fsck_put_dir_info(ctx, &ent);
146 * Normally, add_dir_info is called with each inode in
147 * sequential order; but once in a while (like when pass 3
148 * needs to recreate the root directory or lost+found
149 * directory) it is called out of order. In those cases, we
150 * need to move the dir_info entries down to make room, since
151 * the dir_info array needs to be sorted by inode number for
152 * get_dir_info()'s sake.
154 if (ctx->dir_info->count &&
155 ctx->dir_info->array[ctx->dir_info->count-1].ino >= ino) {
156 for (i = ctx->dir_info->count-1; i > 0; i--)
157 if (ctx->dir_info->array[i-1].ino < ino)
159 dir = &ctx->dir_info->array[i];
161 for (j = ctx->dir_info->count++; j > i; j--)
162 ctx->dir_info->array[j] = ctx->dir_info->array[j-1];
164 dir = &ctx->dir_info->array[ctx->dir_info->count++];
167 dir->dotdot = parent;
168 dir->parent = parent;
172 * get_dir_info() --- given an inode number, try to find the directory
173 * information entry for it.
175 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
177 struct dir_info_db *db = ctx->dir_info;
179 struct dir_info_ent *buf;
180 static struct dir_info ret_dir_info;
186 printf("e2fsck_get_dir_info %d...", ino);
192 key.dptr = (unsigned char *) &ino;
193 key.dsize = sizeof(ext2_ino_t);
195 data = tdb_fetch(db->tdb, key);
197 if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
198 printf("fetch failed: %s\n",
199 tdb_errorstr(db->tdb));
203 buf = (struct dir_info_ent *) data.dptr;
204 ret_dir_info.ino = ino;
205 ret_dir_info.dotdot = buf->dotdot;
206 ret_dir_info.parent = buf->parent;
208 printf("(%d,%d,%d)\n", ino, buf->dotdot, buf->parent);
211 return &ret_dir_info;
214 if (db->last_lookup && db->last_lookup->ino == ino)
215 return db->last_lookup;
218 high = ctx->dir_info->count-1;
219 if (ino == ctx->dir_info->array[low].ino) {
221 printf("(%d,%d,%d)\n", ino,
222 ctx->dir_info->array[low].dotdot,
223 ctx->dir_info->array[low].parent);
225 return &ctx->dir_info->array[low];
227 if (ino == ctx->dir_info->array[high].ino) {
229 printf("(%d,%d,%d)\n", ino,
230 ctx->dir_info->array[high].dotdot,
231 ctx->dir_info->array[high].parent);
233 return &ctx->dir_info->array[high];
238 if (mid == low || mid == high)
240 if (ino == ctx->dir_info->array[mid].ino) {
242 printf("(%d,%d,%d)\n", ino,
243 ctx->dir_info->array[mid].dotdot,
244 ctx->dir_info->array[mid].parent);
246 return &ctx->dir_info->array[mid];
248 if (ino < ctx->dir_info->array[mid].ino)
256 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
258 struct dir_info_db *db = ctx->dir_info;
259 struct dir_info_ent buf;
263 printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
270 buf.parent = dir->parent;
271 buf.dotdot = dir->dotdot;
273 key.dptr = (unsigned char *) &dir->ino;
274 key.dsize = sizeof(ext2_ino_t);
275 data.dptr = (unsigned char *) &buf;
276 data.dsize = sizeof(buf);
278 if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
279 printf("store failed: %s\n", tdb_errorstr(db->tdb));
285 * Free the dir_info structure when it isn't needed any more.
287 void e2fsck_free_dir_info(e2fsck_t ctx)
290 if (ctx->dir_info->tdb)
291 tdb_close(ctx->dir_info->tdb);
292 if (ctx->dir_info->tdb_fn) {
293 unlink(ctx->dir_info->tdb_fn);
294 free(ctx->dir_info->tdb_fn);
296 if (ctx->dir_info->array)
297 ext2fs_free_mem(&ctx->dir_info->array);
298 ctx->dir_info->array = 0;
299 ctx->dir_info->size = 0;
300 ctx->dir_info->count = 0;
301 ext2fs_free_mem(&ctx->dir_info);
307 * Return the count of number of directories in the dir_info structure
309 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
311 return ctx->dir_info ? ctx->dir_info->count : 0;
314 extern struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
316 struct dir_info_iter *iter;
317 struct dir_info_db *db = ctx->dir_info;
319 iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
320 "dir_info iterator");
321 memset(iter, 0, sizeof(iter));
324 iter->tdb_iter = tdb_firstkey(db->tdb);
329 extern void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
330 struct dir_info_iter *iter)
332 free(iter->tdb_iter.dptr);
333 ext2fs_free_mem(&iter);
337 * A simple interator function
339 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
342 struct dir_info_db *db = ctx->dir_info;
343 struct dir_info_ent *buf;
344 static struct dir_info ret_dir_info;
346 if (!ctx->dir_info || !iter)
350 if (iter->tdb_iter.dptr == 0)
352 key = iter->tdb_iter;
353 data = tdb_fetch(db->tdb, key);
355 printf("iter fetch failed: %s\n",
356 tdb_errorstr(db->tdb));
359 buf = (struct dir_info_ent *) data.dptr;
360 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
361 ret_dir_info.dotdot = buf->dotdot;
362 ret_dir_info.parent = buf->parent;
363 iter->tdb_iter = tdb_nextkey(db->tdb, key);
366 return &ret_dir_info;
369 if (iter->i >= ctx->dir_info->count)
373 printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
374 ctx->dir_info->array[iter->i].dotdot,
375 ctx->dir_info->array[iter->i].parent);
377 ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
378 return(ctx->dir_info->last_lookup);
382 * This function only sets the parent pointer, and requires that
383 * dirinfo structure has already been created.
385 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
390 p = e2fsck_get_dir_info(ctx, ino);
394 e2fsck_put_dir_info(ctx, p);
399 * This function only sets the dot dot pointer, and requires that
400 * dirinfo structure has already been created.
402 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
407 p = e2fsck_get_dir_info(ctx, ino);
411 e2fsck_put_dir_info(ctx, p);
416 * This function only sets the parent pointer, and requires that
417 * dirinfo structure has already been created.
419 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
424 p = e2fsck_get_dir_info(ctx, ino);
432 * This function only sets the dot dot pointer, and requires that
433 * dirinfo structure has already been created.
435 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
440 p = e2fsck_get_dir_info(ctx, ino);