Whamcloud - gitweb
f52bc2eee407d34cd20e8e71d5aca187a3a902da
[tools/e2fsprogs.git] / e2fsck / pass2.c
1 /*
2  * pass2.c --- check directory structure
3  * 
4  * 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%
10  * 
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
38  *
39  * Pass 2 frees the following data structures
40  *      - The inode_bad_map bitmap
41  */
42
43 #include "e2fsck.h"
44 #include "problem.h"
45
46 /*
47  * Keeps track of how many times an inode is referenced.
48  */
49 static void deallocate_inode(e2fsck_t ctx, ino_t ino,
50                              char* block_buf);
51 static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino);
52 static int check_dir_block(ext2_filsys fs,
53                            struct ext2_db_entry *dir_blocks_info,
54                            void *priv_data);
55 static int allocate_dir_block(e2fsck_t ctx,
56                               struct ext2_db_entry *dir_blocks_info,
57                               char *buf, struct problem_context *pctx);
58 static int update_dir_block(ext2_filsys fs,
59                             blk_t       *block_nr,
60                             int blockcnt,
61                             void *priv_data);
62
63 struct check_dir_struct {
64         char *buf;
65         struct problem_context  pctx;
66         int     count, max;
67         e2fsck_t ctx;
68 };      
69
70 void e2fsck_pass2(e2fsck_t ctx)
71 {
72         ext2_filsys     fs = ctx->fs;
73         char    *buf;
74 #ifdef RESOURCE_TRACK
75         struct resource_track   rtrack;
76 #endif
77         struct dir_info *dir;
78         struct check_dir_struct cd;
79                 
80 #ifdef RESOURCE_TRACK
81         init_resource_track(&rtrack);
82 #endif
83
84         clear_problem_context(&cd.pctx);
85
86 #ifdef MTRACE
87         mtrace_print("Pass 2");
88 #endif
89
90         if (!(ctx->options & E2F_OPT_PREEN))
91                 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
92
93         cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
94                                                 0, ctx->inode_link_info,
95                                                 &ctx->inode_count);
96         if (cd.pctx.errcode) {
97                 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
98                 ctx->flags |= E2F_FLAG_ABORT;
99                 return;
100         }
101         buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
102                                               "directory scan buffer");
103
104         /*
105          * Set up the parent pointer for the root directory, if
106          * present.  (If the root directory is not present, we will
107          * create it in pass 3.)
108          */
109         dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
110         if (dir)
111                 dir->parent = EXT2_ROOT_INO;
112
113         cd.buf = buf;
114         cd.ctx = ctx;
115         cd.count = 0;
116         cd.max = ext2fs_dblist_count(fs->dblist);
117         
118         cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
119                                                 &cd);
120         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
121                 return;
122         if (cd.pctx.errcode) {
123                 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
124                 ctx->flags |= E2F_FLAG_ABORT;
125                 return;
126         }
127         
128         ext2fs_free_mem((void **) &buf);
129         ext2fs_free_dblist(fs->dblist);
130
131         if (ctx->inode_bad_map) {
132                 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
133                 ctx->inode_bad_map = 0;
134         }
135 #ifdef RESOURCE_TRACK
136         if (ctx->options & E2F_OPT_TIME2)
137                 print_resource_track("Pass 2", &rtrack);
138 #endif
139 }
140
141 /*
142  * Make sure the first entry in the directory is '.', and that the
143  * directory entry is sane.
144  */
145 static int check_dot(e2fsck_t ctx,
146                      struct ext2_dir_entry *dirent,
147                      ino_t ino, struct problem_context *pctx)
148 {
149         struct ext2_dir_entry *nextdir;
150         int     status = 0;
151         int     created = 0;
152         int     new_len;
153         int     problem = 0;
154         
155         if (!dirent->inode)
156                 problem = PR_2_MISSING_DOT;
157         else if (((dirent->name_len & 0xFF) != 1) ||
158                  (dirent->name[0] != '.'))
159                 problem = PR_2_1ST_NOT_DOT;
160         else if (dirent->name[1] != '\0')
161                 problem = PR_2_DOT_NULL_TERM;
162         
163         if (problem) {
164                 if (fix_problem(ctx, problem, pctx)) {
165                         if (dirent->rec_len < 12)
166                                 dirent->rec_len = 12;
167                         dirent->inode = ino;
168                         dirent->name_len = 1;
169                         dirent->name[0] = '.';
170                         dirent->name[1] = '\0';
171                         status = 1;
172                         created = 1;
173                 }
174         }
175         if (dirent->inode != ino) {
176                 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
177                         dirent->inode = ino;
178                         status = 1;
179                 }
180         }
181         if (dirent->rec_len > 12) {
182                 new_len = dirent->rec_len - 12;
183                 if (new_len > 12) {
184                         if (created ||
185                             fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
186                                 nextdir = (struct ext2_dir_entry *)
187                                         ((char *) dirent + 12);
188                                 dirent->rec_len = 12;
189                                 nextdir->rec_len = new_len;
190                                 nextdir->inode = 0;
191                                 nextdir->name_len = 0;
192                                 status = 1;
193                         }
194                 }
195         }
196         return status;
197 }
198
199 /*
200  * Make sure the second entry in the directory is '..', and that the
201  * directory entry is sane.  We do not check the inode number of '..'
202  * here; this gets done in pass 3.
203  */
204 static int check_dotdot(e2fsck_t ctx,
205                         struct ext2_dir_entry *dirent,
206                         struct dir_info *dir, struct problem_context *pctx)
207 {
208         int             problem = 0;
209         
210         if (!dirent->inode)
211                 problem = PR_2_MISSING_DOT_DOT;
212         else if (((dirent->name_len & 0xFF) != 2) ||
213                  (dirent->name[0] != '.') ||
214                  (dirent->name[1] != '.'))
215                 problem = PR_2_2ND_NOT_DOT_DOT;
216         else if (dirent->name[2] != '\0')
217                 problem = PR_2_DOT_DOT_NULL_TERM;
218
219         if (problem) {
220                 if (fix_problem(ctx, problem, pctx)) {
221                         if (dirent->rec_len < 12)
222                                 dirent->rec_len = 12;
223                         /*
224                          * Note: we don't have the parent inode just
225                          * yet, so we will fill it in with the root
226                          * inode.  This will get fixed in pass 3.
227                          */
228                         dirent->inode = EXT2_ROOT_INO;
229                         dirent->name_len = 2;
230                         dirent->name[0] = '.';
231                         dirent->name[1] = '.';
232                         dirent->name[2] = '\0';
233                         return 1;
234                 } 
235                 return 0;
236         }
237         dir->dotdot = dirent->inode;
238         return 0;
239 }
240
241 /*
242  * Check to make sure a directory entry doesn't contain any illegal
243  * characters.
244  */
245 static int check_name(e2fsck_t ctx,
246                       struct ext2_dir_entry *dirent,
247                       ino_t dir_ino, struct problem_context *pctx)
248 {
249         int     i;
250         int     fixup = -1;
251         int     ret = 0;
252         
253         for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
254                 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
255                         if (fixup < 0) {
256                                 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
257                         }
258                         if (fixup) {
259                                 dirent->name[i] = '.';
260                                 ret = 1;
261                         }
262                 }
263         }
264         return ret;
265 }
266
267 static int check_dir_block(ext2_filsys fs,
268                            struct ext2_db_entry *db,
269                            void *priv_data)
270 {
271         struct dir_info         *subdir, *dir;
272         struct ext2_dir_entry   *dirent;
273         int                     offset = 0;
274         int                     dir_modified = 0;
275         int                     dot_state;
276         blk_t                   block_nr = db->blk;
277         ino_t                   ino = db->ino;
278         __u16                   links;
279         struct check_dir_struct *cd;
280         char                    *buf;
281         e2fsck_t                ctx;
282         int                     problem;
283
284         cd = (struct check_dir_struct *) priv_data;
285         buf = cd->buf;
286         ctx = cd->ctx;
287
288         if (ctx->progress)
289                 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
290                         return DIRENT_ABORT;
291         
292         /*
293          * Make sure the inode is still in use (could have been 
294          * deleted in the duplicate/bad blocks pass.
295          */
296         if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))) 
297                 return 0;
298
299         cd->pctx.ino = ino;
300         cd->pctx.blk = block_nr;
301         cd->pctx.blkcount = db->blockcnt;
302         cd->pctx.ino2 = 0;
303         cd->pctx.dirent = 0;
304         cd->pctx.num = 0;
305
306         if (db->blk == 0) {
307                 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
308                         return 0;
309                 block_nr = db->blk;
310         }
311         
312         if (db->blockcnt)
313                 dot_state = 2;
314         else
315                 dot_state = 0;
316
317 #if 0
318         printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
319                db->blockcnt, ino);
320 #endif
321         
322         cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
323         if (cd->pctx.errcode) {
324                 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
325                         ctx->flags |= E2F_FLAG_ABORT;
326                         return DIRENT_ABORT;
327                 }
328                 memset(buf, 0, fs->blocksize);
329         }
330
331         do {
332                 dot_state++;
333                 problem = 0;
334                 dirent = (struct ext2_dir_entry *) (buf + offset);
335                 cd->pctx.dirent = dirent;
336                 cd->pctx.num = offset;
337                 if (((offset + dirent->rec_len) > fs->blocksize) ||
338                     (dirent->rec_len < 8) ||
339                     ((dirent->rec_len % 4) != 0) ||
340                     (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
341                         if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
342                                 dirent->rec_len = fs->blocksize - offset;
343                                 dirent->name_len = 0;
344                                 dirent->inode = 0;
345                                 dir_modified++;
346                         } else
347                                 return DIRENT_ABORT;
348                 }
349                 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
350                         if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
351                                 dirent->name_len = EXT2_NAME_LEN;
352                                 dir_modified++;
353                         }
354                 }
355
356                 if (dot_state == 1) {
357                         if (check_dot(ctx, dirent, ino, &cd->pctx))
358                                 dir_modified++;
359                 } else if (dot_state == 2) {
360                         dir = e2fsck_get_dir_info(ctx, ino);
361                         if (!dir) {
362                                 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
363                                 ctx->flags |= E2F_FLAG_ABORT;
364                                 return DIRENT_ABORT;
365                         }
366                         if (check_dotdot(ctx, dirent, dir, &cd->pctx))
367                                 dir_modified++;
368                 } else if (dirent->inode == ino) {
369                         problem = PR_2_LINK_DOT;
370                         if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
371                                 dirent->inode = 0;
372                                 dir_modified++;
373                                 goto next;
374                         }
375                 }
376                 if (!dirent->inode) 
377                         goto next;
378                 
379                 /*
380                  * Make sure the inode listed is a legal one.
381                  */ 
382                 if (((dirent->inode != EXT2_ROOT_INO) &&
383                      (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
384                     (dirent->inode > fs->super->s_inodes_count)) {
385                         problem = PR_2_BAD_INO;
386                 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
387                                                dirent->inode))) {
388                         /*
389                          * If the inode is unused, offer to clear it.
390                          */
391                         problem = PR_2_UNUSED_INODE;
392                 } else if (ctx->inode_bb_map &&
393                            (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
394                                                      dirent->inode))) {
395                         /*
396                          * If the inode is in a bad block, offer to
397                          * clear it.
398                          */
399                         problem = PR_2_BB_INODE;
400                 } else if ((dot_state > 2) &&
401                            ((dirent->name_len & 0xFF) == 1) &&
402                            (dirent->name[0] == '.')) {
403                         /*
404                          * If there's a '.' entry in anything other
405                          * than the first directory entry, it's a
406                          * duplicate entry that should be removed.
407                          */
408                         problem = PR_2_DUP_DOT;
409                 } else if ((dot_state > 2) &&
410                            ((dirent->name_len & 0xFF) == 2) &&
411                            (dirent->name[0] == '.') && 
412                            (dirent->name[1] == '.')) {
413                         /*
414                          * If there's a '..' entry in anything other
415                          * than the second directory entry, it's a
416                          * duplicate entry that should be removed.
417                          */
418                         problem = PR_2_DUP_DOT_DOT;
419                 } else if ((dot_state > 2) &&
420                            (dirent->inode == EXT2_ROOT_INO)) {
421                         /*
422                          * Don't allow links to the root directory.
423                          * We check this specially to make sure we
424                          * catch this error case even if the root
425                          * directory hasn't been created yet.
426                          */
427                         problem = PR_2_LINK_ROOT;
428                 }
429
430                 if (problem) {
431                         if (fix_problem(ctx, problem, &cd->pctx)) {
432                                 dirent->inode = 0;
433                                 dir_modified++;
434                                 goto next;
435                         } else {
436                                 ext2fs_unmark_valid(fs);
437                                 if (problem == PR_2_BAD_INO)
438                                         goto next;
439                         }
440                 }
441
442                 /*
443                  * If the inode was marked as having bad fields in
444                  * pass1, process it and offer to fix/clear it.
445                  * (We wait until now so that we can display the
446                  * pathname to the user.)
447                  */
448                 if (ctx->inode_bad_map &&
449                     ext2fs_test_inode_bitmap(ctx->inode_bad_map,
450                                              dirent->inode)) {
451                         if (process_bad_inode(ctx, ino, dirent->inode)) {
452                                 dirent->inode = 0;
453                                 dir_modified++;
454                                 goto next;
455                         }
456                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
457                                 return DIRENT_ABORT;
458                 }
459
460                 if (check_name(ctx, dirent, ino, &cd->pctx))
461                         dir_modified++;
462
463                 /*
464                  * If this is a directory, then mark its parent in its
465                  * dir_info structure.  If the parent field is already
466                  * filled in, then this directory has more than one
467                  * hard link.  We assume the first link is correct,
468                  * and ask the user if he/she wants to clear this one.
469                  */
470                 if ((dot_state > 2) &&
471                     (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
472                                               dirent->inode))) {
473                         subdir = e2fsck_get_dir_info(ctx, dirent->inode);
474                         if (!subdir) {
475                                 cd->pctx.ino = dirent->inode;
476                                 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
477                                 ctx->flags |= E2F_FLAG_ABORT;
478                                 return DIRENT_ABORT;
479                         }
480                         if (subdir->parent) {
481                                 cd->pctx.ino2 = subdir->parent;
482                                 if (fix_problem(ctx, PR_2_LINK_DIR,
483                                                 &cd->pctx)) {
484                                         dirent->inode = 0;
485                                         dir_modified++;
486                                         goto next;
487                                 }
488                                 cd->pctx.ino2 = 0;
489                         } else
490                                 subdir->parent = ino;
491                 }
492                 
493                 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
494                                         &links);
495                 if (links > 1)
496                         ctx->fs_links_count++;
497                 ctx->fs_total_count++;
498         next:
499                 offset += dirent->rec_len;
500         } while (offset < fs->blocksize);
501 #if 0
502         printf("\n");
503 #endif
504         if (offset != fs->blocksize) {
505                 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
506                 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
507                         dirent->rec_len = cd->pctx.num;
508                         dir_modified++;
509                 }
510         }
511         if (dir_modified) {
512                 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
513                 if (cd->pctx.errcode) {
514                         if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
515                                          &cd->pctx)) {
516                                 ctx->flags |= E2F_FLAG_ABORT;
517                                 return DIRENT_ABORT;
518                         }
519                 }
520                 ext2fs_mark_changed(fs);
521         }
522         return 0;
523 }
524
525 /*
526  * This function is called to deallocate a block, and is an interator
527  * functioned called by deallocate inode via ext2fs_iterate_block().
528  */
529 static int deallocate_inode_block(ext2_filsys fs,
530                              blk_t      *block_nr,
531                              int blockcnt,
532                              void *priv_data)
533 {
534         e2fsck_t        ctx = (e2fsck_t) priv_data;
535         
536         if (!*block_nr)
537                 return 0;
538         ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
539         ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
540         return 0;
541 }
542                 
543 /*
544  * This fuction deallocates an inode
545  */
546 static void deallocate_inode(e2fsck_t ctx, ino_t ino,
547                              char* block_buf)
548 {
549         ext2_filsys fs = ctx->fs;
550         struct ext2_inode       inode;
551         struct problem_context  pctx;
552         
553         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
554         e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
555         inode.i_links_count = 0;
556         inode.i_dtime = time(0);
557         e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
558         clear_problem_context(&pctx);
559         pctx.ino = ino;
560
561         /*
562          * Fix up the bitmaps...
563          */
564         e2fsck_read_bitmaps(ctx);
565         ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
566         ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
567         if (ctx->inode_bad_map)
568                 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
569         ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
570         ext2fs_mark_ib_dirty(fs);
571
572         if (!ext2fs_inode_has_valid_blocks(&inode))
573                 return;
574         
575         ext2fs_mark_bb_dirty(fs);
576         pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
577                                             deallocate_inode_block, ctx);
578         if (pctx.errcode) {
579                 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
580                 ctx->flags |= E2F_FLAG_ABORT;
581                 return;
582         }
583 }
584
585 static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
586 {
587         ext2_filsys fs = ctx->fs;
588         struct ext2_inode       inode;
589         int                     inode_modified = 0;
590         unsigned char           *frag, *fsize;
591         struct problem_context  pctx;
592         int     problem = 0;
593
594         e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
595
596         clear_problem_context(&pctx);
597         pctx.ino = ino;
598         pctx.dir = dir;
599         pctx.inode = &inode;
600
601         if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
602             !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
603             !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
604             !(LINUX_S_ISSOCK(inode.i_mode)))
605                 problem = PR_2_BAD_MODE;
606
607         if (LINUX_S_ISCHR(inode.i_mode)
608             && !e2fsck_pass1_check_device_inode(&inode))
609                 problem = PR_2_BAD_CHAR_DEV;
610                 
611         if (LINUX_S_ISBLK(inode.i_mode)
612             && !e2fsck_pass1_check_device_inode(&inode))
613                 problem = PR_2_BAD_BLOCK_DEV;
614
615         if (problem) {
616                 if (fix_problem(ctx, problem, &pctx)) {
617                         deallocate_inode(ctx, ino, 0);
618                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
619                                 return 0;
620                         return 1;
621                 }
622                 problem = 0;
623         }
624                 
625         if (inode.i_faddr &&
626             fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
627                 inode.i_faddr = 0;
628                 inode_modified++;
629         }
630
631         switch (fs->super->s_creator_os) {
632             case EXT2_OS_LINUX:
633                 frag = &inode.osd2.linux2.l_i_frag;
634                 fsize = &inode.osd2.linux2.l_i_fsize;
635                 break;
636             case EXT2_OS_HURD:
637                 frag = &inode.osd2.hurd2.h_i_frag;
638                 fsize = &inode.osd2.hurd2.h_i_fsize;
639                 break;
640             case EXT2_OS_MASIX:
641                 frag = &inode.osd2.masix2.m_i_frag;
642                 fsize = &inode.osd2.masix2.m_i_fsize;
643                 break;
644             default:
645                 frag = fsize = 0;
646         }
647         if (frag && *frag) {
648                 pctx.num = *frag;
649                 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
650                         *frag = 0;
651                         inode_modified++;
652                 }
653                 pctx.num = 0;
654         }
655         if (fsize && *fsize) {
656                 pctx.num = *fsize;
657                 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
658                         *fsize = 0;
659                         inode_modified++;
660                 }
661                 pctx.num = 0;
662         }
663
664         if (inode.i_file_acl &&
665             fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
666                 inode.i_file_acl = 0;
667                 inode_modified++;
668         }
669         if (inode.i_dir_acl &&
670             LINUX_S_ISDIR(inode.i_mode) &&
671             fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
672                 inode.i_dir_acl = 0;
673                 inode_modified++;
674         }
675         if (inode_modified)
676                 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
677         return 0;
678 }
679
680
681 /*
682  * allocate_dir_block --- this function allocates a new directory
683  *      block for a particular inode; this is done if a directory has
684  *      a "hole" in it, or if a directory has a illegal block number
685  *      that was zeroed out and now needs to be replaced.
686  */
687 static int allocate_dir_block(e2fsck_t ctx,
688                               struct ext2_db_entry *db,
689                               char *buf, struct problem_context *pctx)
690 {
691         ext2_filsys fs = ctx->fs;
692         blk_t                   blk;
693         char                    *block;
694         struct ext2_inode       inode;
695
696         if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
697                 return 1;
698
699         /*
700          * Read the inode and block bitmaps in; we'll be messing with
701          * them.
702          */
703         e2fsck_read_bitmaps(ctx);
704         
705         /*
706          * First, find a free block
707          */
708         pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
709         if (pctx->errcode) {
710                 pctx->str = "ext2fs_new_block";
711                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
712                 return 1;
713         }
714         ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
715         ext2fs_mark_block_bitmap(fs->block_map, blk);
716         ext2fs_mark_bb_dirty(fs);
717
718         /*
719          * Now let's create the actual data block for the inode
720          */
721         if (db->blockcnt)
722                 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
723         else
724                 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
725                                                      EXT2_ROOT_INO, &block);
726
727         if (pctx->errcode) {
728                 pctx->str = "ext2fs_new_dir_block";
729                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
730                 return 1;
731         }
732
733         pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
734         ext2fs_free_mem((void **) &block);
735         if (pctx->errcode) {
736                 pctx->str = "ext2fs_write_dir_block";
737                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
738                 return 1;
739         }
740
741         /*
742          * Update the inode block count
743          */
744         e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
745         inode.i_blocks += fs->blocksize / 512;
746         if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
747                 inode.i_size = (db->blockcnt+1) * fs->blocksize;
748         e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
749
750         /*
751          * Finally, update the block pointers for the inode
752          */
753         db->blk = blk;
754         pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
755                                       0, update_dir_block, db);
756         if (pctx->errcode) {
757                 pctx->str = "ext2fs_block_iterate";
758                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
759                 return 1;
760         }
761
762         return 0;
763 }
764
765 /*
766  * This is a helper function for allocate_dir_block().
767  */
768 static int update_dir_block(ext2_filsys fs,
769                             blk_t       *block_nr,
770                             int blockcnt,
771                             void *priv_data)
772 {
773         struct ext2_db_entry *db;
774
775         db = (struct ext2_db_entry *) priv_data;
776         if (db->blockcnt == blockcnt) {
777                 *block_nr = db->blk;
778                 return BLOCK_CHANGED;
779         }
780         return 0;
781 }