blob: fdfea21d9af32eed5e71850381f5237f2f208eb5 [file] [log] [blame]
Theodore Ts'ob7a00562002-07-20 00:28:07 -04001/*
2 * rehash.c --- rebuild hash tree directories
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'ob7a00562002-07-20 00:28:07 -04004 * Copyright (C) 2002 Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'oefc6f622008-08-27 23:07:54 -040010 *
Theodore Ts'ob7a00562002-07-20 00:28:07 -040011 * This algorithm is designed for simplicity of implementation and to
12 * pack the directory as much as possible. It however requires twice
13 * as much memory as the size of the directory. The maximum size
14 * directory supported using a 4k blocksize is roughly a gigabyte, and
15 * so there may very well be problems with machines that don't have
16 * virtual memory, and obscenely large directories.
17 *
18 * An alternate algorithm which is much more disk intensive could be
19 * written, and probably will need to be written in the future. The
20 * design goals of such an algorithm are: (a) use (roughly) constant
21 * amounts of memory, no matter how large the directory, (b) the
22 * directory must be safe at all times, even if e2fsck is interrupted
23 * in the middle, (c) we must use minimal amounts of extra disk
24 * blocks. This pretty much requires an incremental approach, where
25 * we are reading from one part of the directory, and inserting into
26 * the front half. So the algorithm will have to keep track of a
27 * moving block boundary between the new tree and the old tree, and
28 * files will need to be moved from the old directory and inserted
29 * into the new tree. If the new directory requires space which isn't
30 * yet available, blocks from the beginning part of the old directory
31 * may need to be moved to the end of the directory to make room for
32 * the new tree:
33 *
34 * --------------------------------------------------------
35 * | new tree | | old tree |
36 * --------------------------------------------------------
37 * ^ ptr ^ptr
38 * tail new head old
Theodore Ts'oefc6f622008-08-27 23:07:54 -040039 *
Theodore Ts'ob7a00562002-07-20 00:28:07 -040040 * This is going to be a pain in the tuckus to implement, and will
41 * require a lot more disk accesses. So I'm going to skip it for now;
42 * it's only really going to be an issue for really, really big
43 * filesystems (when we reach the level of tens of millions of files
44 * in a single directory). It will probably be easier to simply
45 * require that e2fsck use VM first.
46 */
47
Theodore Ts'o520ead32003-04-19 13:48:27 -040048#include <string.h>
49#include <ctype.h>
Theodore Ts'ob7a00562002-07-20 00:28:07 -040050#include <errno.h>
51#include "e2fsck.h"
52#include "problem.h"
53
54struct fill_dir_struct {
55 char *buf;
56 struct ext2_inode *inode;
JP Abgralle0ed7402014-03-19 19:08:39 -070057 errcode_t err;
Theodore Ts'ob7a00562002-07-20 00:28:07 -040058 e2fsck_t ctx;
59 struct hash_entry *harray;
60 int max_array, num_array;
JP Abgralle0ed7402014-03-19 19:08:39 -070061 unsigned int dir_size;
Theodore Ts'o850d05e2002-07-25 00:00:08 -040062 int compress;
Theodore Ts'ob7a00562002-07-20 00:28:07 -040063 ino_t parent;
64};
65
66struct hash_entry {
67 ext2_dirhash_t hash;
68 ext2_dirhash_t minor_hash;
Theodore Ts'od66c3832008-01-01 10:59:57 -050069 ino_t ino;
Theodore Ts'ob7a00562002-07-20 00:28:07 -040070 struct ext2_dir_entry *dir;
71};
72
73struct out_dir {
74 int num;
75 int max;
76 char *buf;
77 ext2_dirhash_t *hashes;
78};
79
80static int fill_dir_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -070081 blk64_t *block_nr,
Theodore Ts'ob7a00562002-07-20 00:28:07 -040082 e2_blkcnt_t blockcnt,
JP Abgralle0ed7402014-03-19 19:08:39 -070083 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -050084 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'ob7a00562002-07-20 00:28:07 -040085 void *priv_data)
86{
87 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
88 struct hash_entry *new_array, *ent;
89 struct ext2_dir_entry *dirent;
90 char *dir;
Theodore Ts'o8a480352009-06-21 21:07:38 -040091 unsigned int offset, dir_offset, rec_len;
92 int hash_alg;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040093
Theodore Ts'ob7a00562002-07-20 00:28:07 -040094 if (blockcnt < 0)
95 return 0;
96
97 offset = blockcnt * fs->blocksize;
98 if (offset + fs->blocksize > fd->inode->i_size) {
99 fd->err = EXT2_ET_DIR_CORRUPTED;
100 return BLOCK_ABORT;
101 }
102 dir = (fd->buf+offset);
103 if (HOLE_BLKADDR(*block_nr)) {
104 memset(dir, 0, fs->blocksize);
105 dirent = (struct ext2_dir_entry *) dir;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400106 (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400107 } else {
JP Abgralle0ed7402014-03-19 19:08:39 -0700108 fd->err = ext2fs_read_dir_block3(fs, *block_nr, dir, 0);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400109 if (fd->err)
110 return BLOCK_ABORT;
111 }
Theodore Ts'of77704e2006-11-11 22:32:35 -0500112 hash_alg = fs->super->s_def_hash_version;
113 if ((hash_alg <= EXT2_HASH_TEA) &&
114 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
115 hash_alg += 3;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400116 /* While the directory block is "hot", index it. */
117 dir_offset = 0;
118 while (dir_offset < fs->blocksize) {
119 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
Theodore Ts'o8a480352009-06-21 21:07:38 -0400120 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400121 if (((dir_offset + rec_len) > fs->blocksize) ||
122 (rec_len < 8) ||
123 ((rec_len % 4) != 0) ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700124 (((dirent->name_len & 0xFF)+8U) > rec_len)) {
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400125 fd->err = EXT2_ET_DIR_CORRUPTED;
126 return BLOCK_ABORT;
127 }
Theodore Ts'o5dd77db2008-08-25 21:08:19 -0400128 dir_offset += rec_len;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400129 if (dirent->inode == 0)
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400130 continue;
131 if (!fd->compress && ((dirent->name_len&0xFF) == 1) &&
132 (dirent->name[0] == '.'))
133 continue;
134 if (!fd->compress && ((dirent->name_len&0xFF) == 2) &&
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400135 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
136 fd->parent = dirent->inode;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400137 continue;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400138 }
139 if (fd->num_array >= fd->max_array) {
140 new_array = realloc(fd->harray,
141 sizeof(struct hash_entry) * (fd->max_array+500));
142 if (!new_array) {
143 fd->err = ENOMEM;
144 return BLOCK_ABORT;
145 }
146 fd->harray = new_array;
147 fd->max_array += 500;
148 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400149 ent = fd->harray + fd->num_array++;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400150 ent->dir = dirent;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400151 fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
Theodore Ts'od66c3832008-01-01 10:59:57 -0500152 ent->ino = dirent->inode;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400153 if (fd->compress)
154 ent->hash = ent->minor_hash = 0;
155 else {
Theodore Ts'of77704e2006-11-11 22:32:35 -0500156 fd->err = ext2fs_dirhash(hash_alg, dirent->name,
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400157 dirent->name_len & 0xFF,
158 fs->super->s_hash_seed,
159 &ent->hash, &ent->minor_hash);
160 if (fd->err)
161 return BLOCK_ABORT;
162 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400163 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400164
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400165 return 0;
166}
167
168/* Used for sorting the hash entry */
Theodore Ts'od66c3832008-01-01 10:59:57 -0500169static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
170{
171 const struct hash_entry *he_a = (const struct hash_entry *) a;
172 const struct hash_entry *he_b = (const struct hash_entry *) b;
173
174 return (he_a->ino - he_b->ino);
175}
176
177/* Used for sorting the hash entry */
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400178static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
179{
180 const struct hash_entry *he_a = (const struct hash_entry *) a;
181 const struct hash_entry *he_b = (const struct hash_entry *) b;
182 int ret;
183 int min_len;
184
185 min_len = he_a->dir->name_len;
186 if (min_len > he_b->dir->name_len)
187 min_len = he_b->dir->name_len;
188
189 ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
190 if (ret == 0) {
191 if (he_a->dir->name_len > he_b->dir->name_len)
192 ret = 1;
193 else if (he_a->dir->name_len < he_b->dir->name_len)
194 ret = -1;
195 else
Theodore Ts'o12dd69f2003-04-17 21:55:38 -0400196 ret = he_b->dir->inode - he_a->dir->inode;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400197 }
198 return ret;
199}
200
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500201/* Used for sorting the hash entry */
202static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
203{
204 const struct hash_entry *he_a = (const struct hash_entry *) a;
205 const struct hash_entry *he_b = (const struct hash_entry *) b;
206 int ret;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400207
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500208 if (he_a->hash > he_b->hash)
209 ret = 1;
210 else if (he_a->hash < he_b->hash)
211 ret = -1;
212 else {
213 if (he_a->minor_hash > he_b->minor_hash)
214 ret = 1;
215 else if (he_a->minor_hash < he_b->minor_hash)
216 ret = -1;
217 else
218 ret = name_cmp(a, b);
219 }
220 return ret;
221}
222
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400223static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400224 int blocks)
225{
226 void *new_mem;
227
228 if (outdir->max) {
229 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
230 if (!new_mem)
231 return ENOMEM;
232 outdir->buf = new_mem;
233 new_mem = realloc(outdir->hashes,
234 blocks * sizeof(ext2_dirhash_t));
235 if (!new_mem)
236 return ENOMEM;
237 outdir->hashes = new_mem;
238 } else {
239 outdir->buf = malloc(blocks * fs->blocksize);
240 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
241 outdir->num = 0;
242 }
243 outdir->max = blocks;
244 return 0;
245}
246
247static void free_out_dir(struct out_dir *outdir)
248{
Jim Meyering45e338f2009-02-23 18:07:50 +0100249 free(outdir->buf);
250 free(outdir->hashes);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400251 outdir->max = 0;
252 outdir->num =0;
253}
254
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400255static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400256 char ** ret)
257{
258 errcode_t retval;
259
260 if (outdir->num >= outdir->max) {
261 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
262 if (retval)
263 return retval;
264 }
265 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400266 memset(*ret, 0, fs->blocksize);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400267 return 0;
268}
269
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500270/*
271 * This function is used to make a unique filename. We do this by
272 * appending ~0, and then incrementing the number. However, we cannot
273 * expand the length of the filename beyond the padding available in
274 * the directory entry.
275 */
276static void mutate_name(char *str, __u16 *len)
277{
278 int i;
279 __u16 l = *len & 0xFF, h = *len & 0xff00;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400280
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500281 /*
282 * First check to see if it looks the name has been mutated
283 * already
284 */
285 for (i = l-1; i > 0; i--) {
286 if (!isdigit(str[i]))
287 break;
288 }
289 if ((i == l-1) || (str[i] != '~')) {
290 if (((l-1) & 3) < 2)
291 l += 2;
292 else
293 l = (l+3) & ~3;
294 str[l-2] = '~';
295 str[l-1] = '0';
296 *len = l | h;
297 return;
298 }
299 for (i = l-1; i >= 0; i--) {
300 if (isdigit(str[i])) {
301 if (str[i] == '9')
302 str[i] = '0';
303 else {
304 str[i]++;
305 return;
306 }
307 continue;
308 }
309 if (i == 1) {
310 if (str[0] == 'z')
311 str[0] = 'A';
312 else if (str[0] == 'Z') {
313 str[0] = '~';
314 str[1] = '0';
315 } else
316 str[0]++;
317 } else if (i > 0) {
318 str[i] = '1';
319 str[i-1] = '~';
320 } else {
321 if (str[0] == '~')
322 str[0] = 'a';
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400323 else
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500324 str[0]++;
325 }
326 break;
327 }
328}
329
330static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
331 ext2_ino_t ino,
332 struct fill_dir_struct *fd)
333{
334 struct problem_context pctx;
335 struct hash_entry *ent, *prev;
336 int i, j;
337 int fixed = 0;
338 char new_name[256];
339 __u16 new_len;
Theodore Ts'of77704e2006-11-11 22:32:35 -0500340 int hash_alg;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400341
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500342 clear_problem_context(&pctx);
343 pctx.ino = ino;
344
Theodore Ts'of77704e2006-11-11 22:32:35 -0500345 hash_alg = fs->super->s_def_hash_version;
346 if ((hash_alg <= EXT2_HASH_TEA) &&
347 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
348 hash_alg += 3;
349
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500350 for (i=1; i < fd->num_array; i++) {
351 ent = fd->harray + i;
352 prev = ent - 1;
353 if (!ent->dir->inode ||
354 ((ent->dir->name_len & 0xFF) !=
355 (prev->dir->name_len & 0xFF)) ||
356 (strncmp(ent->dir->name, prev->dir->name,
357 ent->dir->name_len & 0xFF)))
358 continue;
359 pctx.dirent = ent->dir;
360 if ((ent->dir->inode == prev->dir->inode) &&
361 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
362 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
363 ent->dir->inode = 0;
364 fixed++;
365 continue;
366 }
367 memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF);
368 new_len = ent->dir->name_len;
369 mutate_name(new_name, &new_len);
370 for (j=0; j < fd->num_array; j++) {
371 if ((i==j) ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700372 ((new_len & 0xFF) !=
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500373 (fd->harray[j].dir->name_len & 0xFF)) ||
374 (strncmp(new_name, fd->harray[j].dir->name,
375 new_len & 0xFF)))
376 continue;
377 mutate_name(new_name, &new_len);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400378
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500379 j = -1;
380 }
381 new_name[new_len & 0xFF] = 0;
382 pctx.str = new_name;
383 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
384 memcpy(ent->dir->name, new_name, new_len & 0xFF);
385 ent->dir->name_len = new_len;
Theodore Ts'of77704e2006-11-11 22:32:35 -0500386 ext2fs_dirhash(hash_alg, ent->dir->name,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500387 ent->dir->name_len & 0xFF,
388 fs->super->s_hash_seed,
389 &ent->hash, &ent->minor_hash);
390 fixed++;
391 }
392 }
393 return fixed;
394}
395
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400396
Theodore Ts'o7dca4c82008-12-23 19:10:43 -0500397static errcode_t copy_dir_entries(e2fsck_t ctx,
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400398 struct fill_dir_struct *fd,
399 struct out_dir *outdir)
400{
Theodore Ts'o7dca4c82008-12-23 19:10:43 -0500401 ext2_filsys fs = ctx->fs;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400402 errcode_t retval;
403 char *block_start;
404 struct hash_entry *ent;
405 struct ext2_dir_entry *dirent;
JP Abgralle0ed7402014-03-19 19:08:39 -0700406 unsigned int rec_len, prev_rec_len, left, slack, offset;
407 int i;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400408 ext2_dirhash_t prev_hash;
Theodore Ts'o7dca4c82008-12-23 19:10:43 -0500409
410 if (ctx->htree_slack_percentage == 255) {
411 profile_get_uint(ctx->profile, "options",
412 "indexed_dir_slack_percentage",
413 0, 20,
414 &ctx->htree_slack_percentage);
415 if (ctx->htree_slack_percentage > 100)
416 ctx->htree_slack_percentage = 20;
417 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400418
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400419 outdir->max = 0;
420 retval = alloc_size_dir(fs, outdir,
421 (fd->dir_size / fs->blocksize) + 2);
422 if (retval)
423 return retval;
424 outdir->num = fd->compress ? 0 : 1;
425 offset = 0;
426 outdir->hashes[0] = 0;
427 prev_hash = 1;
428 if ((retval = get_next_block(fs, outdir, &block_start)))
429 return retval;
430 dirent = (struct ext2_dir_entry *) block_start;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400431 prev_rec_len = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700432 rec_len = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400433 left = fs->blocksize;
Theodore Ts'o7dca4c82008-12-23 19:10:43 -0500434 slack = fd->compress ? 12 :
435 (fs->blocksize * ctx->htree_slack_percentage)/100;
436 if (slack < 12)
437 slack = 12;
JP Abgralle0ed7402014-03-19 19:08:39 -0700438 for (i = 0; i < fd->num_array; i++) {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400439 ent = fd->harray + i;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500440 if (ent->dir->inode == 0)
441 continue;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400442 rec_len = EXT2_DIR_REC_LEN(ent->dir->name_len & 0xFF);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400443 if (rec_len > left) {
Theodore Ts'o8a480352009-06-21 21:07:38 -0400444 if (left) {
445 left += prev_rec_len;
446 retval = ext2fs_set_rec_len(fs, left, dirent);
447 if (retval)
448 return retval;
449 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400450 if ((retval = get_next_block(fs, outdir,
451 &block_start)))
452 return retval;
Theodore Ts'ofe5b72d2002-09-29 19:05:26 -0400453 offset = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400454 }
Theodore Ts'ofe5b72d2002-09-29 19:05:26 -0400455 left = fs->blocksize - offset;
456 dirent = (struct ext2_dir_entry *) (block_start + offset);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400457 if (offset == 0) {
458 if (ent->hash == prev_hash)
459 outdir->hashes[outdir->num-1] = ent->hash | 1;
460 else
461 outdir->hashes[outdir->num-1] = ent->hash;
462 }
463 dirent->inode = ent->dir->inode;
464 dirent->name_len = ent->dir->name_len;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400465 retval = ext2fs_set_rec_len(fs, rec_len, dirent);
466 if (retval)
467 return retval;
468 prev_rec_len = rec_len;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400469 memcpy(dirent->name, ent->dir->name, dirent->name_len & 0xFF);
470 offset += rec_len;
471 left -= rec_len;
Theodore Ts'o7dca4c82008-12-23 19:10:43 -0500472 if (left < slack) {
Theodore Ts'o8a480352009-06-21 21:07:38 -0400473 prev_rec_len += left;
474 retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
475 if (retval)
476 return retval;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400477 offset += left;
Theodore Ts'ocf3909e2002-09-06 10:14:12 -0400478 left = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400479 }
480 prev_hash = ent->hash;
481 }
482 if (left)
Theodore Ts'o8a480352009-06-21 21:07:38 -0400483 retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400484
Theodore Ts'o8a480352009-06-21 21:07:38 -0400485 return retval;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400486}
487
488
489static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400490 ext2_ino_t ino, ext2_ino_t parent)
491{
492 struct ext2_dir_entry *dir;
493 struct ext2_dx_root_info *root;
494 struct ext2_dx_countlimit *limits;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400495 int filetype = 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400496
497 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
498 filetype = EXT2_FT_DIR << 8;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400499
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400500 memset(buf, 0, fs->blocksize);
501 dir = (struct ext2_dir_entry *) buf;
502 dir->inode = ino;
503 dir->name[0] = '.';
504 dir->name_len = 1 | filetype;
505 dir->rec_len = 12;
506 dir = (struct ext2_dir_entry *) (buf + 12);
507 dir->inode = parent;
508 dir->name[0] = '.';
509 dir->name[1] = '.';
510 dir->name_len = 2 | filetype;
511 dir->rec_len = fs->blocksize - 12;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400512
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400513 root = (struct ext2_dx_root_info *) (buf+24);
514 root->reserved_zero = 0;
515 root->hash_version = fs->super->s_def_hash_version;
516 root->info_length = 8;
517 root->indirect_levels = 0;
518 root->unused_flags = 0;
519
520 limits = (struct ext2_dx_countlimit *) (buf+32);
521 limits->limit = (fs->blocksize - 32) / sizeof(struct ext2_dx_entry);
522 limits->count = 0;
523
524 return root;
525}
526
527
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400528static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400529{
530 struct ext2_dir_entry *dir;
531 struct ext2_dx_countlimit *limits;
532
533 memset(buf, 0, fs->blocksize);
534 dir = (struct ext2_dir_entry *) buf;
535 dir->inode = 0;
Theodore Ts'o8a480352009-06-21 21:07:38 -0400536 (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400537
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400538 limits = (struct ext2_dx_countlimit *) (buf+8);
539 limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
540 limits->count = 0;
541
542 return (struct ext2_dx_entry *) limits;
543}
544
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400545/*
546 * This function takes the leaf nodes which have been written in
547 * outdir, and populates the root node and any necessary interior nodes.
548 */
549static errcode_t calculate_tree(ext2_filsys fs,
550 struct out_dir *outdir,
551 ext2_ino_t ino,
552 ext2_ino_t parent)
553{
554 struct ext2_dx_root_info *root_info;
555 struct ext2_dx_entry *root, *dx_ent = 0;
556 struct ext2_dx_countlimit *root_limit, *limit;
557 errcode_t retval;
558 char * block_start;
559 int i, c1, c2, nblks;
560 int limit_offset, root_offset;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400561
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400562 root_info = set_root_node(fs, outdir->buf, ino, parent);
563 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
564 root_info->info_length;
565 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
566 c1 = root_limit->limit;
567 nblks = outdir->num;
568
569 /* Write out the pointer blocks */
570 if (nblks-1 <= c1) {
571 /* Just write out the root block, and we're done */
572 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
573 for (i=1; i < nblks; i++) {
574 root->block = ext2fs_cpu_to_le32(i);
575 if (i != 1)
576 root->hash =
577 ext2fs_cpu_to_le32(outdir->hashes[i]);
578 root++;
579 c1--;
580 }
581 } else {
582 c2 = 0;
583 limit = 0;
584 root_info->indirect_levels = 1;
585 for (i=1; i < nblks; i++) {
586 if (c1 == 0)
587 return ENOSPC;
588 if (c2 == 0) {
589 if (limit)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400590 limit->limit = limit->count =
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400591 ext2fs_cpu_to_le16(limit->limit);
592 root = (struct ext2_dx_entry *)
593 (outdir->buf + root_offset);
594 root->block = ext2fs_cpu_to_le32(outdir->num);
595 if (i != 1)
596 root->hash =
597 ext2fs_cpu_to_le32(outdir->hashes[i]);
598 if ((retval = get_next_block(fs, outdir,
599 &block_start)))
600 return retval;
601 dx_ent = set_int_node(fs, block_start);
602 limit = (struct ext2_dx_countlimit *) dx_ent;
603 c2 = limit->limit;
604 root_offset += sizeof(struct ext2_dx_entry);
605 c1--;
606 }
607 dx_ent->block = ext2fs_cpu_to_le32(i);
608 if (c2 != limit->limit)
609 dx_ent->hash =
610 ext2fs_cpu_to_le32(outdir->hashes[i]);
611 dx_ent++;
612 c2--;
613 }
614 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
615 limit->limit = ext2fs_cpu_to_le16(limit->limit);
616 }
617 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
618 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
619 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
620
621 return 0;
622}
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400623
624struct write_dir_struct {
625 struct out_dir *outdir;
626 errcode_t err;
627 e2fsck_t ctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700628 blk64_t cleared;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400629};
630
631/*
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400632 * Helper function which writes out a directory block.
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400633 */
634static int write_dir_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -0700635 blk64_t *block_nr,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400636 e2_blkcnt_t blockcnt,
JP Abgralle0ed7402014-03-19 19:08:39 -0700637 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400638 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400639 void *priv_data)
640{
641 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
JP Abgralle0ed7402014-03-19 19:08:39 -0700642 blk64_t blk;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400643 char *dir;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400644
645 if (*block_nr == 0)
646 return 0;
647 if (blockcnt >= wd->outdir->num) {
648 e2fsck_read_bitmaps(wd->ctx);
649 blk = *block_nr;
JP Abgralle0ed7402014-03-19 19:08:39 -0700650 /*
651 * In theory, we only release blocks from the end of the
652 * directory file, so it's fine to clobber a whole cluster at
653 * once.
654 */
655 if (blk % EXT2FS_CLUSTER_RATIO(fs) == 0) {
656 ext2fs_unmark_block_bitmap2(wd->ctx->block_found_map,
657 blk);
658 ext2fs_block_alloc_stats2(fs, blk, -1);
659 wd->cleared++;
660 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400661 *block_nr = 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400662 return BLOCK_CHANGED;
663 }
664 if (blockcnt < 0)
665 return 0;
666
667 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
JP Abgralle0ed7402014-03-19 19:08:39 -0700668 wd->err = ext2fs_write_dir_block3(fs, *block_nr, dir, 0);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400669 if (wd->err)
670 return BLOCK_ABORT;
671 return 0;
672}
673
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400674static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400675 struct out_dir *outdir,
676 ext2_ino_t ino, int compress)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400677{
678 struct write_dir_struct wd;
679 errcode_t retval;
680 struct ext2_inode inode;
681
682 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
683 if (retval)
684 return retval;
685
686 wd.outdir = outdir;
687 wd.err = 0;
688 wd.ctx = ctx;
689 wd.cleared = 0;
690
JP Abgralle0ed7402014-03-19 19:08:39 -0700691 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400692 write_dir_block, &wd);
693 if (retval)
694 return retval;
695 if (wd.err)
696 return wd.err;
697
698 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400699 if (compress)
700 inode.i_flags &= ~EXT2_INDEX_FL;
701 else
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400702 inode.i_flags |= EXT2_INDEX_FL;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400703 inode.i_size = outdir->num * fs->blocksize;
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400704 ext2fs_iblk_sub_blocks(fs, &inode, wd.cleared);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400705 e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
706
707 return 0;
708}
709
710errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
711{
712 ext2_filsys fs = ctx->fs;
713 errcode_t retval;
714 struct ext2_inode inode;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400715 char *dir_buf = 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400716 struct fill_dir_struct fd;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400717 struct out_dir outdir;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400718
Theodore Ts'o1d2eef42003-03-15 13:30:07 -0500719 outdir.max = outdir.num = 0;
720 outdir.buf = 0;
721 outdir.hashes = 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400722 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400723
724 retval = ENOMEM;
725 fd.harray = 0;
726 dir_buf = malloc(inode.i_size);
727 if (!dir_buf)
728 goto errout;
729
730 fd.max_array = inode.i_size / 32;
731 fd.num_array = 0;
732 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
733 if (!fd.harray)
734 goto errout;
735
736 fd.ctx = ctx;
737 fd.buf = dir_buf;
738 fd.inode = &inode;
739 fd.err = 0;
740 fd.dir_size = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400741 fd.compress = 0;
742 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400743 (inode.i_size / fs->blocksize) < 2)
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400744 fd.compress = 1;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400745 fd.parent = 0;
746
Theodore Ts'of4e14502009-11-29 01:22:44 -0500747retry_nohash:
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400748 /* Read in the entire directory into memory */
JP Abgralle0ed7402014-03-19 19:08:39 -0700749 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400750 fill_dir_block, &fd);
751 if (fd.err) {
752 retval = fd.err;
753 goto errout;
754 }
755
Theodore Ts'of4e14502009-11-29 01:22:44 -0500756 /*
757 * If the entries read are less than a block, then don't index
758 * the directory
759 */
760 if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
761 fd.compress = 1;
762 fd.dir_size = 0;
763 fd.num_array = 0;
764 goto retry_nohash;
765 }
766
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400767#if 0
768 printf("%d entries (%d bytes) found in inode %d\n",
769 fd.num_array, fd.dir_size, ino);
770#endif
771
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400772 /* Sort the list */
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500773resort:
Theodore Ts'o53fbfb22010-02-22 23:51:26 -0500774 if (fd.compress)
775 qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
776 hash_cmp);
777 else
778 qsort(fd.harray, fd.num_array, sizeof(struct hash_entry),
779 hash_cmp);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400780
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400781 /*
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500782 * Look for duplicates
783 */
784 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
785 goto resort;
786
Theodore Ts'o1d2eef42003-03-15 13:30:07 -0500787 if (ctx->options & E2F_OPT_NO) {
788 retval = 0;
789 goto errout;
790 }
791
Theodore Ts'ob71e0182009-11-16 21:56:24 -0500792 /* Sort non-hashed directories by inode number */
793 if (fd.compress)
794 qsort(fd.harray+2, fd.num_array-2,
795 sizeof(struct hash_entry), ino_cmp);
796
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500797 /*
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400798 * Copy the directory entries. In a htree directory these
799 * will become the leaf nodes.
800 */
Theodore Ts'o7dca4c82008-12-23 19:10:43 -0500801 retval = copy_dir_entries(ctx, &fd, &outdir);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400802 if (retval)
803 goto errout;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400804
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400805 free(dir_buf); dir_buf = 0;
806
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400807 if (!fd.compress) {
808 /* Calculate the interior nodes */
809 retval = calculate_tree(fs, &outdir, ino, fd.parent);
810 if (retval)
811 goto errout;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400812 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400813
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400814 retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400815 if (retval)
816 goto errout;
817
818errout:
Jim Meyering45e338f2009-02-23 18:07:50 +0100819 free(dir_buf);
820 free(fd.harray);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400821
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400822 free_out_dir(&outdir);
823 return retval;
824}
825
826void e2fsck_rehash_directories(e2fsck_t ctx)
827{
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400828 struct problem_context pctx;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400829#ifdef RESOURCE_TRACK
830 struct resource_track rtrack;
831#endif
832 struct dir_info *dir;
833 ext2_u32_iterate iter;
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400834 struct dir_info_iter * dirinfo_iter = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400835 ext2_ino_t ino;
836 errcode_t retval;
JP Abgralle0ed7402014-03-19 19:08:39 -0700837 int cur, max, all_dirs, first = 1;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400838
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400839 init_resource_track(&rtrack, ctx->fs->io);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400840 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
841
842 if (!ctx->dirs_to_hash && !all_dirs)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400843 return;
844
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400845 e2fsck_get_lost_and_found(ctx, 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400846
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400847 clear_problem_context(&pctx);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400848
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500849 cur = 0;
850 if (all_dirs) {
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400851 dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500852 max = e2fsck_get_num_dirinfo(ctx);
853 } else {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400854 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400855 &iter);
856 if (retval) {
857 pctx.errcode = retval;
858 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
859 return;
860 }
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500861 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400862 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400863 while (1) {
864 if (all_dirs) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400865 if ((dir = e2fsck_dir_info_iter(ctx,
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400866 dirinfo_iter)) == 0)
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400867 break;
868 ino = dir->ino;
869 } else {
870 if (!ext2fs_u32_list_iterate(iter, &ino))
871 break;
872 }
873 if (ino == ctx->lost_and_found)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400874 continue;
875 pctx.dir = ino;
876 if (first) {
877 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
878 first = 0;
879 }
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500880#if 0
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400881 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500882#endif
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400883 pctx.errcode = e2fsck_rehash_dir(ctx, ino);
884 if (pctx.errcode) {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400885 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
886 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400887 }
Theodore Ts'o52734dc2003-03-15 04:03:43 -0500888 if (ctx->progress && !ctx->progress_fd)
889 e2fsck_simple_progress(ctx, "Rebuilding directory",
Theodore Ts'o1d2eef42003-03-15 13:30:07 -0500890 100.0 * (float) (++cur) / (float) max, ino);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400891 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400892 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400893 if (all_dirs)
894 e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
895 else
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400896 ext2fs_u32_list_iterate_end(iter);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400897
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400898 if (ctx->dirs_to_hash)
899 ext2fs_u32_list_free(ctx->dirs_to_hash);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400900 ctx->dirs_to_hash = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400901
Ken Chen9facd072009-05-28 09:55:10 -0400902 print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400903}