blob: 85a857bfa08e41ca25a6d7b471241a316f643a6d [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'of8188ff1997-11-14 05:23:04 +000066 int count, max;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000067 e2fsck_t ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000068};
69
Theodore Ts'o08b21301997-11-03 19:42:40 +000070void e2fsck_pass2(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000071{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000072 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000073 char *buf;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000074#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000075 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000076#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000077 struct dir_info *dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000078 struct check_dir_struct cd;
79
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000080#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000082#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000083
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000084 clear_problem_context(&cd.pctx);
85
Theodore Ts'o3839e651997-04-26 13:21:57 +000086#ifdef MTRACE
87 mtrace_print("Pass 2");
88#endif
89
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000090 if (!(ctx->options & E2F_OPT_PREEN))
91 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
92
93 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
94 0, ctx->inode_link_info,
95 &ctx->inode_count);
96 if (cd.pctx.errcode) {
97 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000098 ctx->flags |= E2F_FLAG_ABORT;
99 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000100 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000101 buf = e2fsck_allocate_memory(ctx, fs->blocksize,
102 "directory scan buffer");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000104 /*
105 * Set up the parent pointer for the root directory, if
106 * present. (If the root directory is not present, we will
107 * create it in pass 3.)
108 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000109 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000110 if (dir)
111 dir->parent = EXT2_ROOT_INO;
112
113 cd.buf = buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000114 cd.ctx = ctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000115 cd.count = 0;
116 cd.max = ext2fs_dblist_count(fs->dblist);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000117
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000118 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
119 &cd);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000120 if (ctx->flags & E2F_FLAG_ABORT)
121 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000122 if (cd.pctx.errcode) {
123 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000124 ctx->flags |= E2F_FLAG_ABORT;
125 return;
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000126 }
127
Theodore Ts'o08b21301997-11-03 19:42:40 +0000128 ext2fs_free_mem((void **) &buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000129 ext2fs_free_dblist(fs->dblist);
130
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000131 if (ctx->inode_bad_map) {
132 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
133 ctx->inode_bad_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000135#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000136 if (ctx->options & E2F_OPT_TIME2)
137 print_resource_track("Pass 2", &rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000138#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139}
140
141/*
142 * Make sure the first entry in the directory is '.', and that the
143 * directory entry is sane.
144 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000145static int check_dot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000147 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148{
149 struct ext2_dir_entry *nextdir;
150 int status = 0;
151 int created = 0;
152 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000153 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000155 if (!dirent->inode)
156 problem = PR_2_MISSING_DOT;
157 else if ((dirent->name_len != 1) ||
158 (dirent->name[0] != '.'))
159 problem = PR_2_1ST_NOT_DOT;
160 else if (dirent->name[1] != '\0')
161 problem = PR_2_DOT_NULL_TERM;
162
163 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000164 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000165 if (dirent->rec_len < 12)
166 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 dirent->inode = ino;
168 dirent->name_len = 1;
169 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000170 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000171 status = 1;
172 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173 }
174 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 if (dirent->inode != ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000176 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000177 dirent->inode = ino;
178 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000179 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 }
181 if (dirent->rec_len > 12) {
182 new_len = dirent->rec_len - 12;
183 if (new_len > 12) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184 if (created ||
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000185 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186 nextdir = (struct ext2_dir_entry *)
187 ((char *) dirent + 12);
188 dirent->rec_len = 12;
189 nextdir->rec_len = new_len;
190 nextdir->inode = 0;
191 nextdir->name_len = 0;
192 status = 1;
193 }
194 }
195 }
196 return status;
197}
198
199/*
200 * Make sure the second entry in the directory is '..', and that the
201 * directory entry is sane. We do not check the inode number of '..'
202 * here; this gets done in pass 3.
203 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000204static int check_dotdot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000206 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000208 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000210 if (!dirent->inode)
211 problem = PR_2_MISSING_DOT_DOT;
212 else if ((dirent->name_len != 2) ||
213 (dirent->name[0] != '.') ||
214 (dirent->name[1] != '.'))
215 problem = PR_2_2ND_NOT_DOT_DOT;
216 else if (dirent->name[2] != '\0')
217 problem = PR_2_DOT_DOT_NULL_TERM;
218
219 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000220 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000221 if (dirent->rec_len < 12)
222 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 /*
224 * Note: we don't have the parent inode just
225 * yet, so we will fill it in with the root
226 * inode. This will get fixed in pass 3.
227 */
228 dirent->inode = EXT2_ROOT_INO;
229 dirent->name_len = 2;
230 dirent->name[0] = '.';
231 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000232 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000234 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000235 return 0;
236 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 dir->dotdot = dirent->inode;
238 return 0;
239}
240
241/*
242 * Check to make sure a directory entry doesn't contain any illegal
243 * characters.
244 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000245static int check_name(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000246 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000247 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248{
249 int i;
250 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252
253 for ( i = 0; i < dirent->name_len; i++) {
254 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
255 if (fixup < 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000256 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000257 }
258 if (fixup) {
259 dirent->name[i] = '.';
260 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000261 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000262 }
263 }
264 return ret;
265}
266
267static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000268 struct ext2_db_entry *db,
269 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270{
271 struct dir_info *subdir, *dir;
272 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 int offset = 0;
274 int dir_modified = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000275 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 blk_t block_nr = db->blk;
277 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000278 __u16 links;
279 struct check_dir_struct *cd = private;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000280 char *buf;
281 e2fsck_t ctx;
282 int problem;
283
284 buf = cd->buf;
285 ctx = cd->ctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000286
287 if (ctx->progress)
288 (ctx->progress)(ctx, 2, cd->count++, cd->max);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000289
Theodore Ts'o3839e651997-04-26 13:21:57 +0000290 /*
291 * Make sure the inode is still in use (could have been
292 * deleted in the duplicate/bad blocks pass.
293 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000294 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000296
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000297 cd->pctx.ino = ino;
298 cd->pctx.blk = block_nr;
299 cd->pctx.blkcount = db->blockcnt;
300 cd->pctx.ino2 = 0;
301 cd->pctx.dirent = 0;
302 cd->pctx.num = 0;
303
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000304 if (db->blk == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000305 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000306 return 0;
307 block_nr = db->blk;
308 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309
310 if (db->blockcnt)
311 dot_state = 2;
312 else
313 dot_state = 0;
314
315#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000316 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 db->blockcnt, ino);
318#endif
319
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000320 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
321 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000322 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
323 ctx->flags |= E2F_FLAG_ABORT;
324 return DIRENT_ABORT;
325 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000326 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000327 }
328
329 do {
330 dot_state++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000331 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000332 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000333 cd->pctx.dirent = dirent;
334 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335 if (((offset + dirent->rec_len) > fs->blocksize) ||
336 (dirent->rec_len < 8) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000337 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338 ((dirent->name_len+8) > dirent->rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000339 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 dirent->rec_len = fs->blocksize - offset;
341 dirent->name_len = 0;
342 dirent->inode = 0;
343 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000344 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000347 if (dirent->name_len > EXT2_NAME_LEN) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000348 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000349 dirent->name_len = EXT2_NAME_LEN;
350 dir_modified++;
351 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000352 }
353
Theodore Ts'o3839e651997-04-26 13:21:57 +0000354 if (dot_state == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000355 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356 dir_modified++;
357 } else if (dot_state == 2) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000358 dir = e2fsck_get_dir_info(ctx, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 if (!dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000360 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000361 ctx->flags |= E2F_FLAG_ABORT;
362 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000363 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000364 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365 dir_modified++;
366 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000367 problem = PR_2_LINK_DOT;
368 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 dirent->inode = 0;
370 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000371 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372 }
373 }
374 if (!dirent->inode)
375 goto next;
376
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377 /*
378 * Make sure the inode listed is a legal one.
379 */
380 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000381 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000383 problem = PR_2_BAD_INO;
384 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000385 dirent->inode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000386 /*
387 * If the inode is unused, offer to clear it.
388 */
389 problem = PR_2_UNUSED_INODE;
390 } else if (ctx->inode_bb_map &&
391 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
392 dirent->inode))) {
393 /*
394 * If the inode is in a bad block, offer to
395 * clear it.
396 */
397 problem = PR_2_BB_INODE;
398 } else if ((dot_state > 2) &&
399 (dirent->name_len == 1) &&
400 (dirent->name[0] == '.')) {
401 /*
402 * If there's a '.' entry in anything other
403 * than the first directory entry, it's a
404 * duplicate entry that should be removed.
405 */
406 problem = PR_2_DUP_DOT;
407 } else if ((dot_state > 2) &&
408 (dirent->name_len == 2) &&
409 (dirent->name[0] == '.') &&
410 (dirent->name[1] == '.')) {
411 /*
412 * If there's a '..' entry in anything other
413 * than the second directory entry, it's a
414 * duplicate entry that should be removed.
415 */
416 problem = PR_2_DUP_DOT_DOT;
417 } else if ((dot_state > 2) &&
418 (dirent->inode == EXT2_ROOT_INO)) {
419 /*
420 * Don't allow links to the root directory.
421 * We check this specially to make sure we
422 * catch this error case even if the root
423 * directory hasn't been created yet.
424 */
425 problem = PR_2_LINK_ROOT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000426 }
427
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000428 if (problem) {
429 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000430 dirent->inode = 0;
431 dir_modified++;
432 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000433 } else {
434 ext2fs_unmark_valid(fs);
435 if (problem == PR_2_BAD_INO)
436 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000437 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 }
439
440 /*
441 * If the inode was marked as having bad fields in
442 * pass1, process it and offer to fix/clear it.
443 * (We wait until now so that we can display the
444 * pathname to the user.)
445 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000446 if (ctx->inode_bad_map &&
447 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448 dirent->inode)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000449 if (process_bad_inode(ctx, ino, dirent->inode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 dirent->inode = 0;
451 dir_modified++;
452 goto next;
453 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000454 if (ctx->flags & E2F_FLAG_ABORT)
455 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 }
457
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000458 if (check_name(ctx, dirent, ino, &cd->pctx))
459 dir_modified++;
460
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000461 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 * If this is a directory, then mark its parent in its
463 * dir_info structure. If the parent field is already
464 * filled in, then this directory has more than one
465 * hard link. We assume the first link is correct,
466 * and ask the user if he/she wants to clear this one.
467 */
468 if ((dot_state > 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000469 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000470 dirent->inode))) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000471 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 if (!subdir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000473 cd->pctx.ino = dirent->inode;
474 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000475 ctx->flags |= E2F_FLAG_ABORT;
476 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 }
478 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000479 cd->pctx.ino2 = subdir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000480 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000481 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482 dirent->inode = 0;
483 dir_modified++;
484 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000485 }
486 cd->pctx.ino2 = 0;
487 } else
488 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489 }
490
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000491 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
492 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000493 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000494 ctx->fs_links_count++;
495 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 next:
497 offset += dirent->rec_len;
498 } while (offset < fs->blocksize);
499#if 0
500 printf("\n");
501#endif
502 if (offset != fs->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000503 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
504 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
505 dirent->rec_len = cd->pctx.num;
506 dir_modified++;
507 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 }
509 if (dir_modified) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
511 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000512 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
513 &cd->pctx)) {
514 ctx->flags |= E2F_FLAG_ABORT;
515 return DIRENT_ABORT;
516 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 }
518 ext2fs_mark_changed(fs);
519 }
520 return 0;
521}
522
523/*
524 * This function is called to deallocate a block, and is an interator
525 * functioned called by deallocate inode via ext2fs_iterate_block().
526 */
527static int deallocate_inode_block(ext2_filsys fs,
528 blk_t *block_nr,
529 int blockcnt,
530 void *private)
531{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000532 e2fsck_t ctx = (e2fsck_t) private;
533
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 if (!*block_nr)
535 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000536 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000537 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 return 0;
539}
540
541/*
542 * This fuction deallocates an inode
543 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000544static void deallocate_inode(e2fsck_t ctx, ino_t ino,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 char* block_buf)
546{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000547 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000549 struct problem_context pctx;
550
551 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000552 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 inode.i_links_count = 0;
554 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000555 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000556 clear_problem_context(&pctx);
557 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000558
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559 /*
560 * Fix up the bitmaps...
561 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000562 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000563 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
564 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
565 if (ctx->inode_bad_map)
566 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000567 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568 ext2fs_mark_ib_dirty(fs);
569
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000570 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 return;
572
573 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000574 pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
575 deallocate_inode_block, ctx);
576 if (pctx.errcode) {
577 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000578 ctx->flags |= E2F_FLAG_ABORT;
579 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000580 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581}
582
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000583static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000585 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000588 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000589 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000590 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000591
Theodore Ts'o08b21301997-11-03 19:42:40 +0000592 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000593
594 clear_problem_context(&pctx);
595 pctx.ino = ino;
596 pctx.dir = dir;
597 pctx.inode = &inode;
598
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000599 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
600 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
601 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
Theodore Ts'o08b21301997-11-03 19:42:40 +0000602 !(LINUX_S_ISSOCK(inode.i_mode)))
603 problem = PR_2_BAD_MODE;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000604
605 if (LINUX_S_ISCHR(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000606 && !e2fsck_pass1_check_device_inode(&inode))
607 problem = PR_2_BAD_CHAR_DEV;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000608
609 if (LINUX_S_ISBLK(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000610 && !e2fsck_pass1_check_device_inode(&inode))
611 problem = PR_2_BAD_BLOCK_DEV;
612
613 if (problem) {
614 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000615 deallocate_inode(ctx, ino, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000616 if (ctx->flags & E2F_FLAG_ABORT)
617 return 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000618 return 1;
619 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000620 problem = 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000621 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000622
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000623 if (inode.i_faddr &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000624 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000625 inode.i_faddr = 0;
626 inode_modified++;
627 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000628
629 switch (fs->super->s_creator_os) {
630 case EXT2_OS_LINUX:
631 frag = &inode.osd2.linux2.l_i_frag;
632 fsize = &inode.osd2.linux2.l_i_fsize;
633 break;
634 case EXT2_OS_HURD:
635 frag = &inode.osd2.hurd2.h_i_frag;
636 fsize = &inode.osd2.hurd2.h_i_fsize;
637 break;
638 case EXT2_OS_MASIX:
639 frag = &inode.osd2.masix2.m_i_frag;
640 fsize = &inode.osd2.masix2.m_i_fsize;
641 break;
642 default:
643 frag = fsize = 0;
644 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000645 if (frag && *frag) {
646 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000647 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000648 *frag = 0;
649 inode_modified++;
650 }
651 pctx.num = 0;
652 }
653 if (fsize && *fsize) {
654 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000655 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000656 *fsize = 0;
657 inode_modified++;
658 }
659 pctx.num = 0;
660 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000661
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000662 if (inode.i_file_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000663 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000664 inode.i_file_acl = 0;
665 inode_modified++;
666 }
667 if (inode.i_dir_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000668 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000669 inode.i_dir_acl = 0;
670 inode_modified++;
671 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000672 if (inode_modified)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000673 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 return 0;
675}
676
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000677
678/*
679 * allocate_dir_block --- this function allocates a new directory
680 * block for a particular inode; this is done if a directory has
681 * a "hole" in it, or if a directory has a illegal block number
682 * that was zeroed out and now needs to be replaced.
683 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000684static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000685 struct ext2_db_entry *db,
686 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000687{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000688 ext2_filsys fs = ctx->fs;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000689 blk_t blk;
690 char *block;
691 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000692
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000693 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000694 return 1;
695
696 /*
697 * Read the inode and block bitmaps in; we'll be messing with
698 * them.
699 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000700 e2fsck_read_bitmaps(ctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000701
702 /*
703 * First, find a free block
704 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000705 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
706 if (pctx->errcode) {
707 pctx->str = "ext2fs_new_block";
708 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000709 return 1;
710 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000711 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000712 ext2fs_mark_block_bitmap(fs->block_map, blk);
713 ext2fs_mark_bb_dirty(fs);
714
715 /*
716 * Now let's create the actual data block for the inode
717 */
718 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000719 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000720 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000721 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
722 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000723
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000724 if (pctx->errcode) {
725 pctx->str = "ext2fs_new_dir_block";
726 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000727 return 1;
728 }
729
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000730 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000731 ext2fs_free_mem((void **) &block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000732 if (pctx->errcode) {
733 pctx->str = "ext2fs_write_dir_block";
734 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000735 return 1;
736 }
737
738 /*
739 * Update the inode block count
740 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000741 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000742 inode.i_blocks += fs->blocksize / 512;
743 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
744 inode.i_size = (db->blockcnt+1) * fs->blocksize;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000745 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000746
747 /*
748 * Finally, update the block pointers for the inode
749 */
750 db->blk = blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000751 pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000752 0, update_dir_block, db);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000753 if (pctx->errcode) {
754 pctx->str = "ext2fs_block_iterate";
755 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000756 return 1;
757 }
758
759 return 0;
760}
761
762/*
763 * This is a helper function for allocate_dir_block().
764 */
765static int update_dir_block(ext2_filsys fs,
766 blk_t *block_nr,
767 int blockcnt,
768 void *private)
769{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000770 struct ext2_db_entry *db = private;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000771
772 if (db->blockcnt == blockcnt) {
773 *block_nr = db->blk;
774 return BLOCK_CHANGED;
775 }
776 return 0;
777}
778