xref: /aosp_15_r20/external/e2fsprogs/e2fsck/dirinfo.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * dirinfo.c --- maintains the directory information table for e2fsck.
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
5*6a54128fSAndroid Build Coastguard Worker  * under the terms of the GNU Public License.
6*6a54128fSAndroid Build Coastguard Worker  */
7*6a54128fSAndroid Build Coastguard Worker 
8*6a54128fSAndroid Build Coastguard Worker #undef DIRINFO_DEBUG
9*6a54128fSAndroid Build Coastguard Worker 
10*6a54128fSAndroid Build Coastguard Worker #include "config.h"
11*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
12*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
13*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
14*6a54128fSAndroid Build Coastguard Worker #include "uuid/uuid.h"
15*6a54128fSAndroid Build Coastguard Worker 
16*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2fs.h"
17*6a54128fSAndroid Build Coastguard Worker #include <ext2fs/tdb.h>
18*6a54128fSAndroid Build Coastguard Worker 
19*6a54128fSAndroid Build Coastguard Worker struct dir_info_db {
20*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	count;
21*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	size;
22*6a54128fSAndroid Build Coastguard Worker 	struct dir_info *array;
23*6a54128fSAndroid Build Coastguard Worker 	struct dir_info *last_lookup;
24*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
25*6a54128fSAndroid Build Coastguard Worker 	char		*tdb_fn;
26*6a54128fSAndroid Build Coastguard Worker 	TDB_CONTEXT	*tdb;
27*6a54128fSAndroid Build Coastguard Worker #endif
28*6a54128fSAndroid Build Coastguard Worker };
29*6a54128fSAndroid Build Coastguard Worker 
30*6a54128fSAndroid Build Coastguard Worker struct dir_info_iter {
31*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	i;
32*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
33*6a54128fSAndroid Build Coastguard Worker 	TDB_DATA	tdb_iter;
34*6a54128fSAndroid Build Coastguard Worker #endif
35*6a54128fSAndroid Build Coastguard Worker };
36*6a54128fSAndroid Build Coastguard Worker 
37*6a54128fSAndroid Build Coastguard Worker struct dir_info_ent {
38*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t		dotdot;	/* Parent according to '..' */
39*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t		parent; /* Parent according to treewalk */
40*6a54128fSAndroid Build Coastguard Worker };
41*6a54128fSAndroid Build Coastguard Worker 
42*6a54128fSAndroid Build Coastguard Worker 
43*6a54128fSAndroid Build Coastguard Worker static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
44*6a54128fSAndroid Build Coastguard Worker 
45*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
setup_tdb(e2fsck_t ctx,ext2_ino_t num_dirs)46*6a54128fSAndroid Build Coastguard Worker static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
47*6a54128fSAndroid Build Coastguard Worker {
48*6a54128fSAndroid Build Coastguard Worker 	struct dir_info_db	*db = ctx->dir_info;
49*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t		threshold;
50*6a54128fSAndroid Build Coastguard Worker 	errcode_t		retval;
51*6a54128fSAndroid Build Coastguard Worker 	mode_t			save_umask;
52*6a54128fSAndroid Build Coastguard Worker 	char			*tdb_dir, uuid[40];
53*6a54128fSAndroid Build Coastguard Worker 	int			fd, enable;
54*6a54128fSAndroid Build Coastguard Worker 
55*6a54128fSAndroid Build Coastguard Worker 	profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
56*6a54128fSAndroid Build Coastguard Worker 			   &tdb_dir);
57*6a54128fSAndroid Build Coastguard Worker 	profile_get_uint(ctx->profile, "scratch_files",
58*6a54128fSAndroid Build Coastguard Worker 			 "numdirs_threshold", 0, 0, &threshold);
59*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "scratch_files",
60*6a54128fSAndroid Build Coastguard Worker 			    "dirinfo", 0, 1, &enable);
61*6a54128fSAndroid Build Coastguard Worker 
62*6a54128fSAndroid Build Coastguard Worker 	if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
63*6a54128fSAndroid Build Coastguard Worker 	    (threshold && num_dirs <= threshold))
64*6a54128fSAndroid Build Coastguard Worker 		return;
65*6a54128fSAndroid Build Coastguard Worker 
66*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
67*6a54128fSAndroid Build Coastguard Worker 	if (retval)
68*6a54128fSAndroid Build Coastguard Worker 		return;
69*6a54128fSAndroid Build Coastguard Worker 
70*6a54128fSAndroid Build Coastguard Worker 	uuid_unparse(ctx->fs->super->s_uuid, uuid);
71*6a54128fSAndroid Build Coastguard Worker 	sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
72*6a54128fSAndroid Build Coastguard Worker 	save_umask = umask(077);
73*6a54128fSAndroid Build Coastguard Worker 	fd = mkstemp(db->tdb_fn);
74*6a54128fSAndroid Build Coastguard Worker 	umask(save_umask);
75*6a54128fSAndroid Build Coastguard Worker 	if (fd < 0) {
76*6a54128fSAndroid Build Coastguard Worker 		db->tdb = NULL;
77*6a54128fSAndroid Build Coastguard Worker 		return;
78*6a54128fSAndroid Build Coastguard Worker 	}
79*6a54128fSAndroid Build Coastguard Worker 
80*6a54128fSAndroid Build Coastguard Worker 	if (num_dirs < 99991)
81*6a54128fSAndroid Build Coastguard Worker 		num_dirs = 99991; /* largest 5 digit prime */
82*6a54128fSAndroid Build Coastguard Worker 
83*6a54128fSAndroid Build Coastguard Worker 	db->tdb = tdb_open(db->tdb_fn, num_dirs, TDB_NOLOCK | TDB_NOSYNC,
84*6a54128fSAndroid Build Coastguard Worker 			   O_RDWR | O_CREAT | O_TRUNC, 0600);
85*6a54128fSAndroid Build Coastguard Worker 	close(fd);
86*6a54128fSAndroid Build Coastguard Worker }
87*6a54128fSAndroid Build Coastguard Worker #endif
88*6a54128fSAndroid Build Coastguard Worker 
setup_db(e2fsck_t ctx)89*6a54128fSAndroid Build Coastguard Worker static void setup_db(e2fsck_t ctx)
90*6a54128fSAndroid Build Coastguard Worker {
91*6a54128fSAndroid Build Coastguard Worker 	struct dir_info_db	*db;
92*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t		num_dirs;
93*6a54128fSAndroid Build Coastguard Worker 	errcode_t		retval;
94*6a54128fSAndroid Build Coastguard Worker 
95*6a54128fSAndroid Build Coastguard Worker 	db = (struct dir_info_db *)
96*6a54128fSAndroid Build Coastguard Worker 		e2fsck_allocate_memory(ctx, sizeof(struct dir_info_db),
97*6a54128fSAndroid Build Coastguard Worker 				       "directory map db");
98*6a54128fSAndroid Build Coastguard Worker 	db->count = db->size = 0;
99*6a54128fSAndroid Build Coastguard Worker 	db->array = 0;
100*6a54128fSAndroid Build Coastguard Worker 
101*6a54128fSAndroid Build Coastguard Worker 	ctx->dir_info = db;
102*6a54128fSAndroid Build Coastguard Worker 
103*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
104*6a54128fSAndroid Build Coastguard Worker 	if (retval)
105*6a54128fSAndroid Build Coastguard Worker 		num_dirs = 1024;	/* Guess */
106*6a54128fSAndroid Build Coastguard Worker 
107*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
108*6a54128fSAndroid Build Coastguard Worker 	setup_tdb(ctx, num_dirs);
109*6a54128fSAndroid Build Coastguard Worker 
110*6a54128fSAndroid Build Coastguard Worker 	if (db->tdb) {
111*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
112*6a54128fSAndroid Build Coastguard Worker 		printf("Note: using tdb!\n");
113*6a54128fSAndroid Build Coastguard Worker #endif
114*6a54128fSAndroid Build Coastguard Worker 		return;
115*6a54128fSAndroid Build Coastguard Worker 	}
116*6a54128fSAndroid Build Coastguard Worker #endif
117*6a54128fSAndroid Build Coastguard Worker 
118*6a54128fSAndroid Build Coastguard Worker 	db->size = num_dirs + 10;
119*6a54128fSAndroid Build Coastguard Worker 	db->array  = (struct dir_info *)
120*6a54128fSAndroid Build Coastguard Worker 		e2fsck_allocate_memory(ctx, db->size
121*6a54128fSAndroid Build Coastguard Worker 				       * sizeof (struct dir_info),
122*6a54128fSAndroid Build Coastguard Worker 				       "directory map");
123*6a54128fSAndroid Build Coastguard Worker }
124*6a54128fSAndroid Build Coastguard Worker 
125*6a54128fSAndroid Build Coastguard Worker /*
126*6a54128fSAndroid Build Coastguard Worker  * This subroutine is called during pass1 to create a directory info
127*6a54128fSAndroid Build Coastguard Worker  * entry.  During pass1, the passed-in parent is 0; it will get filled
128*6a54128fSAndroid Build Coastguard Worker  * in during pass2.
129*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_add_dir_info(e2fsck_t ctx,ext2_ino_t ino,ext2_ino_t parent)130*6a54128fSAndroid Build Coastguard Worker void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
131*6a54128fSAndroid Build Coastguard Worker {
132*6a54128fSAndroid Build Coastguard Worker 	struct dir_info		*dir, *old_array;
133*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t		i, j;
134*6a54128fSAndroid Build Coastguard Worker 	errcode_t		retval;
135*6a54128fSAndroid Build Coastguard Worker 	unsigned long		old_size;
136*6a54128fSAndroid Build Coastguard Worker 
137*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
138*6a54128fSAndroid Build Coastguard Worker 	printf("add_dir_info for inode (%u, %u)...\n", ino, parent);
139*6a54128fSAndroid Build Coastguard Worker #endif
140*6a54128fSAndroid Build Coastguard Worker 	if (!ctx->dir_info)
141*6a54128fSAndroid Build Coastguard Worker 		setup_db(ctx);
142*6a54128fSAndroid Build Coastguard Worker 
143*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dir_info->count >= ctx->dir_info->size) {
144*6a54128fSAndroid Build Coastguard Worker 		old_size = ctx->dir_info->size * sizeof(struct dir_info);
145*6a54128fSAndroid Build Coastguard Worker 		ctx->dir_info->size += 10;
146*6a54128fSAndroid Build Coastguard Worker 		old_array = ctx->dir_info->array;
147*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
148*6a54128fSAndroid Build Coastguard Worker 					   sizeof(struct dir_info),
149*6a54128fSAndroid Build Coastguard Worker 					   &ctx->dir_info->array);
150*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
151*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, "Couldn't reallocate dir_info "
152*6a54128fSAndroid Build Coastguard Worker 				"structure to %u entries\n",
153*6a54128fSAndroid Build Coastguard Worker 				ctx->dir_info->size);
154*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, 0);
155*6a54128fSAndroid Build Coastguard Worker 			ctx->dir_info->size -= 10;
156*6a54128fSAndroid Build Coastguard Worker 			return;
157*6a54128fSAndroid Build Coastguard Worker 		}
158*6a54128fSAndroid Build Coastguard Worker 		if (old_array != ctx->dir_info->array)
159*6a54128fSAndroid Build Coastguard Worker 			ctx->dir_info->last_lookup = NULL;
160*6a54128fSAndroid Build Coastguard Worker 	}
161*6a54128fSAndroid Build Coastguard Worker 
162*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
163*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dir_info->tdb) {
164*6a54128fSAndroid Build Coastguard Worker 		struct dir_info ent;
165*6a54128fSAndroid Build Coastguard Worker 
166*6a54128fSAndroid Build Coastguard Worker 		ent.ino = ino;
167*6a54128fSAndroid Build Coastguard Worker 		ent.parent = parent;
168*6a54128fSAndroid Build Coastguard Worker 		ent.dotdot = parent;
169*6a54128fSAndroid Build Coastguard Worker 		e2fsck_put_dir_info(ctx, &ent);
170*6a54128fSAndroid Build Coastguard Worker 		return;
171*6a54128fSAndroid Build Coastguard Worker 	}
172*6a54128fSAndroid Build Coastguard Worker #endif
173*6a54128fSAndroid Build Coastguard Worker 
174*6a54128fSAndroid Build Coastguard Worker 	/*
175*6a54128fSAndroid Build Coastguard Worker 	 * Normally, add_dir_info is called with each inode in
176*6a54128fSAndroid Build Coastguard Worker 	 * sequential order; but once in a while (like when pass 3
177*6a54128fSAndroid Build Coastguard Worker 	 * needs to recreate the root directory or lost+found
178*6a54128fSAndroid Build Coastguard Worker 	 * directory) it is called out of order.  In those cases, we
179*6a54128fSAndroid Build Coastguard Worker 	 * need to move the dir_info entries down to make room, since
180*6a54128fSAndroid Build Coastguard Worker 	 * the dir_info array needs to be sorted by inode number for
181*6a54128fSAndroid Build Coastguard Worker 	 * get_dir_info()'s sake.
182*6a54128fSAndroid Build Coastguard Worker 	 */
183*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dir_info->count &&
184*6a54128fSAndroid Build Coastguard Worker 	    ctx->dir_info->array[ctx->dir_info->count-1].ino >= ino) {
185*6a54128fSAndroid Build Coastguard Worker 		for (i = ctx->dir_info->count-1; i > 0; i--)
186*6a54128fSAndroid Build Coastguard Worker 			if (ctx->dir_info->array[i-1].ino < ino)
187*6a54128fSAndroid Build Coastguard Worker 				break;
188*6a54128fSAndroid Build Coastguard Worker 		dir = &ctx->dir_info->array[i];
189*6a54128fSAndroid Build Coastguard Worker 		if (dir->ino != ino)
190*6a54128fSAndroid Build Coastguard Worker 			for (j = ctx->dir_info->count++; j > i; j--)
191*6a54128fSAndroid Build Coastguard Worker 				ctx->dir_info->array[j] = ctx->dir_info->array[j-1];
192*6a54128fSAndroid Build Coastguard Worker 	} else
193*6a54128fSAndroid Build Coastguard Worker 		dir = &ctx->dir_info->array[ctx->dir_info->count++];
194*6a54128fSAndroid Build Coastguard Worker 
195*6a54128fSAndroid Build Coastguard Worker 	dir->ino = ino;
196*6a54128fSAndroid Build Coastguard Worker 	dir->dotdot = parent;
197*6a54128fSAndroid Build Coastguard Worker 	dir->parent = parent;
198*6a54128fSAndroid Build Coastguard Worker }
199*6a54128fSAndroid Build Coastguard Worker 
200*6a54128fSAndroid Build Coastguard Worker /*
201*6a54128fSAndroid Build Coastguard Worker  * get_dir_info() --- given an inode number, try to find the directory
202*6a54128fSAndroid Build Coastguard Worker  * information entry for it.
203*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_get_dir_info(e2fsck_t ctx,ext2_ino_t ino)204*6a54128fSAndroid Build Coastguard Worker static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
205*6a54128fSAndroid Build Coastguard Worker {
206*6a54128fSAndroid Build Coastguard Worker 	struct dir_info_db	*db = ctx->dir_info;
207*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t low, high, mid;
208*6a54128fSAndroid Build Coastguard Worker 
209*6a54128fSAndroid Build Coastguard Worker 	if (!db)
210*6a54128fSAndroid Build Coastguard Worker 		return 0;
211*6a54128fSAndroid Build Coastguard Worker 
212*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
213*6a54128fSAndroid Build Coastguard Worker 	printf("e2fsck_get_dir_info %u...", ino);
214*6a54128fSAndroid Build Coastguard Worker #endif
215*6a54128fSAndroid Build Coastguard Worker 
216*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
217*6a54128fSAndroid Build Coastguard Worker 	if (db->tdb) {
218*6a54128fSAndroid Build Coastguard Worker 		static struct dir_info	ret_dir_info;
219*6a54128fSAndroid Build Coastguard Worker 		TDB_DATA key, data;
220*6a54128fSAndroid Build Coastguard Worker 		struct dir_info_ent	*buf;
221*6a54128fSAndroid Build Coastguard Worker 
222*6a54128fSAndroid Build Coastguard Worker 		key.dptr = (unsigned char *) &ino;
223*6a54128fSAndroid Build Coastguard Worker 		key.dsize = sizeof(ext2_ino_t);
224*6a54128fSAndroid Build Coastguard Worker 
225*6a54128fSAndroid Build Coastguard Worker 		data = tdb_fetch(db->tdb, key);
226*6a54128fSAndroid Build Coastguard Worker 		if (!data.dptr) {
227*6a54128fSAndroid Build Coastguard Worker 			if (tdb_error(db->tdb) != TDB_ERR_NOEXIST)
228*6a54128fSAndroid Build Coastguard Worker 				printf("fetch failed: %s\n",
229*6a54128fSAndroid Build Coastguard Worker 				       tdb_errorstr(db->tdb));
230*6a54128fSAndroid Build Coastguard Worker 			return 0;
231*6a54128fSAndroid Build Coastguard Worker 		}
232*6a54128fSAndroid Build Coastguard Worker 
233*6a54128fSAndroid Build Coastguard Worker 		buf = (struct dir_info_ent *) data.dptr;
234*6a54128fSAndroid Build Coastguard Worker 		ret_dir_info.ino = ino;
235*6a54128fSAndroid Build Coastguard Worker 		ret_dir_info.dotdot = buf->dotdot;
236*6a54128fSAndroid Build Coastguard Worker 		ret_dir_info.parent = buf->parent;
237*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
238*6a54128fSAndroid Build Coastguard Worker 		printf("(%u,%u,%u)\n", ino, buf->dotdot, buf->parent);
239*6a54128fSAndroid Build Coastguard Worker #endif
240*6a54128fSAndroid Build Coastguard Worker 		free(data.dptr);
241*6a54128fSAndroid Build Coastguard Worker 		return &ret_dir_info;
242*6a54128fSAndroid Build Coastguard Worker 	}
243*6a54128fSAndroid Build Coastguard Worker #endif
244*6a54128fSAndroid Build Coastguard Worker 
245*6a54128fSAndroid Build Coastguard Worker 	if (db->last_lookup && db->last_lookup->ino == ino)
246*6a54128fSAndroid Build Coastguard Worker 		return db->last_lookup;
247*6a54128fSAndroid Build Coastguard Worker 
248*6a54128fSAndroid Build Coastguard Worker 	low = 0;
249*6a54128fSAndroid Build Coastguard Worker 	high = ctx->dir_info->count - 1;
250*6a54128fSAndroid Build Coastguard Worker 	if (ino == ctx->dir_info->array[low].ino) {
251*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
252*6a54128fSAndroid Build Coastguard Worker 		printf("(%u,%u,%u)\n", ino,
253*6a54128fSAndroid Build Coastguard Worker 		       ctx->dir_info->array[low].dotdot,
254*6a54128fSAndroid Build Coastguard Worker 		       ctx->dir_info->array[low].parent);
255*6a54128fSAndroid Build Coastguard Worker #endif
256*6a54128fSAndroid Build Coastguard Worker 		return &ctx->dir_info->array[low];
257*6a54128fSAndroid Build Coastguard Worker 	}
258*6a54128fSAndroid Build Coastguard Worker 	if (ino == ctx->dir_info->array[high].ino) {
259*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
260*6a54128fSAndroid Build Coastguard Worker 		printf("(%u,%u,%u)\n", ino,
261*6a54128fSAndroid Build Coastguard Worker 		       ctx->dir_info->array[high].dotdot,
262*6a54128fSAndroid Build Coastguard Worker 		       ctx->dir_info->array[high].parent);
263*6a54128fSAndroid Build Coastguard Worker #endif
264*6a54128fSAndroid Build Coastguard Worker 		return &ctx->dir_info->array[high];
265*6a54128fSAndroid Build Coastguard Worker 	}
266*6a54128fSAndroid Build Coastguard Worker 
267*6a54128fSAndroid Build Coastguard Worker 	while (low < high) {
268*6a54128fSAndroid Build Coastguard Worker 		/* sum may overflow, but result will fit into mid again */
269*6a54128fSAndroid Build Coastguard Worker 		mid = (unsigned long long)(low + high) / 2;
270*6a54128fSAndroid Build Coastguard Worker 		if (mid == low || mid == high)
271*6a54128fSAndroid Build Coastguard Worker 			break;
272*6a54128fSAndroid Build Coastguard Worker 		if (ino == ctx->dir_info->array[mid].ino) {
273*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
274*6a54128fSAndroid Build Coastguard Worker 			printf("(%u,%u,%u)\n", ino,
275*6a54128fSAndroid Build Coastguard Worker 			       ctx->dir_info->array[mid].dotdot,
276*6a54128fSAndroid Build Coastguard Worker 			       ctx->dir_info->array[mid].parent);
277*6a54128fSAndroid Build Coastguard Worker #endif
278*6a54128fSAndroid Build Coastguard Worker 			return &ctx->dir_info->array[mid];
279*6a54128fSAndroid Build Coastguard Worker 		}
280*6a54128fSAndroid Build Coastguard Worker 		if (ino < ctx->dir_info->array[mid].ino)
281*6a54128fSAndroid Build Coastguard Worker 			high = mid;
282*6a54128fSAndroid Build Coastguard Worker 		else
283*6a54128fSAndroid Build Coastguard Worker 			low = mid;
284*6a54128fSAndroid Build Coastguard Worker 	}
285*6a54128fSAndroid Build Coastguard Worker 	return 0;
286*6a54128fSAndroid Build Coastguard Worker }
287*6a54128fSAndroid Build Coastguard Worker 
e2fsck_put_dir_info(e2fsck_t ctx EXT2FS_NO_TDB_UNUSED,struct dir_info * dir EXT2FS_NO_TDB_UNUSED)288*6a54128fSAndroid Build Coastguard Worker static void e2fsck_put_dir_info(e2fsck_t ctx EXT2FS_NO_TDB_UNUSED,
289*6a54128fSAndroid Build Coastguard Worker 				struct dir_info *dir EXT2FS_NO_TDB_UNUSED)
290*6a54128fSAndroid Build Coastguard Worker {
291*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
292*6a54128fSAndroid Build Coastguard Worker 	struct dir_info_db	*db = ctx->dir_info;
293*6a54128fSAndroid Build Coastguard Worker 	struct dir_info_ent	buf;
294*6a54128fSAndroid Build Coastguard Worker 	TDB_DATA		key, data;
295*6a54128fSAndroid Build Coastguard Worker #endif
296*6a54128fSAndroid Build Coastguard Worker 
297*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
298*6a54128fSAndroid Build Coastguard Worker 	printf("e2fsck_put_dir_info (%u, %u, %u)...", dir->ino, dir->dotdot,
299*6a54128fSAndroid Build Coastguard Worker 	       dir->parent);
300*6a54128fSAndroid Build Coastguard Worker #endif
301*6a54128fSAndroid Build Coastguard Worker 
302*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
303*6a54128fSAndroid Build Coastguard Worker 	if (!db->tdb)
304*6a54128fSAndroid Build Coastguard Worker 		return;
305*6a54128fSAndroid Build Coastguard Worker 
306*6a54128fSAndroid Build Coastguard Worker 	buf.parent = dir->parent;
307*6a54128fSAndroid Build Coastguard Worker 	buf.dotdot = dir->dotdot;
308*6a54128fSAndroid Build Coastguard Worker 
309*6a54128fSAndroid Build Coastguard Worker 	key.dptr = (unsigned char *) &dir->ino;
310*6a54128fSAndroid Build Coastguard Worker 	key.dsize = sizeof(ext2_ino_t);
311*6a54128fSAndroid Build Coastguard Worker 	data.dptr = (unsigned char *) &buf;
312*6a54128fSAndroid Build Coastguard Worker 	data.dsize = sizeof(buf);
313*6a54128fSAndroid Build Coastguard Worker 
314*6a54128fSAndroid Build Coastguard Worker 	if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
315*6a54128fSAndroid Build Coastguard Worker 		printf("store failed: %s\n", tdb_errorstr(db->tdb));
316*6a54128fSAndroid Build Coastguard Worker 	}
317*6a54128fSAndroid Build Coastguard Worker #endif
318*6a54128fSAndroid Build Coastguard Worker }
319*6a54128fSAndroid Build Coastguard Worker 
320*6a54128fSAndroid Build Coastguard Worker /*
321*6a54128fSAndroid Build Coastguard Worker  * Free the dir_info structure when it isn't needed any more.
322*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_free_dir_info(e2fsck_t ctx)323*6a54128fSAndroid Build Coastguard Worker void e2fsck_free_dir_info(e2fsck_t ctx)
324*6a54128fSAndroid Build Coastguard Worker {
325*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dir_info) {
326*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
327*6a54128fSAndroid Build Coastguard Worker 		if (ctx->dir_info->tdb)
328*6a54128fSAndroid Build Coastguard Worker 			tdb_close(ctx->dir_info->tdb);
329*6a54128fSAndroid Build Coastguard Worker 		if (ctx->dir_info->tdb_fn) {
330*6a54128fSAndroid Build Coastguard Worker 			if (unlink(ctx->dir_info->tdb_fn) < 0)
331*6a54128fSAndroid Build Coastguard Worker 				com_err("e2fsck_free_dir_info", errno,
332*6a54128fSAndroid Build Coastguard Worker 					_("while freeing dir_info tdb file"));
333*6a54128fSAndroid Build Coastguard Worker 			ext2fs_free_mem(&ctx->dir_info->tdb_fn);
334*6a54128fSAndroid Build Coastguard Worker 		}
335*6a54128fSAndroid Build Coastguard Worker #endif
336*6a54128fSAndroid Build Coastguard Worker 		if (ctx->dir_info->array)
337*6a54128fSAndroid Build Coastguard Worker 			ext2fs_free_mem(&ctx->dir_info->array);
338*6a54128fSAndroid Build Coastguard Worker 		ctx->dir_info->array = 0;
339*6a54128fSAndroid Build Coastguard Worker 		ctx->dir_info->size = 0;
340*6a54128fSAndroid Build Coastguard Worker 		ctx->dir_info->count = 0;
341*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&ctx->dir_info);
342*6a54128fSAndroid Build Coastguard Worker 		ctx->dir_info = 0;
343*6a54128fSAndroid Build Coastguard Worker 	}
344*6a54128fSAndroid Build Coastguard Worker }
345*6a54128fSAndroid Build Coastguard Worker 
346*6a54128fSAndroid Build Coastguard Worker /*
347*6a54128fSAndroid Build Coastguard Worker  * Return the count of number of directories in the dir_info structure
348*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_get_num_dirinfo(e2fsck_t ctx)349*6a54128fSAndroid Build Coastguard Worker int e2fsck_get_num_dirinfo(e2fsck_t ctx)
350*6a54128fSAndroid Build Coastguard Worker {
351*6a54128fSAndroid Build Coastguard Worker 	return ctx->dir_info ? ctx->dir_info->count : 0;
352*6a54128fSAndroid Build Coastguard Worker }
353*6a54128fSAndroid Build Coastguard Worker 
e2fsck_dir_info_iter_begin(e2fsck_t ctx)354*6a54128fSAndroid Build Coastguard Worker struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
355*6a54128fSAndroid Build Coastguard Worker {
356*6a54128fSAndroid Build Coastguard Worker 	struct dir_info_iter *iter;
357*6a54128fSAndroid Build Coastguard Worker 
358*6a54128fSAndroid Build Coastguard Worker 	iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
359*6a54128fSAndroid Build Coastguard Worker 				      "dir_info iterator");
360*6a54128fSAndroid Build Coastguard Worker 
361*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
362*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dir_info->tdb)
363*6a54128fSAndroid Build Coastguard Worker 		iter->tdb_iter = tdb_firstkey(ctx->dir_info->tdb);
364*6a54128fSAndroid Build Coastguard Worker #endif
365*6a54128fSAndroid Build Coastguard Worker 
366*6a54128fSAndroid Build Coastguard Worker 	return iter;
367*6a54128fSAndroid Build Coastguard Worker }
368*6a54128fSAndroid Build Coastguard Worker 
e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR ((unused)),struct dir_info_iter * iter)369*6a54128fSAndroid Build Coastguard Worker void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
370*6a54128fSAndroid Build Coastguard Worker 			      struct dir_info_iter *iter)
371*6a54128fSAndroid Build Coastguard Worker {
372*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
373*6a54128fSAndroid Build Coastguard Worker 	free(iter->tdb_iter.dptr);
374*6a54128fSAndroid Build Coastguard Worker #endif
375*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&iter);
376*6a54128fSAndroid Build Coastguard Worker }
377*6a54128fSAndroid Build Coastguard Worker 
378*6a54128fSAndroid Build Coastguard Worker /*
379*6a54128fSAndroid Build Coastguard Worker  * A simple iterator function
380*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_dir_info_iter(e2fsck_t ctx,struct dir_info_iter * iter)381*6a54128fSAndroid Build Coastguard Worker struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
382*6a54128fSAndroid Build Coastguard Worker {
383*6a54128fSAndroid Build Coastguard Worker 	if (!ctx->dir_info || !iter)
384*6a54128fSAndroid Build Coastguard Worker 		return 0;
385*6a54128fSAndroid Build Coastguard Worker 
386*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TDB
387*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dir_info->tdb) {
388*6a54128fSAndroid Build Coastguard Worker 		static struct dir_info ret_dir_info;
389*6a54128fSAndroid Build Coastguard Worker 		struct dir_info_ent *buf;
390*6a54128fSAndroid Build Coastguard Worker 		TDB_DATA data, key;
391*6a54128fSAndroid Build Coastguard Worker 
392*6a54128fSAndroid Build Coastguard Worker 		if (iter->tdb_iter.dptr == 0)
393*6a54128fSAndroid Build Coastguard Worker 			return 0;
394*6a54128fSAndroid Build Coastguard Worker 		key = iter->tdb_iter;
395*6a54128fSAndroid Build Coastguard Worker 		data = tdb_fetch(ctx->dir_info->tdb, key);
396*6a54128fSAndroid Build Coastguard Worker 		if (!data.dptr) {
397*6a54128fSAndroid Build Coastguard Worker 			printf("iter fetch failed: %s\n",
398*6a54128fSAndroid Build Coastguard Worker 			       tdb_errorstr(ctx->dir_info->tdb));
399*6a54128fSAndroid Build Coastguard Worker 			return 0;
400*6a54128fSAndroid Build Coastguard Worker 		}
401*6a54128fSAndroid Build Coastguard Worker 		buf = (struct dir_info_ent *) data.dptr;
402*6a54128fSAndroid Build Coastguard Worker 		ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
403*6a54128fSAndroid Build Coastguard Worker 		ret_dir_info.dotdot = buf->dotdot;
404*6a54128fSAndroid Build Coastguard Worker 		ret_dir_info.parent = buf->parent;
405*6a54128fSAndroid Build Coastguard Worker 		iter->tdb_iter = tdb_nextkey(ctx->dir_info->tdb, key);
406*6a54128fSAndroid Build Coastguard Worker 		free(key.dptr);
407*6a54128fSAndroid Build Coastguard Worker 		free(data.dptr);
408*6a54128fSAndroid Build Coastguard Worker 		return &ret_dir_info;
409*6a54128fSAndroid Build Coastguard Worker 	}
410*6a54128fSAndroid Build Coastguard Worker #endif
411*6a54128fSAndroid Build Coastguard Worker 
412*6a54128fSAndroid Build Coastguard Worker 	if (iter->i >= ctx->dir_info->count)
413*6a54128fSAndroid Build Coastguard Worker 		return 0;
414*6a54128fSAndroid Build Coastguard Worker 
415*6a54128fSAndroid Build Coastguard Worker #ifdef DIRINFO_DEBUG
416*6a54128fSAndroid Build Coastguard Worker 	printf("iter(%u, %u, %u)...", ctx->dir_info->array[iter->i].ino,
417*6a54128fSAndroid Build Coastguard Worker 	       ctx->dir_info->array[iter->i].dotdot,
418*6a54128fSAndroid Build Coastguard Worker 	       ctx->dir_info->array[iter->i].parent);
419*6a54128fSAndroid Build Coastguard Worker #endif
420*6a54128fSAndroid Build Coastguard Worker 	ctx->dir_info->last_lookup = ctx->dir_info->array + iter->i++;
421*6a54128fSAndroid Build Coastguard Worker 	return(ctx->dir_info->last_lookup);
422*6a54128fSAndroid Build Coastguard Worker }
423*6a54128fSAndroid Build Coastguard Worker 
424*6a54128fSAndroid Build Coastguard Worker /*
425*6a54128fSAndroid Build Coastguard Worker  * This function only sets the parent pointer, and requires that
426*6a54128fSAndroid Build Coastguard Worker  * dirinfo structure has already been created.
427*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_dir_info_set_parent(e2fsck_t ctx,ext2_ino_t ino,ext2_ino_t parent)428*6a54128fSAndroid Build Coastguard Worker int e2fsck_dir_info_set_parent(e2fsck_t ctx, ext2_ino_t ino,
429*6a54128fSAndroid Build Coastguard Worker 			       ext2_ino_t parent)
430*6a54128fSAndroid Build Coastguard Worker {
431*6a54128fSAndroid Build Coastguard Worker 	struct dir_info *p;
432*6a54128fSAndroid Build Coastguard Worker 
433*6a54128fSAndroid Build Coastguard Worker 	p = e2fsck_get_dir_info(ctx, ino);
434*6a54128fSAndroid Build Coastguard Worker 	if (!p)
435*6a54128fSAndroid Build Coastguard Worker 		return 1;
436*6a54128fSAndroid Build Coastguard Worker 	p->parent = parent;
437*6a54128fSAndroid Build Coastguard Worker 	e2fsck_put_dir_info(ctx, p);
438*6a54128fSAndroid Build Coastguard Worker 	return 0;
439*6a54128fSAndroid Build Coastguard Worker }
440*6a54128fSAndroid Build Coastguard Worker 
441*6a54128fSAndroid Build Coastguard Worker /*
442*6a54128fSAndroid Build Coastguard Worker  * This function only sets the dot dot pointer, and requires that
443*6a54128fSAndroid Build Coastguard Worker  * dirinfo structure has already been created.
444*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_dir_info_set_dotdot(e2fsck_t ctx,ext2_ino_t ino,ext2_ino_t dotdot)445*6a54128fSAndroid Build Coastguard Worker int e2fsck_dir_info_set_dotdot(e2fsck_t ctx, ext2_ino_t ino,
446*6a54128fSAndroid Build Coastguard Worker 			       ext2_ino_t dotdot)
447*6a54128fSAndroid Build Coastguard Worker {
448*6a54128fSAndroid Build Coastguard Worker 	struct dir_info *p;
449*6a54128fSAndroid Build Coastguard Worker 
450*6a54128fSAndroid Build Coastguard Worker 	p = e2fsck_get_dir_info(ctx, ino);
451*6a54128fSAndroid Build Coastguard Worker 	if (!p)
452*6a54128fSAndroid Build Coastguard Worker 		return 1;
453*6a54128fSAndroid Build Coastguard Worker 	p->dotdot = dotdot;
454*6a54128fSAndroid Build Coastguard Worker 	e2fsck_put_dir_info(ctx, p);
455*6a54128fSAndroid Build Coastguard Worker 	return 0;
456*6a54128fSAndroid Build Coastguard Worker }
457*6a54128fSAndroid Build Coastguard Worker 
458*6a54128fSAndroid Build Coastguard Worker /*
459*6a54128fSAndroid Build Coastguard Worker  * This function only sets the parent pointer, and requires that
460*6a54128fSAndroid Build Coastguard Worker  * dirinfo structure has already been created.
461*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_dir_info_get_parent(e2fsck_t ctx,ext2_ino_t ino,ext2_ino_t * parent)462*6a54128fSAndroid Build Coastguard Worker int e2fsck_dir_info_get_parent(e2fsck_t ctx, ext2_ino_t ino,
463*6a54128fSAndroid Build Coastguard Worker 			       ext2_ino_t *parent)
464*6a54128fSAndroid Build Coastguard Worker {
465*6a54128fSAndroid Build Coastguard Worker 	struct dir_info *p;
466*6a54128fSAndroid Build Coastguard Worker 
467*6a54128fSAndroid Build Coastguard Worker 	p = e2fsck_get_dir_info(ctx, ino);
468*6a54128fSAndroid Build Coastguard Worker 	if (!p)
469*6a54128fSAndroid Build Coastguard Worker 		return 1;
470*6a54128fSAndroid Build Coastguard Worker 	*parent = p->parent;
471*6a54128fSAndroid Build Coastguard Worker 	return 0;
472*6a54128fSAndroid Build Coastguard Worker }
473*6a54128fSAndroid Build Coastguard Worker 
474*6a54128fSAndroid Build Coastguard Worker /*
475*6a54128fSAndroid Build Coastguard Worker  * This function only sets the dot dot pointer, and requires that
476*6a54128fSAndroid Build Coastguard Worker  * dirinfo structure has already been created.
477*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_dir_info_get_dotdot(e2fsck_t ctx,ext2_ino_t ino,ext2_ino_t * dotdot)478*6a54128fSAndroid Build Coastguard Worker int e2fsck_dir_info_get_dotdot(e2fsck_t ctx, ext2_ino_t ino,
479*6a54128fSAndroid Build Coastguard Worker 			       ext2_ino_t *dotdot)
480*6a54128fSAndroid Build Coastguard Worker {
481*6a54128fSAndroid Build Coastguard Worker 	struct dir_info *p;
482*6a54128fSAndroid Build Coastguard Worker 
483*6a54128fSAndroid Build Coastguard Worker 	p = e2fsck_get_dir_info(ctx, ino);
484*6a54128fSAndroid Build Coastguard Worker 	if (!p)
485*6a54128fSAndroid Build Coastguard Worker 		return 1;
486*6a54128fSAndroid Build Coastguard Worker 	*dotdot = p->dotdot;
487*6a54128fSAndroid Build Coastguard Worker 	return 0;
488*6a54128fSAndroid Build Coastguard Worker }
489*6a54128fSAndroid Build Coastguard Worker 
490