1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * rehash.c --- rebuild hash tree directories
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 2002 Theodore Ts'o
5*6a54128fSAndroid Build Coastguard Worker *
6*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Public
8*6a54128fSAndroid Build Coastguard Worker * License.
9*6a54128fSAndroid Build Coastguard Worker * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker *
11*6a54128fSAndroid Build Coastguard Worker * This algorithm is designed for simplicity of implementation and to
12*6a54128fSAndroid Build Coastguard Worker * pack the directory as much as possible. It however requires twice
13*6a54128fSAndroid Build Coastguard Worker * as much memory as the size of the directory. The maximum size
14*6a54128fSAndroid Build Coastguard Worker * directory supported using a 4k blocksize is roughly a gigabyte, and
15*6a54128fSAndroid Build Coastguard Worker * so there may very well be problems with machines that don't have
16*6a54128fSAndroid Build Coastguard Worker * virtual memory, and obscenely large directories.
17*6a54128fSAndroid Build Coastguard Worker *
18*6a54128fSAndroid Build Coastguard Worker * An alternate algorithm which is much more disk intensive could be
19*6a54128fSAndroid Build Coastguard Worker * written, and probably will need to be written in the future. The
20*6a54128fSAndroid Build Coastguard Worker * design goals of such an algorithm are: (a) use (roughly) constant
21*6a54128fSAndroid Build Coastguard Worker * amounts of memory, no matter how large the directory, (b) the
22*6a54128fSAndroid Build Coastguard Worker * directory must be safe at all times, even if e2fsck is interrupted
23*6a54128fSAndroid Build Coastguard Worker * in the middle, (c) we must use minimal amounts of extra disk
24*6a54128fSAndroid Build Coastguard Worker * blocks. This pretty much requires an incremental approach, where
25*6a54128fSAndroid Build Coastguard Worker * we are reading from one part of the directory, and inserting into
26*6a54128fSAndroid Build Coastguard Worker * the front half. So the algorithm will have to keep track of a
27*6a54128fSAndroid Build Coastguard Worker * moving block boundary between the new tree and the old tree, and
28*6a54128fSAndroid Build Coastguard Worker * files will need to be moved from the old directory and inserted
29*6a54128fSAndroid Build Coastguard Worker * into the new tree. If the new directory requires space which isn't
30*6a54128fSAndroid Build Coastguard Worker * yet available, blocks from the beginning part of the old directory
31*6a54128fSAndroid Build Coastguard Worker * may need to be moved to the end of the directory to make room for
32*6a54128fSAndroid Build Coastguard Worker * the new tree:
33*6a54128fSAndroid Build Coastguard Worker *
34*6a54128fSAndroid Build Coastguard Worker * --------------------------------------------------------
35*6a54128fSAndroid Build Coastguard Worker * | new tree | | old tree |
36*6a54128fSAndroid Build Coastguard Worker * --------------------------------------------------------
37*6a54128fSAndroid Build Coastguard Worker * ^ ptr ^ptr
38*6a54128fSAndroid Build Coastguard Worker * tail new head old
39*6a54128fSAndroid Build Coastguard Worker *
40*6a54128fSAndroid Build Coastguard Worker * This is going to be a pain in the tuckus to implement, and will
41*6a54128fSAndroid Build Coastguard Worker * require a lot more disk accesses. So I'm going to skip it for now;
42*6a54128fSAndroid Build Coastguard Worker * it's only really going to be an issue for really, really big
43*6a54128fSAndroid Build Coastguard Worker * filesystems (when we reach the level of tens of millions of files
44*6a54128fSAndroid Build Coastguard Worker * in a single directory). It will probably be easier to simply
45*6a54128fSAndroid Build Coastguard Worker * require that e2fsck use VM first.
46*6a54128fSAndroid Build Coastguard Worker */
47*6a54128fSAndroid Build Coastguard Worker
48*6a54128fSAndroid Build Coastguard Worker #include "config.h"
49*6a54128fSAndroid Build Coastguard Worker #include <string.h>
50*6a54128fSAndroid Build Coastguard Worker #include <ctype.h>
51*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
52*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
53*6a54128fSAndroid Build Coastguard Worker #include "problem.h"
54*6a54128fSAndroid Build Coastguard Worker #include "support/sort_r.h"
55*6a54128fSAndroid Build Coastguard Worker
56*6a54128fSAndroid Build Coastguard Worker /* Schedule a dir to be rebuilt during pass 3A. */
e2fsck_rehash_dir_later(e2fsck_t ctx,ext2_ino_t ino)57*6a54128fSAndroid Build Coastguard Worker void e2fsck_rehash_dir_later(e2fsck_t ctx, ext2_ino_t ino)
58*6a54128fSAndroid Build Coastguard Worker {
59*6a54128fSAndroid Build Coastguard Worker if (!ctx->dirs_to_hash)
60*6a54128fSAndroid Build Coastguard Worker ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
61*6a54128fSAndroid Build Coastguard Worker if (ctx->dirs_to_hash)
62*6a54128fSAndroid Build Coastguard Worker ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
63*6a54128fSAndroid Build Coastguard Worker }
64*6a54128fSAndroid Build Coastguard Worker
65*6a54128fSAndroid Build Coastguard Worker /* Ask if a dir will be rebuilt during pass 3A. */
e2fsck_dir_will_be_rehashed(e2fsck_t ctx,ext2_ino_t ino)66*6a54128fSAndroid Build Coastguard Worker int e2fsck_dir_will_be_rehashed(e2fsck_t ctx, ext2_ino_t ino)
67*6a54128fSAndroid Build Coastguard Worker {
68*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_COMPRESS_DIRS)
69*6a54128fSAndroid Build Coastguard Worker return 1;
70*6a54128fSAndroid Build Coastguard Worker if (!ctx->dirs_to_hash)
71*6a54128fSAndroid Build Coastguard Worker return 0;
72*6a54128fSAndroid Build Coastguard Worker return ext2fs_u32_list_test(ctx->dirs_to_hash, ino);
73*6a54128fSAndroid Build Coastguard Worker }
74*6a54128fSAndroid Build Coastguard Worker
75*6a54128fSAndroid Build Coastguard Worker #undef REHASH_DEBUG
76*6a54128fSAndroid Build Coastguard Worker
77*6a54128fSAndroid Build Coastguard Worker struct fill_dir_struct {
78*6a54128fSAndroid Build Coastguard Worker char *buf;
79*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode;
80*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
81*6a54128fSAndroid Build Coastguard Worker errcode_t err;
82*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
83*6a54128fSAndroid Build Coastguard Worker struct hash_entry *harray;
84*6a54128fSAndroid Build Coastguard Worker blk_t max_array, num_array;
85*6a54128fSAndroid Build Coastguard Worker ext2_off64_t dir_size;
86*6a54128fSAndroid Build Coastguard Worker int compress;
87*6a54128fSAndroid Build Coastguard Worker ext2_ino_t parent;
88*6a54128fSAndroid Build Coastguard Worker ext2_ino_t dir;
89*6a54128fSAndroid Build Coastguard Worker };
90*6a54128fSAndroid Build Coastguard Worker
91*6a54128fSAndroid Build Coastguard Worker struct hash_entry {
92*6a54128fSAndroid Build Coastguard Worker ext2_dirhash_t hash;
93*6a54128fSAndroid Build Coastguard Worker ext2_dirhash_t minor_hash;
94*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
95*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry *dir;
96*6a54128fSAndroid Build Coastguard Worker };
97*6a54128fSAndroid Build Coastguard Worker
98*6a54128fSAndroid Build Coastguard Worker struct out_dir {
99*6a54128fSAndroid Build Coastguard Worker blk_t num;
100*6a54128fSAndroid Build Coastguard Worker blk_t max;
101*6a54128fSAndroid Build Coastguard Worker char *buf;
102*6a54128fSAndroid Build Coastguard Worker ext2_dirhash_t *hashes;
103*6a54128fSAndroid Build Coastguard Worker };
104*6a54128fSAndroid Build Coastguard Worker
105*6a54128fSAndroid Build Coastguard Worker #define DOTDOT_OFFSET 12
106*6a54128fSAndroid Build Coastguard Worker
is_fake_entry(ext2_filsys fs,int lblk,unsigned int offset)107*6a54128fSAndroid Build Coastguard Worker static int is_fake_entry(ext2_filsys fs, int lblk, unsigned int offset)
108*6a54128fSAndroid Build Coastguard Worker {
109*6a54128fSAndroid Build Coastguard Worker /* Entries in the first block before this value refer to . or .. */
110*6a54128fSAndroid Build Coastguard Worker if (lblk == 0 && offset <= DOTDOT_OFFSET)
111*6a54128fSAndroid Build Coastguard Worker return 1;
112*6a54128fSAndroid Build Coastguard Worker /* Check if this is likely the csum entry */
113*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_metadata_csum(fs->super) &&
114*6a54128fSAndroid Build Coastguard Worker (offset & (fs->blocksize - 1)) ==
115*6a54128fSAndroid Build Coastguard Worker fs->blocksize - sizeof(struct ext2_dir_entry_tail))
116*6a54128fSAndroid Build Coastguard Worker return 1;
117*6a54128fSAndroid Build Coastguard Worker return 0;
118*6a54128fSAndroid Build Coastguard Worker }
119*6a54128fSAndroid Build Coastguard Worker
fill_dir_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)120*6a54128fSAndroid Build Coastguard Worker static int fill_dir_block(ext2_filsys fs,
121*6a54128fSAndroid Build Coastguard Worker blk64_t *block_nr,
122*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t blockcnt,
123*6a54128fSAndroid Build Coastguard Worker blk64_t ref_block EXT2FS_ATTR((unused)),
124*6a54128fSAndroid Build Coastguard Worker int ref_offset EXT2FS_ATTR((unused)),
125*6a54128fSAndroid Build Coastguard Worker void *priv_data)
126*6a54128fSAndroid Build Coastguard Worker {
127*6a54128fSAndroid Build Coastguard Worker struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
128*6a54128fSAndroid Build Coastguard Worker struct hash_entry *ent;
129*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry *dirent;
130*6a54128fSAndroid Build Coastguard Worker char *dir;
131*6a54128fSAndroid Build Coastguard Worker unsigned int offset, dir_offset, rec_len, name_len;
132*6a54128fSAndroid Build Coastguard Worker int hash_alg, hash_flags, hash_in_entry;
133*6a54128fSAndroid Build Coastguard Worker
134*6a54128fSAndroid Build Coastguard Worker if (blockcnt < 0)
135*6a54128fSAndroid Build Coastguard Worker return 0;
136*6a54128fSAndroid Build Coastguard Worker
137*6a54128fSAndroid Build Coastguard Worker offset = blockcnt * fs->blocksize;
138*6a54128fSAndroid Build Coastguard Worker if (offset + fs->blocksize > fd->inode->i_size) {
139*6a54128fSAndroid Build Coastguard Worker fd->err = EXT2_ET_DIR_CORRUPTED;
140*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
141*6a54128fSAndroid Build Coastguard Worker }
142*6a54128fSAndroid Build Coastguard Worker
143*6a54128fSAndroid Build Coastguard Worker dir = (fd->buf+offset);
144*6a54128fSAndroid Build Coastguard Worker if (*block_nr == 0) {
145*6a54128fSAndroid Build Coastguard Worker memset(dir, 0, fs->blocksize);
146*6a54128fSAndroid Build Coastguard Worker dirent = (struct ext2_dir_entry *) dir;
147*6a54128fSAndroid Build Coastguard Worker (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
148*6a54128fSAndroid Build Coastguard Worker } else {
149*6a54128fSAndroid Build Coastguard Worker int flags = fs->flags;
150*6a54128fSAndroid Build Coastguard Worker fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
151*6a54128fSAndroid Build Coastguard Worker fd->err = ext2fs_read_dir_block4(fs, *block_nr, dir, 0,
152*6a54128fSAndroid Build Coastguard Worker fd->dir);
153*6a54128fSAndroid Build Coastguard Worker fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
154*6a54128fSAndroid Build Coastguard Worker (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
155*6a54128fSAndroid Build Coastguard Worker if (fd->err)
156*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
157*6a54128fSAndroid Build Coastguard Worker }
158*6a54128fSAndroid Build Coastguard Worker hash_flags = fd->inode->i_flags & EXT4_CASEFOLD_FL;
159*6a54128fSAndroid Build Coastguard Worker hash_in_entry = ext4_hash_in_dirent(fd->inode);
160*6a54128fSAndroid Build Coastguard Worker hash_alg = fs->super->s_def_hash_version;
161*6a54128fSAndroid Build Coastguard Worker if ((hash_alg <= EXT2_HASH_TEA) &&
162*6a54128fSAndroid Build Coastguard Worker (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
163*6a54128fSAndroid Build Coastguard Worker hash_alg += 3;
164*6a54128fSAndroid Build Coastguard Worker /* While the directory block is "hot", index it. */
165*6a54128fSAndroid Build Coastguard Worker dir_offset = 0;
166*6a54128fSAndroid Build Coastguard Worker while (dir_offset < fs->blocksize) {
167*6a54128fSAndroid Build Coastguard Worker unsigned int min_rec = EXT2_DIR_ENTRY_HEADER_LEN;
168*6a54128fSAndroid Build Coastguard Worker int extended = hash_in_entry && !is_fake_entry(fs, blockcnt, dir_offset);
169*6a54128fSAndroid Build Coastguard Worker
170*6a54128fSAndroid Build Coastguard Worker if (extended)
171*6a54128fSAndroid Build Coastguard Worker min_rec += EXT2_DIR_ENTRY_HASH_LEN;
172*6a54128fSAndroid Build Coastguard Worker dirent = (struct ext2_dir_entry *) (dir + dir_offset);
173*6a54128fSAndroid Build Coastguard Worker (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
174*6a54128fSAndroid Build Coastguard Worker name_len = ext2fs_dirent_name_len(dirent);
175*6a54128fSAndroid Build Coastguard Worker if (((dir_offset + rec_len) > fs->blocksize) ||
176*6a54128fSAndroid Build Coastguard Worker (rec_len < min_rec) ||
177*6a54128fSAndroid Build Coastguard Worker ((rec_len % 4) != 0) ||
178*6a54128fSAndroid Build Coastguard Worker (name_len + min_rec > rec_len)) {
179*6a54128fSAndroid Build Coastguard Worker fd->err = EXT2_ET_DIR_CORRUPTED;
180*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
181*6a54128fSAndroid Build Coastguard Worker }
182*6a54128fSAndroid Build Coastguard Worker dir_offset += rec_len;
183*6a54128fSAndroid Build Coastguard Worker if (dirent->inode == 0)
184*6a54128fSAndroid Build Coastguard Worker continue;
185*6a54128fSAndroid Build Coastguard Worker if ((name_len) == 0) {
186*6a54128fSAndroid Build Coastguard Worker fd->err = EXT2_ET_DIR_CORRUPTED;
187*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
188*6a54128fSAndroid Build Coastguard Worker }
189*6a54128fSAndroid Build Coastguard Worker if (!fd->compress && (name_len == 1) &&
190*6a54128fSAndroid Build Coastguard Worker (dirent->name[0] == '.'))
191*6a54128fSAndroid Build Coastguard Worker continue;
192*6a54128fSAndroid Build Coastguard Worker if (!fd->compress && (name_len == 2) &&
193*6a54128fSAndroid Build Coastguard Worker (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
194*6a54128fSAndroid Build Coastguard Worker fd->parent = dirent->inode;
195*6a54128fSAndroid Build Coastguard Worker continue;
196*6a54128fSAndroid Build Coastguard Worker }
197*6a54128fSAndroid Build Coastguard Worker if (fd->num_array >= fd->max_array) {
198*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
199*6a54128fSAndroid Build Coastguard Worker
200*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_resize_array(sizeof(struct hash_entry),
201*6a54128fSAndroid Build Coastguard Worker fd->max_array,
202*6a54128fSAndroid Build Coastguard Worker fd->max_array + 500,
203*6a54128fSAndroid Build Coastguard Worker &fd->harray);
204*6a54128fSAndroid Build Coastguard Worker if (retval) {
205*6a54128fSAndroid Build Coastguard Worker fd->err = retval;
206*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
207*6a54128fSAndroid Build Coastguard Worker }
208*6a54128fSAndroid Build Coastguard Worker fd->max_array += 500;
209*6a54128fSAndroid Build Coastguard Worker }
210*6a54128fSAndroid Build Coastguard Worker ent = fd->harray + fd->num_array++;
211*6a54128fSAndroid Build Coastguard Worker ent->dir = dirent;
212*6a54128fSAndroid Build Coastguard Worker fd->dir_size += ext2fs_dir_rec_len(name_len, extended);
213*6a54128fSAndroid Build Coastguard Worker ent->ino = dirent->inode;
214*6a54128fSAndroid Build Coastguard Worker if (extended) {
215*6a54128fSAndroid Build Coastguard Worker ent->hash = EXT2_DIRENT_HASH(dirent);
216*6a54128fSAndroid Build Coastguard Worker ent->minor_hash = EXT2_DIRENT_MINOR_HASH(dirent);
217*6a54128fSAndroid Build Coastguard Worker } else if (fd->compress) {
218*6a54128fSAndroid Build Coastguard Worker ent->hash = ent->minor_hash = 0;
219*6a54128fSAndroid Build Coastguard Worker } else {
220*6a54128fSAndroid Build Coastguard Worker fd->err = ext2fs_dirhash2(hash_alg,
221*6a54128fSAndroid Build Coastguard Worker dirent->name, name_len,
222*6a54128fSAndroid Build Coastguard Worker fs->encoding, hash_flags,
223*6a54128fSAndroid Build Coastguard Worker fs->super->s_hash_seed,
224*6a54128fSAndroid Build Coastguard Worker &ent->hash, &ent->minor_hash);
225*6a54128fSAndroid Build Coastguard Worker if (fd->err)
226*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
227*6a54128fSAndroid Build Coastguard Worker }
228*6a54128fSAndroid Build Coastguard Worker }
229*6a54128fSAndroid Build Coastguard Worker
230*6a54128fSAndroid Build Coastguard Worker return 0;
231*6a54128fSAndroid Build Coastguard Worker }
232*6a54128fSAndroid Build Coastguard Worker
233*6a54128fSAndroid Build Coastguard Worker /* Used for sorting the hash entry */
ino_cmp(const void * a,const void * b)234*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
235*6a54128fSAndroid Build Coastguard Worker {
236*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_a = (const struct hash_entry *) a;
237*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_b = (const struct hash_entry *) b;
238*6a54128fSAndroid Build Coastguard Worker
239*6a54128fSAndroid Build Coastguard Worker return (he_a->ino - he_b->ino);
240*6a54128fSAndroid Build Coastguard Worker }
241*6a54128fSAndroid Build Coastguard Worker
242*6a54128fSAndroid Build Coastguard Worker struct name_cmp_ctx
243*6a54128fSAndroid Build Coastguard Worker {
244*6a54128fSAndroid Build Coastguard Worker int casefold;
245*6a54128fSAndroid Build Coastguard Worker const struct ext2fs_nls_table *tbl;
246*6a54128fSAndroid Build Coastguard Worker };
247*6a54128fSAndroid Build Coastguard Worker
same_name(const struct name_cmp_ctx * cmp_ctx,char * s1,int len1,char * s2,int len2)248*6a54128fSAndroid Build Coastguard Worker static int same_name(const struct name_cmp_ctx *cmp_ctx, char *s1,
249*6a54128fSAndroid Build Coastguard Worker int len1, char *s2, int len2)
250*6a54128fSAndroid Build Coastguard Worker {
251*6a54128fSAndroid Build Coastguard Worker if (!cmp_ctx->casefold)
252*6a54128fSAndroid Build Coastguard Worker return (len1 == len2 && !memcmp(s1, s2, len1));
253*6a54128fSAndroid Build Coastguard Worker else
254*6a54128fSAndroid Build Coastguard Worker return !ext2fs_casefold_cmp(cmp_ctx->tbl,
255*6a54128fSAndroid Build Coastguard Worker (unsigned char *) s1, len1,
256*6a54128fSAndroid Build Coastguard Worker (unsigned char *) s2, len2);
257*6a54128fSAndroid Build Coastguard Worker }
258*6a54128fSAndroid Build Coastguard Worker
259*6a54128fSAndroid Build Coastguard Worker /* Used for sorting the hash entry */
name_cmp(const void * a,const void * b)260*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
261*6a54128fSAndroid Build Coastguard Worker {
262*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_a = (const struct hash_entry *) a;
263*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_b = (const struct hash_entry *) b;
264*6a54128fSAndroid Build Coastguard Worker unsigned int he_a_len, he_b_len, min_len;
265*6a54128fSAndroid Build Coastguard Worker int ret;
266*6a54128fSAndroid Build Coastguard Worker
267*6a54128fSAndroid Build Coastguard Worker he_a_len = ext2fs_dirent_name_len(he_a->dir);
268*6a54128fSAndroid Build Coastguard Worker he_b_len = ext2fs_dirent_name_len(he_b->dir);
269*6a54128fSAndroid Build Coastguard Worker min_len = he_a_len;
270*6a54128fSAndroid Build Coastguard Worker if (min_len > he_b_len)
271*6a54128fSAndroid Build Coastguard Worker min_len = he_b_len;
272*6a54128fSAndroid Build Coastguard Worker
273*6a54128fSAndroid Build Coastguard Worker ret = memcmp(he_a->dir->name, he_b->dir->name, min_len);
274*6a54128fSAndroid Build Coastguard Worker if (ret == 0) {
275*6a54128fSAndroid Build Coastguard Worker if (he_a_len > he_b_len)
276*6a54128fSAndroid Build Coastguard Worker ret = 1;
277*6a54128fSAndroid Build Coastguard Worker else if (he_a_len < he_b_len)
278*6a54128fSAndroid Build Coastguard Worker ret = -1;
279*6a54128fSAndroid Build Coastguard Worker else
280*6a54128fSAndroid Build Coastguard Worker ret = he_b->dir->inode - he_a->dir->inode;
281*6a54128fSAndroid Build Coastguard Worker }
282*6a54128fSAndroid Build Coastguard Worker return ret;
283*6a54128fSAndroid Build Coastguard Worker }
284*6a54128fSAndroid Build Coastguard Worker
name_cf_cmp(const struct name_cmp_ctx * ctx,const void * a,const void * b)285*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE name_cf_cmp(const struct name_cmp_ctx *ctx,
286*6a54128fSAndroid Build Coastguard Worker const void *a, const void *b)
287*6a54128fSAndroid Build Coastguard Worker {
288*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_a = (const struct hash_entry *) a;
289*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_b = (const struct hash_entry *) b;
290*6a54128fSAndroid Build Coastguard Worker unsigned int he_a_len, he_b_len;
291*6a54128fSAndroid Build Coastguard Worker int ret;
292*6a54128fSAndroid Build Coastguard Worker
293*6a54128fSAndroid Build Coastguard Worker he_a_len = ext2fs_dirent_name_len(he_a->dir);
294*6a54128fSAndroid Build Coastguard Worker he_b_len = ext2fs_dirent_name_len(he_b->dir);
295*6a54128fSAndroid Build Coastguard Worker
296*6a54128fSAndroid Build Coastguard Worker ret = ext2fs_casefold_cmp(ctx->tbl,
297*6a54128fSAndroid Build Coastguard Worker (unsigned char *) he_a->dir->name, he_a_len,
298*6a54128fSAndroid Build Coastguard Worker (unsigned char *) he_b->dir->name, he_b_len);
299*6a54128fSAndroid Build Coastguard Worker if (ret == 0) {
300*6a54128fSAndroid Build Coastguard Worker if (he_a_len > he_b_len)
301*6a54128fSAndroid Build Coastguard Worker ret = 1;
302*6a54128fSAndroid Build Coastguard Worker else if (he_a_len < he_b_len)
303*6a54128fSAndroid Build Coastguard Worker ret = -1;
304*6a54128fSAndroid Build Coastguard Worker else
305*6a54128fSAndroid Build Coastguard Worker ret = he_b->dir->inode - he_a->dir->inode;
306*6a54128fSAndroid Build Coastguard Worker }
307*6a54128fSAndroid Build Coastguard Worker return ret;
308*6a54128fSAndroid Build Coastguard Worker }
309*6a54128fSAndroid Build Coastguard Worker
310*6a54128fSAndroid Build Coastguard Worker /* Used for sorting the hash entry */
hash_cmp(const void * a,const void * b,void * arg)311*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b, void *arg)
312*6a54128fSAndroid Build Coastguard Worker {
313*6a54128fSAndroid Build Coastguard Worker const struct name_cmp_ctx *ctx = (struct name_cmp_ctx *) arg;
314*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_a = (const struct hash_entry *) a;
315*6a54128fSAndroid Build Coastguard Worker const struct hash_entry *he_b = (const struct hash_entry *) b;
316*6a54128fSAndroid Build Coastguard Worker int ret;
317*6a54128fSAndroid Build Coastguard Worker
318*6a54128fSAndroid Build Coastguard Worker if (he_a->hash > he_b->hash)
319*6a54128fSAndroid Build Coastguard Worker ret = 1;
320*6a54128fSAndroid Build Coastguard Worker else if (he_a->hash < he_b->hash)
321*6a54128fSAndroid Build Coastguard Worker ret = -1;
322*6a54128fSAndroid Build Coastguard Worker else {
323*6a54128fSAndroid Build Coastguard Worker if (he_a->minor_hash > he_b->minor_hash)
324*6a54128fSAndroid Build Coastguard Worker ret = 1;
325*6a54128fSAndroid Build Coastguard Worker else if (he_a->minor_hash < he_b->minor_hash)
326*6a54128fSAndroid Build Coastguard Worker ret = -1;
327*6a54128fSAndroid Build Coastguard Worker else {
328*6a54128fSAndroid Build Coastguard Worker if (ctx->casefold)
329*6a54128fSAndroid Build Coastguard Worker ret = name_cf_cmp(ctx, a, b);
330*6a54128fSAndroid Build Coastguard Worker else
331*6a54128fSAndroid Build Coastguard Worker ret = name_cmp(a, b);
332*6a54128fSAndroid Build Coastguard Worker }
333*6a54128fSAndroid Build Coastguard Worker }
334*6a54128fSAndroid Build Coastguard Worker return ret;
335*6a54128fSAndroid Build Coastguard Worker }
336*6a54128fSAndroid Build Coastguard Worker
alloc_size_dir(ext2_filsys fs,struct out_dir * outdir,blk_t blocks)337*6a54128fSAndroid Build Coastguard Worker static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
338*6a54128fSAndroid Build Coastguard Worker blk_t blocks)
339*6a54128fSAndroid Build Coastguard Worker {
340*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
341*6a54128fSAndroid Build Coastguard Worker
342*6a54128fSAndroid Build Coastguard Worker if (outdir->max) {
343*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_resize_array(fs->blocksize, outdir->max, blocks,
344*6a54128fSAndroid Build Coastguard Worker &outdir->buf);
345*6a54128fSAndroid Build Coastguard Worker if (retval)
346*6a54128fSAndroid Build Coastguard Worker return retval;
347*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_resize_array(sizeof(ext2_dirhash_t),
348*6a54128fSAndroid Build Coastguard Worker outdir->max, blocks,
349*6a54128fSAndroid Build Coastguard Worker &outdir->hashes);
350*6a54128fSAndroid Build Coastguard Worker if (retval)
351*6a54128fSAndroid Build Coastguard Worker return retval;
352*6a54128fSAndroid Build Coastguard Worker } else {
353*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_array(fs->blocksize, blocks, &outdir->buf);
354*6a54128fSAndroid Build Coastguard Worker if (retval)
355*6a54128fSAndroid Build Coastguard Worker return retval;
356*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_array(sizeof(ext2_dirhash_t), blocks,
357*6a54128fSAndroid Build Coastguard Worker &outdir->hashes);
358*6a54128fSAndroid Build Coastguard Worker if (retval)
359*6a54128fSAndroid Build Coastguard Worker return retval;
360*6a54128fSAndroid Build Coastguard Worker outdir->num = 0;
361*6a54128fSAndroid Build Coastguard Worker }
362*6a54128fSAndroid Build Coastguard Worker outdir->max = blocks;
363*6a54128fSAndroid Build Coastguard Worker return 0;
364*6a54128fSAndroid Build Coastguard Worker }
365*6a54128fSAndroid Build Coastguard Worker
free_out_dir(struct out_dir * outdir)366*6a54128fSAndroid Build Coastguard Worker static void free_out_dir(struct out_dir *outdir)
367*6a54128fSAndroid Build Coastguard Worker {
368*6a54128fSAndroid Build Coastguard Worker free(outdir->buf);
369*6a54128fSAndroid Build Coastguard Worker free(outdir->hashes);
370*6a54128fSAndroid Build Coastguard Worker outdir->max = 0;
371*6a54128fSAndroid Build Coastguard Worker outdir->num =0;
372*6a54128fSAndroid Build Coastguard Worker }
373*6a54128fSAndroid Build Coastguard Worker
get_next_block(ext2_filsys fs,struct out_dir * outdir,char ** ret)374*6a54128fSAndroid Build Coastguard Worker static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
375*6a54128fSAndroid Build Coastguard Worker char ** ret)
376*6a54128fSAndroid Build Coastguard Worker {
377*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
378*6a54128fSAndroid Build Coastguard Worker
379*6a54128fSAndroid Build Coastguard Worker if (outdir->num >= outdir->max) {
380*6a54128fSAndroid Build Coastguard Worker int increment = outdir->max / 10;
381*6a54128fSAndroid Build Coastguard Worker
382*6a54128fSAndroid Build Coastguard Worker if (increment < 50)
383*6a54128fSAndroid Build Coastguard Worker increment = 50;
384*6a54128fSAndroid Build Coastguard Worker retval = alloc_size_dir(fs, outdir, outdir->max + increment);
385*6a54128fSAndroid Build Coastguard Worker if (retval)
386*6a54128fSAndroid Build Coastguard Worker return retval;
387*6a54128fSAndroid Build Coastguard Worker }
388*6a54128fSAndroid Build Coastguard Worker *ret = outdir->buf + (size_t)outdir->num++ * fs->blocksize;
389*6a54128fSAndroid Build Coastguard Worker memset(*ret, 0, fs->blocksize);
390*6a54128fSAndroid Build Coastguard Worker return 0;
391*6a54128fSAndroid Build Coastguard Worker }
392*6a54128fSAndroid Build Coastguard Worker
393*6a54128fSAndroid Build Coastguard Worker /*
394*6a54128fSAndroid Build Coastguard Worker * This function is used to make a unique filename. We do this by
395*6a54128fSAndroid Build Coastguard Worker * appending ~0, and then incrementing the number. However, we cannot
396*6a54128fSAndroid Build Coastguard Worker * expand the length of the filename beyond the padding available in
397*6a54128fSAndroid Build Coastguard Worker * the directory entry.
398*6a54128fSAndroid Build Coastguard Worker */
mutate_name(char * str,unsigned int * len)399*6a54128fSAndroid Build Coastguard Worker static void mutate_name(char *str, unsigned int *len)
400*6a54128fSAndroid Build Coastguard Worker {
401*6a54128fSAndroid Build Coastguard Worker int i;
402*6a54128fSAndroid Build Coastguard Worker unsigned int l = *len;
403*6a54128fSAndroid Build Coastguard Worker
404*6a54128fSAndroid Build Coastguard Worker /*
405*6a54128fSAndroid Build Coastguard Worker * First check to see if it looks the name has been mutated
406*6a54128fSAndroid Build Coastguard Worker * already
407*6a54128fSAndroid Build Coastguard Worker */
408*6a54128fSAndroid Build Coastguard Worker for (i = l-1; i > 0; i--) {
409*6a54128fSAndroid Build Coastguard Worker if (!isdigit(str[i]))
410*6a54128fSAndroid Build Coastguard Worker break;
411*6a54128fSAndroid Build Coastguard Worker }
412*6a54128fSAndroid Build Coastguard Worker if ((i == (int)l - 1) || (str[i] != '~')) {
413*6a54128fSAndroid Build Coastguard Worker if (((l-1) & 3) < 2)
414*6a54128fSAndroid Build Coastguard Worker l += 2;
415*6a54128fSAndroid Build Coastguard Worker else
416*6a54128fSAndroid Build Coastguard Worker l = (l+3) & ~3;
417*6a54128fSAndroid Build Coastguard Worker if (l > 255)
418*6a54128fSAndroid Build Coastguard Worker l = 255;
419*6a54128fSAndroid Build Coastguard Worker str[l-2] = '~';
420*6a54128fSAndroid Build Coastguard Worker str[l-1] = '0';
421*6a54128fSAndroid Build Coastguard Worker *len = l;
422*6a54128fSAndroid Build Coastguard Worker return;
423*6a54128fSAndroid Build Coastguard Worker }
424*6a54128fSAndroid Build Coastguard Worker for (i = l-1; i >= 0; i--) {
425*6a54128fSAndroid Build Coastguard Worker if (isdigit(str[i])) {
426*6a54128fSAndroid Build Coastguard Worker if (str[i] == '9')
427*6a54128fSAndroid Build Coastguard Worker str[i] = '0';
428*6a54128fSAndroid Build Coastguard Worker else {
429*6a54128fSAndroid Build Coastguard Worker str[i]++;
430*6a54128fSAndroid Build Coastguard Worker return;
431*6a54128fSAndroid Build Coastguard Worker }
432*6a54128fSAndroid Build Coastguard Worker continue;
433*6a54128fSAndroid Build Coastguard Worker }
434*6a54128fSAndroid Build Coastguard Worker if (i == 1) {
435*6a54128fSAndroid Build Coastguard Worker if (str[0] == 'z')
436*6a54128fSAndroid Build Coastguard Worker str[0] = 'A';
437*6a54128fSAndroid Build Coastguard Worker else if (str[0] == 'Z') {
438*6a54128fSAndroid Build Coastguard Worker str[0] = '~';
439*6a54128fSAndroid Build Coastguard Worker str[1] = '0';
440*6a54128fSAndroid Build Coastguard Worker } else
441*6a54128fSAndroid Build Coastguard Worker str[0]++;
442*6a54128fSAndroid Build Coastguard Worker } else if (i > 0) {
443*6a54128fSAndroid Build Coastguard Worker str[i] = '1';
444*6a54128fSAndroid Build Coastguard Worker str[i-1] = '~';
445*6a54128fSAndroid Build Coastguard Worker } else {
446*6a54128fSAndroid Build Coastguard Worker if (str[0] == '~')
447*6a54128fSAndroid Build Coastguard Worker str[0] = 'a';
448*6a54128fSAndroid Build Coastguard Worker else
449*6a54128fSAndroid Build Coastguard Worker str[0]++;
450*6a54128fSAndroid Build Coastguard Worker }
451*6a54128fSAndroid Build Coastguard Worker break;
452*6a54128fSAndroid Build Coastguard Worker }
453*6a54128fSAndroid Build Coastguard Worker }
454*6a54128fSAndroid Build Coastguard Worker
duplicate_search_and_fix(e2fsck_t ctx,ext2_filsys fs,ext2_ino_t ino,struct fill_dir_struct * fd,const struct name_cmp_ctx * cmp_ctx)455*6a54128fSAndroid Build Coastguard Worker static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
456*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino,
457*6a54128fSAndroid Build Coastguard Worker struct fill_dir_struct *fd,
458*6a54128fSAndroid Build Coastguard Worker const struct name_cmp_ctx *cmp_ctx)
459*6a54128fSAndroid Build Coastguard Worker {
460*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
461*6a54128fSAndroid Build Coastguard Worker struct hash_entry *ent, *prev;
462*6a54128fSAndroid Build Coastguard Worker blk_t i, j;
463*6a54128fSAndroid Build Coastguard Worker int fixed = 0;
464*6a54128fSAndroid Build Coastguard Worker char new_name[256];
465*6a54128fSAndroid Build Coastguard Worker unsigned int new_len;
466*6a54128fSAndroid Build Coastguard Worker int hash_alg;
467*6a54128fSAndroid Build Coastguard Worker int hash_flags = fd->inode->i_flags & EXT4_CASEFOLD_FL;
468*6a54128fSAndroid Build Coastguard Worker
469*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
470*6a54128fSAndroid Build Coastguard Worker pctx.ino = ino;
471*6a54128fSAndroid Build Coastguard Worker
472*6a54128fSAndroid Build Coastguard Worker hash_alg = fs->super->s_def_hash_version;
473*6a54128fSAndroid Build Coastguard Worker if ((hash_alg <= EXT2_HASH_TEA) &&
474*6a54128fSAndroid Build Coastguard Worker (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
475*6a54128fSAndroid Build Coastguard Worker hash_alg += 3;
476*6a54128fSAndroid Build Coastguard Worker
477*6a54128fSAndroid Build Coastguard Worker for (i=1; i < fd->num_array; i++) {
478*6a54128fSAndroid Build Coastguard Worker ent = fd->harray + i;
479*6a54128fSAndroid Build Coastguard Worker prev = ent - 1;
480*6a54128fSAndroid Build Coastguard Worker if (!ent->dir->inode ||
481*6a54128fSAndroid Build Coastguard Worker !same_name(cmp_ctx, ent->dir->name,
482*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_name_len(ent->dir),
483*6a54128fSAndroid Build Coastguard Worker prev->dir->name,
484*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_name_len(prev->dir)))
485*6a54128fSAndroid Build Coastguard Worker continue;
486*6a54128fSAndroid Build Coastguard Worker pctx.dirent = ent->dir;
487*6a54128fSAndroid Build Coastguard Worker if ((ent->dir->inode == prev->dir->inode) &&
488*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
489*6a54128fSAndroid Build Coastguard Worker e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
490*6a54128fSAndroid Build Coastguard Worker ent->dir->inode = 0;
491*6a54128fSAndroid Build Coastguard Worker fixed++;
492*6a54128fSAndroid Build Coastguard Worker continue;
493*6a54128fSAndroid Build Coastguard Worker }
494*6a54128fSAndroid Build Coastguard Worker /* Can't alter encrypted name without key, so just drop it */
495*6a54128fSAndroid Build Coastguard Worker if (fd->inode->i_flags & EXT4_ENCRYPT_FL) {
496*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE_NO_RENAME, &pctx)) {
497*6a54128fSAndroid Build Coastguard Worker e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
498*6a54128fSAndroid Build Coastguard Worker ent->dir->inode = 0;
499*6a54128fSAndroid Build Coastguard Worker fixed++;
500*6a54128fSAndroid Build Coastguard Worker continue;
501*6a54128fSAndroid Build Coastguard Worker }
502*6a54128fSAndroid Build Coastguard Worker }
503*6a54128fSAndroid Build Coastguard Worker new_len = ext2fs_dirent_name_len(ent->dir);
504*6a54128fSAndroid Build Coastguard Worker if (new_len == 0) {
505*6a54128fSAndroid Build Coastguard Worker /* should never happen */
506*6a54128fSAndroid Build Coastguard Worker ext2fs_unmark_valid(fs);
507*6a54128fSAndroid Build Coastguard Worker continue;
508*6a54128fSAndroid Build Coastguard Worker }
509*6a54128fSAndroid Build Coastguard Worker memcpy(new_name, ent->dir->name, new_len);
510*6a54128fSAndroid Build Coastguard Worker mutate_name(new_name, &new_len);
511*6a54128fSAndroid Build Coastguard Worker for (j=0; j < fd->num_array; j++) {
512*6a54128fSAndroid Build Coastguard Worker if ((i==j) ||
513*6a54128fSAndroid Build Coastguard Worker !same_name(cmp_ctx, new_name, new_len,
514*6a54128fSAndroid Build Coastguard Worker fd->harray[j].dir->name,
515*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_name_len(fd->harray[j].dir))) {
516*6a54128fSAndroid Build Coastguard Worker continue;
517*6a54128fSAndroid Build Coastguard Worker }
518*6a54128fSAndroid Build Coastguard Worker mutate_name(new_name, &new_len);
519*6a54128fSAndroid Build Coastguard Worker
520*6a54128fSAndroid Build Coastguard Worker j = -1;
521*6a54128fSAndroid Build Coastguard Worker }
522*6a54128fSAndroid Build Coastguard Worker new_name[new_len] = 0;
523*6a54128fSAndroid Build Coastguard Worker pctx.str = new_name;
524*6a54128fSAndroid Build Coastguard Worker if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
525*6a54128fSAndroid Build Coastguard Worker memcpy(ent->dir->name, new_name, new_len);
526*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_set_name_len(ent->dir, new_len);
527*6a54128fSAndroid Build Coastguard Worker ext2fs_dirhash2(hash_alg, new_name, new_len,
528*6a54128fSAndroid Build Coastguard Worker fs->encoding, hash_flags,
529*6a54128fSAndroid Build Coastguard Worker fs->super->s_hash_seed,
530*6a54128fSAndroid Build Coastguard Worker &ent->hash, &ent->minor_hash);
531*6a54128fSAndroid Build Coastguard Worker fixed++;
532*6a54128fSAndroid Build Coastguard Worker }
533*6a54128fSAndroid Build Coastguard Worker }
534*6a54128fSAndroid Build Coastguard Worker return fixed;
535*6a54128fSAndroid Build Coastguard Worker }
536*6a54128fSAndroid Build Coastguard Worker
537*6a54128fSAndroid Build Coastguard Worker
copy_dir_entries(e2fsck_t ctx,struct fill_dir_struct * fd,struct out_dir * outdir)538*6a54128fSAndroid Build Coastguard Worker static errcode_t copy_dir_entries(e2fsck_t ctx,
539*6a54128fSAndroid Build Coastguard Worker struct fill_dir_struct *fd,
540*6a54128fSAndroid Build Coastguard Worker struct out_dir *outdir)
541*6a54128fSAndroid Build Coastguard Worker {
542*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
543*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
544*6a54128fSAndroid Build Coastguard Worker char *block_start;
545*6a54128fSAndroid Build Coastguard Worker struct hash_entry *ent;
546*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry *dirent;
547*6a54128fSAndroid Build Coastguard Worker unsigned int rec_len, prev_rec_len, left, slack, offset;
548*6a54128fSAndroid Build Coastguard Worker blk_t i;
549*6a54128fSAndroid Build Coastguard Worker ext2_dirhash_t prev_hash;
550*6a54128fSAndroid Build Coastguard Worker int csum_size = 0;
551*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry_tail *t;
552*6a54128fSAndroid Build Coastguard Worker int hash_in_entry = ext4_hash_in_dirent(fd->inode);
553*6a54128fSAndroid Build Coastguard Worker unsigned int min_rec_len = ext2fs_dir_rec_len(1, hash_in_entry);
554*6a54128fSAndroid Build Coastguard Worker
555*6a54128fSAndroid Build Coastguard Worker if (ctx->htree_slack_percentage == 255) {
556*6a54128fSAndroid Build Coastguard Worker profile_get_uint(ctx->profile, "options",
557*6a54128fSAndroid Build Coastguard Worker "indexed_dir_slack_percentage",
558*6a54128fSAndroid Build Coastguard Worker 0, 20,
559*6a54128fSAndroid Build Coastguard Worker &ctx->htree_slack_percentage);
560*6a54128fSAndroid Build Coastguard Worker if (ctx->htree_slack_percentage > 100)
561*6a54128fSAndroid Build Coastguard Worker ctx->htree_slack_percentage = 20;
562*6a54128fSAndroid Build Coastguard Worker }
563*6a54128fSAndroid Build Coastguard Worker
564*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_metadata_csum(fs->super))
565*6a54128fSAndroid Build Coastguard Worker csum_size = sizeof(struct ext2_dir_entry_tail);
566*6a54128fSAndroid Build Coastguard Worker
567*6a54128fSAndroid Build Coastguard Worker outdir->max = 0;
568*6a54128fSAndroid Build Coastguard Worker retval = alloc_size_dir(fs, outdir,
569*6a54128fSAndroid Build Coastguard Worker (fd->dir_size / fs->blocksize) + 2);
570*6a54128fSAndroid Build Coastguard Worker if (retval)
571*6a54128fSAndroid Build Coastguard Worker return retval;
572*6a54128fSAndroid Build Coastguard Worker outdir->num = fd->compress ? 0 : 1;
573*6a54128fSAndroid Build Coastguard Worker offset = 0;
574*6a54128fSAndroid Build Coastguard Worker outdir->hashes[0] = 0;
575*6a54128fSAndroid Build Coastguard Worker prev_hash = 1;
576*6a54128fSAndroid Build Coastguard Worker if ((retval = get_next_block(fs, outdir, &block_start)))
577*6a54128fSAndroid Build Coastguard Worker return retval;
578*6a54128fSAndroid Build Coastguard Worker dirent = (struct ext2_dir_entry *) block_start;
579*6a54128fSAndroid Build Coastguard Worker prev_rec_len = 0;
580*6a54128fSAndroid Build Coastguard Worker rec_len = 0;
581*6a54128fSAndroid Build Coastguard Worker left = fs->blocksize - csum_size;
582*6a54128fSAndroid Build Coastguard Worker slack = fd->compress ? min_rec_len :
583*6a54128fSAndroid Build Coastguard Worker ((fs->blocksize - csum_size) * ctx->htree_slack_percentage)/100;
584*6a54128fSAndroid Build Coastguard Worker if (slack < min_rec_len)
585*6a54128fSAndroid Build Coastguard Worker slack = min_rec_len;
586*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < fd->num_array; i++) {
587*6a54128fSAndroid Build Coastguard Worker ent = fd->harray + i;
588*6a54128fSAndroid Build Coastguard Worker if (ent->dir->inode == 0)
589*6a54128fSAndroid Build Coastguard Worker continue;
590*6a54128fSAndroid Build Coastguard Worker rec_len = ext2fs_dir_rec_len(ext2fs_dirent_name_len(ent->dir),
591*6a54128fSAndroid Build Coastguard Worker hash_in_entry);
592*6a54128fSAndroid Build Coastguard Worker if (rec_len > left) {
593*6a54128fSAndroid Build Coastguard Worker if (left) {
594*6a54128fSAndroid Build Coastguard Worker left += prev_rec_len;
595*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_set_rec_len(fs, left, dirent);
596*6a54128fSAndroid Build Coastguard Worker if (retval)
597*6a54128fSAndroid Build Coastguard Worker return retval;
598*6a54128fSAndroid Build Coastguard Worker }
599*6a54128fSAndroid Build Coastguard Worker if (csum_size) {
600*6a54128fSAndroid Build Coastguard Worker t = EXT2_DIRENT_TAIL(block_start,
601*6a54128fSAndroid Build Coastguard Worker fs->blocksize);
602*6a54128fSAndroid Build Coastguard Worker ext2fs_initialize_dirent_tail(fs, t);
603*6a54128fSAndroid Build Coastguard Worker }
604*6a54128fSAndroid Build Coastguard Worker if ((retval = get_next_block(fs, outdir,
605*6a54128fSAndroid Build Coastguard Worker &block_start)))
606*6a54128fSAndroid Build Coastguard Worker return retval;
607*6a54128fSAndroid Build Coastguard Worker offset = 0;
608*6a54128fSAndroid Build Coastguard Worker }
609*6a54128fSAndroid Build Coastguard Worker left = (fs->blocksize - csum_size) - offset;
610*6a54128fSAndroid Build Coastguard Worker dirent = (struct ext2_dir_entry *) (block_start + offset);
611*6a54128fSAndroid Build Coastguard Worker if (offset == 0) {
612*6a54128fSAndroid Build Coastguard Worker if (ent->hash == prev_hash)
613*6a54128fSAndroid Build Coastguard Worker outdir->hashes[outdir->num-1] = ent->hash | 1;
614*6a54128fSAndroid Build Coastguard Worker else
615*6a54128fSAndroid Build Coastguard Worker outdir->hashes[outdir->num-1] = ent->hash;
616*6a54128fSAndroid Build Coastguard Worker }
617*6a54128fSAndroid Build Coastguard Worker dirent->inode = ent->dir->inode;
618*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_set_name_len(dirent,
619*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_name_len(ent->dir));
620*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_set_file_type(dirent,
621*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_file_type(ent->dir));
622*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_set_rec_len(fs, rec_len, dirent);
623*6a54128fSAndroid Build Coastguard Worker if (retval)
624*6a54128fSAndroid Build Coastguard Worker return retval;
625*6a54128fSAndroid Build Coastguard Worker prev_rec_len = rec_len;
626*6a54128fSAndroid Build Coastguard Worker memcpy(dirent->name, ent->dir->name,
627*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_name_len(dirent));
628*6a54128fSAndroid Build Coastguard Worker if (hash_in_entry) {
629*6a54128fSAndroid Build Coastguard Worker EXT2_DIRENT_HASHES(dirent)->hash = ext2fs_cpu_to_le32(ent->hash);
630*6a54128fSAndroid Build Coastguard Worker EXT2_DIRENT_HASHES(dirent)->minor_hash =
631*6a54128fSAndroid Build Coastguard Worker ext2fs_cpu_to_le32(ent->minor_hash);
632*6a54128fSAndroid Build Coastguard Worker }
633*6a54128fSAndroid Build Coastguard Worker offset += rec_len;
634*6a54128fSAndroid Build Coastguard Worker left -= rec_len;
635*6a54128fSAndroid Build Coastguard Worker if (left < slack) {
636*6a54128fSAndroid Build Coastguard Worker prev_rec_len += left;
637*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
638*6a54128fSAndroid Build Coastguard Worker if (retval)
639*6a54128fSAndroid Build Coastguard Worker return retval;
640*6a54128fSAndroid Build Coastguard Worker offset += left;
641*6a54128fSAndroid Build Coastguard Worker left = 0;
642*6a54128fSAndroid Build Coastguard Worker }
643*6a54128fSAndroid Build Coastguard Worker prev_hash = ent->hash;
644*6a54128fSAndroid Build Coastguard Worker }
645*6a54128fSAndroid Build Coastguard Worker if (left)
646*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
647*6a54128fSAndroid Build Coastguard Worker if (csum_size) {
648*6a54128fSAndroid Build Coastguard Worker t = EXT2_DIRENT_TAIL(block_start, fs->blocksize);
649*6a54128fSAndroid Build Coastguard Worker ext2fs_initialize_dirent_tail(fs, t);
650*6a54128fSAndroid Build Coastguard Worker }
651*6a54128fSAndroid Build Coastguard Worker
652*6a54128fSAndroid Build Coastguard Worker return retval;
653*6a54128fSAndroid Build Coastguard Worker }
654*6a54128fSAndroid Build Coastguard Worker
655*6a54128fSAndroid Build Coastguard Worker
set_root_node(ext2_filsys fs,char * buf,ext2_ino_t ino,ext2_ino_t parent,struct ext2_inode * inode)656*6a54128fSAndroid Build Coastguard Worker static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
657*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino, ext2_ino_t parent,
658*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
659*6a54128fSAndroid Build Coastguard Worker {
660*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry *dir;
661*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_root_info *root;
662*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_countlimit *limits;
663*6a54128fSAndroid Build Coastguard Worker int filetype = 0;
664*6a54128fSAndroid Build Coastguard Worker int csum_size = 0;
665*6a54128fSAndroid Build Coastguard Worker
666*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_filetype(fs->super))
667*6a54128fSAndroid Build Coastguard Worker filetype = EXT2_FT_DIR;
668*6a54128fSAndroid Build Coastguard Worker
669*6a54128fSAndroid Build Coastguard Worker memset(buf, 0, fs->blocksize);
670*6a54128fSAndroid Build Coastguard Worker dir = (struct ext2_dir_entry *) buf;
671*6a54128fSAndroid Build Coastguard Worker dir->inode = ino;
672*6a54128fSAndroid Build Coastguard Worker dir->name[0] = '.';
673*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_set_name_len(dir, 1);
674*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_set_file_type(dir, filetype);
675*6a54128fSAndroid Build Coastguard Worker dir->rec_len = 12;
676*6a54128fSAndroid Build Coastguard Worker dir = (struct ext2_dir_entry *) (buf + 12);
677*6a54128fSAndroid Build Coastguard Worker dir->inode = parent;
678*6a54128fSAndroid Build Coastguard Worker dir->name[0] = '.';
679*6a54128fSAndroid Build Coastguard Worker dir->name[1] = '.';
680*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_set_name_len(dir, 2);
681*6a54128fSAndroid Build Coastguard Worker ext2fs_dirent_set_file_type(dir, filetype);
682*6a54128fSAndroid Build Coastguard Worker dir->rec_len = fs->blocksize - 12;
683*6a54128fSAndroid Build Coastguard Worker
684*6a54128fSAndroid Build Coastguard Worker root = (struct ext2_dx_root_info *) (buf+24);
685*6a54128fSAndroid Build Coastguard Worker root->reserved_zero = 0;
686*6a54128fSAndroid Build Coastguard Worker if (ext4_hash_in_dirent(inode))
687*6a54128fSAndroid Build Coastguard Worker root->hash_version = EXT2_HASH_SIPHASH;
688*6a54128fSAndroid Build Coastguard Worker else
689*6a54128fSAndroid Build Coastguard Worker root->hash_version = fs->super->s_def_hash_version;
690*6a54128fSAndroid Build Coastguard Worker root->info_length = 8;
691*6a54128fSAndroid Build Coastguard Worker root->indirect_levels = 0;
692*6a54128fSAndroid Build Coastguard Worker root->unused_flags = 0;
693*6a54128fSAndroid Build Coastguard Worker
694*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_metadata_csum(fs->super))
695*6a54128fSAndroid Build Coastguard Worker csum_size = sizeof(struct ext2_dx_tail);
696*6a54128fSAndroid Build Coastguard Worker
697*6a54128fSAndroid Build Coastguard Worker limits = (struct ext2_dx_countlimit *) (buf+32);
698*6a54128fSAndroid Build Coastguard Worker limits->limit = (fs->blocksize - (32 + csum_size)) /
699*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_dx_entry);
700*6a54128fSAndroid Build Coastguard Worker limits->count = 0;
701*6a54128fSAndroid Build Coastguard Worker
702*6a54128fSAndroid Build Coastguard Worker return root;
703*6a54128fSAndroid Build Coastguard Worker }
704*6a54128fSAndroid Build Coastguard Worker
705*6a54128fSAndroid Build Coastguard Worker
set_int_node(ext2_filsys fs,char * buf)706*6a54128fSAndroid Build Coastguard Worker static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
707*6a54128fSAndroid Build Coastguard Worker {
708*6a54128fSAndroid Build Coastguard Worker struct ext2_dir_entry *dir;
709*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_countlimit *limits;
710*6a54128fSAndroid Build Coastguard Worker int csum_size = 0;
711*6a54128fSAndroid Build Coastguard Worker
712*6a54128fSAndroid Build Coastguard Worker memset(buf, 0, fs->blocksize);
713*6a54128fSAndroid Build Coastguard Worker dir = (struct ext2_dir_entry *) buf;
714*6a54128fSAndroid Build Coastguard Worker dir->inode = 0;
715*6a54128fSAndroid Build Coastguard Worker (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
716*6a54128fSAndroid Build Coastguard Worker
717*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_metadata_csum(fs->super))
718*6a54128fSAndroid Build Coastguard Worker csum_size = sizeof(struct ext2_dx_tail);
719*6a54128fSAndroid Build Coastguard Worker
720*6a54128fSAndroid Build Coastguard Worker limits = (struct ext2_dx_countlimit *) (buf+8);
721*6a54128fSAndroid Build Coastguard Worker limits->limit = (fs->blocksize - (8 + csum_size)) /
722*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext2_dx_entry);
723*6a54128fSAndroid Build Coastguard Worker limits->count = 0;
724*6a54128fSAndroid Build Coastguard Worker
725*6a54128fSAndroid Build Coastguard Worker return (struct ext2_dx_entry *) limits;
726*6a54128fSAndroid Build Coastguard Worker }
727*6a54128fSAndroid Build Coastguard Worker
alloc_blocks(ext2_filsys fs,struct ext2_dx_countlimit ** limit,struct ext2_dx_entry ** prev_ent,struct ext2_dx_entry ** next_ent,int * prev_offset,int * next_offset,struct out_dir * outdir,int i,int * prev_count,int * next_count)728*6a54128fSAndroid Build Coastguard Worker static int alloc_blocks(ext2_filsys fs,
729*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_countlimit **limit,
730*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_entry **prev_ent,
731*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_entry **next_ent,
732*6a54128fSAndroid Build Coastguard Worker int *prev_offset, int *next_offset,
733*6a54128fSAndroid Build Coastguard Worker struct out_dir *outdir, int i,
734*6a54128fSAndroid Build Coastguard Worker int *prev_count, int *next_count)
735*6a54128fSAndroid Build Coastguard Worker {
736*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
737*6a54128fSAndroid Build Coastguard Worker char *block_start;
738*6a54128fSAndroid Build Coastguard Worker
739*6a54128fSAndroid Build Coastguard Worker if (*limit)
740*6a54128fSAndroid Build Coastguard Worker (*limit)->limit = (*limit)->count =
741*6a54128fSAndroid Build Coastguard Worker ext2fs_cpu_to_le16((*limit)->limit);
742*6a54128fSAndroid Build Coastguard Worker *prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset);
743*6a54128fSAndroid Build Coastguard Worker (*prev_ent)->block = ext2fs_cpu_to_le32(outdir->num);
744*6a54128fSAndroid Build Coastguard Worker
745*6a54128fSAndroid Build Coastguard Worker if (i != 1)
746*6a54128fSAndroid Build Coastguard Worker (*prev_ent)->hash =
747*6a54128fSAndroid Build Coastguard Worker ext2fs_cpu_to_le32(outdir->hashes[i]);
748*6a54128fSAndroid Build Coastguard Worker
749*6a54128fSAndroid Build Coastguard Worker retval = get_next_block(fs, outdir, &block_start);
750*6a54128fSAndroid Build Coastguard Worker if (retval)
751*6a54128fSAndroid Build Coastguard Worker return retval;
752*6a54128fSAndroid Build Coastguard Worker
753*6a54128fSAndroid Build Coastguard Worker /* outdir->buf might be reallocated */
754*6a54128fSAndroid Build Coastguard Worker *prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset);
755*6a54128fSAndroid Build Coastguard Worker
756*6a54128fSAndroid Build Coastguard Worker *next_ent = set_int_node(fs, block_start);
757*6a54128fSAndroid Build Coastguard Worker *limit = (struct ext2_dx_countlimit *)(*next_ent);
758*6a54128fSAndroid Build Coastguard Worker if (next_offset)
759*6a54128fSAndroid Build Coastguard Worker *next_offset = ((char *) *next_ent - outdir->buf);
760*6a54128fSAndroid Build Coastguard Worker
761*6a54128fSAndroid Build Coastguard Worker *next_count = (*limit)->limit;
762*6a54128fSAndroid Build Coastguard Worker (*prev_offset) += sizeof(struct ext2_dx_entry);
763*6a54128fSAndroid Build Coastguard Worker (*prev_count)--;
764*6a54128fSAndroid Build Coastguard Worker
765*6a54128fSAndroid Build Coastguard Worker return 0;
766*6a54128fSAndroid Build Coastguard Worker }
767*6a54128fSAndroid Build Coastguard Worker
768*6a54128fSAndroid Build Coastguard Worker /*
769*6a54128fSAndroid Build Coastguard Worker * This function takes the leaf nodes which have been written in
770*6a54128fSAndroid Build Coastguard Worker * outdir, and populates the root node and any necessary interior nodes.
771*6a54128fSAndroid Build Coastguard Worker */
calculate_tree(ext2_filsys fs,struct out_dir * outdir,ext2_ino_t ino,ext2_ino_t parent,struct ext2_inode * inode)772*6a54128fSAndroid Build Coastguard Worker static errcode_t calculate_tree(ext2_filsys fs,
773*6a54128fSAndroid Build Coastguard Worker struct out_dir *outdir,
774*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino,
775*6a54128fSAndroid Build Coastguard Worker ext2_ino_t parent,
776*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
777*6a54128fSAndroid Build Coastguard Worker {
778*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_root_info *root_info;
779*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_entry *root, *int_ent, *dx_ent = 0;
780*6a54128fSAndroid Build Coastguard Worker struct ext2_dx_countlimit *root_limit, *int_limit, *limit;
781*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
782*6a54128fSAndroid Build Coastguard Worker int i, c1, c2, c3, nblks;
783*6a54128fSAndroid Build Coastguard Worker int limit_offset, int_offset, root_offset;
784*6a54128fSAndroid Build Coastguard Worker
785*6a54128fSAndroid Build Coastguard Worker root_info = set_root_node(fs, outdir->buf, ino, parent, inode);
786*6a54128fSAndroid Build Coastguard Worker root_offset = limit_offset = ((char *) root_info - outdir->buf) +
787*6a54128fSAndroid Build Coastguard Worker root_info->info_length;
788*6a54128fSAndroid Build Coastguard Worker root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
789*6a54128fSAndroid Build Coastguard Worker c1 = root_limit->limit;
790*6a54128fSAndroid Build Coastguard Worker nblks = outdir->num;
791*6a54128fSAndroid Build Coastguard Worker
792*6a54128fSAndroid Build Coastguard Worker /* Write out the pointer blocks */
793*6a54128fSAndroid Build Coastguard Worker if (nblks - 1 <= c1) {
794*6a54128fSAndroid Build Coastguard Worker /* Just write out the root block, and we're done */
795*6a54128fSAndroid Build Coastguard Worker root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
796*6a54128fSAndroid Build Coastguard Worker for (i=1; i < nblks; i++) {
797*6a54128fSAndroid Build Coastguard Worker root->block = ext2fs_cpu_to_le32(i);
798*6a54128fSAndroid Build Coastguard Worker if (i != 1)
799*6a54128fSAndroid Build Coastguard Worker root->hash =
800*6a54128fSAndroid Build Coastguard Worker ext2fs_cpu_to_le32(outdir->hashes[i]);
801*6a54128fSAndroid Build Coastguard Worker root++;
802*6a54128fSAndroid Build Coastguard Worker c1--;
803*6a54128fSAndroid Build Coastguard Worker }
804*6a54128fSAndroid Build Coastguard Worker } else if (nblks - 1 <= ext2fs_htree_intnode_maxrecs(fs, c1)) {
805*6a54128fSAndroid Build Coastguard Worker c2 = 0;
806*6a54128fSAndroid Build Coastguard Worker limit = NULL;
807*6a54128fSAndroid Build Coastguard Worker root_info->indirect_levels = 1;
808*6a54128fSAndroid Build Coastguard Worker for (i=1; i < nblks; i++) {
809*6a54128fSAndroid Build Coastguard Worker if (c2 == 0 && c1 == 0)
810*6a54128fSAndroid Build Coastguard Worker return ENOSPC;
811*6a54128fSAndroid Build Coastguard Worker if (c2 == 0) {
812*6a54128fSAndroid Build Coastguard Worker retval = alloc_blocks(fs, &limit, &root,
813*6a54128fSAndroid Build Coastguard Worker &dx_ent, &root_offset,
814*6a54128fSAndroid Build Coastguard Worker NULL, outdir, i, &c1,
815*6a54128fSAndroid Build Coastguard Worker &c2);
816*6a54128fSAndroid Build Coastguard Worker if (retval)
817*6a54128fSAndroid Build Coastguard Worker return retval;
818*6a54128fSAndroid Build Coastguard Worker }
819*6a54128fSAndroid Build Coastguard Worker dx_ent->block = ext2fs_cpu_to_le32(i);
820*6a54128fSAndroid Build Coastguard Worker if (c2 != limit->limit)
821*6a54128fSAndroid Build Coastguard Worker dx_ent->hash =
822*6a54128fSAndroid Build Coastguard Worker ext2fs_cpu_to_le32(outdir->hashes[i]);
823*6a54128fSAndroid Build Coastguard Worker dx_ent++;
824*6a54128fSAndroid Build Coastguard Worker c2--;
825*6a54128fSAndroid Build Coastguard Worker }
826*6a54128fSAndroid Build Coastguard Worker limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
827*6a54128fSAndroid Build Coastguard Worker limit->limit = ext2fs_cpu_to_le16(limit->limit);
828*6a54128fSAndroid Build Coastguard Worker } else {
829*6a54128fSAndroid Build Coastguard Worker c2 = 0;
830*6a54128fSAndroid Build Coastguard Worker c3 = 0;
831*6a54128fSAndroid Build Coastguard Worker limit = NULL;
832*6a54128fSAndroid Build Coastguard Worker int_limit = 0;
833*6a54128fSAndroid Build Coastguard Worker root_info->indirect_levels = 2;
834*6a54128fSAndroid Build Coastguard Worker for (i = 1; i < nblks; i++) {
835*6a54128fSAndroid Build Coastguard Worker if (c3 == 0 && c2 == 0 && c1 == 0)
836*6a54128fSAndroid Build Coastguard Worker return ENOSPC;
837*6a54128fSAndroid Build Coastguard Worker if (c3 == 0 && c2 == 0) {
838*6a54128fSAndroid Build Coastguard Worker retval = alloc_blocks(fs, &int_limit, &root,
839*6a54128fSAndroid Build Coastguard Worker &int_ent, &root_offset,
840*6a54128fSAndroid Build Coastguard Worker &int_offset, outdir, i,
841*6a54128fSAndroid Build Coastguard Worker &c1, &c2);
842*6a54128fSAndroid Build Coastguard Worker if (retval)
843*6a54128fSAndroid Build Coastguard Worker return retval;
844*6a54128fSAndroid Build Coastguard Worker }
845*6a54128fSAndroid Build Coastguard Worker if (c3 == 0) {
846*6a54128fSAndroid Build Coastguard Worker int delta1 = (char *)int_limit - outdir->buf;
847*6a54128fSAndroid Build Coastguard Worker int delta2 = (char *)root - outdir->buf;
848*6a54128fSAndroid Build Coastguard Worker
849*6a54128fSAndroid Build Coastguard Worker retval = alloc_blocks(fs, &limit, &int_ent,
850*6a54128fSAndroid Build Coastguard Worker &dx_ent, &int_offset,
851*6a54128fSAndroid Build Coastguard Worker NULL, outdir, i, &c2,
852*6a54128fSAndroid Build Coastguard Worker &c3);
853*6a54128fSAndroid Build Coastguard Worker if (retval)
854*6a54128fSAndroid Build Coastguard Worker return retval;
855*6a54128fSAndroid Build Coastguard Worker
856*6a54128fSAndroid Build Coastguard Worker /* outdir->buf might be reallocated */
857*6a54128fSAndroid Build Coastguard Worker int_limit = (struct ext2_dx_countlimit *)
858*6a54128fSAndroid Build Coastguard Worker (outdir->buf + delta1);
859*6a54128fSAndroid Build Coastguard Worker root = (struct ext2_dx_entry *)
860*6a54128fSAndroid Build Coastguard Worker (outdir->buf + delta2);
861*6a54128fSAndroid Build Coastguard Worker }
862*6a54128fSAndroid Build Coastguard Worker dx_ent->block = ext2fs_cpu_to_le32(i);
863*6a54128fSAndroid Build Coastguard Worker if (c3 != limit->limit)
864*6a54128fSAndroid Build Coastguard Worker dx_ent->hash =
865*6a54128fSAndroid Build Coastguard Worker ext2fs_cpu_to_le32(outdir->hashes[i]);
866*6a54128fSAndroid Build Coastguard Worker dx_ent++;
867*6a54128fSAndroid Build Coastguard Worker c3--;
868*6a54128fSAndroid Build Coastguard Worker }
869*6a54128fSAndroid Build Coastguard Worker int_limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
870*6a54128fSAndroid Build Coastguard Worker int_limit->limit = ext2fs_cpu_to_le16(limit->limit);
871*6a54128fSAndroid Build Coastguard Worker
872*6a54128fSAndroid Build Coastguard Worker limit->count = ext2fs_cpu_to_le16(limit->limit - c3);
873*6a54128fSAndroid Build Coastguard Worker limit->limit = ext2fs_cpu_to_le16(limit->limit);
874*6a54128fSAndroid Build Coastguard Worker
875*6a54128fSAndroid Build Coastguard Worker }
876*6a54128fSAndroid Build Coastguard Worker root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
877*6a54128fSAndroid Build Coastguard Worker root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
878*6a54128fSAndroid Build Coastguard Worker root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
879*6a54128fSAndroid Build Coastguard Worker
880*6a54128fSAndroid Build Coastguard Worker return 0;
881*6a54128fSAndroid Build Coastguard Worker }
882*6a54128fSAndroid Build Coastguard Worker
883*6a54128fSAndroid Build Coastguard Worker struct write_dir_struct {
884*6a54128fSAndroid Build Coastguard Worker struct out_dir *outdir;
885*6a54128fSAndroid Build Coastguard Worker errcode_t err;
886*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
887*6a54128fSAndroid Build Coastguard Worker e2fsck_t ctx;
888*6a54128fSAndroid Build Coastguard Worker ext2_ino_t dir;
889*6a54128fSAndroid Build Coastguard Worker };
890*6a54128fSAndroid Build Coastguard Worker
891*6a54128fSAndroid Build Coastguard Worker /*
892*6a54128fSAndroid Build Coastguard Worker * Helper function which writes out a directory block.
893*6a54128fSAndroid Build Coastguard Worker */
write_dir_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)894*6a54128fSAndroid Build Coastguard Worker static int write_dir_block(ext2_filsys fs,
895*6a54128fSAndroid Build Coastguard Worker blk64_t *block_nr,
896*6a54128fSAndroid Build Coastguard Worker e2_blkcnt_t blockcnt,
897*6a54128fSAndroid Build Coastguard Worker blk64_t ref_block EXT2FS_ATTR((unused)),
898*6a54128fSAndroid Build Coastguard Worker int ref_offset EXT2FS_ATTR((unused)),
899*6a54128fSAndroid Build Coastguard Worker void *priv_data)
900*6a54128fSAndroid Build Coastguard Worker {
901*6a54128fSAndroid Build Coastguard Worker struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
902*6a54128fSAndroid Build Coastguard Worker char *dir, *buf = 0;
903*6a54128fSAndroid Build Coastguard Worker
904*6a54128fSAndroid Build Coastguard Worker #ifdef REHASH_DEBUG
905*6a54128fSAndroid Build Coastguard Worker printf("%u: write_dir_block %lld:%lld", wd->ino, blockcnt, *block_nr);
906*6a54128fSAndroid Build Coastguard Worker #endif
907*6a54128fSAndroid Build Coastguard Worker if ((*block_nr == 0) || (blockcnt < 0)) {
908*6a54128fSAndroid Build Coastguard Worker #ifdef REHASH_DEBUG
909*6a54128fSAndroid Build Coastguard Worker printf(" - skip\n");
910*6a54128fSAndroid Build Coastguard Worker #endif
911*6a54128fSAndroid Build Coastguard Worker return 0;
912*6a54128fSAndroid Build Coastguard Worker }
913*6a54128fSAndroid Build Coastguard Worker if (blockcnt < wd->outdir->num)
914*6a54128fSAndroid Build Coastguard Worker dir = wd->outdir->buf + (blockcnt * fs->blocksize);
915*6a54128fSAndroid Build Coastguard Worker else if (wd->ctx->lost_and_found == wd->dir) {
916*6a54128fSAndroid Build Coastguard Worker /* Don't release any extra directory blocks for lost+found */
917*6a54128fSAndroid Build Coastguard Worker wd->err = ext2fs_new_dir_block(fs, 0, 0, &buf);
918*6a54128fSAndroid Build Coastguard Worker if (wd->err)
919*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
920*6a54128fSAndroid Build Coastguard Worker dir = buf;
921*6a54128fSAndroid Build Coastguard Worker wd->outdir->num++;
922*6a54128fSAndroid Build Coastguard Worker } else {
923*6a54128fSAndroid Build Coastguard Worker /* Don't free blocks at the end of the directory, they
924*6a54128fSAndroid Build Coastguard Worker * will be truncated by the caller. */
925*6a54128fSAndroid Build Coastguard Worker #ifdef REHASH_DEBUG
926*6a54128fSAndroid Build Coastguard Worker printf(" - not freed\n");
927*6a54128fSAndroid Build Coastguard Worker #endif
928*6a54128fSAndroid Build Coastguard Worker return 0;
929*6a54128fSAndroid Build Coastguard Worker }
930*6a54128fSAndroid Build Coastguard Worker wd->err = ext2fs_write_dir_block4(fs, *block_nr, dir, 0, wd->dir);
931*6a54128fSAndroid Build Coastguard Worker if (buf)
932*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&buf);
933*6a54128fSAndroid Build Coastguard Worker
934*6a54128fSAndroid Build Coastguard Worker #ifdef REHASH_DEBUG
935*6a54128fSAndroid Build Coastguard Worker printf(" - write (%d)\n", wd->err);
936*6a54128fSAndroid Build Coastguard Worker #endif
937*6a54128fSAndroid Build Coastguard Worker if (wd->err)
938*6a54128fSAndroid Build Coastguard Worker return BLOCK_ABORT;
939*6a54128fSAndroid Build Coastguard Worker return 0;
940*6a54128fSAndroid Build Coastguard Worker }
941*6a54128fSAndroid Build Coastguard Worker
write_directory(e2fsck_t ctx,ext2_filsys fs,struct out_dir * outdir,ext2_ino_t ino,struct ext2_inode * inode,int compress)942*6a54128fSAndroid Build Coastguard Worker static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
943*6a54128fSAndroid Build Coastguard Worker struct out_dir *outdir,
944*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino, struct ext2_inode *inode,
945*6a54128fSAndroid Build Coastguard Worker int compress)
946*6a54128fSAndroid Build Coastguard Worker {
947*6a54128fSAndroid Build Coastguard Worker struct write_dir_struct wd;
948*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
949*6a54128fSAndroid Build Coastguard Worker
950*6a54128fSAndroid Build Coastguard Worker retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
951*6a54128fSAndroid Build Coastguard Worker if (retval)
952*6a54128fSAndroid Build Coastguard Worker return retval;
953*6a54128fSAndroid Build Coastguard Worker
954*6a54128fSAndroid Build Coastguard Worker wd.outdir = outdir;
955*6a54128fSAndroid Build Coastguard Worker wd.err = 0;
956*6a54128fSAndroid Build Coastguard Worker wd.ino = ino;
957*6a54128fSAndroid Build Coastguard Worker wd.ctx = ctx;
958*6a54128fSAndroid Build Coastguard Worker wd.dir = ino;
959*6a54128fSAndroid Build Coastguard Worker
960*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_block_iterate3(fs, ino, 0, NULL,
961*6a54128fSAndroid Build Coastguard Worker write_dir_block, &wd);
962*6a54128fSAndroid Build Coastguard Worker if (retval)
963*6a54128fSAndroid Build Coastguard Worker return retval;
964*6a54128fSAndroid Build Coastguard Worker if (wd.err)
965*6a54128fSAndroid Build Coastguard Worker return wd.err;
966*6a54128fSAndroid Build Coastguard Worker
967*6a54128fSAndroid Build Coastguard Worker e2fsck_read_inode(ctx, ino, inode, "rehash_dir");
968*6a54128fSAndroid Build Coastguard Worker if (compress)
969*6a54128fSAndroid Build Coastguard Worker inode->i_flags &= ~EXT2_INDEX_FL;
970*6a54128fSAndroid Build Coastguard Worker else
971*6a54128fSAndroid Build Coastguard Worker inode->i_flags |= EXT2_INDEX_FL;
972*6a54128fSAndroid Build Coastguard Worker #ifdef REHASH_DEBUG
973*6a54128fSAndroid Build Coastguard Worker printf("%u: set inode size to %u blocks = %u bytes\n",
974*6a54128fSAndroid Build Coastguard Worker ino, outdir->num, outdir->num * fs->blocksize);
975*6a54128fSAndroid Build Coastguard Worker #endif
976*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_inode_size_set(fs, inode, (ext2_off64_t)outdir->num *
977*6a54128fSAndroid Build Coastguard Worker fs->blocksize);
978*6a54128fSAndroid Build Coastguard Worker if (retval)
979*6a54128fSAndroid Build Coastguard Worker return retval;
980*6a54128fSAndroid Build Coastguard Worker
981*6a54128fSAndroid Build Coastguard Worker /* ext2fs_punch() calls ext2fs_write_inode() which writes the size */
982*6a54128fSAndroid Build Coastguard Worker return ext2fs_punch(fs, ino, inode, NULL, outdir->num, ~0ULL);
983*6a54128fSAndroid Build Coastguard Worker }
984*6a54128fSAndroid Build Coastguard Worker
e2fsck_rehash_dir(e2fsck_t ctx,ext2_ino_t ino,struct problem_context * pctx)985*6a54128fSAndroid Build Coastguard Worker errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino,
986*6a54128fSAndroid Build Coastguard Worker struct problem_context *pctx)
987*6a54128fSAndroid Build Coastguard Worker {
988*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs = ctx->fs;
989*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
990*6a54128fSAndroid Build Coastguard Worker struct ext2_inode inode;
991*6a54128fSAndroid Build Coastguard Worker char *dir_buf = 0;
992*6a54128fSAndroid Build Coastguard Worker struct fill_dir_struct fd = { NULL, NULL, 0, 0, 0, NULL,
993*6a54128fSAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0 };
994*6a54128fSAndroid Build Coastguard Worker struct out_dir outdir = { 0, 0, 0, 0 };
995*6a54128fSAndroid Build Coastguard Worker struct name_cmp_ctx name_cmp_ctx = {0, NULL};
996*6a54128fSAndroid Build Coastguard Worker
997*6a54128fSAndroid Build Coastguard Worker e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
998*6a54128fSAndroid Build Coastguard Worker
999*6a54128fSAndroid Build Coastguard Worker if (ext2fs_has_feature_inline_data(fs->super) &&
1000*6a54128fSAndroid Build Coastguard Worker (inode.i_flags & EXT4_INLINE_DATA_FL))
1001*6a54128fSAndroid Build Coastguard Worker return 0;
1002*6a54128fSAndroid Build Coastguard Worker
1003*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(inode.i_size, &dir_buf);
1004*6a54128fSAndroid Build Coastguard Worker if (retval)
1005*6a54128fSAndroid Build Coastguard Worker goto errout;
1006*6a54128fSAndroid Build Coastguard Worker
1007*6a54128fSAndroid Build Coastguard Worker fd.max_array = inode.i_size / 32;
1008*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_array(sizeof(struct hash_entry),
1009*6a54128fSAndroid Build Coastguard Worker fd.max_array, &fd.harray);
1010*6a54128fSAndroid Build Coastguard Worker if (retval)
1011*6a54128fSAndroid Build Coastguard Worker goto errout;
1012*6a54128fSAndroid Build Coastguard Worker
1013*6a54128fSAndroid Build Coastguard Worker fd.ino = ino;
1014*6a54128fSAndroid Build Coastguard Worker fd.ctx = ctx;
1015*6a54128fSAndroid Build Coastguard Worker fd.buf = dir_buf;
1016*6a54128fSAndroid Build Coastguard Worker fd.inode = &inode;
1017*6a54128fSAndroid Build Coastguard Worker fd.dir = ino;
1018*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_has_feature_dir_index(fs->super) ||
1019*6a54128fSAndroid Build Coastguard Worker (inode.i_size / fs->blocksize) < 2)
1020*6a54128fSAndroid Build Coastguard Worker fd.compress = 1;
1021*6a54128fSAndroid Build Coastguard Worker fd.parent = 0;
1022*6a54128fSAndroid Build Coastguard Worker
1023*6a54128fSAndroid Build Coastguard Worker if (fs->encoding && (inode.i_flags & EXT4_CASEFOLD_FL)) {
1024*6a54128fSAndroid Build Coastguard Worker name_cmp_ctx.casefold = 1;
1025*6a54128fSAndroid Build Coastguard Worker name_cmp_ctx.tbl = fs->encoding;
1026*6a54128fSAndroid Build Coastguard Worker }
1027*6a54128fSAndroid Build Coastguard Worker
1028*6a54128fSAndroid Build Coastguard Worker retry_nohash:
1029*6a54128fSAndroid Build Coastguard Worker /* Read in the entire directory into memory */
1030*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_block_iterate3(fs, ino, 0, 0,
1031*6a54128fSAndroid Build Coastguard Worker fill_dir_block, &fd);
1032*6a54128fSAndroid Build Coastguard Worker if (fd.err) {
1033*6a54128fSAndroid Build Coastguard Worker retval = fd.err;
1034*6a54128fSAndroid Build Coastguard Worker goto errout;
1035*6a54128fSAndroid Build Coastguard Worker }
1036*6a54128fSAndroid Build Coastguard Worker
1037*6a54128fSAndroid Build Coastguard Worker /*
1038*6a54128fSAndroid Build Coastguard Worker * If the entries read are less than a block, then don't index
1039*6a54128fSAndroid Build Coastguard Worker * the directory
1040*6a54128fSAndroid Build Coastguard Worker */
1041*6a54128fSAndroid Build Coastguard Worker if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
1042*6a54128fSAndroid Build Coastguard Worker fd.compress = 1;
1043*6a54128fSAndroid Build Coastguard Worker fd.dir_size = 0;
1044*6a54128fSAndroid Build Coastguard Worker fd.num_array = 0;
1045*6a54128fSAndroid Build Coastguard Worker goto retry_nohash;
1046*6a54128fSAndroid Build Coastguard Worker }
1047*6a54128fSAndroid Build Coastguard Worker
1048*6a54128fSAndroid Build Coastguard Worker #if 0
1049*6a54128fSAndroid Build Coastguard Worker printf("%d entries (%d bytes) found in inode %d\n",
1050*6a54128fSAndroid Build Coastguard Worker fd.num_array, fd.dir_size, ino);
1051*6a54128fSAndroid Build Coastguard Worker #endif
1052*6a54128fSAndroid Build Coastguard Worker
1053*6a54128fSAndroid Build Coastguard Worker /* Sort the list */
1054*6a54128fSAndroid Build Coastguard Worker resort:
1055*6a54128fSAndroid Build Coastguard Worker if (fd.compress && fd.num_array > 1)
1056*6a54128fSAndroid Build Coastguard Worker sort_r(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
1057*6a54128fSAndroid Build Coastguard Worker hash_cmp, &name_cmp_ctx);
1058*6a54128fSAndroid Build Coastguard Worker else
1059*6a54128fSAndroid Build Coastguard Worker sort_r(fd.harray, fd.num_array, sizeof(struct hash_entry),
1060*6a54128fSAndroid Build Coastguard Worker hash_cmp, &name_cmp_ctx);
1061*6a54128fSAndroid Build Coastguard Worker
1062*6a54128fSAndroid Build Coastguard Worker /*
1063*6a54128fSAndroid Build Coastguard Worker * Look for duplicates
1064*6a54128fSAndroid Build Coastguard Worker */
1065*6a54128fSAndroid Build Coastguard Worker if (duplicate_search_and_fix(ctx, fs, ino, &fd, &name_cmp_ctx))
1066*6a54128fSAndroid Build Coastguard Worker goto resort;
1067*6a54128fSAndroid Build Coastguard Worker
1068*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_NO) {
1069*6a54128fSAndroid Build Coastguard Worker retval = 0;
1070*6a54128fSAndroid Build Coastguard Worker goto errout;
1071*6a54128fSAndroid Build Coastguard Worker }
1072*6a54128fSAndroid Build Coastguard Worker
1073*6a54128fSAndroid Build Coastguard Worker /* Sort non-hashed directories by inode number */
1074*6a54128fSAndroid Build Coastguard Worker if (fd.compress && fd.num_array > 1)
1075*6a54128fSAndroid Build Coastguard Worker qsort(fd.harray+2, fd.num_array-2,
1076*6a54128fSAndroid Build Coastguard Worker sizeof(struct hash_entry), ino_cmp);
1077*6a54128fSAndroid Build Coastguard Worker
1078*6a54128fSAndroid Build Coastguard Worker /*
1079*6a54128fSAndroid Build Coastguard Worker * Copy the directory entries. In a htree directory these
1080*6a54128fSAndroid Build Coastguard Worker * will become the leaf nodes.
1081*6a54128fSAndroid Build Coastguard Worker */
1082*6a54128fSAndroid Build Coastguard Worker retval = copy_dir_entries(ctx, &fd, &outdir);
1083*6a54128fSAndroid Build Coastguard Worker if (retval)
1084*6a54128fSAndroid Build Coastguard Worker goto errout;
1085*6a54128fSAndroid Build Coastguard Worker
1086*6a54128fSAndroid Build Coastguard Worker free(dir_buf); dir_buf = 0;
1087*6a54128fSAndroid Build Coastguard Worker
1088*6a54128fSAndroid Build Coastguard Worker if (!fd.compress) {
1089*6a54128fSAndroid Build Coastguard Worker /* Calculate the interior nodes */
1090*6a54128fSAndroid Build Coastguard Worker retval = calculate_tree(fs, &outdir, ino, fd.parent, fd.inode);
1091*6a54128fSAndroid Build Coastguard Worker if (retval)
1092*6a54128fSAndroid Build Coastguard Worker goto errout;
1093*6a54128fSAndroid Build Coastguard Worker }
1094*6a54128fSAndroid Build Coastguard Worker
1095*6a54128fSAndroid Build Coastguard Worker retval = write_directory(ctx, fs, &outdir, ino, &inode, fd.compress);
1096*6a54128fSAndroid Build Coastguard Worker if (retval)
1097*6a54128fSAndroid Build Coastguard Worker goto errout;
1098*6a54128fSAndroid Build Coastguard Worker
1099*6a54128fSAndroid Build Coastguard Worker if (ctx->options & E2F_OPT_CONVERT_BMAP)
1100*6a54128fSAndroid Build Coastguard Worker retval = e2fsck_rebuild_extents_later(ctx, ino);
1101*6a54128fSAndroid Build Coastguard Worker else
1102*6a54128fSAndroid Build Coastguard Worker retval = e2fsck_check_rebuild_extents(ctx, ino, &inode, pctx);
1103*6a54128fSAndroid Build Coastguard Worker errout:
1104*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&dir_buf);
1105*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&fd.harray);
1106*6a54128fSAndroid Build Coastguard Worker
1107*6a54128fSAndroid Build Coastguard Worker free_out_dir(&outdir);
1108*6a54128fSAndroid Build Coastguard Worker return retval;
1109*6a54128fSAndroid Build Coastguard Worker }
1110*6a54128fSAndroid Build Coastguard Worker
e2fsck_rehash_directories(e2fsck_t ctx)1111*6a54128fSAndroid Build Coastguard Worker void e2fsck_rehash_directories(e2fsck_t ctx)
1112*6a54128fSAndroid Build Coastguard Worker {
1113*6a54128fSAndroid Build Coastguard Worker struct problem_context pctx;
1114*6a54128fSAndroid Build Coastguard Worker #ifdef RESOURCE_TRACK
1115*6a54128fSAndroid Build Coastguard Worker struct resource_track rtrack;
1116*6a54128fSAndroid Build Coastguard Worker #endif
1117*6a54128fSAndroid Build Coastguard Worker struct dir_info *dir;
1118*6a54128fSAndroid Build Coastguard Worker ext2_u32_iterate iter;
1119*6a54128fSAndroid Build Coastguard Worker struct dir_info_iter * dirinfo_iter = 0;
1120*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
1121*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
1122*6a54128fSAndroid Build Coastguard Worker int cur, max, all_dirs, first = 1;
1123*6a54128fSAndroid Build Coastguard Worker
1124*6a54128fSAndroid Build Coastguard Worker init_resource_track(&rtrack, ctx->fs->io);
1125*6a54128fSAndroid Build Coastguard Worker all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
1126*6a54128fSAndroid Build Coastguard Worker
1127*6a54128fSAndroid Build Coastguard Worker if (!ctx->dirs_to_hash && !all_dirs)
1128*6a54128fSAndroid Build Coastguard Worker return;
1129*6a54128fSAndroid Build Coastguard Worker
1130*6a54128fSAndroid Build Coastguard Worker (void) e2fsck_get_lost_and_found(ctx, 0);
1131*6a54128fSAndroid Build Coastguard Worker
1132*6a54128fSAndroid Build Coastguard Worker clear_problem_context(&pctx);
1133*6a54128fSAndroid Build Coastguard Worker
1134*6a54128fSAndroid Build Coastguard Worker cur = 0;
1135*6a54128fSAndroid Build Coastguard Worker if (all_dirs) {
1136*6a54128fSAndroid Build Coastguard Worker dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
1137*6a54128fSAndroid Build Coastguard Worker max = e2fsck_get_num_dirinfo(ctx);
1138*6a54128fSAndroid Build Coastguard Worker } else {
1139*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
1140*6a54128fSAndroid Build Coastguard Worker &iter);
1141*6a54128fSAndroid Build Coastguard Worker if (retval) {
1142*6a54128fSAndroid Build Coastguard Worker pctx.errcode = retval;
1143*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
1144*6a54128fSAndroid Build Coastguard Worker return;
1145*6a54128fSAndroid Build Coastguard Worker }
1146*6a54128fSAndroid Build Coastguard Worker max = ext2fs_u32_list_count(ctx->dirs_to_hash);
1147*6a54128fSAndroid Build Coastguard Worker }
1148*6a54128fSAndroid Build Coastguard Worker while (1) {
1149*6a54128fSAndroid Build Coastguard Worker if (all_dirs) {
1150*6a54128fSAndroid Build Coastguard Worker if ((dir = e2fsck_dir_info_iter(ctx,
1151*6a54128fSAndroid Build Coastguard Worker dirinfo_iter)) == 0)
1152*6a54128fSAndroid Build Coastguard Worker break;
1153*6a54128fSAndroid Build Coastguard Worker ino = dir->ino;
1154*6a54128fSAndroid Build Coastguard Worker } else {
1155*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_u32_list_iterate(iter, &ino))
1156*6a54128fSAndroid Build Coastguard Worker break;
1157*6a54128fSAndroid Build Coastguard Worker }
1158*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
1159*6a54128fSAndroid Build Coastguard Worker continue;
1160*6a54128fSAndroid Build Coastguard Worker
1161*6a54128fSAndroid Build Coastguard Worker pctx.dir = ino;
1162*6a54128fSAndroid Build Coastguard Worker if (first) {
1163*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
1164*6a54128fSAndroid Build Coastguard Worker first = 0;
1165*6a54128fSAndroid Build Coastguard Worker }
1166*6a54128fSAndroid Build Coastguard Worker #if 0
1167*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
1168*6a54128fSAndroid Build Coastguard Worker #endif
1169*6a54128fSAndroid Build Coastguard Worker pctx.errcode = e2fsck_rehash_dir(ctx, ino, &pctx);
1170*6a54128fSAndroid Build Coastguard Worker if (pctx.errcode) {
1171*6a54128fSAndroid Build Coastguard Worker end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
1172*6a54128fSAndroid Build Coastguard Worker fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
1173*6a54128fSAndroid Build Coastguard Worker }
1174*6a54128fSAndroid Build Coastguard Worker if (ctx->progress && !ctx->progress_fd)
1175*6a54128fSAndroid Build Coastguard Worker e2fsck_simple_progress(ctx, "Rebuilding directory",
1176*6a54128fSAndroid Build Coastguard Worker 100.0 * (float) (++cur) / (float) max, ino);
1177*6a54128fSAndroid Build Coastguard Worker }
1178*6a54128fSAndroid Build Coastguard Worker end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
1179*6a54128fSAndroid Build Coastguard Worker if (all_dirs)
1180*6a54128fSAndroid Build Coastguard Worker e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
1181*6a54128fSAndroid Build Coastguard Worker else
1182*6a54128fSAndroid Build Coastguard Worker ext2fs_u32_list_iterate_end(iter);
1183*6a54128fSAndroid Build Coastguard Worker
1184*6a54128fSAndroid Build Coastguard Worker if (ctx->dirs_to_hash)
1185*6a54128fSAndroid Build Coastguard Worker ext2fs_u32_list_free(ctx->dirs_to_hash);
1186*6a54128fSAndroid Build Coastguard Worker ctx->dirs_to_hash = 0;
1187*6a54128fSAndroid Build Coastguard Worker
1188*6a54128fSAndroid Build Coastguard Worker print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);
1189*6a54128fSAndroid Build Coastguard Worker }
1190