blob: a885f6f18b271c8406af303b04fb6298562d659f [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
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000041 * - The inode_reg_map bitmap
Theodore Ts'o3839e651997-04-26 13:21:57 +000042 */
43
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000045#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000046
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000047#ifdef NO_INLINE_FUNCS
48#define _INLINE_
49#else
50#define _INLINE_ inline
51#endif
52
Theodore Ts'o3839e651997-04-26 13:21:57 +000053/*
54 * Keeps track of how many times an inode is referenced.
55 */
Theodore Ts'o4035f402001-01-12 20:25:50 +000056static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +000057static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000058 struct ext2_db_entry *dir_blocks_info,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +000059 void *priv_data);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000060static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000061 struct ext2_db_entry *dir_blocks_info,
62 char *buf, struct problem_context *pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000063static int update_dir_block(ext2_filsys fs,
64 blk_t *block_nr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +000065 e2_blkcnt_t blockcnt,
Theodore Ts'o4035f402001-01-12 20:25:50 +000066 blk_t ref_block,
67 int ref_offset,
68 void *priv_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
Theodore Ts'o21c84b71997-04-29 16:15:03 +000070struct check_dir_struct {
71 char *buf;
72 struct problem_context pctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000073 int count, max;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000074 e2fsck_t ctx;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000075};
76
Theodore Ts'o08b21301997-11-03 19:42:40 +000077void e2fsck_pass2(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000078{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000079 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000080 char *buf;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000081#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000083#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000084 struct dir_info *dir;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000085 struct check_dir_struct cd;
86
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000087#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000089#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000090
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000091 clear_problem_context(&cd.pctx);
92
Theodore Ts'o3839e651997-04-26 13:21:57 +000093#ifdef MTRACE
94 mtrace_print("Pass 2");
95#endif
96
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000097 if (!(ctx->options & E2F_OPT_PREEN))
98 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
99
100 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
101 0, ctx->inode_link_info,
102 &ctx->inode_count);
103 if (cd.pctx.errcode) {
104 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000105 ctx->flags |= E2F_FLAG_ABORT;
106 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000107 }
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000108 buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
109 "directory scan buffer");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000111 /*
112 * Set up the parent pointer for the root directory, if
113 * present. (If the root directory is not present, we will
114 * create it in pass 3.)
115 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000116 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000117 if (dir)
118 dir->parent = EXT2_ROOT_INO;
119
120 cd.buf = buf;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000121 cd.ctx = ctx;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000122 cd.count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000123 cd.max = ext2fs_dblist_count(fs->dblist);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000124
125 if (ctx->progress)
126 (void) (ctx->progress)(ctx, 2, 0, cd.max);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000127
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000128 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
129 &cd);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000130 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000131 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000132 if (cd.pctx.errcode) {
133 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000134 ctx->flags |= E2F_FLAG_ABORT;
135 return;
Theodore Ts'o7ac02a51997-06-11 18:32:35 +0000136 }
137
Theodore Ts'o08b21301997-11-03 19:42:40 +0000138 ext2fs_free_mem((void **) &buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000139 ext2fs_free_dblist(fs->dblist);
140
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000141 if (ctx->inode_bad_map) {
142 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
143 ctx->inode_bad_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000145 if (ctx->inode_reg_map) {
146 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
147 ctx->inode_reg_map = 0;
148 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000149#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +0000150 if (ctx->options & E2F_OPT_TIME2) {
151 e2fsck_clear_progbar(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000152 print_resource_track("Pass 2", &rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000153 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000154#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155}
156
157/*
158 * Make sure the first entry in the directory is '.', and that the
159 * directory entry is sane.
160 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000161static int check_dot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 struct ext2_dir_entry *dirent,
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000163 ext2_ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164{
165 struct ext2_dir_entry *nextdir;
166 int status = 0;
167 int created = 0;
168 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000169 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000171 if (!dirent->inode)
172 problem = PR_2_MISSING_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000173 else if (((dirent->name_len & 0xFF) != 1) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000174 (dirent->name[0] != '.'))
175 problem = PR_2_1ST_NOT_DOT;
176 else if (dirent->name[1] != '\0')
177 problem = PR_2_DOT_NULL_TERM;
178
179 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000180 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000181 if (dirent->rec_len < 12)
182 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 dirent->inode = ino;
184 dirent->name_len = 1;
185 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000186 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 status = 1;
188 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 }
190 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191 if (dirent->inode != ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000192 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 dirent->inode = ino;
194 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000195 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196 }
197 if (dirent->rec_len > 12) {
198 new_len = dirent->rec_len - 12;
199 if (new_len > 12) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 if (created ||
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000201 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 nextdir = (struct ext2_dir_entry *)
203 ((char *) dirent + 12);
204 dirent->rec_len = 12;
205 nextdir->rec_len = new_len;
206 nextdir->inode = 0;
207 nextdir->name_len = 0;
208 status = 1;
209 }
210 }
211 }
212 return status;
213}
214
215/*
216 * Make sure the second entry in the directory is '..', and that the
217 * directory entry is sane. We do not check the inode number of '..'
218 * here; this gets done in pass 3.
219 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000220static int check_dotdot(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000221 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000222 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000224 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000226 if (!dirent->inode)
227 problem = PR_2_MISSING_DOT_DOT;
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000228 else if (((dirent->name_len & 0xFF) != 2) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000229 (dirent->name[0] != '.') ||
230 (dirent->name[1] != '.'))
231 problem = PR_2_2ND_NOT_DOT_DOT;
232 else if (dirent->name[2] != '\0')
233 problem = PR_2_DOT_DOT_NULL_TERM;
234
235 if (problem) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000236 if (fix_problem(ctx, problem, pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000237 if (dirent->rec_len < 12)
238 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 /*
240 * Note: we don't have the parent inode just
241 * yet, so we will fill it in with the root
242 * inode. This will get fixed in pass 3.
243 */
244 dirent->inode = EXT2_ROOT_INO;
245 dirent->name_len = 2;
246 dirent->name[0] = '.';
247 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000248 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000250 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251 return 0;
252 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253 dir->dotdot = dirent->inode;
254 return 0;
255}
256
257/*
258 * Check to make sure a directory entry doesn't contain any illegal
259 * characters.
260 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000261static int check_name(e2fsck_t ctx,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000262 struct ext2_dir_entry *dirent,
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000263 ext2_ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264{
265 int i;
266 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000269 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
271 if (fixup < 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000272 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 }
274 if (fixup) {
275 dirent->name[i] = '.';
276 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000277 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 }
279 }
280 return ret;
281}
282
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000283/*
284 * Check the directory filetype (if present)
285 */
286static _INLINE_ int check_filetype(e2fsck_t ctx,
287 struct ext2_dir_entry *dirent,
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000288 ext2_ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000289{
290 int filetype = dirent->name_len >> 8;
291 int should_be = EXT2_FT_UNKNOWN;
292 struct ext2_inode inode;
293
294 if (!(ctx->fs->super->s_feature_incompat &
Theodore Ts'o7847c1d1999-10-22 15:11:42 +0000295 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
296 if (filetype == 0 ||
297 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
298 return 0;
299 dirent->name_len = dirent->name_len & 0xFF;
300 return 1;
301 }
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000302
303 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) {
304 should_be = EXT2_FT_DIR;
305 } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map,
306 dirent->inode)) {
307 should_be = EXT2_FT_REG_FILE;
308 } else if (ctx->inode_bad_map &&
309 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
310 dirent->inode))
311 should_be = 0;
312 else {
313 e2fsck_read_inode(ctx, dirent->inode, &inode,
314 "check_filetype");
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000315 should_be = ext2_file_type(inode.i_mode);
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000316 }
317 if (filetype == should_be)
318 return 0;
319 pctx->num = should_be;
320
321 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
322 pctx) == 0)
323 return 0;
324
325 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
326 return 1;
327}
328
329
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000331 struct ext2_db_entry *db,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000332 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000333{
334 struct dir_info *subdir, *dir;
335 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336 int offset = 0;
337 int dir_modified = 0;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000338 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339 blk_t block_nr = db->blk;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000340 ext2_ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000341 __u16 links;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000342 struct check_dir_struct *cd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000343 char *buf;
344 e2fsck_t ctx;
345 int problem;
346
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000347 cd = (struct check_dir_struct *) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000348 buf = cd->buf;
349 ctx = cd->ctx;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000350
351 if (ctx->progress)
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000352 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
353 return DIRENT_ABORT;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000354
Theodore Ts'o3839e651997-04-26 13:21:57 +0000355 /*
356 * Make sure the inode is still in use (could have been
357 * deleted in the duplicate/bad blocks pass.
358 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000359 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000361
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000362 cd->pctx.ino = ino;
363 cd->pctx.blk = block_nr;
364 cd->pctx.blkcount = db->blockcnt;
365 cd->pctx.ino2 = 0;
366 cd->pctx.dirent = 0;
367 cd->pctx.num = 0;
368
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000369 if (db->blk == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000370 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000371 return 0;
372 block_nr = db->blk;
373 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374
375 if (db->blockcnt)
376 dot_state = 2;
377 else
378 dot_state = 0;
379
380#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000381 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 db->blockcnt, ino);
383#endif
384
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000385 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
Theodore Ts'ob9852cd2001-05-05 05:14:59 +0000386 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
387 cd->pctx.errcode = 0; /* We'll handle this ourselves */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000388 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000389 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
390 ctx->flags |= E2F_FLAG_ABORT;
391 return DIRENT_ABORT;
392 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000393 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394 }
395
396 do {
397 dot_state++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000398 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000399 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000400 cd->pctx.dirent = dirent;
401 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000402 if (((offset + dirent->rec_len) > fs->blocksize) ||
Theodore Ts'oc40db6d1999-10-25 21:03:34 +0000403 (dirent->rec_len < 12) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000404 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000405 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000406 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407 dirent->rec_len = fs->blocksize - offset;
408 dirent->name_len = 0;
409 dirent->inode = 0;
410 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000411 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413 }
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000414 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000415 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000416 dirent->name_len = EXT2_NAME_LEN;
417 dir_modified++;
418 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000419 }
420
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 if (dot_state == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000422 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423 dir_modified++;
424 } else if (dot_state == 2) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000425 dir = e2fsck_get_dir_info(ctx, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000426 if (!dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000427 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000428 ctx->flags |= E2F_FLAG_ABORT;
429 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000431 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 dir_modified++;
433 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000434 problem = PR_2_LINK_DOT;
435 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 dirent->inode = 0;
437 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000438 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 }
440 }
441 if (!dirent->inode)
442 goto next;
443
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444 /*
445 * Make sure the inode listed is a legal one.
446 */
447 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000448 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000450 problem = PR_2_BAD_INO;
451 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 dirent->inode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000453 /*
454 * If the inode is unused, offer to clear it.
455 */
456 problem = PR_2_UNUSED_INODE;
457 } else if (ctx->inode_bb_map &&
458 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
459 dirent->inode))) {
460 /*
461 * If the inode is in a bad block, offer to
462 * clear it.
463 */
464 problem = PR_2_BB_INODE;
465 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000466 ((dirent->name_len & 0xFF) == 1) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000467 (dirent->name[0] == '.')) {
468 /*
469 * If there's a '.' entry in anything other
470 * than the first directory entry, it's a
471 * duplicate entry that should be removed.
472 */
473 problem = PR_2_DUP_DOT;
474 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000475 ((dirent->name_len & 0xFF) == 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000476 (dirent->name[0] == '.') &&
477 (dirent->name[1] == '.')) {
478 /*
479 * If there's a '..' entry in anything other
480 * than the second directory entry, it's a
481 * duplicate entry that should be removed.
482 */
483 problem = PR_2_DUP_DOT_DOT;
484 } else if ((dot_state > 2) &&
485 (dirent->inode == EXT2_ROOT_INO)) {
486 /*
487 * Don't allow links to the root directory.
488 * We check this specially to make sure we
489 * catch this error case even if the root
490 * directory hasn't been created yet.
491 */
492 problem = PR_2_LINK_ROOT;
Theodore Ts'oc40db6d1999-10-25 21:03:34 +0000493 } else if ((dot_state > 2) &&
494 (dirent->name_len & 0xFF) == 0) {
495 /*
496 * Don't allow zero-length directory names.
497 */
498 problem = PR_2_NULL_NAME;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000499 }
500
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000501 if (problem) {
502 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000503 dirent->inode = 0;
504 dir_modified++;
505 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000506 } else {
507 ext2fs_unmark_valid(fs);
508 if (problem == PR_2_BAD_INO)
509 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000510 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 }
512
513 /*
514 * If the inode was marked as having bad fields in
515 * pass1, process it and offer to fix/clear it.
516 * (We wait until now so that we can display the
517 * pathname to the user.)
518 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000519 if (ctx->inode_bad_map &&
520 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 dirent->inode)) {
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000522 if (e2fsck_process_bad_inode(ctx, ino,
523 dirent->inode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 dirent->inode = 0;
525 dir_modified++;
526 goto next;
527 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000528 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000529 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 }
531
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000532 if (check_name(ctx, dirent, ino, &cd->pctx))
533 dir_modified++;
534
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000535 if (check_filetype(ctx, dirent, ino, &cd->pctx))
536 dir_modified++;
537
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000538 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 * If this is a directory, then mark its parent in its
540 * dir_info structure. If the parent field is already
541 * filled in, then this directory has more than one
542 * hard link. We assume the first link is correct,
543 * and ask the user if he/she wants to clear this one.
544 */
545 if ((dot_state > 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000546 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 dirent->inode))) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000548 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 if (!subdir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000550 cd->pctx.ino = dirent->inode;
551 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000552 ctx->flags |= E2F_FLAG_ABORT;
553 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554 }
555 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000556 cd->pctx.ino2 = subdir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000557 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000558 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559 dirent->inode = 0;
560 dir_modified++;
561 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000562 }
563 cd->pctx.ino2 = 0;
564 } else
565 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566 }
567
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000568 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
569 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000570 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000571 ctx->fs_links_count++;
572 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573 next:
574 offset += dirent->rec_len;
575 } while (offset < fs->blocksize);
576#if 0
577 printf("\n");
578#endif
579 if (offset != fs->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000580 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
581 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
582 dirent->rec_len = cd->pctx.num;
583 dir_modified++;
584 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 }
586 if (dir_modified) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000587 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
588 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000589 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
590 &cd->pctx)) {
591 ctx->flags |= E2F_FLAG_ABORT;
592 return DIRENT_ABORT;
593 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594 }
595 ext2fs_mark_changed(fs);
596 }
597 return 0;
598}
599
600/*
601 * This function is called to deallocate a block, and is an interator
602 * functioned called by deallocate inode via ext2fs_iterate_block().
603 */
604static int deallocate_inode_block(ext2_filsys fs,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000605 blk_t *block_nr,
606 e2_blkcnt_t blockcnt,
607 blk_t ref_block,
608 int ref_offset,
609 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000611 e2fsck_t ctx = (e2fsck_t) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000612
Theodore Ts'o19178752000-02-11 15:55:07 +0000613 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000615 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000616 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 return 0;
618}
619
620/*
621 * This fuction deallocates an inode
622 */
Theodore Ts'o4035f402001-01-12 20:25:50 +0000623static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000627 struct problem_context pctx;
628
629 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000630 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 inode.i_links_count = 0;
632 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000633 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000634 clear_problem_context(&pctx);
635 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000636
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 /*
638 * Fix up the bitmaps...
639 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000640 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000641 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
642 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
643 if (ctx->inode_bad_map)
644 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000645 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 ext2fs_mark_ib_dirty(fs);
647
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000648 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649 return;
650
Theodore Ts'o342d8472001-07-02 11:54:09 -0400651 if (inode.i_file_acl) {
652 ext2fs_unmark_block_bitmap(ctx->block_found_map,
653 inode.i_file_acl);
654 ext2fs_unmark_block_bitmap(fs->block_map, inode.i_file_acl);
655 }
656
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000658 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000659 deallocate_inode_block, ctx);
660 if (pctx.errcode) {
661 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000662 ctx->flags |= E2F_FLAG_ABORT;
663 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000664 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665}
666
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000667extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
668 ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000669{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000670 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000672 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000673 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000674 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000675 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000676
Theodore Ts'o08b21301997-11-03 19:42:40 +0000677 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000678
679 clear_problem_context(&pctx);
680 pctx.ino = ino;
681 pctx.dir = dir;
682 pctx.inode = &inode;
683
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000684 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
685 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
686 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
Theodore Ts'o08b21301997-11-03 19:42:40 +0000687 !(LINUX_S_ISSOCK(inode.i_mode)))
688 problem = PR_2_BAD_MODE;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +0000689 else if (LINUX_S_ISCHR(inode.i_mode)
690 && !e2fsck_pass1_check_device_inode(&inode))
Theodore Ts'o08b21301997-11-03 19:42:40 +0000691 problem = PR_2_BAD_CHAR_DEV;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +0000692 else if (LINUX_S_ISBLK(inode.i_mode)
693 && !e2fsck_pass1_check_device_inode(&inode))
Theodore Ts'o08b21301997-11-03 19:42:40 +0000694 problem = PR_2_BAD_BLOCK_DEV;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +0000695 else if (LINUX_S_ISFIFO(inode.i_mode)
696 && !e2fsck_pass1_check_device_inode(&inode))
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000697 problem = PR_2_BAD_FIFO;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +0000698 else if (LINUX_S_ISSOCK(inode.i_mode)
699 && !e2fsck_pass1_check_device_inode(&inode))
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000700 problem = PR_2_BAD_SOCKET;
Theodore Ts'ofdbdea02001-06-02 04:26:26 +0000701 else if (LINUX_S_ISLNK(inode.i_mode)
702 && (inode.i_size_high ||
703 (inode.i_size > EXT2_N_BLOCKS*4)))
704 problem = PR_2_SYMLINK_SIZE;
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000705
Theodore Ts'o08b21301997-11-03 19:42:40 +0000706 if (problem) {
707 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000708 deallocate_inode(ctx, ino, 0);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000709 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000710 return 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000711 return 1;
712 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000713 problem = 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000714 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000715
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000716 if (inode.i_faddr &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000717 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000718 inode.i_faddr = 0;
719 inode_modified++;
720 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000721
722 switch (fs->super->s_creator_os) {
723 case EXT2_OS_LINUX:
724 frag = &inode.osd2.linux2.l_i_frag;
725 fsize = &inode.osd2.linux2.l_i_fsize;
726 break;
727 case EXT2_OS_HURD:
728 frag = &inode.osd2.hurd2.h_i_frag;
729 fsize = &inode.osd2.hurd2.h_i_fsize;
730 break;
731 case EXT2_OS_MASIX:
732 frag = &inode.osd2.masix2.m_i_frag;
733 fsize = &inode.osd2.masix2.m_i_fsize;
734 break;
735 default:
736 frag = fsize = 0;
737 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000738 if (frag && *frag) {
739 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000740 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000741 *frag = 0;
742 inode_modified++;
743 }
744 pctx.num = 0;
745 }
746 if (fsize && *fsize) {
747 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000748 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000749 *fsize = 0;
750 inode_modified++;
751 }
752 pctx.num = 0;
753 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000754
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000755 if (inode.i_file_acl &&
Theodore Ts'o342d8472001-07-02 11:54:09 -0400756 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000757 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000758 inode.i_file_acl = 0;
759 inode_modified++;
760 }
Theodore Ts'o342d8472001-07-02 11:54:09 -0400761 if (inode.i_file_acl &&
762 ((inode.i_file_acl < fs->super->s_first_data_block) ||
763 (inode.i_file_acl >= fs->super->s_blocks_count)) &&
764 fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
765 inode.i_file_acl = 0;
766 inode_modified++;
767 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000768 if (inode.i_dir_acl &&
Theodore Ts'o246501c1998-03-24 16:22:38 +0000769 LINUX_S_ISDIR(inode.i_mode) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000770 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000771 inode.i_dir_acl = 0;
772 inode_modified++;
773 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000774 if (inode_modified)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000775 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000776 return 0;
777}
778
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000779
780/*
781 * allocate_dir_block --- this function allocates a new directory
782 * block for a particular inode; this is done if a directory has
783 * a "hole" in it, or if a directory has a illegal block number
784 * that was zeroed out and now needs to be replaced.
785 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000786static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000787 struct ext2_db_entry *db,
788 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000789{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000790 ext2_filsys fs = ctx->fs;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000791 blk_t blk;
792 char *block;
793 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000794
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000795 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000796 return 1;
797
798 /*
799 * Read the inode and block bitmaps in; we'll be messing with
800 * them.
801 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000802 e2fsck_read_bitmaps(ctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000803
804 /*
805 * First, find a free block
806 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000807 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
808 if (pctx->errcode) {
809 pctx->str = "ext2fs_new_block";
810 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000811 return 1;
812 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000813 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000814 ext2fs_mark_block_bitmap(fs->block_map, blk);
815 ext2fs_mark_bb_dirty(fs);
816
817 /*
818 * Now let's create the actual data block for the inode
819 */
820 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000821 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000822 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000823 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
824 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000825
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000826 if (pctx->errcode) {
827 pctx->str = "ext2fs_new_dir_block";
828 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000829 return 1;
830 }
831
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000832 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000833 ext2fs_free_mem((void **) &block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000834 if (pctx->errcode) {
835 pctx->str = "ext2fs_write_dir_block";
836 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000837 return 1;
838 }
839
840 /*
841 * Update the inode block count
842 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000843 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000844 inode.i_blocks += fs->blocksize / 512;
845 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
846 inode.i_size = (db->blockcnt+1) * fs->blocksize;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000847 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000848
849 /*
850 * Finally, update the block pointers for the inode
851 */
852 db->blk = blk;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000853 pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE,
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000854 0, update_dir_block, db);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000855 if (pctx->errcode) {
856 pctx->str = "ext2fs_block_iterate";
857 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000858 return 1;
859 }
860
861 return 0;
862}
863
864/*
865 * This is a helper function for allocate_dir_block().
866 */
867static int update_dir_block(ext2_filsys fs,
868 blk_t *block_nr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000869 e2_blkcnt_t blockcnt,
870 blk_t ref_block,
871 int ref_offset,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000872 void *priv_data)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000873{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000874 struct ext2_db_entry *db;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000875
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000876 db = (struct ext2_db_entry *) priv_data;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000877 if (db->blockcnt == (int) blockcnt) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000878 *block_nr = db->blk;
879 return BLOCK_CHANGED;
880 }
881 return 0;
882}