blob: 8822e8f5cc27f39719a39bc1083d8edbf94837e3 [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'o521e3681997-04-29 17:48:10 +000089 retval = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
90 0, inode_link_info, &inode_count);
Theodore Ts'o21c84b71997-04-29 16:15:03 +000091 if (retval) {
92 com_err("ext2fs_create_icount", retval,
93 "while creating inode_count");
94 fatal_error(0);
95 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 buf = allocate_memory(fs->blocksize, "directory scan buffer");
97
Theodore Ts'o21c84b71997-04-29 16:15:03 +000098 /*
99 * Set up the parent pointer for the root directory, if
100 * present. (If the root directory is not present, we will
101 * create it in pass 3.)
102 */
103 dir = get_dir_info(EXT2_ROOT_INO);
104 if (dir)
105 dir->parent = EXT2_ROOT_INO;
106
107 cd.buf = buf;
108 clear_problem_context(&cd.pctx);
109
110 retval = ext2fs_dblist_iterate(fs->dblist, check_dir_block, &cd);
111
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112 free(buf);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000113 ext2fs_free_dblist(fs->dblist);
114
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 if (inode_bad_map) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000116 ext2fs_free_inode_bitmap(inode_bad_map);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117 inode_bad_map = 0;
118 }
119 if (tflag > 1) {
120 printf("Pass 2: ");
121 print_resource_track(&rtrack);
122 }
123}
124
125/*
126 * Make sure the first entry in the directory is '.', and that the
127 * directory entry is sane.
128 */
129static int check_dot(ext2_filsys fs,
130 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000131 ino_t ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132{
133 struct ext2_dir_entry *nextdir;
134 int status = 0;
135 int created = 0;
136 int new_len;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000137 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000139 if (!dirent->inode)
140 problem = PR_2_MISSING_DOT;
141 else if ((dirent->name_len != 1) ||
142 (dirent->name[0] != '.'))
143 problem = PR_2_1ST_NOT_DOT;
144 else if (dirent->name[1] != '\0')
145 problem = PR_2_DOT_NULL_TERM;
146
147 if (problem) {
148 if (fix_problem(fs, problem, pctx)) {
149 if (dirent->rec_len < 12)
150 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 dirent->inode = ino;
152 dirent->name_len = 1;
153 dirent->name[0] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000154 dirent->name[1] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 status = 1;
156 created = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 }
158 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 if (dirent->inode != ino) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000160 if (fix_problem(fs, PR_2_BAD_INODE_DOT, pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 dirent->inode = ino;
162 status = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000163 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164 }
165 if (dirent->rec_len > 12) {
166 new_len = dirent->rec_len - 12;
167 if (new_len > 12) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000168 preenhalt(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 if (created ||
170 ask("Directory entry for '.' is big. Split", 1)) {
171 nextdir = (struct ext2_dir_entry *)
172 ((char *) dirent + 12);
173 dirent->rec_len = 12;
174 nextdir->rec_len = new_len;
175 nextdir->inode = 0;
176 nextdir->name_len = 0;
177 status = 1;
178 }
179 }
180 }
181 return status;
182}
183
184/*
185 * Make sure the second entry in the directory is '..', and that the
186 * directory entry is sane. We do not check the inode number of '..'
187 * here; this gets done in pass 3.
188 */
189static int check_dotdot(ext2_filsys fs,
190 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000191 struct dir_info *dir, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000193 int problem = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000195 if (!dirent->inode)
196 problem = PR_2_MISSING_DOT_DOT;
197 else if ((dirent->name_len != 2) ||
198 (dirent->name[0] != '.') ||
199 (dirent->name[1] != '.'))
200 problem = PR_2_2ND_NOT_DOT_DOT;
201 else if (dirent->name[2] != '\0')
202 problem = PR_2_DOT_DOT_NULL_TERM;
203
204 if (problem) {
205 if (fix_problem(fs, problem, pctx)) {
206 if (dirent->rec_len < 12)
207 dirent->rec_len = 12;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 /*
209 * Note: we don't have the parent inode just
210 * yet, so we will fill it in with the root
211 * inode. This will get fixed in pass 3.
212 */
213 dirent->inode = EXT2_ROOT_INO;
214 dirent->name_len = 2;
215 dirent->name[0] = '.';
216 dirent->name[1] = '.';
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000217 dirent->name[2] = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000219 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 return 0;
221 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 dir->dotdot = dirent->inode;
223 return 0;
224}
225
226/*
227 * Check to make sure a directory entry doesn't contain any illegal
228 * characters.
229 */
230static int check_name(ext2_filsys fs,
231 struct ext2_dir_entry *dirent,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000232 ino_t dir_ino, struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233{
234 int i;
235 int fixup = -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 int ret = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237
238 for ( i = 0; i < dirent->name_len; i++) {
239 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
240 if (fixup < 0) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000241 fixup = fix_problem(fs, PR_2_BAD_NAME, pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000242 }
243 if (fixup) {
244 dirent->name[i] = '.';
245 ret = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000246 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 }
248 }
249 return ret;
250}
251
252static int check_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000253 struct ext2_db_entry *db,
254 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000255{
256 struct dir_info *subdir, *dir;
257 struct ext2_dir_entry *dirent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258 int offset = 0;
259 int dir_modified = 0;
260 errcode_t retval;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000261 int dot_state;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000262 blk_t block_nr = db->blk;
263 ino_t ino = db->ino;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000264 __u16 links;
265 struct check_dir_struct *cd = private;
266 char *buf = cd->buf;
267
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268 /*
269 * Make sure the inode is still in use (could have been
270 * deleted in the duplicate/bad blocks pass.
271 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000272 if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000274
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000275 cd->pctx.ino = ino;
276 cd->pctx.blk = block_nr;
277 cd->pctx.blkcount = db->blockcnt;
278 cd->pctx.ino2 = 0;
279 cd->pctx.dirent = 0;
280 cd->pctx.num = 0;
281
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000282 if (db->blk == 0) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000283 if (allocate_dir_block(fs, db, buf, &cd->pctx))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000284 return 0;
285 block_nr = db->blk;
286 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287
288 if (db->blockcnt)
289 dot_state = 2;
290 else
291 dot_state = 0;
292
293#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000294 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295 db->blockcnt, ino);
296#endif
297
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000298 retval = ext2fs_read_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299 if (retval) {
300 com_err(program_name, retval,
301 "while reading directory block %d", block_nr);
302 }
303
304 do {
305 dot_state++;
306 dirent = (struct ext2_dir_entry *) (buf + offset);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000307 cd->pctx.dirent = dirent;
308 cd->pctx.num = offset;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 if (((offset + dirent->rec_len) > fs->blocksize) ||
310 (dirent->rec_len < 8) ||
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000311 ((dirent->rec_len % 4) != 0) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 ((dirent->name_len+8) > dirent->rec_len)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000313 if (fix_problem(fs, PR_2_DIR_CORRUPTED, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 dirent->rec_len = fs->blocksize - offset;
315 dirent->name_len = 0;
316 dirent->inode = 0;
317 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000318 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 return DIRENT_ABORT;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000321
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000322 if (dirent->name_len > EXT2_NAME_LEN) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000323 if (fix_problem(fs, PR_2_FILENAME_LONG, &cd->pctx)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000324 dirent->name_len = EXT2_NAME_LEN;
325 dir_modified++;
326 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000327 }
328
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329 if (dot_state == 1) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000330 if (check_dot(fs, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000331 dir_modified++;
332 } else if (dot_state == 2) {
333 dir = get_dir_info(ino);
334 if (!dir) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000335 printf("Internal error: couldn't find dir_info for %lu\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336 ino);
337 fatal_error(0);
338 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000339 if (check_dotdot(fs, dirent, dir, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 dir_modified++;
341 } else if (dirent->inode == ino) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000342 if (fix_problem(fs, PR_2_LINK_DOT, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000343 dirent->inode = 0;
344 dir_modified++;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000345 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 }
347 }
348 if (!dirent->inode)
349 goto next;
350
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000351 if (check_name(fs, dirent, ino, &cd->pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 dir_modified++;
353
354 /*
355 * Make sure the inode listed is a legal one.
356 */
357 if (((dirent->inode != EXT2_ROOT_INO) &&
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000358 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 (dirent->inode > fs->super->s_inodes_count)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000360 if (fix_problem(fs, PR_2_BAD_INO, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000361 dirent->inode = 0;
362 dir_modified++;
363 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000364 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000365 }
366
367 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000368 * If the inode is unused, offer to clear it.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000370 if (!(ext2fs_test_inode_bitmap(inode_used_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371 dirent->inode))) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000372 if (fix_problem(fs, PR_2_UNUSED_INODE, &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000373 dirent->inode = 0;
374 dir_modified++;
375 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000376 }
377 }
378
379 /*
380 * If the inode is in a bad block, offer to clear it.
381 */
382 if (inode_bb_map &&
383 (ext2fs_test_inode_bitmap(inode_bb_map,
384 dirent->inode))) {
385 if (fix_problem(fs, PR_2_BB_INODE, &cd->pctx)) {
386 dirent->inode = 0;
387 dir_modified++;
388 goto next;
389 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000390 }
391
392 /*
393 * If the inode was marked as having bad fields in
394 * pass1, process it and offer to fix/clear it.
395 * (We wait until now so that we can display the
396 * pathname to the user.)
397 */
398 if (inode_bad_map &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000399 ext2fs_test_inode_bitmap(inode_bad_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400 dirent->inode)) {
401 if (process_bad_inode(fs, ino, dirent->inode)) {
402 dirent->inode = 0;
403 dir_modified++;
404 goto next;
405 }
406 }
407
408 /*
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000409 * Don't allow links to the root directory. We check
410 * this specially to make sure we catch this error
411 * case even if the root directory hasn't been created
412 * yet.
413 */
414 if ((dot_state > 2) && (dirent->inode == EXT2_ROOT_INO)) {
415 if (fix_problem(fs, PR_2_LINK_ROOT, &cd->pctx)) {
416 dirent->inode = 0;
417 dir_modified++;
418 goto next;
419 }
420 }
421
422 /*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423 * If this is a directory, then mark its parent in its
424 * dir_info structure. If the parent field is already
425 * filled in, then this directory has more than one
426 * hard link. We assume the first link is correct,
427 * and ask the user if he/she wants to clear this one.
428 */
429 if ((dot_state > 2) &&
Theodore Ts'of3db3561997-04-26 13:34:30 +0000430 (ext2fs_test_inode_bitmap(inode_dir_map,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 dirent->inode))) {
432 subdir = get_dir_info(dirent->inode);
433 if (!subdir) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000434 printf("INTERNAL ERROR: missing dir %u\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 dirent->inode);
436 fatal_error(0);
437 }
438 if (subdir->parent) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000439 cd->pctx.ino2 = subdir->parent;
440 if (fix_problem(fs, PR_2_LINK_DIR,
441 &cd->pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 dirent->inode = 0;
443 dir_modified++;
444 goto next;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000445 }
446 cd->pctx.ino2 = 0;
447 } else
448 subdir->parent = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 }
450
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000451 ext2fs_icount_increment(inode_count, dirent->inode, &links);
452 if (links > 1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 fs_links_count++;
454 fs_total_count++;
455 next:
456 offset += dirent->rec_len;
457 } while (offset < fs->blocksize);
458#if 0
459 printf("\n");
460#endif
461 if (offset != fs->blocksize) {
462 printf("Final rec_len is %d, should be %d\n",
463 dirent->rec_len,
464 dirent->rec_len - fs->blocksize + offset);
465 }
466 if (dir_modified) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000467 retval = ext2fs_write_dir_block(fs, block_nr, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 if (retval) {
469 com_err(program_name, retval,
470 "while writing directory block %d", block_nr);
471 }
472 ext2fs_mark_changed(fs);
473 }
474 return 0;
475}
476
477/*
478 * This function is called to deallocate a block, and is an interator
479 * functioned called by deallocate inode via ext2fs_iterate_block().
480 */
481static int deallocate_inode_block(ext2_filsys fs,
482 blk_t *block_nr,
483 int blockcnt,
484 void *private)
485{
486 if (!*block_nr)
487 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000488 ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
489 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 return 0;
491}
492
493/*
494 * This fuction deallocates an inode
495 */
496static void deallocate_inode(ext2_filsys fs, ino_t ino,
497 char* block_buf)
498{
499 errcode_t retval;
500 struct ext2_inode inode;
501
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000502 ext2fs_icount_store(inode_link_info, ino, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000503 e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504 inode.i_links_count = 0;
505 inode.i_dtime = time(0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000506 e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
507
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 /*
509 * Fix up the bitmaps...
510 */
511 read_bitmaps(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000512 ext2fs_unmark_inode_bitmap(inode_used_map, ino);
513 ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 if (inode_bad_map)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000515 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
516 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 ext2fs_mark_ib_dirty(fs);
518
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000519 if (!ext2fs_inode_has_valid_blocks(&inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000520 return;
521
522 ext2fs_mark_bb_dirty(fs);
523 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
524 deallocate_inode_block, 0);
525 if (retval)
526 com_err("deallocate_inode", retval,
527 "while calling ext2fs_block_iterate for inode %d",
528 ino);
529}
530
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
532{
533 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 int inode_modified = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000535 unsigned char *frag, *fsize;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000536 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000537
Theodore Ts'of3db3561997-04-26 13:34:30 +0000538 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000539
540 clear_problem_context(&pctx);
541 pctx.ino = ino;
542 pctx.dir = dir;
543 pctx.inode = &inode;
544
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000545 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
546 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
547 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
548 !(LINUX_S_ISSOCK(inode.i_mode))) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000549 if (fix_problem(fs, PR_2_BAD_MODE, &pctx)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 deallocate_inode(fs, ino, 0);
551 return 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000552 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000554 if (inode.i_faddr &&
555 fix_problem(fs, PR_2_FADDR_ZERO, &pctx)) {
556 inode.i_faddr = 0;
557 inode_modified++;
558 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000559
560 switch (fs->super->s_creator_os) {
561 case EXT2_OS_LINUX:
562 frag = &inode.osd2.linux2.l_i_frag;
563 fsize = &inode.osd2.linux2.l_i_fsize;
564 break;
565 case EXT2_OS_HURD:
566 frag = &inode.osd2.hurd2.h_i_frag;
567 fsize = &inode.osd2.hurd2.h_i_fsize;
568 break;
569 case EXT2_OS_MASIX:
570 frag = &inode.osd2.masix2.m_i_frag;
571 fsize = &inode.osd2.masix2.m_i_fsize;
572 break;
573 default:
574 frag = fsize = 0;
575 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000576 if (frag && *frag) {
577 pctx.num = *frag;
578 if (fix_problem(fs, PR_2_FRAG_ZERO, &pctx)) {
579 *frag = 0;
580 inode_modified++;
581 }
582 pctx.num = 0;
583 }
584 if (fsize && *fsize) {
585 pctx.num = *fsize;
586 if (fix_problem(fs, PR_2_FSIZE_ZERO, &pctx)) {
587 *fsize = 0;
588 inode_modified++;
589 }
590 pctx.num = 0;
591 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000592
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000593 if (inode.i_file_acl &&
594 fix_problem(fs, PR_2_FILE_ACL_ZERO, &pctx)) {
595 inode.i_file_acl = 0;
596 inode_modified++;
597 }
598 if (inode.i_dir_acl &&
599 fix_problem(fs, PR_2_DIR_ACL_ZERO, &pctx)) {
600 inode.i_dir_acl = 0;
601 inode_modified++;
602 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000603 if (inode_modified)
604 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605 return 0;
606}
607
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000608
609/*
610 * allocate_dir_block --- this function allocates a new directory
611 * block for a particular inode; this is done if a directory has
612 * a "hole" in it, or if a directory has a illegal block number
613 * that was zeroed out and now needs to be replaced.
614 */
615static int allocate_dir_block(ext2_filsys fs,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000616 struct ext2_db_entry *db,
617 char *buf, struct problem_context *pctx)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000618{
619 blk_t blk;
620 char *block;
621 struct ext2_inode inode;
622 errcode_t retval;
623
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000624 if (fix_problem(fs, PR_2_DIRECTORY_HOLE, pctx) == 0)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000625 return 1;
626
627 /*
628 * Read the inode and block bitmaps in; we'll be messing with
629 * them.
630 */
631 read_bitmaps(fs);
632
633 /*
634 * First, find a free block
635 */
636 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
637 if (retval) {
638 com_err("allocate_dir_block", retval,
639 "while trying to fill a hole in a directory inode");
640 return 1;
641 }
642 ext2fs_mark_block_bitmap(block_found_map, blk);
643 ext2fs_mark_block_bitmap(fs->block_map, blk);
644 ext2fs_mark_bb_dirty(fs);
645
646 /*
647 * Now let's create the actual data block for the inode
648 */
649 if (db->blockcnt)
650 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
651 else
652 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
653 &block);
654
655 if (retval) {
656 com_err("allocate_dir_block", retval,
657 "while creating new directory block");
658 return 1;
659 }
660
661 retval = ext2fs_write_dir_block(fs, blk, block);
662 free(block);
663 if (retval) {
664 com_err("allocate_dir_block", retval,
665 "while writing an empty directory block");
666 return 1;
667 }
668
669 /*
670 * Update the inode block count
671 */
672 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
673 inode.i_blocks += fs->blocksize / 512;
674 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
675 inode.i_size = (db->blockcnt+1) * fs->blocksize;
676 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
677
678 /*
679 * Finally, update the block pointers for the inode
680 */
681 db->blk = blk;
682 retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
683 0, update_dir_block, db);
684 if (retval) {
685 com_err("allocate_dir_block", retval,
686 "while calling ext2fs_block_iterate");
687 return 1;
688 }
689
690 return 0;
691}
692
693/*
694 * This is a helper function for allocate_dir_block().
695 */
696static int update_dir_block(ext2_filsys fs,
697 blk_t *block_nr,
698 int blockcnt,
699 void *private)
700{
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000701 struct ext2_db_entry *db = private;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000702
703 if (db->blockcnt == blockcnt) {
704 *block_nr = db->blk;
705 return BLOCK_CHANGED;
706 }
707 return 0;
708}
709