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 <ext2fs/tdb.h>
19 struct dir_info *array;
20 struct dir_info *last_lookup;
25 struct dir_info_iter {
31 ext2_ino_t dotdot; /* Parent according to '..' */
32 ext2_ino_t parent; /* Parent according to treewalk */
36 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
38 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
40 struct dir_info_db *db = ctx->dir_info;
42 char *tdb_dir, uuid[40];
43 int fd, threshold, enable;
45 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
47 profile_get_integer(ctx->profile, "scratch_files",
48 "numdirs_threshold", 0, 0, &threshold);
49 profile_get_boolean(ctx->profile, "scratch_files",
50 "dirinfo", 0, 1, &enable);
52 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
53 (threshold && num_dirs <= threshold))
56 retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
60 uuid_unparse(ctx->fs->super->s_uuid, uuid);
61 sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
62 fd = mkstemp(db->tdb_fn);
63 db->tdb = tdb_open(db->tdb_fn, 0, TDB_CLEAR_IF_FIRST,
64 O_RDWR | O_CREAT | O_TRUNC, 0600);
68 void setup_db(e2fsck_t ctx)
70 struct dir_info_db *db;
74 db = (struct dir_info_db *)
75 e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
77 db->count = db->size = 0;
82 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
84 num_dirs = 1024; /* Guess */
86 setup_tdb(ctx, num_dirs);
90 printf("Note: using tdb!\n");
95 db->size = num_dirs + 10;
96 db->array = (struct dir_info *)
97 e2fsck_allocate_memory(ctx, db->size
98 * sizeof (struct dir_info),
103 * This subroutine is called during pass1 to create a directory info
104 * entry. During pass1, the passed-in parent is 0; it will get filled
107 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
109 struct dir_info_db *db;
110 struct dir_info *dir, ent;
113 unsigned long old_size;
116 printf("add_dir_info for inode (%lu, %lu)...\n", ino, parent);
122 if (ctx->dir_info->count >= ctx->dir_info->size) {
123 old_size = ctx->dir_info->size * sizeof(struct dir_info);
124 ctx->dir_info->size += 10;
125 retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
126 sizeof(struct dir_info),
129 ctx->dir_info->size -= 10;
139 e2fsck_put_dir_info(ctx, &ent);
144 * Normally, add_dir_info is called with each inode in
145 * sequential order; but once in a while (like when pass 3
146 * needs to recreate the root directory or lost+found
147 * directory) it is called out of order. In those cases, we
148 * need to move the dir_info entries down to make room, since
149 * the dir_info array needs to be sorted by inode number for
150 * get_dir_info()'s sake.
152 if (ctx->dir_info->count &&
153 ctx->dir_info->array[ctx->dir_info->count-1].ino >= ino) {
154 for (i = ctx->dir_info->count-1; i > 0; i--)
155 if (ctx->dir_info->array[i-1].ino < ino)
157 dir = &ctx->dir_info->array[i];
159 for (j = ctx->dir_info->count++; j > i; j--)
160 ctx->dir_info->array[j] = ctx->dir_info->array[j-1];
162 dir = &ctx->dir_info->array[ctx->dir_info->count++];
165 dir->dotdot = parent;
166 dir->parent = parent;
170 * get_dir_info() --- given an inode number, try to find the directory
171 * information entry for it.
173 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
175 struct dir_info_db *db = ctx->dir_info;
176 int low, high, mid, ret;
177 struct dir_info_ent *buf;
178 static struct dir_info ret_dir_info;
184 printf("e2fsck_get_dir_info %d...", ino);
190 key.dptr = (unsigned char *) &ino;
191 key.dsize = sizeof(ext2_ino_t);
193 data = tdb_fetch(db->tdb, key);
195 if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
196 printf("fetch failed: %s\n",
197 tdb_errorstr(db->tdb));
201 buf = (struct dir_info_ent *) data.dptr;
202 ret_dir_info.ino = ino;
203 ret_dir_info.dotdot = buf->dotdot;
204 ret_dir_info.parent = buf->parent;
206 printf("(%d,%d,%d)\n", ino, buf->dotdot, buf->parent);
209 return &ret_dir_info;
212 if (db->last_lookup && db->last_lookup->ino == ino)
213 return db->last_lookup;
216 high = ctx->dir_info->count-1;
217 if (ino == ctx->dir_info->array[low].ino) {
219 printf("(%d,%d,%d)\n", ino,
220 ctx->dir_info->array[low].dotdot,
221 ctx->dir_info->array[low].parent);
223 return &ctx->dir_info->array[low];
225 if (ino == ctx->dir_info->array[high].ino) {
227 printf("(%d,%d,%d)\n", ino,
228 ctx->dir_info->array[high].dotdot,
229 ctx->dir_info->array[high].parent);
231 return &ctx->dir_info->array[high];
236 if (mid == low || mid == high)
238 if (ino == ctx->dir_info->array[mid].ino) {
240 printf("(%d,%d,%d)\n", ino,
241 ctx->dir_info->array[mid].dotdot,
242 ctx->dir_info->array[mid].parent);
244 return &ctx->dir_info->array[mid];
246 if (ino < ctx->dir_info->array[mid].ino)
254 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
256 struct dir_info_db *db = ctx->dir_info;
257 struct dir_info_ent buf;
261 printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
268 buf.parent = dir->parent;
269 buf.dotdot = dir->dotdot;
271 key.dptr = (unsigned char *) &dir->ino;
272 key.dsize = sizeof(ext2_ino_t);
273 data.dptr = (unsigned char *) &buf;
274 data.dsize = sizeof(buf);
276 if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
277 printf("store failed: %s\n", tdb_errorstr(db->tdb));
283 * Free the dir_info structure when it isn't needed any more.
285 void e2fsck_free_dir_info(e2fsck_t ctx)
288 if (ctx->dir_info->tdb)
289 tdb_close(ctx->dir_info->tdb);
290 if (ctx->dir_info->tdb_fn) {
291 unlink(ctx->dir_info->tdb_fn);
292 free(ctx->dir_info->tdb_fn);
294 ctx->dir_info->size = 0;
295 ctx->dir_info->count = 0;
296 ext2fs_free_mem(&ctx->dir_info);
302 * Return the count of number of directories in the dir_info structure
304 int e2fsck_get_num_dirinfo(e2fsck_t ctx)
306 return ctx->dir_info ? ctx->dir_info->count : 0;
309 extern struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
311 struct dir_info_iter *iter;
312 struct dir_info_db *db = ctx->dir_info;
315 iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
316 "dir_info iterator");
317 memset(iter, 0, sizeof(iter));
320 iter->tdb_iter = tdb_firstkey(db->tdb);
325 extern void e2fsck_dir_info_iter_end(e2fsck_t ctx,
326 struct dir_info_iter *iter)
328 if (iter->tdb_iter.dptr)
329 free(iter->tdb_iter.dptr);
330 ext2fs_free_mem(&iter);
334 * A simple interator function
336 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
339 struct dir_info_db *db = ctx->dir_info;
340 struct dir_info_ent *buf;
341 static struct dir_info ret_dir_info;
343 if (!ctx->dir_info || !iter)
347 if (iter->tdb_iter.dptr == 0)
349 key = iter->tdb_iter;
350 data = tdb_fetch(db->tdb, key);
352 printf("iter fetch failed: %s\n",
353 tdb_errorstr(db->tdb));
356 buf = (struct dir_info_ent *) data.dptr;
357 ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
358 ret_dir_info.dotdot = buf->dotdot;
359 ret_dir_info.parent = buf->parent;
360 iter->tdb_iter = tdb_nextkey(db->tdb, key);
363 return &ret_dir_info;
366 if (iter->i >= ctx->dir_info->count)
370 printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
371 ctx->dir_info->array[iter->i].dotdot,
372 ctx->dir_info->array[iter->i].parent);
374 ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
375 return(ctx->dir_info->last_lookup);
379 * This function only sets the parent pointer, and requires that
380 * dirinfo structure has already been created.
382 int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
387 p = e2fsck_get_dir_info(ctx, ino);
391 e2fsck_put_dir_info(ctx, p);
396 * This function only sets the dot dot pointer, and requires that
397 * dirinfo structure has already been created.
399 int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
404 p = e2fsck_get_dir_info(ctx, ino);
408 e2fsck_put_dir_info(ctx, p);
413 * This function only sets the parent pointer, and requires that
414 * dirinfo structure has already been created.
416 int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
421 p = e2fsck_get_dir_info(ctx, ino);
429 * This function only sets the dot dot pointer, and requires that
430 * dirinfo structure has already been created.
432 int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
437 p = e2fsck_get_dir_info(ctx, ino);