blob: 955f5eff148d1c81d5d9fbbf24079fbb402a3edd [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);
386 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000387 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
388 ctx->flags |= E2F_FLAG_ABORT;
389 return DIRENT_ABORT;
390 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000391 memset(buf, 0, fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392 }
393
394 do {
395 dot_state++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000396 problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000398 cd->pctx.dirent = dirent;
399 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400 if (((offset + dirent->rec_len) > fs->blocksize) ||
Theodore Ts'oc40db6d1999-10-25 21:03:34 +0000401 (dirent->rec_len < 12) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000402 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000403 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000404 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405 dirent->rec_len = fs->blocksize - offset;
406 dirent->name_len = 0;
407 dirent->inode = 0;
408 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000409 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000411 }
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000412 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000413 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000414 dirent->name_len = EXT2_NAME_LEN;
415 dir_modified++;
416 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000417 }
418
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 if (dot_state == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000420 if (check_dot(ctx, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 dir_modified++;
422 } else if (dot_state == 2) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000423 dir = e2fsck_get_dir_info(ctx, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000424 if (!dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000425 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000426 ctx->flags |= E2F_FLAG_ABORT;
427 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000429 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 dir_modified++;
431 } else if (dirent->inode == ino) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000432 problem = PR_2_LINK_DOT;
433 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434 dirent->inode = 0;
435 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000436 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437 }
438 }
439 if (!dirent->inode)
440 goto next;
441
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 /*
443 * Make sure the inode listed is a legal one.
444 */
445 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000446 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000448 problem = PR_2_BAD_INO;
449 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 dirent->inode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000451 /*
452 * If the inode is unused, offer to clear it.
453 */
454 problem = PR_2_UNUSED_INODE;
455 } else if (ctx->inode_bb_map &&
456 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
457 dirent->inode))) {
458 /*
459 * If the inode is in a bad block, offer to
460 * clear it.
461 */
462 problem = PR_2_BB_INODE;
463 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000464 ((dirent->name_len & 0xFF) == 1) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000465 (dirent->name[0] == '.')) {
466 /*
467 * If there's a '.' entry in anything other
468 * than the first directory entry, it's a
469 * duplicate entry that should be removed.
470 */
471 problem = PR_2_DUP_DOT;
472 } else if ((dot_state > 2) &&
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000473 ((dirent->name_len & 0xFF) == 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000474 (dirent->name[0] == '.') &&
475 (dirent->name[1] == '.')) {
476 /*
477 * If there's a '..' entry in anything other
478 * than the second directory entry, it's a
479 * duplicate entry that should be removed.
480 */
481 problem = PR_2_DUP_DOT_DOT;
482 } else if ((dot_state > 2) &&
483 (dirent->inode == EXT2_ROOT_INO)) {
484 /*
485 * Don't allow links to the root directory.
486 * We check this specially to make sure we
487 * catch this error case even if the root
488 * directory hasn't been created yet.
489 */
490 problem = PR_2_LINK_ROOT;
Theodore Ts'oc40db6d1999-10-25 21:03:34 +0000491 } else if ((dot_state > 2) &&
492 (dirent->name_len & 0xFF) == 0) {
493 /*
494 * Don't allow zero-length directory names.
495 */
496 problem = PR_2_NULL_NAME;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000497 }
498
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000499 if (problem) {
500 if (fix_problem(ctx, problem, &cd->pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000501 dirent->inode = 0;
502 dir_modified++;
503 goto next;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000504 } else {
505 ext2fs_unmark_valid(fs);
506 if (problem == PR_2_BAD_INO)
507 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000508 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 }
510
511 /*
512 * If the inode was marked as having bad fields in
513 * pass1, process it and offer to fix/clear it.
514 * (We wait until now so that we can display the
515 * pathname to the user.)
516 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000517 if (ctx->inode_bad_map &&
518 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 dirent->inode)) {
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000520 if (e2fsck_process_bad_inode(ctx, ino,
521 dirent->inode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 dirent->inode = 0;
523 dir_modified++;
524 goto next;
525 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000526 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000527 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528 }
529
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000530 if (check_name(ctx, dirent, ino, &cd->pctx))
531 dir_modified++;
532
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000533 if (check_filetype(ctx, dirent, ino, &cd->pctx))
534 dir_modified++;
535
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000536 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537 * If this is a directory, then mark its parent in its
538 * dir_info structure. If the parent field is already
539 * filled in, then this directory has more than one
540 * hard link. We assume the first link is correct,
541 * and ask the user if he/she wants to clear this one.
542 */
543 if ((dot_state > 2) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000544 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 dirent->inode))) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000546 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 if (!subdir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000548 cd->pctx.ino = dirent->inode;
549 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000550 ctx->flags |= E2F_FLAG_ABORT;
551 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 }
553 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000554 cd->pctx.ino2 = subdir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000555 if (fix_problem(ctx, PR_2_LINK_DIR,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000556 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557 dirent->inode = 0;
558 dir_modified++;
559 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000560 }
561 cd->pctx.ino2 = 0;
562 } else
563 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 }
565
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000566 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
567 &links);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000568 if (links > 1)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000569 ctx->fs_links_count++;
570 ctx->fs_total_count++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 next:
572 offset += dirent->rec_len;
573 } while (offset < fs->blocksize);
574#if 0
575 printf("\n");
576#endif
577 if (offset != fs->blocksize) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000578 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
579 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
580 dirent->rec_len = cd->pctx.num;
581 dir_modified++;
582 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000583 }
584 if (dir_modified) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000585 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
586 if (cd->pctx.errcode) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000587 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
588 &cd->pctx)) {
589 ctx->flags |= E2F_FLAG_ABORT;
590 return DIRENT_ABORT;
591 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 }
593 ext2fs_mark_changed(fs);
594 }
595 return 0;
596}
597
598/*
599 * This function is called to deallocate a block, and is an interator
600 * functioned called by deallocate inode via ext2fs_iterate_block().
601 */
602static int deallocate_inode_block(ext2_filsys fs,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000603 blk_t *block_nr,
604 e2_blkcnt_t blockcnt,
605 blk_t ref_block,
606 int ref_offset,
607 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000609 e2fsck_t ctx = (e2fsck_t) priv_data;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000610
Theodore Ts'o19178752000-02-11 15:55:07 +0000611 if (HOLE_BLKADDR(*block_nr))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000613 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000614 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 return 0;
616}
617
618/*
619 * This fuction deallocates an inode
620 */
Theodore Ts'o4035f402001-01-12 20:25:50 +0000621static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000622{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000623 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624 struct ext2_inode inode;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000625 struct problem_context pctx;
626
627 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000628 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 inode.i_links_count = 0;
630 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000631 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000632 clear_problem_context(&pctx);
633 pctx.ino = ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000634
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 /*
636 * Fix up the bitmaps...
637 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000638 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000639 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
640 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
641 if (ctx->inode_bad_map)
642 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000643 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644 ext2fs_mark_ib_dirty(fs);
645
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000646 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 return;
648
649 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000650 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000651 deallocate_inode_block, ctx);
652 if (pctx.errcode) {
653 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000654 ctx->flags |= E2F_FLAG_ABORT;
655 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000656 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657}
658
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000659extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
660 ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000665 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000666 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000667 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668
Theodore Ts'o08b21301997-11-03 19:42:40 +0000669 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000670
671 clear_problem_context(&pctx);
672 pctx.ino = ino;
673 pctx.dir = dir;
674 pctx.inode = &inode;
675
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000676 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
677 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
678 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
Theodore Ts'o08b21301997-11-03 19:42:40 +0000679 !(LINUX_S_ISSOCK(inode.i_mode)))
680 problem = PR_2_BAD_MODE;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000681
682 if (LINUX_S_ISCHR(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000683 && !e2fsck_pass1_check_device_inode(&inode))
684 problem = PR_2_BAD_CHAR_DEV;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000685
686 if (LINUX_S_ISBLK(inode.i_mode)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000687 && !e2fsck_pass1_check_device_inode(&inode))
688 problem = PR_2_BAD_BLOCK_DEV;
689
Theodore Ts'o1dde43f1998-11-14 04:18:28 +0000690 if (LINUX_S_ISFIFO(inode.i_mode)
691 && !e2fsck_pass1_check_device_inode(&inode))
692 problem = PR_2_BAD_FIFO;
693
694 if (LINUX_S_ISSOCK(inode.i_mode)
695 && !e2fsck_pass1_check_device_inode(&inode))
696 problem = PR_2_BAD_SOCKET;
697
Theodore Ts'o08b21301997-11-03 19:42:40 +0000698 if (problem) {
699 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000700 deallocate_inode(ctx, ino, 0);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000701 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000702 return 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000703 return 1;
704 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000705 problem = 0;
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000706 }
Theodore Ts'o7cf73dc1997-08-14 17:17:16 +0000707
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000708 if (inode.i_faddr &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000709 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000710 inode.i_faddr = 0;
711 inode_modified++;
712 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000713
714 switch (fs->super->s_creator_os) {
715 case EXT2_OS_LINUX:
716 frag = &inode.osd2.linux2.l_i_frag;
717 fsize = &inode.osd2.linux2.l_i_fsize;
718 break;
719 case EXT2_OS_HURD:
720 frag = &inode.osd2.hurd2.h_i_frag;
721 fsize = &inode.osd2.hurd2.h_i_fsize;
722 break;
723 case EXT2_OS_MASIX:
724 frag = &inode.osd2.masix2.m_i_frag;
725 fsize = &inode.osd2.masix2.m_i_fsize;
726 break;
727 default:
728 frag = fsize = 0;
729 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000730 if (frag && *frag) {
731 pctx.num = *frag;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000732 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000733 *frag = 0;
734 inode_modified++;
735 }
736 pctx.num = 0;
737 }
738 if (fsize && *fsize) {
739 pctx.num = *fsize;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000740 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000741 *fsize = 0;
742 inode_modified++;
743 }
744 pctx.num = 0;
745 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000746
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000747 if (inode.i_file_acl &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000748 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000749 inode.i_file_acl = 0;
750 inode_modified++;
751 }
752 if (inode.i_dir_acl &&
Theodore Ts'o246501c1998-03-24 16:22:38 +0000753 LINUX_S_ISDIR(inode.i_mode) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000754 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000755 inode.i_dir_acl = 0;
756 inode_modified++;
757 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000758 if (inode_modified)
Theodore Ts'o08b21301997-11-03 19:42:40 +0000759 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000760 return 0;
761}
762
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000763
764/*
765 * allocate_dir_block --- this function allocates a new directory
766 * block for a particular inode; this is done if a directory has
767 * a "hole" in it, or if a directory has a illegal block number
768 * that was zeroed out and now needs to be replaced.
769 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000770static int allocate_dir_block(e2fsck_t ctx,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000771 struct ext2_db_entry *db,
772 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000773{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000774 ext2_filsys fs = ctx->fs;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000775 blk_t blk;
776 char *block;
777 struct ext2_inode inode;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000778
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000779 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000780 return 1;
781
782 /*
783 * Read the inode and block bitmaps in; we'll be messing with
784 * them.
785 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000786 e2fsck_read_bitmaps(ctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000787
788 /*
789 * First, find a free block
790 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000791 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
792 if (pctx->errcode) {
793 pctx->str = "ext2fs_new_block";
794 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000795 return 1;
796 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000797 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000798 ext2fs_mark_block_bitmap(fs->block_map, blk);
799 ext2fs_mark_bb_dirty(fs);
800
801 /*
802 * Now let's create the actual data block for the inode
803 */
804 if (db->blockcnt)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000805 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000806 else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000807 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
808 EXT2_ROOT_INO, &block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000809
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000810 if (pctx->errcode) {
811 pctx->str = "ext2fs_new_dir_block";
812 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000813 return 1;
814 }
815
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000816 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000817 ext2fs_free_mem((void **) &block);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000818 if (pctx->errcode) {
819 pctx->str = "ext2fs_write_dir_block";
820 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000821 return 1;
822 }
823
824 /*
825 * Update the inode block count
826 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000827 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000828 inode.i_blocks += fs->blocksize / 512;
829 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
830 inode.i_size = (db->blockcnt+1) * fs->blocksize;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000831 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000832
833 /*
834 * Finally, update the block pointers for the inode
835 */
836 db->blk = blk;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000837 pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE,
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000838 0, update_dir_block, db);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000839 if (pctx->errcode) {
840 pctx->str = "ext2fs_block_iterate";
841 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000842 return 1;
843 }
844
845 return 0;
846}
847
848/*
849 * This is a helper function for allocate_dir_block().
850 */
851static int update_dir_block(ext2_filsys fs,
852 blk_t *block_nr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000853 e2_blkcnt_t blockcnt,
854 blk_t ref_block,
855 int ref_offset,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000856 void *priv_data)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000857{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000858 struct ext2_db_entry *db;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000859
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000860 db = (struct ext2_db_entry *) priv_data;
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000861 if (db->blockcnt == (int) blockcnt) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000862 *block_nr = db->blk;
863 return BLOCK_CHANGED;
864 }
865 return 0;
866}