blob: edb248219606066497882fb789b4ca8d9a616cf3 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass2.c --- check directory structure
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * Copyright (C) 1993, 1994, 1995, 1996, 1997 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'o3839e651997-04-26 13:21:57 +000010 *
11 * Pass 2 of e2fsck iterates through all active directory inodes, and
12 * applies to following tests to each directory entry in the directory
13 * blocks in the inodes:
14 *
15 * - The length of the directory entry (rec_len) should be at
16 * least 8 bytes, and no more than the remaining space
17 * left in the directory block.
18 * - The length of the name in the directory entry (name_len)
19 * should be less than (rec_len - 8).
20 * - The inode number in the directory entry should be within
21 * legal bounds.
22 * - The inode number should refer to a in-use inode.
23 * - The first entry should be '.', and its inode should be
24 * the inode of the directory.
25 * - The second entry should be '..'.
26 *
27 * To minimize disk seek time, the directory blocks are processed in
28 * sorted order of block numbers.
29 *
30 * Pass 2 also collects the following information:
31 * - The inode numbers of the subdirectories for each directory.
32 *
33 * Pass 2 relies on the following information from previous passes:
34 * - The directory information collected in pass 1.
35 * - The inode_used_map bitmap
36 * - The inode_bad_map bitmap
37 * - The inode_dir_map bitmap
Theodore Ts'o3839e651997-04-26 13:21:57 +000038 *
39 * Pass 2 frees the following data structures
40 * - The inode_bad_map bitmap
41 */
42
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000044#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000045
46/*
47 * Keeps track of how many times an inode is referenced.
48 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000049static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +000050 char* block_buf);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000051static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000052static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000053 struct ext2_db_entry *dir_blocks_info,
54 void *private);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000055static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000056 struct ext2_db_entry *dir_blocks_info,
57 char *buf, struct problem_context *pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000058static int update_dir_block(ext2_filsys fs,
59 blk_t *block_nr,
60 int blockcnt,
61 void *private);
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
Theodore Ts'o21c84b71997-04-29 16:15:03 +000063struct check_dir_struct {
64 char *buf;
65 struct problem_context pctx;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066 e2fsck_t ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000067};
68
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000069void pass2(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000070{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000071 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000072 char *buf;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000073#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000074 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000075#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000076 struct dir_info *dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000077 struct check_dir_struct cd;
78
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000079#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000080 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000081#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000082
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000083 clear_problem_context(&cd.pctx);
84
Theodore Ts'o3839e651997-04-26 13:21:57 +000085#ifdef MTRACE
86 mtrace_print("Pass 2");
87#endif
88
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000089 if (!(ctx->options & E2F_OPT_PREEN))
90 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
91
92 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
93 0, ctx->inode_link_info,
94 &ctx->inode_count);
95 if (cd.pctx.errcode) {
96 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
Theodore Ts'o21c84b71997-04-29 16:15:03 +000097 fatal_error(0);
98 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000099 buf = allocate_memory(fs->blocksize, "directory scan buffer");
100
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000101 /*
102 * Set up the parent pointer for the root directory, if
103 * present. (If the root directory is not present, we will
104 * create it in pass 3.)
105 */
106 dir = get_dir_info(EXT2_ROOT_INO);
107 if (dir)
108 dir->parent = EXT2_ROOT_INO;
109
110 cd.buf = buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000111 cd.ctx = ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000112
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000113 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
114 &cd);
115 if (cd.pctx.errcode) {
116 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000117 fatal_error(0);
118 }
119
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120 free(buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000121 ext2fs_free_dblist(fs->dblist);
122
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000123 if (ctx->inode_bad_map) {
124 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
125 ctx->inode_bad_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000127#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000128 if (ctx->options & E2F_OPT_TIME2)
129 print_resource_track("Pass 2", &rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000130#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131}
132
133/*
134 * Make sure the first entry in the directory is '.', and that the
135 * directory entry is sane.
136 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000137static int check_dot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000139 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140{
141 struct ext2_dir_entry *nextdir;
142 int status = 0;
143 int created = 0;
144 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000145 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000147 if (!dirent->inode)
148 problem = PR_2_MISSING_DOT;
149 else if ((dirent->name_len != 1) ||
150 (dirent->name[0] != '.'))
151 problem = PR_2_1ST_NOT_DOT;
152 else if (dirent->name[1] != '\0')
153 problem = PR_2_DOT_NULL_TERM;
154
155 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000156 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000157 if (dirent->rec_len < 12)
158 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 dirent->inode = ino;
160 dirent->name_len = 1;
161 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000162 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 status = 1;
164 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 }
166 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 if (dirent->inode != ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000168 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 dirent->inode = ino;
170 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000171 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 }
173 if (dirent->rec_len > 12) {
174 new_len = dirent->rec_len - 12;
175 if (new_len > 12) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000176 preenhalt(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 if (created ||
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000178 ask(ctx, "Directory entry for '.' is big. Split", 1)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 nextdir = (struct ext2_dir_entry *)
180 ((char *) dirent + 12);
181 dirent->rec_len = 12;
182 nextdir->rec_len = new_len;
183 nextdir->inode = 0;
184 nextdir->name_len = 0;
185 status = 1;
186 }
187 }
188 }
189 return status;
190}
191
192/*
193 * Make sure the second entry in the directory is '..', and that the
194 * directory entry is sane. We do not check the inode number of '..'
195 * here; this gets done in pass 3.
196 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000197static int check_dotdot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000199 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000201 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000203 if (!dirent->inode)
204 problem = PR_2_MISSING_DOT_DOT;
205 else if ((dirent->name_len != 2) ||
206 (dirent->name[0] != '.') ||
207 (dirent->name[1] != '.'))
208 problem = PR_2_2ND_NOT_DOT_DOT;
209 else if (dirent->name[2] != '\0')
210 problem = PR_2_DOT_DOT_NULL_TERM;
211
212 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000213 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000214 if (dirent->rec_len < 12)
215 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000216 /*
217 * Note: we don't have the parent inode just
218 * yet, so we will fill it in with the root
219 * inode. This will get fixed in pass 3.
220 */
221 dirent->inode = EXT2_ROOT_INO;
222 dirent->name_len = 2;
223 dirent->name[0] = '.';
224 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000225 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000227 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 return 0;
229 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230 dir->dotdot = dirent->inode;
231 return 0;
232}
233
234/*
235 * Check to make sure a directory entry doesn't contain any illegal
236 * characters.
237 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000238static int check_name(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000240 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241{
242 int i;
243 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245
246 for ( i = 0; i < dirent->name_len; i++) {
247 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
248 if (fixup < 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000249 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250 }
251 if (fixup) {
252 dirent->name[i] = '.';
253 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000254 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255 }
256 }
257 return ret;
258}
259
260static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000261 struct ext2_db_entry *db,
262 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263{
264 struct dir_info *subdir, *dir;
265 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 int offset = 0;
267 int dir_modified = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000268 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 blk_t block_nr = db->blk;
270 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000271 __u16 links;
272 struct check_dir_struct *cd = private;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000273 char *buf;
274 e2fsck_t ctx;
275 int problem;
276
277 buf = cd->buf;
278 ctx = cd->ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000279
Theodore Ts'o3839e651997-04-26 13:21:57 +0000280 /*
281 * Make sure the inode is still in use (could have been
282 * deleted in the duplicate/bad blocks pass.
283 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000284 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000286
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000287 cd->pctx.ino = ino;
288 cd->pctx.blk = block_nr;
289 cd->pctx.blkcount = db->blockcnt;
290 cd->pctx.ino2 = 0;
291 cd->pctx.dirent = 0;
292 cd->pctx.num = 0;
293
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000294 if (db->blk == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000295 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000296 return 0;
297 block_nr = db->blk;
298 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299
300 if (db->blockcnt)
301 dot_state = 2;
302 else
303 dot_state = 0;
304
305#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000306 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 db->blockcnt, ino);
308#endif
309
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000310 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
311 if (cd->pctx.errcode) {
312 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx))
313 fatal_error(0);
314 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315 }
316
317 do {
318 dot_state++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000319 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000321 cd->pctx.dirent = dirent;
322 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323 if (((offset + dirent->rec_len) > fs->blocksize) ||
324 (dirent->rec_len < 8) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000325 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000326 ((dirent->name_len+8) > dirent->rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000327 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328 dirent->rec_len = fs->blocksize - offset;
329 dirent->name_len = 0;
330 dirent->inode = 0;
331 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000332 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000333 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000334 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000335 if (dirent->name_len > EXT2_NAME_LEN) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000336 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000337 dirent->name_len = EXT2_NAME_LEN;
338 dir_modified++;
339 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000340 }
341
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342 if (dot_state == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000343 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 dir_modified++;
345 } else if (dot_state == 2) {
346 dir = get_dir_info(ino);
347 if (!dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000348 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349 fatal_error(0);
350 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000351 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 dir_modified++;
353 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000354 problem = PR_2_LINK_DOT;
355 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356 dirent->inode = 0;
357 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000358 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 }
360 }
361 if (!dirent->inode)
362 goto next;
363
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364 /*
365 * Make sure the inode listed is a legal one.
366 */
367 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000368 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000370 problem = PR_2_BAD_INO;
371 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372 dirent->inode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000373 /*
374 * If the inode is unused, offer to clear it.
375 */
376 problem = PR_2_UNUSED_INODE;
377 } else if (ctx->inode_bb_map &&
378 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
379 dirent->inode))) {
380 /*
381 * If the inode is in a bad block, offer to
382 * clear it.
383 */
384 problem = PR_2_BB_INODE;
385 } else if ((dot_state > 2) &&
386 (dirent->name_len == 1) &&
387 (dirent->name[0] == '.')) {
388 /*
389 * If there's a '.' entry in anything other
390 * than the first directory entry, it's a
391 * duplicate entry that should be removed.
392 */
393 problem = PR_2_DUP_DOT;
394 } else if ((dot_state > 2) &&
395 (dirent->name_len == 2) &&
396 (dirent->name[0] == '.') &&
397 (dirent->name[1] == '.')) {
398 /*
399 * If there's a '..' entry in anything other
400 * than the second directory entry, it's a
401 * duplicate entry that should be removed.
402 */
403 problem = PR_2_DUP_DOT_DOT;
404 } else if ((dot_state > 2) &&
405 (dirent->inode == EXT2_ROOT_INO)) {
406 /*
407 * Don't allow links to the root directory.
408 * We check this specially to make sure we
409 * catch this error case even if the root
410 * directory hasn't been created yet.
411 */
412 problem = PR_2_LINK_ROOT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000413 }
414
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000415 if (problem) {
416 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000417 dirent->inode = 0;
418 dir_modified++;
419 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000420 } else {
421 ext2fs_unmark_valid(fs);
422 if (problem == PR_2_BAD_INO)
423 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000424 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425 }
426
427 /*
428 * If the inode was marked as having bad fields in
429 * pass1, process it and offer to fix/clear it.
430 * (We wait until now so that we can display the
431 * pathname to the user.)
432 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000433 if (ctx->inode_bad_map &&
434 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 dirent->inode)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000436 if (process_bad_inode(ctx, ino, dirent->inode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437 dirent->inode = 0;
438 dir_modified++;
439 goto next;
440 }
441 }
442
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000443 if (check_name(ctx, dirent, ino, &cd->pctx))
444 dir_modified++;
445
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000446 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447 * If this is a directory, then mark its parent in its
448 * dir_info structure. If the parent field is already
449 * filled in, then this directory has more than one
450 * hard link. We assume the first link is correct,
451 * and ask the user if he/she wants to clear this one.
452 */
453 if ((dot_state > 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000454 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 dirent->inode))) {
456 subdir = get_dir_info(dirent->inode);
457 if (!subdir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000458 cd->pctx.ino = dirent->inode;
459 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 fatal_error(0);
461 }
462 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000463 cd->pctx.ino2 = subdir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000464 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000465 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466 dirent->inode = 0;
467 dir_modified++;
468 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000469 }
470 cd->pctx.ino2 = 0;
471 } else
472 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 }
474
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000475 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
476 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000477 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000478 ctx->fs_links_count++;
479 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480 next:
481 offset += dirent->rec_len;
482 } while (offset < fs->blocksize);
483#if 0
484 printf("\n");
485#endif
486 if (offset != fs->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000487 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
488 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
489 dirent->rec_len = cd->pctx.num;
490 dir_modified++;
491 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492 }
493 if (dir_modified) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000494 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
495 if (cd->pctx.errcode) {
496 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx))
497 fatal_error(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 }
499 ext2fs_mark_changed(fs);
500 }
501 return 0;
502}
503
504/*
505 * This function is called to deallocate a block, and is an interator
506 * functioned called by deallocate inode via ext2fs_iterate_block().
507 */
508static int deallocate_inode_block(ext2_filsys fs,
509 blk_t *block_nr,
510 int blockcnt,
511 void *private)
512{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000513 e2fsck_t ctx = (e2fsck_t) private;
514
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 if (!*block_nr)
516 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000517 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000518 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 return 0;
520}
521
522/*
523 * This fuction deallocates an inode
524 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000525static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 char* block_buf)
527{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000528 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000530 struct problem_context pctx;
531
532 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
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");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000537 clear_problem_context(&pctx);
538 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000539
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 /*
541 * Fix up the bitmaps...
542 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000543 read_bitmaps(ctx);
544 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
545 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
546 if (ctx->inode_bad_map)
547 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000548 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 ext2fs_mark_ib_dirty(fs);
550
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000551 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 return;
553
554 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000555 pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
556 deallocate_inode_block, ctx);
557 if (pctx.errcode) {
558 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
559 fatal_error(0);
560 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561}
562
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000563static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000565 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000568 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000569 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570
Theodore Ts'of3db3561997-04-26 13:34:30 +0000571 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000572
573 clear_problem_context(&pctx);
574 pctx.ino = ino;
575 pctx.dir = dir;
576 pctx.inode = &inode;
577
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000578 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
579 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
580 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
581 !(LINUX_S_ISSOCK(inode.i_mode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000582 if (fix_problem(ctx, PR_2_BAD_MODE, &pctx)) {
583 deallocate_inode(ctx, ino, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000585 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000587
588 if (LINUX_S_ISCHR(inode.i_mode)
589 && !e2fsck_pass1_check_device_inode(&inode)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000590 if (fix_problem(ctx, PR_2_BAD_CHAR_DEV, &pctx)) {
591 deallocate_inode(ctx, ino, 0);
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000592 return 1;
593 }
594 }
595
596 if (LINUX_S_ISBLK(inode.i_mode)
597 && !e2fsck_pass1_check_device_inode(&inode)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000598 if (fix_problem(ctx, PR_2_BAD_BLOCK_DEV, &pctx)) {
599 deallocate_inode(ctx, ino, 0);
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000600 return 1;
601 }
602 }
603
604
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000605 if (inode.i_faddr &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000606 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000607 inode.i_faddr = 0;
608 inode_modified++;
609 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000610
611 switch (fs->super->s_creator_os) {
612 case EXT2_OS_LINUX:
613 frag = &inode.osd2.linux2.l_i_frag;
614 fsize = &inode.osd2.linux2.l_i_fsize;
615 break;
616 case EXT2_OS_HURD:
617 frag = &inode.osd2.hurd2.h_i_frag;
618 fsize = &inode.osd2.hurd2.h_i_fsize;
619 break;
620 case EXT2_OS_MASIX:
621 frag = &inode.osd2.masix2.m_i_frag;
622 fsize = &inode.osd2.masix2.m_i_fsize;
623 break;
624 default:
625 frag = fsize = 0;
626 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000627 if (frag && *frag) {
628 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000630 *frag = 0;
631 inode_modified++;
632 }
633 pctx.num = 0;
634 }
635 if (fsize && *fsize) {
636 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000637 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000638 *fsize = 0;
639 inode_modified++;
640 }
641 pctx.num = 0;
642 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000643
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000644 if (inode.i_file_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000645 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000646 inode.i_file_acl = 0;
647 inode_modified++;
648 }
649 if (inode.i_dir_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000650 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000651 inode.i_dir_acl = 0;
652 inode_modified++;
653 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000654 if (inode_modified)
655 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 return 0;
657}
658
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000659
660/*
661 * allocate_dir_block --- this function allocates a new directory
662 * block for a particular inode; this is done if a directory has
663 * a "hole" in it, or if a directory has a illegal block number
664 * that was zeroed out and now needs to be replaced.
665 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000666static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000667 struct ext2_db_entry *db,
668 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000669{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000670 ext2_filsys fs = ctx->fs;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000671 blk_t blk;
672 char *block;
673 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000674
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000675 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000676 return 1;
677
678 /*
679 * Read the inode and block bitmaps in; we'll be messing with
680 * them.
681 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000682 read_bitmaps(ctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000683
684 /*
685 * First, find a free block
686 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000687 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
688 if (pctx->errcode) {
689 pctx->str = "ext2fs_new_block";
690 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000691 return 1;
692 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000693 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000694 ext2fs_mark_block_bitmap(fs->block_map, blk);
695 ext2fs_mark_bb_dirty(fs);
696
697 /*
698 * Now let's create the actual data block for the inode
699 */
700 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000701 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000702 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000703 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
704 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000705
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000706 if (pctx->errcode) {
707 pctx->str = "ext2fs_new_dir_block";
708 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000709 return 1;
710 }
711
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000712 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000713 free(block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000714 if (pctx->errcode) {
715 pctx->str = "ext2fs_write_dir_block";
716 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000717 return 1;
718 }
719
720 /*
721 * Update the inode block count
722 */
723 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
724 inode.i_blocks += fs->blocksize / 512;
725 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
726 inode.i_size = (db->blockcnt+1) * fs->blocksize;
727 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
728
729 /*
730 * Finally, update the block pointers for the inode
731 */
732 db->blk = blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000733 pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000734 0, update_dir_block, db);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000735 if (pctx->errcode) {
736 pctx->str = "ext2fs_block_iterate";
737 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000738 return 1;
739 }
740
741 return 0;
742}
743
744/*
745 * This is a helper function for allocate_dir_block().
746 */
747static int update_dir_block(ext2_filsys fs,
748 blk_t *block_nr,
749 int blockcnt,
750 void *private)
751{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000752 struct ext2_db_entry *db = private;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000753
754 if (db->blockcnt == blockcnt) {
755 *block_nr = db->blk;
756 return BLOCK_CHANGED;
757 }
758 return 0;
759}
760