blob: f4a4d32cbeec2c8bc97bb6bacae612a78fb94d7c [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
Theodore Ts'o3839e651997-04-26 13:21:57 +000034 *
35 * Pass 2 frees the following data structures
36 * - The inode_bad_map bitmap
37 */
38
39#include "et/com_err.h"
40
41#include "e2fsck.h"
42
43/*
44 * Keeps track of how many times an inode is referenced.
45 */
46unsigned short * inode_count;
47
48static void deallocate_inode(ext2_filsys fs, ino_t ino,
49 char* block_buf);
50static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
51static int check_dir_block(ext2_filsys fs,
52 struct dir_block_struct *dir_blocks_info,
53 char *buf);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000054static int allocate_dir_block(ext2_filsys fs,
55 struct dir_block_struct *dir_blocks_info,
56 char *buf);
57static int update_dir_block(ext2_filsys fs,
58 blk_t *block_nr,
59 int blockcnt,
60 void *private);
Theodore Ts'o3839e651997-04-26 13:21:57 +000061
62void pass2(ext2_filsys fs)
63{
64 int i;
65 char *buf;
66 struct resource_track rtrack;
67
68 init_resource_track(&rtrack);
69
70#ifdef MTRACE
71 mtrace_print("Pass 2");
72#endif
73
74 if (!preen)
75 printf("Pass 2: Checking directory structure\n");
76 inode_count = allocate_memory((fs->super->s_inodes_count + 1) *
77 sizeof(unsigned short),
78 "buffer for inode count");
79
80 buf = allocate_memory(fs->blocksize, "directory scan buffer");
81
82 for (i=0; i < dir_block_count; i++)
83 check_dir_block(fs, &dir_blocks[i], buf);
84
85 free(buf);
86 free(dir_blocks);
87 if (inode_bad_map) {
Theodore Ts'of3db3561997-04-26 13:34:30 +000088 ext2fs_free_inode_bitmap(inode_bad_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +000089 inode_bad_map = 0;
90 }
91 if (tflag > 1) {
92 printf("Pass 2: ");
93 print_resource_track(&rtrack);
94 }
95}
96
97/*
98 * Make sure the first entry in the directory is '.', and that the
99 * directory entry is sane.
100 */
101static int check_dot(ext2_filsys fs,
102 struct ext2_dir_entry *dirent,
103 ino_t ino)
104{
105 struct ext2_dir_entry *nextdir;
106 int status = 0;
107 int created = 0;
108 int new_len;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000109 const char *question = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110
111 if (!dirent->inode) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000112 printf("Missing '.' in directory inode %lu.\n", ino);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000113 question = "Fix";
114 } else if ((dirent->name_len != 1) ||
115 strncmp(dirent->name, ".", dirent->name_len)) {
116 char *name = malloc(dirent->name_len + 1);
117 if (!name)
118 fatal_error("Couldn't allocate . name");
119 strncpy(name, dirent->name, dirent->name_len);
120 name[dirent->name_len] = '\0';
121 printf("First entry in directory inode %lu contains '%s' "
122 "(inode=%u)\n", ino, name, dirent->inode);
123 printf("instead of '.'.\n");
124 free(name);
125 question = "Change to be '.'";
126 }
127 if (question) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 if (dirent->rec_len < 12)
129 fatal_error("Cannot fix, insufficient space to add '.'");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000130 preenhalt(fs);
131 if (ask(question, 1)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132 dirent->inode = ino;
133 dirent->name_len = 1;
134 dirent->name[0] = '.';
135 status = 1;
136 created = 1;
137 } else {
138 ext2fs_unmark_valid(fs);
139 return 0;
140 }
141 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142 if (dirent->inode != ino) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000143 printf("Bad inode number for '.' in directory inode %lu.\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144 ino);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000145 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146 if (ask("Fix", 1)) {
147 dirent->inode = ino;
148 status = 1;
149 } else
150 ext2fs_unmark_valid(fs);
151 }
152 if (dirent->rec_len > 12) {
153 new_len = dirent->rec_len - 12;
154 if (new_len > 12) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000155 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 if (created ||
157 ask("Directory entry for '.' is big. Split", 1)) {
158 nextdir = (struct ext2_dir_entry *)
159 ((char *) dirent + 12);
160 dirent->rec_len = 12;
161 nextdir->rec_len = new_len;
162 nextdir->inode = 0;
163 nextdir->name_len = 0;
164 status = 1;
165 }
166 }
167 }
168 return status;
169}
170
171/*
172 * Make sure the second entry in the directory is '..', and that the
173 * directory entry is sane. We do not check the inode number of '..'
174 * here; this gets done in pass 3.
175 */
176static int check_dotdot(ext2_filsys fs,
177 struct ext2_dir_entry *dirent,
178 struct dir_info *dir)
179{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000180 ino_t ino = dir->ino;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000181 const char *question = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182
183 if (!dirent->inode) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000184 printf("Missing '..' in directory inode %lu.\n", ino);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000185 question = "Fix";
186 } else if ((dirent->name_len != 2) ||
187 strncmp(dirent->name, "..", dirent->name_len)) {
188 char *name = malloc(dirent->name_len + 1);
189 if (!name)
190 fatal_error("Couldn't allocate bad .. name");
191 strncpy(name, dirent->name, dirent->name_len);
192 name[dirent->name_len] = '\0';
193 printf("Second entry in directory inode %lu contains '%s' "
194 "(inode=%u)\n", ino, name, dirent->inode);
195 printf("instead of '..'.\n");
196 free(name);
197 question = "Change to be '..'";
198 }
199 if (question) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 if (dirent->rec_len < 12)
201 fatal_error("Cannot fix, insufficient space to add '..'");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000202 preenhalt(fs);
203 if (ask(question, 1)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 /*
205 * Note: we don't have the parent inode just
206 * yet, so we will fill it in with the root
207 * inode. This will get fixed in pass 3.
208 */
209 dirent->inode = EXT2_ROOT_INO;
210 dirent->name_len = 2;
211 dirent->name[0] = '.';
212 dirent->name[1] = '.';
213 return 1;
214 } else
215 ext2fs_unmark_valid(fs);
216 return 0;
217 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 dir->dotdot = dirent->inode;
219 return 0;
220}
221
Theodore Ts'of3db3561997-04-26 13:34:30 +0000222static char unknown_pathname[] = "???";
223
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224/*
225 * Check to make sure a directory entry doesn't contain any illegal
226 * characters.
227 */
228static int check_name(ext2_filsys fs,
229 struct ext2_dir_entry *dirent,
230 ino_t dir_ino,
231 char *name)
232{
233 int i;
234 int fixup = -1;
235 char *pathname;
236 int ret = 0;
237 errcode_t retval;
238
239 for ( i = 0; i < dirent->name_len; i++) {
240 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
241 if (fixup < 0) {
242 retval = ext2fs_get_pathname(fs, dir_ino,
243 0, &pathname);
244 if (retval) {
245 com_err(program_name, retval, "while getting pathname in check_name");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000246 pathname = unknown_pathname;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 }
248 printf ("Bad file name '%s' (contains '/' or "
Theodore Ts'of3db3561997-04-26 13:34:30 +0000249 " null) in directory '%s' (%lu)\n",
250 name, pathname, dir_ino);
251 if (pathname != unknown_pathname)
252 free(pathname);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000253 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 fixup = ask("Replace '/' or null by '.'", 1);
255 }
256 if (fixup) {
257 dirent->name[i] = '.';
258 ret = 1;
259 } else
260 ext2fs_unmark_valid(fs);
261 }
262 }
263 return ret;
264}
265
266static int check_dir_block(ext2_filsys fs,
267 struct dir_block_struct *db,
268 char *buf)
269{
270 struct dir_info *subdir, *dir;
271 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 int offset = 0;
273 int dir_modified = 0;
274 errcode_t retval;
275 char *path1, *path2;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000276 int dot_state, name_len;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 blk_t block_nr = db->blk;
278 ino_t ino = db->ino;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000279 char name[EXT2_NAME_LEN+1];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000280
281 /*
282 * Make sure the inode is still in use (could have been
283 * deleted in the duplicate/bad blocks pass.
284 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000285 if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000287
288 if (db->blk == 0) {
289 if (allocate_dir_block(fs, db, buf))
290 return 0;
291 block_nr = db->blk;
292 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293
294 if (db->blockcnt)
295 dot_state = 2;
296 else
297 dot_state = 0;
298
299#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000300 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 db->blockcnt, ino);
302#endif
303
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000304 retval = ext2fs_read_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 if (retval) {
306 com_err(program_name, retval,
307 "while reading directory block %d", block_nr);
308 }
309
310 do {
311 dot_state++;
312 dirent = (struct ext2_dir_entry *) (buf + offset);
313 if (((offset + dirent->rec_len) > fs->blocksize) ||
314 (dirent->rec_len < 8) ||
315 ((dirent->name_len+8) > dirent->rec_len)) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000316 printf("Directory inode %lu, block %d, offset %d: directory corrupted\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 ino, db->blockcnt, offset);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000318 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 if (ask("Salvage", 1)) {
320 dirent->rec_len = fs->blocksize - offset;
321 dirent->name_len = 0;
322 dirent->inode = 0;
323 dir_modified++;
324 } else {
325 ext2fs_unmark_valid(fs);
326 return DIRENT_ABORT;
327 }
328 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000329
330 name_len = dirent->name_len;
331 if (dirent->name_len > EXT2_NAME_LEN) {
332 printf("Directory inode %lu, block %d, offset %d: filename too long\n",
333 ino, db->blockcnt, offset);
334 preenhalt(fs);
335 if (ask("Truncate filename", 1)) {
336 dirent->name_len = EXT2_NAME_LEN;
337 dir_modified++;
338 }
339 name_len = EXT2_NAME_LEN;
340 }
341
342 strncpy(name, dirent->name, name_len);
343 name[name_len] = '\0';
344
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 if (dot_state == 1) {
346 if (check_dot(fs, dirent, ino))
347 dir_modified++;
348 } else if (dot_state == 2) {
349 dir = get_dir_info(ino);
350 if (!dir) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000351 printf("Internal error: couldn't find dir_info for %lu\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 ino);
353 fatal_error(0);
354 }
355 if (check_dotdot(fs, dirent, dir))
356 dir_modified++;
357 } else if (dirent->inode == ino) {
358 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
359 if (retval)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000360 path1 = unknown_pathname;
361 printf("Entry '%s' in %s (%lu) is a link to '.' ",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000362 name, path1, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000363 if (path1 != unknown_pathname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364 free(path1);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000365 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366 if (ask("Clear", 1)) {
367 dirent->inode = 0;
368 dir_modified++;
369 }
370 }
371 if (!dirent->inode)
372 goto next;
373
374#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000375 printf("Entry '%s', name_len %d, rec_len %d, inode %lu... ",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376 name, dirent->name_len, dirent->rec_len, dirent->inode);
377#endif
378 if (check_name(fs, dirent, ino, name))
379 dir_modified++;
380
381 /*
382 * Make sure the inode listed is a legal one.
383 */
384 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000385 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386 (dirent->inode > fs->super->s_inodes_count)) {
387 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
388 if (retval)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000389 path1 = unknown_pathname;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000390 printf("Entry '%s' in %s (%lu) has bad inode #: %u.\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 name, path1, ino, dirent->inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000392 if (path1 != unknown_pathname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000393 free(path1);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000394 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395 if (ask("Clear", 1)) {
396 dirent->inode = 0;
397 dir_modified++;
398 goto next;
399 } else
400 ext2fs_unmark_valid(fs);
401 }
402
403 /*
404 * If the inode is unusued, offer to clear it.
405 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000406 if (!(ext2fs_test_inode_bitmap(inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407 dirent->inode))) {
408 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
409 if (retval)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000410 path1 = unknown_pathname;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000411 printf("Entry '%s' in %s (%lu) has deleted/unused inode %u.\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 name, path1, ino, dirent->inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000413 if (path1 != unknown_pathname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 free(path1);
415 if (ask("Clear", 1)) {
416 dirent->inode = 0;
417 dir_modified++;
418 goto next;
419 } else
420 ext2fs_unmark_valid(fs);
421 }
422
423 /*
424 * If the inode was marked as having bad fields in
425 * pass1, process it and offer to fix/clear it.
426 * (We wait until now so that we can display the
427 * pathname to the user.)
428 */
429 if (inode_bad_map &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000430 ext2fs_test_inode_bitmap(inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 dirent->inode)) {
432 if (process_bad_inode(fs, ino, dirent->inode)) {
433 dirent->inode = 0;
434 dir_modified++;
435 goto next;
436 }
437 }
438
439 /*
440 * If this is a directory, then mark its parent in its
441 * dir_info structure. If the parent field is already
442 * filled in, then this directory has more than one
443 * hard link. We assume the first link is correct,
444 * and ask the user if he/she wants to clear this one.
445 */
446 if ((dot_state > 2) &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000447 (ext2fs_test_inode_bitmap(inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 dirent->inode))) {
449 subdir = get_dir_info(dirent->inode);
450 if (!subdir) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000451 printf("INTERNAL ERROR: missing dir %u\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 dirent->inode);
453 fatal_error(0);
454 }
455 if (subdir->parent) {
456 retval = ext2fs_get_pathname(fs, ino,
457 0, &path1);
458 if (retval)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000459 path1 = unknown_pathname;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 retval = ext2fs_get_pathname(fs,
461 subdir->parent,
462 dirent->inode,
463 &path2);
464 if (retval)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000465 path2 = unknown_pathname;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000466 printf("Entry '%s' in %s (%lu) is a link to directory %s (%u).\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 name, path1, ino, path2,
468 dirent->inode);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000469 if (path1 != unknown_pathname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000470 free(path1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000471 if (path2 != unknown_pathname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 free(path2);
473 if (ask("Clear", 1)) {
474 dirent->inode = 0;
475 dir_modified++;
476 goto next;
477 } else
478 ext2fs_unmark_valid(fs);
479 }
480 subdir->parent = ino;
481 }
482
483 if (inode_count[dirent->inode]++ > 0)
484 fs_links_count++;
485 fs_total_count++;
486 next:
487 offset += dirent->rec_len;
488 } while (offset < fs->blocksize);
489#if 0
490 printf("\n");
491#endif
492 if (offset != fs->blocksize) {
493 printf("Final rec_len is %d, should be %d\n",
494 dirent->rec_len,
495 dirent->rec_len - fs->blocksize + offset);
496 }
497 if (dir_modified) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000498 retval = ext2fs_write_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 if (retval) {
500 com_err(program_name, retval,
501 "while writing directory block %d", block_nr);
502 }
503 ext2fs_mark_changed(fs);
504 }
505 return 0;
506}
507
508/*
509 * This function is called to deallocate a block, and is an interator
510 * functioned called by deallocate inode via ext2fs_iterate_block().
511 */
512static int deallocate_inode_block(ext2_filsys fs,
513 blk_t *block_nr,
514 int blockcnt,
515 void *private)
516{
517 if (!*block_nr)
518 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000519 ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
520 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 return 0;
522}
523
524/*
525 * This fuction deallocates an inode
526 */
527static void deallocate_inode(ext2_filsys fs, ino_t ino,
528 char* block_buf)
529{
530 errcode_t retval;
531 struct ext2_inode inode;
532
Theodore Ts'of3db3561997-04-26 13:34:30 +0000533 e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 inode.i_links_count = 0;
535 inode.i_dtime = time(0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000536 e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
537
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 /*
539 * Fix up the bitmaps...
540 */
541 read_bitmaps(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000542 ext2fs_unmark_inode_bitmap(inode_used_map, ino);
543 ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 if (inode_bad_map)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
546 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 ext2fs_mark_ib_dirty(fs);
548
549 if (!inode_has_valid_blocks(&inode))
550 return;
551
552 ext2fs_mark_bb_dirty(fs);
553 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
554 deallocate_inode_block, 0);
555 if (retval)
556 com_err("deallocate_inode", retval,
557 "while calling ext2fs_block_iterate for inode %d",
558 ino);
559}
560
561/*
562 * These two subroutines are used by process_bad_inode; it is used to
563 * make sure that certain reserved fields are really zero. If not,
564 * prompt the user if he/she wants us to zeroize them.
565 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000566static void check_for_zero_u32(ext2_filsys fs, ino_t ino, char *pathname,
567 const char *name, __u32 *val,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568 int *modified)
569{
570 char prompt[80];
571
572 if (*val) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000573 printf("%s for inode %lu (%s) is %u, should be zero.\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574 name, ino, pathname, *val);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000575 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 sprintf(prompt, "Clear %s", name);
577 if (ask(prompt, 1)) {
578 *val = 0;
579 *modified = 1;
580 } else
581 ext2fs_unmark_valid(fs);
582 }
583}
584
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000585static void check_for_zero_u8(ext2_filsys fs, ino_t ino, char *pathname,
586 const char *name, __u8 *val,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587 int *modified)
588{
589 char prompt[80];
590
591 if (*val) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000592 printf("%s for inode %lu (%s) is %d, should be zero.\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000593 name, ino, pathname, *val);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000594 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595 sprintf(prompt, "Clear %s", name);
596 if (ask(prompt, 1)) {
597 *val = 0;
598 *modified = 1;
599 } else
600 ext2fs_unmark_valid(fs);
601 }
602}
603
604
605
606static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
607{
608 struct ext2_inode inode;
609 errcode_t retval;
610 int inode_modified = 0;
611 char *pathname;
612
Theodore Ts'of3db3561997-04-26 13:34:30 +0000613 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614 retval = ext2fs_get_pathname(fs, dir, ino, &pathname);
615 if (retval) {
616 com_err("process_bad_inode", retval,
617 "while getting pathname for inode %d",
618 ino);
619 return 0;
620 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000621 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
622 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
623 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
624 !(LINUX_S_ISSOCK(inode.i_mode))) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000625 printf("Inode %lu (%s) has a bad mode (0%o).\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 ino, pathname, inode.i_mode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000627 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 if (ask("Clear", 1)) {
629 deallocate_inode(fs, ino, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000630 free(pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 return 1;
632 } else
633 ext2fs_unmark_valid(fs);
634 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000635 check_for_zero_u32(fs, ino, pathname, "i_faddr", &inode.i_faddr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 &inode_modified);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000637#if HAVE_EXT2_FRAGS
638 check_for_zero_u8(fs, ino, pathname, "i_frag", &inode.i_frag,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 &inode_modified);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000640 check_for_zero_u8(fs, ino, pathname, "i_fsize", &inode.i_fsize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641 &inode_modified);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000642#else
643 /*
644 * Even if the OS specific fields don't support i_frag and
645 * i_fsize, make sure they are set to zero anyway. This may
646 * cause problems if on some other OS these fields are reused
647 * for something else, but that's probably a bad idea....
648 */
649 check_for_zero_u8(fs, ino, pathname, "i_frag",
650 &inode.osd2.linux2.l_i_frag, &inode_modified);
651 check_for_zero_u8(fs, ino, pathname, "i_fsize",
652 &inode.osd2.linux2.l_i_fsize, &inode_modified);
653#endif
654 check_for_zero_u32(fs, ino, pathname, "i_file_acl", &inode.i_file_acl,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655 &inode_modified);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000656 check_for_zero_u32(fs, ino, pathname, "i_dir_acl", &inode.i_dir_acl,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657 &inode_modified);
658 free(pathname);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000659 if (inode_modified)
660 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 return 0;
662}
663
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000664
665/*
666 * allocate_dir_block --- this function allocates a new directory
667 * block for a particular inode; this is done if a directory has
668 * a "hole" in it, or if a directory has a illegal block number
669 * that was zeroed out and now needs to be replaced.
670 */
671static int allocate_dir_block(ext2_filsys fs,
672 struct dir_block_struct *db,
673 char *buf)
674{
675 blk_t blk;
676 char *block;
677 struct ext2_inode inode;
678 errcode_t retval;
679
680 printf("Directory inode %lu has a hole at block #%d\n",
681 db->ino, db->blockcnt);
682 if (ask("Allocate block", 1) == 0)
683 return 1;
684
685 /*
686 * Read the inode and block bitmaps in; we'll be messing with
687 * them.
688 */
689 read_bitmaps(fs);
690
691 /*
692 * First, find a free block
693 */
694 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
695 if (retval) {
696 com_err("allocate_dir_block", retval,
697 "while trying to fill a hole in a directory inode");
698 return 1;
699 }
700 ext2fs_mark_block_bitmap(block_found_map, blk);
701 ext2fs_mark_block_bitmap(fs->block_map, blk);
702 ext2fs_mark_bb_dirty(fs);
703
704 /*
705 * Now let's create the actual data block for the inode
706 */
707 if (db->blockcnt)
708 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
709 else
710 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
711 &block);
712
713 if (retval) {
714 com_err("allocate_dir_block", retval,
715 "while creating new directory block");
716 return 1;
717 }
718
719 retval = ext2fs_write_dir_block(fs, blk, block);
720 free(block);
721 if (retval) {
722 com_err("allocate_dir_block", retval,
723 "while writing an empty directory block");
724 return 1;
725 }
726
727 /*
728 * Update the inode block count
729 */
730 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
731 inode.i_blocks += fs->blocksize / 512;
732 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
733 inode.i_size = (db->blockcnt+1) * fs->blocksize;
734 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
735
736 /*
737 * Finally, update the block pointers for the inode
738 */
739 db->blk = blk;
740 retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
741 0, update_dir_block, db);
742 if (retval) {
743 com_err("allocate_dir_block", retval,
744 "while calling ext2fs_block_iterate");
745 return 1;
746 }
747
748 return 0;
749}
750
751/*
752 * This is a helper function for allocate_dir_block().
753 */
754static int update_dir_block(ext2_filsys fs,
755 blk_t *block_nr,
756 int blockcnt,
757 void *private)
758{
759 struct dir_block_struct *db = private;
760
761 if (db->blockcnt == blockcnt) {
762 *block_nr = db->blk;
763 return BLOCK_CHANGED;
764 }
765 return 0;
766}
767