blob: 7838a406f610f20a53dfe0bad35e9fad6b81aed9 [file] [log] [blame]
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001/*
2 * dirinfo.c --- maintains the directory information table for e2fsck.
3 *
4 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
Theodore Ts'od1154eb2011-09-18 17:34:37 -04008#include "config.h"
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04009#include "e2fsck.h"
10#ifdef ENABLE_HTREE
11
12/*
13 * This subroutine is called during pass1 to create a directory info
14 * entry. During pass1, the passed-in parent is 0; it will get filled
Theodore Ts'oefc6f622008-08-27 23:07:54 -040015 * in during pass2.
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040016 */
17void e2fsck_add_dx_dir(e2fsck_t ctx, ext2_ino_t ino, int num_blocks)
18{
19 struct dx_dir_info *dir;
20 int i, j;
21 errcode_t retval;
22 unsigned long old_size;
23
24#if 0
25 printf("add_dx_dir_info for inode %lu...\n", ino);
26#endif
27 if (!ctx->dx_dir_info) {
28 ctx->dx_dir_info_count = 0;
29 ctx->dx_dir_info_size = 100; /* Guess */
30 ctx->dx_dir_info = (struct dx_dir_info *)
31 e2fsck_allocate_memory(ctx, ctx->dx_dir_info_size
32 * sizeof (struct dx_dir_info),
33 "directory map");
34 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040035
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040036 if (ctx->dx_dir_info_count >= ctx->dx_dir_info_size) {
37 old_size = ctx->dx_dir_info_size * sizeof(struct dx_dir_info);
38 ctx->dx_dir_info_size += 10;
39 retval = ext2fs_resize_mem(old_size, ctx->dx_dir_info_size *
40 sizeof(struct dx_dir_info),
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040041 &ctx->dx_dir_info);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040042 if (retval) {
43 ctx->dx_dir_info_size -= 10;
44 return;
45 }
46 }
47
48 /*
49 * Normally, add_dx_dir_info is called with each inode in
50 * sequential order; but once in a while (like when pass 3
51 * needs to recreate the root directory or lost+found
52 * directory) it is called out of order. In those cases, we
53 * need to move the dx_dir_info entries down to make room, since
54 * the dx_dir_info array needs to be sorted by inode number for
55 * get_dx_dir_info()'s sake.
56 */
57 if (ctx->dx_dir_info_count &&
58 ctx->dx_dir_info[ctx->dx_dir_info_count-1].ino >= ino) {
59 for (i = ctx->dx_dir_info_count-1; i > 0; i--)
60 if (ctx->dx_dir_info[i-1].ino < ino)
61 break;
62 dir = &ctx->dx_dir_info[i];
Theodore Ts'oefc6f622008-08-27 23:07:54 -040063 if (dir->ino != ino)
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040064 for (j = ctx->dx_dir_info_count++; j > i; j--)
65 ctx->dx_dir_info[j] = ctx->dx_dir_info[j-1];
66 } else
67 dir = &ctx->dx_dir_info[ctx->dx_dir_info_count++];
Theodore Ts'oefc6f622008-08-27 23:07:54 -040068
Theodore Ts'o8fdc9982002-06-25 23:26:34 -040069 dir->ino = ino;
70 dir->numblocks = num_blocks;
71 dir->hashversion = 0;
72 dir->dx_block = e2fsck_allocate_memory(ctx, num_blocks
73 * sizeof (struct dx_dirblock_info),
74 "dx_block info array");
75
76}
77
78/*
79 * get_dx_dir_info() --- given an inode number, try to find the directory
80 * information entry for it.
81 */
82struct dx_dir_info *e2fsck_get_dx_dir_info(e2fsck_t ctx, ext2_ino_t ino)
83{
84 int low, high, mid;
85
86 low = 0;
87 high = ctx->dx_dir_info_count-1;
88 if (!ctx->dx_dir_info)
89 return 0;
90 if (ino == ctx->dx_dir_info[low].ino)
91 return &ctx->dx_dir_info[low];
92 if (ino == ctx->dx_dir_info[high].ino)
93 return &ctx->dx_dir_info[high];
94
95 while (low < high) {
96 mid = (low+high)/2;
97 if (mid == low || mid == high)
98 break;
99 if (ino == ctx->dx_dir_info[mid].ino)
100 return &ctx->dx_dir_info[mid];
101 if (ino < ctx->dx_dir_info[mid].ino)
102 high = mid;
103 else
104 low = mid;
105 }
106 return 0;
107}
108
109/*
110 * Free the dx_dir_info structure when it isn't needed any more.
111 */
112void e2fsck_free_dx_dir_info(e2fsck_t ctx)
113{
114 int i;
115 struct dx_dir_info *dir;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400116
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400117 if (ctx->dx_dir_info) {
118 dir = ctx->dx_dir_info;
Theodore Ts'o23f75f62009-06-15 03:50:07 -0400119 for (i=0; i < ctx->dx_dir_info_count; i++,dir++) {
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400120 if (dir->dx_block) {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400121 ext2fs_free_mem(&dir->dx_block);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400122 dir->dx_block = 0;
123 }
124 }
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400125 ext2fs_free_mem(&ctx->dx_dir_info);
Theodore Ts'o8fdc9982002-06-25 23:26:34 -0400126 ctx->dx_dir_info = 0;
127 }
128 ctx->dx_dir_info_size = 0;
129 ctx->dx_dir_info_count = 0;
130}
131
132/*
133 * Return the count of number of directories in the dx_dir_info structure
134 */
135int e2fsck_get_num_dx_dirinfo(e2fsck_t ctx)
136{
137 return ctx->dx_dir_info_count;
138}
139
140/*
141 * A simple interator function
142 */
143struct dx_dir_info *e2fsck_dx_dir_info_iter(e2fsck_t ctx, int *control)
144{
145 if (*control >= ctx->dx_dir_info_count)
146 return 0;
147
148 return(ctx->dx_dir_info + (*control)++);
149}
150
151#endif /* ENABLE_HTREE */