blob: 0032c8526872751fe050c053ec6554a7899d2238 [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
43#include "et/com_err.h"
44
45#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000046#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000047
48/*
49 * Keeps track of how many times an inode is referenced.
50 */
Theodore Ts'o21c84b71997-04-29 16:15:03 +000051ext2_icount_t inode_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
53static void deallocate_inode(ext2_filsys fs, ino_t ino,
54 char* block_buf);
55static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
56static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000057 struct ext2_db_entry *dir_blocks_info,
58 void *private);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000059static int allocate_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +000060 struct ext2_db_entry *dir_blocks_info,
61 char *buf, struct problem_context *pctx);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000062static int update_dir_block(ext2_filsys fs,
63 blk_t *block_nr,
64 int blockcnt,
65 void *private);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
Theodore Ts'o21c84b71997-04-29 16:15:03 +000067struct check_dir_struct {
68 char *buf;
69 struct problem_context pctx;
70};
71
Theodore Ts'o3839e651997-04-26 13:21:57 +000072void pass2(ext2_filsys fs)
73{
Theodore Ts'o3839e651997-04-26 13:21:57 +000074 char *buf;
75 struct resource_track rtrack;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000076 struct dir_info *dir;
77 errcode_t retval;
78 ino_t size;
79 struct check_dir_struct cd;
80
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 init_resource_track(&rtrack);
82
83#ifdef MTRACE
84 mtrace_print("Pass 2");
85#endif
86
87 if (!preen)
88 printf("Pass 2: Checking directory structure\n");
Theodore Ts'o21c84b71997-04-29 16:15:03 +000089 size = ext2fs_get_icount_size(inode_link_info) + 10;
90 retval = ext2fs_create_icount(fs, EXT2_ICOUNT_OPT_INCREMENT,
91 size, &inode_count);
92 if (retval) {
93 com_err("ext2fs_create_icount", retval,
94 "while creating inode_count");
95 fatal_error(0);
96 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000097 buf = allocate_memory(fs->blocksize, "directory scan buffer");
98
Theodore Ts'o21c84b71997-04-29 16:15:03 +000099 /*
100 * Set up the parent pointer for the root directory, if
101 * present. (If the root directory is not present, we will
102 * create it in pass 3.)
103 */
104 dir = get_dir_info(EXT2_ROOT_INO);
105 if (dir)
106 dir->parent = EXT2_ROOT_INO;
107
108 cd.buf = buf;
109 clear_problem_context(&cd.pctx);
110
111 retval = ext2fs_dblist_iterate(fs->dblist, check_dir_block, &cd);
112
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113 free(buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000114 ext2fs_free_dblist(fs->dblist);
115
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 if (inode_bad_map) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000117 ext2fs_free_inode_bitmap(inode_bad_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 inode_bad_map = 0;
119 }
120 if (tflag > 1) {
121 printf("Pass 2: ");
122 print_resource_track(&rtrack);
123 }
124}
125
126/*
127 * Make sure the first entry in the directory is '.', and that the
128 * directory entry is sane.
129 */
130static int check_dot(ext2_filsys fs,
131 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000132 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133{
134 struct ext2_dir_entry *nextdir;
135 int status = 0;
136 int created = 0;
137 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000138 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000140 if (!dirent->inode)
141 problem = PR_2_MISSING_DOT;
142 else if ((dirent->name_len != 1) ||
143 (dirent->name[0] != '.'))
144 problem = PR_2_1ST_NOT_DOT;
145 else if (dirent->name[1] != '\0')
146 problem = PR_2_DOT_NULL_TERM;
147
148 if (problem) {
149 if (fix_problem(fs, problem, pctx)) {
150 if (dirent->rec_len < 12)
151 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 dirent->inode = ino;
153 dirent->name_len = 1;
154 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000155 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 status = 1;
157 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158 }
159 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 if (dirent->inode != ino) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000161 if (fix_problem(fs, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 dirent->inode = ino;
163 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000164 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 }
166 if (dirent->rec_len > 12) {
167 new_len = dirent->rec_len - 12;
168 if (new_len > 12) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000169 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 if (created ||
171 ask("Directory entry for '.' is big. Split", 1)) {
172 nextdir = (struct ext2_dir_entry *)
173 ((char *) dirent + 12);
174 dirent->rec_len = 12;
175 nextdir->rec_len = new_len;
176 nextdir->inode = 0;
177 nextdir->name_len = 0;
178 status = 1;
179 }
180 }
181 }
182 return status;
183}
184
185/*
186 * Make sure the second entry in the directory is '..', and that the
187 * directory entry is sane. We do not check the inode number of '..'
188 * here; this gets done in pass 3.
189 */
190static int check_dotdot(ext2_filsys fs,
191 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000192 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000194 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000196 if (!dirent->inode)
197 problem = PR_2_MISSING_DOT_DOT;
198 else if ((dirent->name_len != 2) ||
199 (dirent->name[0] != '.') ||
200 (dirent->name[1] != '.'))
201 problem = PR_2_2ND_NOT_DOT_DOT;
202 else if (dirent->name[2] != '\0')
203 problem = PR_2_DOT_DOT_NULL_TERM;
204
205 if (problem) {
206 if (fix_problem(fs, problem, pctx)) {
207 if (dirent->rec_len < 12)
208 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 /*
210 * Note: we don't have the parent inode just
211 * yet, so we will fill it in with the root
212 * inode. This will get fixed in pass 3.
213 */
214 dirent->inode = EXT2_ROOT_INO;
215 dirent->name_len = 2;
216 dirent->name[0] = '.';
217 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000218 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000219 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000220 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000221 return 0;
222 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 dir->dotdot = dirent->inode;
224 return 0;
225}
226
227/*
228 * Check to make sure a directory entry doesn't contain any illegal
229 * characters.
230 */
231static int check_name(ext2_filsys fs,
232 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000233 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234{
235 int i;
236 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238
239 for ( i = 0; i < dirent->name_len; i++) {
240 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
241 if (fixup < 0) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000242 fixup = fix_problem(fs, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 }
244 if (fixup) {
245 dirent->name[i] = '.';
246 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000247 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 }
249 }
250 return ret;
251}
252
253static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000254 struct ext2_db_entry *db,
255 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256{
257 struct dir_info *subdir, *dir;
258 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259 int offset = 0;
260 int dir_modified = 0;
261 errcode_t retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000262 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 blk_t block_nr = db->blk;
264 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000265 __u16 links;
266 struct check_dir_struct *cd = private;
267 char *buf = cd->buf;
268
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 /*
270 * Make sure the inode is still in use (could have been
271 * deleted in the duplicate/bad blocks pass.
272 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000273 if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000275
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000276 cd->pctx.ino = ino;
277 cd->pctx.blk = block_nr;
278 cd->pctx.blkcount = db->blockcnt;
279 cd->pctx.ino2 = 0;
280 cd->pctx.dirent = 0;
281 cd->pctx.num = 0;
282
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000283 if (db->blk == 0) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000284 if (allocate_dir_block(fs, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000285 return 0;
286 block_nr = db->blk;
287 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288
289 if (db->blockcnt)
290 dot_state = 2;
291 else
292 dot_state = 0;
293
294#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000295 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296 db->blockcnt, ino);
297#endif
298
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000299 retval = ext2fs_read_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 if (retval) {
301 com_err(program_name, retval,
302 "while reading directory block %d", block_nr);
303 }
304
305 do {
306 dot_state++;
307 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000308 cd->pctx.dirent = dirent;
309 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 if (((offset + dirent->rec_len) > fs->blocksize) ||
311 (dirent->rec_len < 8) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000312 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 ((dirent->name_len+8) > dirent->rec_len)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000314 if (fix_problem(fs, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315 dirent->rec_len = fs->blocksize - offset;
316 dirent->name_len = 0;
317 dirent->inode = 0;
318 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000319 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000322
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000323 if (dirent->name_len > EXT2_NAME_LEN) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000324 if (fix_problem(fs, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000325 dirent->name_len = EXT2_NAME_LEN;
326 dir_modified++;
327 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000328 }
329
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330 if (dot_state == 1) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000331 if (check_dot(fs, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000332 dir_modified++;
333 } else if (dot_state == 2) {
334 dir = get_dir_info(ino);
335 if (!dir) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000336 printf("Internal error: couldn't find dir_info for %lu\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000337 ino);
338 fatal_error(0);
339 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000340 if (check_dotdot(fs, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341 dir_modified++;
342 } else if (dirent->inode == ino) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000343 if (fix_problem(fs, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 dirent->inode = 0;
345 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000346 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347 }
348 }
349 if (!dirent->inode)
350 goto next;
351
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000352 if (check_name(fs, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000353 dir_modified++;
354
355 /*
356 * Make sure the inode listed is a legal one.
357 */
358 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000359 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000361 if (fix_problem(fs, PR_2_BAD_INO, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000362 dirent->inode = 0;
363 dir_modified++;
364 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000365 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366 }
367
368 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000369 * If the inode is unused, offer to clear it.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000371 if (!(ext2fs_test_inode_bitmap(inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372 dirent->inode))) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000373 if (fix_problem(fs, PR_2_UNUSED_INODE, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374 dirent->inode = 0;
375 dir_modified++;
376 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000377 }
378 }
379
380 /*
381 * If the inode is in a bad block, offer to clear it.
382 */
383 if (inode_bb_map &&
384 (ext2fs_test_inode_bitmap(inode_bb_map,
385 dirent->inode))) {
386 if (fix_problem(fs, PR_2_BB_INODE, &cd->pctx)) {
387 dirent->inode = 0;
388 dir_modified++;
389 goto next;
390 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391 }
392
393 /*
394 * If the inode was marked as having bad fields in
395 * pass1, process it and offer to fix/clear it.
396 * (We wait until now so that we can display the
397 * pathname to the user.)
398 */
399 if (inode_bad_map &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000400 ext2fs_test_inode_bitmap(inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401 dirent->inode)) {
402 if (process_bad_inode(fs, ino, dirent->inode)) {
403 dirent->inode = 0;
404 dir_modified++;
405 goto next;
406 }
407 }
408
409 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000410 * Don't allow links to the root directory. We check
411 * this specially to make sure we catch this error
412 * case even if the root directory hasn't been created
413 * yet.
414 */
415 if ((dot_state > 2) && (dirent->inode == EXT2_ROOT_INO)) {
416 if (fix_problem(fs, PR_2_LINK_ROOT, &cd->pctx)) {
417 dirent->inode = 0;
418 dir_modified++;
419 goto next;
420 }
421 }
422
423 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000424 * If this is a directory, then mark its parent in its
425 * dir_info structure. If the parent field is already
426 * filled in, then this directory has more than one
427 * hard link. We assume the first link is correct,
428 * and ask the user if he/she wants to clear this one.
429 */
430 if ((dot_state > 2) &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000431 (ext2fs_test_inode_bitmap(inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 dirent->inode))) {
433 subdir = get_dir_info(dirent->inode);
434 if (!subdir) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000435 printf("INTERNAL ERROR: missing dir %u\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 dirent->inode);
437 fatal_error(0);
438 }
439 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000440 cd->pctx.ino2 = subdir->parent;
441 if (fix_problem(fs, PR_2_LINK_DIR,
442 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 dirent->inode = 0;
444 dir_modified++;
445 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000446 }
447 cd->pctx.ino2 = 0;
448 } else
449 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 }
451
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000452 ext2fs_icount_increment(inode_count, dirent->inode, &links);
453 if (links > 1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 fs_links_count++;
455 fs_total_count++;
456 next:
457 offset += dirent->rec_len;
458 } while (offset < fs->blocksize);
459#if 0
460 printf("\n");
461#endif
462 if (offset != fs->blocksize) {
463 printf("Final rec_len is %d, should be %d\n",
464 dirent->rec_len,
465 dirent->rec_len - fs->blocksize + offset);
466 }
467 if (dir_modified) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000468 retval = ext2fs_write_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 if (retval) {
470 com_err(program_name, retval,
471 "while writing directory block %d", block_nr);
472 }
473 ext2fs_mark_changed(fs);
474 }
475 return 0;
476}
477
478/*
479 * This function is called to deallocate a block, and is an interator
480 * functioned called by deallocate inode via ext2fs_iterate_block().
481 */
482static int deallocate_inode_block(ext2_filsys fs,
483 blk_t *block_nr,
484 int blockcnt,
485 void *private)
486{
487 if (!*block_nr)
488 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000489 ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
490 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491 return 0;
492}
493
494/*
495 * This fuction deallocates an inode
496 */
497static void deallocate_inode(ext2_filsys fs, ino_t ino,
498 char* block_buf)
499{
500 errcode_t retval;
501 struct ext2_inode inode;
502
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000503 ext2fs_icount_store(inode_link_info, ino, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000504 e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 inode.i_links_count = 0;
506 inode.i_dtime = time(0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000507 e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
508
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 /*
510 * Fix up the bitmaps...
511 */
512 read_bitmaps(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000513 ext2fs_unmark_inode_bitmap(inode_used_map, ino);
514 ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 if (inode_bad_map)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000516 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
517 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 ext2fs_mark_ib_dirty(fs);
519
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000520 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 return;
522
523 ext2fs_mark_bb_dirty(fs);
524 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
525 deallocate_inode_block, 0);
526 if (retval)
527 com_err("deallocate_inode", retval,
528 "while calling ext2fs_block_iterate for inode %d",
529 ino);
530}
531
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
533{
534 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000536 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000537 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538
Theodore Ts'of3db3561997-04-26 13:34:30 +0000539 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000540
541 clear_problem_context(&pctx);
542 pctx.ino = ino;
543 pctx.dir = dir;
544 pctx.inode = &inode;
545
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000546 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
547 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
548 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
549 !(LINUX_S_ISSOCK(inode.i_mode))) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000550 if (fix_problem(fs, PR_2_BAD_MODE, &pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 deallocate_inode(fs, ino, 0);
552 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000553 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000555 if (inode.i_faddr &&
556 fix_problem(fs, PR_2_FADDR_ZERO, &pctx)) {
557 inode.i_faddr = 0;
558 inode_modified++;
559 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000560
561 switch (fs->super->s_creator_os) {
562 case EXT2_OS_LINUX:
563 frag = &inode.osd2.linux2.l_i_frag;
564 fsize = &inode.osd2.linux2.l_i_fsize;
565 break;
566 case EXT2_OS_HURD:
567 frag = &inode.osd2.hurd2.h_i_frag;
568 fsize = &inode.osd2.hurd2.h_i_fsize;
569 break;
570 case EXT2_OS_MASIX:
571 frag = &inode.osd2.masix2.m_i_frag;
572 fsize = &inode.osd2.masix2.m_i_fsize;
573 break;
574 default:
575 frag = fsize = 0;
576 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000577 if (frag && *frag) {
578 pctx.num = *frag;
579 if (fix_problem(fs, PR_2_FRAG_ZERO, &pctx)) {
580 *frag = 0;
581 inode_modified++;
582 }
583 pctx.num = 0;
584 }
585 if (fsize && *fsize) {
586 pctx.num = *fsize;
587 if (fix_problem(fs, PR_2_FSIZE_ZERO, &pctx)) {
588 *fsize = 0;
589 inode_modified++;
590 }
591 pctx.num = 0;
592 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000593
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000594 if (inode.i_file_acl &&
595 fix_problem(fs, PR_2_FILE_ACL_ZERO, &pctx)) {
596 inode.i_file_acl = 0;
597 inode_modified++;
598 }
599 if (inode.i_dir_acl &&
600 fix_problem(fs, PR_2_DIR_ACL_ZERO, &pctx)) {
601 inode.i_dir_acl = 0;
602 inode_modified++;
603 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000604 if (inode_modified)
605 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 return 0;
607}
608
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000609
610/*
611 * allocate_dir_block --- this function allocates a new directory
612 * block for a particular inode; this is done if a directory has
613 * a "hole" in it, or if a directory has a illegal block number
614 * that was zeroed out and now needs to be replaced.
615 */
616static int allocate_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000617 struct ext2_db_entry *db,
618 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000619{
620 blk_t blk;
621 char *block;
622 struct ext2_inode inode;
623 errcode_t retval;
624
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000625 if (fix_problem(fs, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000626 return 1;
627
628 /*
629 * Read the inode and block bitmaps in; we'll be messing with
630 * them.
631 */
632 read_bitmaps(fs);
633
634 /*
635 * First, find a free block
636 */
637 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
638 if (retval) {
639 com_err("allocate_dir_block", retval,
640 "while trying to fill a hole in a directory inode");
641 return 1;
642 }
643 ext2fs_mark_block_bitmap(block_found_map, blk);
644 ext2fs_mark_block_bitmap(fs->block_map, blk);
645 ext2fs_mark_bb_dirty(fs);
646
647 /*
648 * Now let's create the actual data block for the inode
649 */
650 if (db->blockcnt)
651 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
652 else
653 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
654 &block);
655
656 if (retval) {
657 com_err("allocate_dir_block", retval,
658 "while creating new directory block");
659 return 1;
660 }
661
662 retval = ext2fs_write_dir_block(fs, blk, block);
663 free(block);
664 if (retval) {
665 com_err("allocate_dir_block", retval,
666 "while writing an empty directory block");
667 return 1;
668 }
669
670 /*
671 * Update the inode block count
672 */
673 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
674 inode.i_blocks += fs->blocksize / 512;
675 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
676 inode.i_size = (db->blockcnt+1) * fs->blocksize;
677 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
678
679 /*
680 * Finally, update the block pointers for the inode
681 */
682 db->blk = blk;
683 retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
684 0, update_dir_block, db);
685 if (retval) {
686 com_err("allocate_dir_block", retval,
687 "while calling ext2fs_block_iterate");
688 return 1;
689 }
690
691 return 0;
692}
693
694/*
695 * This is a helper function for allocate_dir_block().
696 */
697static int update_dir_block(ext2_filsys fs,
698 blk_t *block_nr,
699 int blockcnt,
700 void *private)
701{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000702 struct ext2_db_entry *db = private;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000703
704 if (db->blockcnt == blockcnt) {
705 *block_nr = db->blk;
706 return BLOCK_CHANGED;
707 }
708 return 0;
709}
710