blob: 7838a406f610f20a53dfe0bad35e9fad6b81aed9 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
8#include "config.h"
9#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
15 * in during pass2.
16 */
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 }
35
36 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),
41 &ctx->dx_dir_info);
42 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];
63 if (dir->ino != ino)
64 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++];
68
69 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;
116
117 if (ctx->dx_dir_info) {
118 dir = ctx->dx_dir_info;
119 for (i=0; i < ctx->dx_dir_info_count; i++,dir++) {
120 if (dir->dx_block) {
121 ext2fs_free_mem(&dir->dx_block);
122 dir->dx_block = 0;
123 }
124 }
125 ext2fs_free_mem(&ctx->dx_dir_info);
126 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 */