blob: 327cf16da82d2f94864cdd9d86e630b7ecad1b60 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass2.c --- check directory structure
3 *
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
6 *
7 * Pass 2 of e2fsck iterates through all active directory inodes, and
8 * applies to following tests to each directory entry in the directory
9 * blocks in the inodes:
10 *
11 * - The length of the directory entry (rec_len) should be at
12 * least 8 bytes, and no more than the remaining space
13 * left in the directory block.
14 * - The length of the name in the directory entry (name_len)
15 * should be less than (rec_len - 8).
16 * - The inode number in the directory entry should be within
17 * legal bounds.
18 * - The inode number should refer to a in-use inode.
19 * - The first entry should be '.', and its inode should be
20 * the inode of the directory.
21 * - The second entry should be '..'.
22 *
23 * To minimize disk seek time, the directory blocks are processed in
24 * sorted order of block numbers.
25 *
26 * Pass 2 also collects the following information:
27 * - The inode numbers of the subdirectories for each directory.
28 *
29 * Pass 2 relies on the following information from previous passes:
30 * - The directory information collected in pass 1.
31 * - The inode_used_map bitmap
32 * - The inode_bad_map bitmap
33 * - The inode_dir_map bitmap
34 * - The block_dup_map bitmap
35 *
36 * Pass 2 frees the following data structures
37 * - The inode_bad_map bitmap
38 */
39
40#include "et/com_err.h"
41
42#include "e2fsck.h"
43
44/*
45 * Keeps track of how many times an inode is referenced.
46 */
47unsigned short * inode_count;
48
49static void deallocate_inode(ext2_filsys fs, ino_t ino,
50 char* block_buf);
51static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
52static int check_dir_block(ext2_filsys fs,
53 struct dir_block_struct *dir_blocks_info,
54 char *buf);
55
56void pass2(ext2_filsys fs)
57{
58 int i;
59 char *buf;
60 struct resource_track rtrack;
61
62 init_resource_track(&rtrack);
63
64#ifdef MTRACE
65 mtrace_print("Pass 2");
66#endif
67
68 if (!preen)
69 printf("Pass 2: Checking directory structure\n");
70 inode_count = allocate_memory((fs->super->s_inodes_count + 1) *
71 sizeof(unsigned short),
72 "buffer for inode count");
73
74 buf = allocate_memory(fs->blocksize, "directory scan buffer");
75
76 for (i=0; i < dir_block_count; i++)
77 check_dir_block(fs, &dir_blocks[i], buf);
78
79 free(buf);
80 free(dir_blocks);
81 if (inode_bad_map) {
82 free(inode_bad_map);
83 inode_bad_map = 0;
84 }
85 if (tflag > 1) {
86 printf("Pass 2: ");
87 print_resource_track(&rtrack);
88 }
89}
90
91/*
92 * Make sure the first entry in the directory is '.', and that the
93 * directory entry is sane.
94 */
95static int check_dot(ext2_filsys fs,
96 struct ext2_dir_entry *dirent,
97 ino_t ino)
98{
99 struct ext2_dir_entry *nextdir;
100 int status = 0;
101 int created = 0;
102 int new_len;
103 char name[BLOCK_SIZE];
104
105 if (!dirent->inode) {
106 printf("Missing '.' in directory inode %ld.\n", ino);
107 if (dirent->rec_len < 12)
108 fatal_error("Cannot fix, insufficient space to add '.'");
109 preenhalt();
110 if (ask("Fix", 1)) {
111 dirent->inode = ino;
112 dirent->name_len = 1;
113 dirent->name[0] = '.';
114 status = 1;
115 created = 1;
116 } else {
117 ext2fs_unmark_valid(fs);
118 return 0;
119 }
120 }
121 if ((dirent->name_len != 1) ||
122 strncmp(dirent->name, ".", dirent->name_len)) {
123 strncpy(name, dirent->name, dirent->name_len);
124 name[dirent->name_len] = '\0';
125 printf("Missing '.' in directory inode %ld.\n", ino);
126 printf("Cannot fix, first entry in directory contains '%s'\n",
127 name);
128 exit(FSCK_ERROR);
129 }
130 if (dirent->inode != ino) {
131 printf("Bad inode number for '.' in directory inode %ld.\n",
132 ino);
133 preenhalt();
134 if (ask("Fix", 1)) {
135 dirent->inode = ino;
136 status = 1;
137 } else
138 ext2fs_unmark_valid(fs);
139 }
140 if (dirent->rec_len > 12) {
141 new_len = dirent->rec_len - 12;
142 if (new_len > 12) {
143 preenhalt();
144 if (created ||
145 ask("Directory entry for '.' is big. Split", 1)) {
146 nextdir = (struct ext2_dir_entry *)
147 ((char *) dirent + 12);
148 dirent->rec_len = 12;
149 nextdir->rec_len = new_len;
150 nextdir->inode = 0;
151 nextdir->name_len = 0;
152 status = 1;
153 }
154 }
155 }
156 return status;
157}
158
159/*
160 * Make sure the second entry in the directory is '..', and that the
161 * directory entry is sane. We do not check the inode number of '..'
162 * here; this gets done in pass 3.
163 */
164static int check_dotdot(ext2_filsys fs,
165 struct ext2_dir_entry *dirent,
166 struct dir_info *dir)
167{
168 char name[BLOCK_SIZE];
169 int ino = dir->ino;
170
171 if (!dirent->inode) {
172 printf("Missing '..' in directory inode %d.\n", ino);
173 if (dirent->rec_len < 12)
174 fatal_error("Cannot fix, insufficient space to add '..'");
175 preenhalt();
176 if (ask("Fix", 1)) {
177 /*
178 * Note: we don't have the parent inode just
179 * yet, so we will fill it in with the root
180 * inode. This will get fixed in pass 3.
181 */
182 dirent->inode = EXT2_ROOT_INO;
183 dirent->name_len = 2;
184 dirent->name[0] = '.';
185 dirent->name[1] = '.';
186 return 1;
187 } else
188 ext2fs_unmark_valid(fs);
189 return 0;
190 }
191 if ((dirent->name_len != 2) ||
192 strncmp(dirent->name, "..", dirent->name_len)) {
193 strncpy(name, dirent->name, dirent->name_len);
194 name[dirent->name_len] = '\0';
195 printf("Missing '..' in directory inode %d.\n", ino);
196 printf("Cannot fix, first entry in directory contains %s\n",
197 name);
198 exit(FSCK_ERROR);
199 }
200 dir->dotdot = dirent->inode;
201 return 0;
202}
203
204/*
205 * Check to make sure a directory entry doesn't contain any illegal
206 * characters.
207 */
208static int check_name(ext2_filsys fs,
209 struct ext2_dir_entry *dirent,
210 ino_t dir_ino,
211 char *name)
212{
213 int i;
214 int fixup = -1;
215 char *pathname;
216 int ret = 0;
217 errcode_t retval;
218
219 for ( i = 0; i < dirent->name_len; i++) {
220 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
221 if (fixup < 0) {
222 retval = ext2fs_get_pathname(fs, dir_ino,
223 0, &pathname);
224 if (retval) {
225 com_err(program_name, retval, "while getting pathname in check_name");
226 fatal_error(0);
227 }
228 printf ("Bad file name '%s' (contains '/' or "
229 " null) in directory '%s'",
230 pathname, name);
231 free(pathname);
232 preenhalt();
233 fixup = ask("Replace '/' or null by '.'", 1);
234 }
235 if (fixup) {
236 dirent->name[i] = '.';
237 ret = 1;
238 } else
239 ext2fs_unmark_valid(fs);
240 }
241 }
242 return ret;
243}
244
245static int check_dir_block(ext2_filsys fs,
246 struct dir_block_struct *db,
247 char *buf)
248{
249 struct dir_info *subdir, *dir;
250 struct ext2_dir_entry *dirent;
251 char name[BLOCK_SIZE];
252 int offset = 0;
253 int dir_modified = 0;
254 errcode_t retval;
255 char *path1, *path2;
256 int dot_state;
257 blk_t block_nr = db->blk;
258 ino_t ino = db->ino;
259 static char unknown[] = "???";
260
261 /*
262 * Make sure the inode is still in use (could have been
263 * deleted in the duplicate/bad blocks pass.
264 */
265 if (!(ext2fs_test_inode_bitmap(fs, inode_used_map, ino)))
266 return 0;
267
268 if (db->blockcnt)
269 dot_state = 2;
270 else
271 dot_state = 0;
272
273#if 0
274 printf("In process_dir_block block %d, #%d, inode %d\n", block_nr,
275 db->blockcnt, ino);
276#endif
277
278 retval = io_channel_read_blk(fs->io, block_nr, 1, buf);
279 if (retval) {
280 com_err(program_name, retval,
281 "while reading directory block %d", block_nr);
282 }
283
284 do {
285 dot_state++;
286 dirent = (struct ext2_dir_entry *) (buf + offset);
287 if (((offset + dirent->rec_len) > fs->blocksize) ||
288 (dirent->rec_len < 8) ||
289 ((dirent->name_len+8) > dirent->rec_len)) {
290 printf("Directory inode %ld, block %d, offset %d: directory corrupted\n",
291 ino, db->blockcnt, offset);
292 preenhalt();
293 if (ask("Salvage", 1)) {
294 dirent->rec_len = fs->blocksize - offset;
295 dirent->name_len = 0;
296 dirent->inode = 0;
297 dir_modified++;
298 } else {
299 ext2fs_unmark_valid(fs);
300 return DIRENT_ABORT;
301 }
302 }
303 strncpy(name, dirent->name, dirent->name_len);
304 name[dirent->name_len] = '\0';
305 if (dot_state == 1) {
306 if (check_dot(fs, dirent, ino))
307 dir_modified++;
308 } else if (dot_state == 2) {
309 dir = get_dir_info(ino);
310 if (!dir) {
311 printf("Internal error: couldn't find dir_info for %ld\n",
312 ino);
313 fatal_error(0);
314 }
315 if (check_dotdot(fs, dirent, dir))
316 dir_modified++;
317 } else if (dirent->inode == ino) {
318 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
319 if (retval)
320 path1 = unknown;
321 printf("Entry '%s' in %s (%ld) is a link to '.' ",
322 name, path1, ino);
323 if (path1 != unknown)
324 free(path1);
325 preenhalt();
326 if (ask("Clear", 1)) {
327 dirent->inode = 0;
328 dir_modified++;
329 }
330 }
331 if (!dirent->inode)
332 goto next;
333
334#if 0
335 printf("Entry '%s', name_len %d, rec_len %d, inode %d... ",
336 name, dirent->name_len, dirent->rec_len, dirent->inode);
337#endif
338 if (check_name(fs, dirent, ino, name))
339 dir_modified++;
340
341 /*
342 * Make sure the inode listed is a legal one.
343 */
344 if (((dirent->inode != EXT2_ROOT_INO) &&
345 (dirent->inode < EXT2_FIRST_INO)) ||
346 (dirent->inode > fs->super->s_inodes_count)) {
347 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
348 if (retval)
349 path1 = unknown;
350 printf("Entry '%s' in %s (%ld) has bad inode #: %ld.\n",
351 name, path1, ino, dirent->inode);
352 if (path1 != unknown)
353 free(path1);
354 preenhalt();
355 if (ask("Clear", 1)) {
356 dirent->inode = 0;
357 dir_modified++;
358 goto next;
359 } else
360 ext2fs_unmark_valid(fs);
361 }
362
363 /*
364 * If the inode is unusued, offer to clear it.
365 */
366 if (!(ext2fs_test_inode_bitmap(fs, inode_used_map,
367 dirent->inode))) {
368 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
369 if (retval)
370 path1 = unknown;
371 printf("Entry '%s' in %s (%ld) has deleted/unused inode %ld.\n",
372 name, path1, ino, dirent->inode);
373 if (path1 != unknown)
374 free(path1);
375 if (ask("Clear", 1)) {
376 dirent->inode = 0;
377 dir_modified++;
378 goto next;
379 } else
380 ext2fs_unmark_valid(fs);
381 }
382
383 /*
384 * If the inode was marked as having bad fields in
385 * pass1, process it and offer to fix/clear it.
386 * (We wait until now so that we can display the
387 * pathname to the user.)
388 */
389 if (inode_bad_map &&
390 ext2fs_test_inode_bitmap(fs, inode_bad_map,
391 dirent->inode)) {
392 if (process_bad_inode(fs, ino, dirent->inode)) {
393 dirent->inode = 0;
394 dir_modified++;
395 goto next;
396 }
397 }
398
399 /*
400 * If this is a directory, then mark its parent in its
401 * dir_info structure. If the parent field is already
402 * filled in, then this directory has more than one
403 * hard link. We assume the first link is correct,
404 * and ask the user if he/she wants to clear this one.
405 */
406 if ((dot_state > 2) &&
407 (ext2fs_test_inode_bitmap(fs, inode_dir_map,
408 dirent->inode))) {
409 subdir = get_dir_info(dirent->inode);
410 if (!subdir) {
411 printf("INTERNAL ERROR: missing dir %ld\n",
412 dirent->inode);
413 fatal_error(0);
414 }
415 if (subdir->parent) {
416 retval = ext2fs_get_pathname(fs, ino,
417 0, &path1);
418 if (retval)
419 path1 = unknown;
420 retval = ext2fs_get_pathname(fs,
421 subdir->parent,
422 dirent->inode,
423 &path2);
424 if (retval)
425 path2 = unknown;
426 printf("Entry '%s' in %s (%ld) is a link to directory %s (%ld).\n",
427 name, path1, ino, path2,
428 dirent->inode);
429 if (path1 != unknown)
430 free(path1);
431 if (path2 != unknown)
432 free(path2);
433 if (ask("Clear", 1)) {
434 dirent->inode = 0;
435 dir_modified++;
436 goto next;
437 } else
438 ext2fs_unmark_valid(fs);
439 }
440 subdir->parent = ino;
441 }
442
443 if (inode_count[dirent->inode]++ > 0)
444 fs_links_count++;
445 fs_total_count++;
446 next:
447 offset += dirent->rec_len;
448 } while (offset < fs->blocksize);
449#if 0
450 printf("\n");
451#endif
452 if (offset != fs->blocksize) {
453 printf("Final rec_len is %d, should be %d\n",
454 dirent->rec_len,
455 dirent->rec_len - fs->blocksize + offset);
456 }
457 if (dir_modified) {
458 retval = io_channel_write_blk(fs->io, block_nr,
459 1, buf);
460 if (retval) {
461 com_err(program_name, retval,
462 "while writing directory block %d", block_nr);
463 }
464 ext2fs_mark_changed(fs);
465 }
466 return 0;
467}
468
469/*
470 * This function is called to deallocate a block, and is an interator
471 * functioned called by deallocate inode via ext2fs_iterate_block().
472 */
473static int deallocate_inode_block(ext2_filsys fs,
474 blk_t *block_nr,
475 int blockcnt,
476 void *private)
477{
478 if (!*block_nr)
479 return 0;
480 ext2fs_unmark_block_bitmap(fs, block_found_map, *block_nr);
481 ext2fs_unmark_block_bitmap(fs, fs->block_map, *block_nr);
482 return 0;
483}
484
485/*
486 * This fuction deallocates an inode
487 */
488static void deallocate_inode(ext2_filsys fs, ino_t ino,
489 char* block_buf)
490{
491 errcode_t retval;
492 struct ext2_inode inode;
493
494 retval = ext2fs_read_inode(fs, ino, &inode);
495 if (retval) {
496 com_err("deallocate_inode", retval, "while reading inode %d",
497 ino);
498 return;
499 }
500 inode.i_links_count = 0;
501 inode.i_dtime = time(0);
502 retval = ext2fs_write_inode(fs, ino, &inode);
503 if (retval) {
504 com_err("deallocate_inode", retval, "while writing inode %d",
505 ino);
506 return;
507 }
508 /*
509 * Fix up the bitmaps...
510 */
511 read_bitmaps(fs);
512 ext2fs_unmark_inode_bitmap(fs, inode_used_map, ino);
513 ext2fs_unmark_inode_bitmap(fs, inode_dir_map, ino);
514 if (inode_bad_map)
515 ext2fs_unmark_inode_bitmap(fs, inode_bad_map, ino);
516 ext2fs_unmark_inode_bitmap(fs, fs->inode_map, ino);
517 ext2fs_mark_ib_dirty(fs);
518
519 if (!inode_has_valid_blocks(&inode))
520 return;
521
522 ext2fs_mark_bb_dirty(fs);
523 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
524 deallocate_inode_block, 0);
525 if (retval)
526 com_err("deallocate_inode", retval,
527 "while calling ext2fs_block_iterate for inode %d",
528 ino);
529}
530
531/*
532 * These two subroutines are used by process_bad_inode; it is used to
533 * make sure that certain reserved fields are really zero. If not,
534 * prompt the user if he/she wants us to zeroize them.
535 */
536static void check_for_zero_long(ext2_filsys fs, ino_t ino, char *pathname,
537 const char *name, unsigned long *val,
538 int *modified)
539{
540 char prompt[80];
541
542 if (*val) {
543 printf("%s for inode %ld (%s) is %ld, should be zero.\n",
544 name, ino, pathname, *val);
545 preenhalt();
546 sprintf(prompt, "Clear %s", name);
547 if (ask(prompt, 1)) {
548 *val = 0;
549 *modified = 1;
550 } else
551 ext2fs_unmark_valid(fs);
552 }
553}
554
555static void check_for_zero_char(ext2_filsys fs, ino_t ino, char *pathname,
556 const char *name, unsigned char *val,
557 int *modified)
558{
559 char prompt[80];
560
561 if (*val) {
562 printf("%s for inode %ld (%s) is %d, should be zero.\n",
563 name, ino, pathname, *val);
564 preenhalt();
565 sprintf(prompt, "Clear %s", name);
566 if (ask(prompt, 1)) {
567 *val = 0;
568 *modified = 1;
569 } else
570 ext2fs_unmark_valid(fs);
571 }
572}
573
574
575
576static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
577{
578 struct ext2_inode inode;
579 errcode_t retval;
580 int inode_modified = 0;
581 char *pathname;
582
583 retval = ext2fs_read_inode(fs, ino, &inode);
584 if (retval) {
585 com_err("process_bad_inode", retval, "while reading inode %d",
586 ino);
587 return 0;
588 }
589 retval = ext2fs_get_pathname(fs, dir, ino, &pathname);
590 if (retval) {
591 com_err("process_bad_inode", retval,
592 "while getting pathname for inode %d",
593 ino);
594 return 0;
595 }
596 if (!S_ISDIR(inode.i_mode) && !S_ISREG(inode.i_mode) &&
597 !S_ISCHR(inode.i_mode) && !S_ISBLK(inode.i_mode) &&
598 !S_ISLNK(inode.i_mode) && !S_ISFIFO(inode.i_mode) &&
599 !(S_ISSOCK(inode.i_mode))) {
600 printf("Inode %ld (%s) has a bad mode (0%o).\n",
601 ino, pathname, inode.i_mode);
602 preenhalt();
603 if (ask("Clear", 1)) {
604 deallocate_inode(fs, ino, 0);
605 return 1;
606 } else
607 ext2fs_unmark_valid(fs);
608 }
609 check_for_zero_long(fs, ino, pathname, "i_faddr", &inode.i_faddr,
610 &inode_modified);
611 check_for_zero_char(fs, ino, pathname, "i_frag", &inode.i_frag,
612 &inode_modified);
613 check_for_zero_char(fs, ino, pathname, "i_fsize", &inode.i_fsize,
614 &inode_modified);
615 check_for_zero_long(fs, ino, pathname, "i_file_acl", &inode.i_file_acl,
616 &inode_modified);
617 check_for_zero_long(fs, ino, pathname, "i_dir_acl", &inode.i_dir_acl,
618 &inode_modified);
619 free(pathname);
620 if (inode_modified) {
621 retval = ext2fs_write_inode(fs, ino, &inode);
622 if (retval) {
623 com_err("process_bad_inode", retval,
624 "while writing inode %d",
625 ino);
626 return 0;
627 }
628 }
629 return 0;
630}
631