Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / e2fsck / pass2.c
1 /*
2  * pass2.c --- check directory structure
3  * 
4  * Copyright (C) 1993, 1994 Theodore Ts'o.  This file may be
5  * redistributed under the terms of the GNU Public License.
6  * 
7  * Pass 2 of e2fsck iterates through all active directory inodes, and
8  * applies to following tests to each directory entry in the directory
9  * blocks in the inodes:
10  *
11  *      - The length of the directory entry (rec_len) should be at
12  *              least 8 bytes, and no more than the remaining space
13  *              left in the directory block.
14  *      - The length of the name in the directory entry (name_len)
15  *              should be less than (rec_len - 8).  
16  *      - The inode number in the directory entry should be within
17  *              legal bounds.
18  *      - The inode number should refer to a in-use inode.
19  *      - The first entry should be '.', and its inode should be
20  *              the inode of the directory.
21  *      - The second entry should be '..'.
22  *
23  * To minimize disk seek time, the directory blocks are processed in
24  * sorted order of block numbers.
25  *
26  * Pass 2 also collects the following information:
27  *      - The inode numbers of the subdirectories for each directory.
28  *
29  * Pass 2 relies on the following information from previous passes:
30  *      - The directory information collected in pass 1.
31  *      - The inode_used_map bitmap
32  *      - The inode_bad_map bitmap
33  *      - The inode_dir_map bitmap
34  *
35  * Pass 2 frees the following data structures
36  *      - The inode_bad_map bitmap
37  */
38
39 #include "et/com_err.h"
40
41 #include "e2fsck.h"
42
43 /*
44  * Keeps track of how many times an inode is referenced.
45  */
46 unsigned short * inode_count;
47
48 static void deallocate_inode(ext2_filsys fs, ino_t ino,
49                              char* block_buf);
50 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
51 static int check_dir_block(ext2_filsys fs,
52                            struct dir_block_struct *dir_blocks_info,
53                            char *buf);
54 static int allocate_dir_block(ext2_filsys fs,
55                               struct dir_block_struct *dir_blocks_info,
56                               char *buf);
57 static int update_dir_block(ext2_filsys fs,
58                             blk_t       *block_nr,
59                             int blockcnt,
60                             void *private);
61
62 void pass2(ext2_filsys fs)
63 {
64         int     i;
65         char    *buf;
66         struct resource_track   rtrack;
67         
68         init_resource_track(&rtrack);
69
70 #ifdef MTRACE
71         mtrace_print("Pass 2");
72 #endif
73
74         if (!preen)
75                 printf("Pass 2: Checking directory structure\n");
76         inode_count = allocate_memory((fs->super->s_inodes_count + 1) *
77                                       sizeof(unsigned short),
78                                       "buffer for inode count");
79
80         buf = allocate_memory(fs->blocksize, "directory scan buffer");
81
82         for (i=0; i < dir_block_count; i++)
83                 check_dir_block(fs, &dir_blocks[i], buf);
84              
85         free(buf);
86         free(dir_blocks);
87         if (inode_bad_map) {
88                 ext2fs_free_inode_bitmap(inode_bad_map);
89                 inode_bad_map = 0;
90         }
91         if (tflag > 1) {
92                 printf("Pass 2: ");
93                 print_resource_track(&rtrack);
94         }
95 }
96
97 /*
98  * Make sure the first entry in the directory is '.', and that the
99  * directory entry is sane.
100  */
101 static int check_dot(ext2_filsys fs,
102                      struct ext2_dir_entry *dirent,
103                      ino_t ino)
104 {
105         struct ext2_dir_entry *nextdir;
106         int     status = 0;
107         int     created = 0;
108         int     new_len;
109         const char      *question = 0;
110         
111         if (!dirent->inode) {
112                 printf("Missing '.' in directory inode %lu.\n", ino);
113                 question = "Fix";
114         } else if ((dirent->name_len != 1) ||
115                    strncmp(dirent->name, ".", dirent->name_len)) {
116                 char *name = malloc(dirent->name_len + 1);
117                 if (!name)
118                         fatal_error("Couldn't allocate . name");
119                 strncpy(name, dirent->name, dirent->name_len);
120                 name[dirent->name_len] = '\0';
121                 printf("First entry in directory inode %lu contains '%s' "
122                        "(inode=%u)\n", ino, name, dirent->inode);
123                 printf("instead of '.'.\n");
124                 free(name);
125                 question = "Change to be '.'";
126         }
127         if (question) {
128                 if (dirent->rec_len < 12)
129                         fatal_error("Cannot fix, insufficient space to add '.'");
130                 preenhalt(fs);
131                 if (ask(question, 1)) {
132                         dirent->inode = ino;
133                         dirent->name_len = 1;
134                         dirent->name[0] = '.';
135                         status = 1;
136                         created = 1;
137                 } else {
138                         ext2fs_unmark_valid(fs);
139                         return 0;
140                 }
141         }
142         if (dirent->inode != ino) {
143                 printf("Bad inode number for '.' in directory inode %lu.\n",
144                        ino);
145                 preenhalt(fs);
146                 if (ask("Fix", 1)) {
147                         dirent->inode = ino;
148                         status = 1;
149                 } else
150                         ext2fs_unmark_valid(fs);
151         }
152         if (dirent->rec_len > 12) {
153                 new_len = dirent->rec_len - 12;
154                 if (new_len > 12) {
155                         preenhalt(fs);
156                         if (created ||
157                             ask("Directory entry for '.' is big.  Split", 1)) {
158                                 nextdir = (struct ext2_dir_entry *)
159                                         ((char *) dirent + 12);
160                                 dirent->rec_len = 12;
161                                 nextdir->rec_len = new_len;
162                                 nextdir->inode = 0;
163                                 nextdir->name_len = 0;
164                                 status = 1;
165                         }
166                 }
167         }
168         return status;
169 }
170
171 /*
172  * Make sure the second entry in the directory is '..', and that the
173  * directory entry is sane.  We do not check the inode number of '..'
174  * here; this gets done in pass 3.
175  */
176 static int check_dotdot(ext2_filsys fs,
177                         struct ext2_dir_entry *dirent,
178                         struct dir_info *dir)
179 {
180         ino_t   ino = dir->ino;
181         const char      *question = 0;
182         
183         if (!dirent->inode) {
184                 printf("Missing '..' in directory inode %lu.\n", ino);
185                 question = "Fix";
186         } else if ((dirent->name_len != 2) ||
187             strncmp(dirent->name, "..", dirent->name_len)) {
188                 char *name = malloc(dirent->name_len + 1);
189                 if (!name)
190                         fatal_error("Couldn't allocate bad .. name");
191                 strncpy(name, dirent->name, dirent->name_len);
192                 name[dirent->name_len] = '\0';
193                 printf("Second entry in directory inode %lu contains '%s' "
194                        "(inode=%u)\n", ino, name, dirent->inode);
195                 printf("instead of '..'.\n");
196                 free(name);
197                 question = "Change to be '..'";
198         }
199         if (question) {
200                 if (dirent->rec_len < 12)
201                         fatal_error("Cannot fix, insufficient space to add '..'");
202                 preenhalt(fs);
203                 if (ask(question, 1)) {
204                         /*
205                          * Note: we don't have the parent inode just
206                          * yet, so we will fill it in with the root
207                          * inode.  This will get fixed in pass 3.
208                          */
209                         dirent->inode = EXT2_ROOT_INO;
210                         dirent->name_len = 2;
211                         dirent->name[0] = '.';
212                         dirent->name[1] = '.';
213                         return 1;
214                 } else
215                         ext2fs_unmark_valid(fs);
216                 return 0;
217         }
218         dir->dotdot = dirent->inode;
219         return 0;
220 }
221
222 static char unknown_pathname[] = "???";
223
224 /*
225  * Check to make sure a directory entry doesn't contain any illegal
226  * characters.
227  */
228 static int check_name(ext2_filsys fs,
229                       struct ext2_dir_entry *dirent,
230                       ino_t dir_ino,
231                       char *name)
232 {
233         int     i;
234         int     fixup = -1;
235         char    *pathname;
236         int     ret = 0;
237         errcode_t       retval;
238         
239         for ( i = 0; i < dirent->name_len; i++) {
240                 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
241                         if (fixup < 0) {
242                                 retval = ext2fs_get_pathname(fs, dir_ino,
243                                                              0, &pathname);
244                                 if (retval) {
245                                         com_err(program_name, retval, "while getting pathname in check_name");
246                                         pathname = unknown_pathname;
247                                 }
248                                 printf ("Bad file name '%s' (contains '/' or "
249                                         " null) in directory '%s' (%lu)\n",
250                                         name, pathname, dir_ino);
251                                 if (pathname != unknown_pathname)
252                                         free(pathname);
253                                 preenhalt(fs);
254                                 fixup = ask("Replace '/' or null by '.'", 1);
255                         }
256                         if (fixup) {
257                                 dirent->name[i] = '.';
258                                 ret = 1;
259                         } else
260                                 ext2fs_unmark_valid(fs);
261                 }
262         }
263         return ret;
264 }
265
266 static int check_dir_block(ext2_filsys fs,
267                            struct dir_block_struct *db,
268                            char *buf)
269 {
270         struct dir_info         *subdir, *dir;
271         struct ext2_dir_entry   *dirent;
272         int                     offset = 0;
273         int                     dir_modified = 0;
274         errcode_t               retval;
275         char                    *path1, *path2;
276         int                     dot_state, name_len;
277         blk_t                   block_nr = db->blk;
278         ino_t                   ino = db->ino;
279         char                    name[EXT2_NAME_LEN+1];
280
281         /*
282          * Make sure the inode is still in use (could have been 
283          * deleted in the duplicate/bad blocks pass.
284          */
285         if (!(ext2fs_test_inode_bitmap(inode_used_map, ino))) 
286                 return 0;
287
288         if (db->blk == 0) {
289                 if (allocate_dir_block(fs, db, buf))
290                         return 0;
291                 block_nr = db->blk;
292         }
293         
294         if (db->blockcnt)
295                 dot_state = 2;
296         else
297                 dot_state = 0;
298
299 #if 0
300         printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
301                db->blockcnt, ino);
302 #endif
303         
304         retval = ext2fs_read_dir_block(fs, block_nr, buf);
305         if (retval) {
306                 com_err(program_name, retval,
307                         "while reading directory block %d", block_nr);
308         }
309
310         do {
311                 dot_state++;
312                 dirent = (struct ext2_dir_entry *) (buf + offset);
313                 if (((offset + dirent->rec_len) > fs->blocksize) ||
314                     (dirent->rec_len < 8) ||
315                     ((dirent->name_len+8) > dirent->rec_len)) {
316                         printf("Directory inode %lu, block %d, offset %d: directory corrupted\n",
317                                ino, db->blockcnt, offset);
318                         preenhalt(fs);
319                         if (ask("Salvage", 1)) {
320                                 dirent->rec_len = fs->blocksize - offset;
321                                 dirent->name_len = 0;
322                                 dirent->inode = 0;
323                                 dir_modified++;
324                         } else {
325                                 ext2fs_unmark_valid(fs);
326                                 return DIRENT_ABORT;
327                         }
328                 }
329
330                 name_len = dirent->name_len;
331                 if (dirent->name_len > EXT2_NAME_LEN) {
332                         printf("Directory inode %lu, block %d, offset %d: filename too long\n",
333                                ino, db->blockcnt, offset);
334                         preenhalt(fs);
335                         if (ask("Truncate filename", 1)) {
336                                 dirent->name_len = EXT2_NAME_LEN;
337                                 dir_modified++;
338                         }
339                         name_len = EXT2_NAME_LEN;
340                 }
341
342                 strncpy(name, dirent->name, name_len);
343                 name[name_len] = '\0';
344
345                 if (dot_state == 1) {
346                         if (check_dot(fs, dirent, ino))
347                                 dir_modified++;
348                 } else if (dot_state == 2) {
349                         dir = get_dir_info(ino);
350                         if (!dir) {
351                                 printf("Internal error: couldn't find dir_info for %lu\n",
352                                        ino);
353                                 fatal_error(0);
354                         }
355                         if (check_dotdot(fs, dirent, dir))
356                                 dir_modified++;
357                 } else if (dirent->inode == ino) {
358                         retval = ext2fs_get_pathname(fs, ino, 0, &path1);
359                         if (retval)
360                                 path1 = unknown_pathname;
361                         printf("Entry '%s' in %s (%lu) is a link to '.'  ",
362                                name, path1, ino);
363                         if (path1 != unknown_pathname)
364                                 free(path1);
365                         preenhalt(fs);
366                         if (ask("Clear", 1)) {
367                                 dirent->inode = 0;
368                                 dir_modified++;
369                         }
370                 }
371                 if (!dirent->inode) 
372                         goto next;
373                 
374 #if 0
375                 printf("Entry '%s', name_len %d, rec_len %d, inode %lu... ",
376                        name, dirent->name_len, dirent->rec_len, dirent->inode);
377 #endif
378                 if (check_name(fs, dirent, ino, name))
379                         dir_modified++;
380
381                 /*
382                  * Make sure the inode listed is a legal one.
383                  */ 
384                 if (((dirent->inode != EXT2_ROOT_INO) &&
385                      (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
386                     (dirent->inode > fs->super->s_inodes_count)) {
387                         retval = ext2fs_get_pathname(fs, ino, 0, &path1);
388                         if (retval)
389                                 path1 = unknown_pathname;
390                         printf("Entry '%s' in %s (%lu) has bad inode #: %u.\n",
391                                name, path1, ino, dirent->inode);
392                         if (path1 != unknown_pathname)
393                                 free(path1);
394                         preenhalt(fs);
395                         if (ask("Clear", 1)) {
396                                 dirent->inode = 0;
397                                 dir_modified++;
398                                 goto next;
399                         } else
400                                 ext2fs_unmark_valid(fs);
401                 }
402
403                 /*
404                  * If the inode is unusued, offer to clear it.
405                  */
406                 if (!(ext2fs_test_inode_bitmap(inode_used_map,
407                                                dirent->inode))) {
408                         retval = ext2fs_get_pathname(fs, ino, 0, &path1);
409                         if (retval)
410                                 path1 = unknown_pathname;
411                         printf("Entry '%s' in %s (%lu) has deleted/unused inode %u.\n",
412                                name, path1, ino, dirent->inode);
413                         if (path1 != unknown_pathname)
414                                 free(path1);
415                         if (ask("Clear", 1)) {
416                                 dirent->inode = 0;
417                                 dir_modified++;
418                                 goto next;
419                         } else
420                                 ext2fs_unmark_valid(fs);
421                 }
422
423                 /*
424                  * If the inode was marked as having bad fields in
425                  * pass1, process it and offer to fix/clear it.
426                  * (We wait until now so that we can display the
427                  * pathname to the user.)
428                  */
429                 if (inode_bad_map &&
430                     ext2fs_test_inode_bitmap(inode_bad_map,
431                                              dirent->inode)) {
432                         if (process_bad_inode(fs, ino, dirent->inode)) {
433                                 dirent->inode = 0;
434                                 dir_modified++;
435                                 goto next;
436                         }
437                 }
438
439                 /*
440                  * If this is a directory, then mark its parent in its
441                  * dir_info structure.  If the parent field is already
442                  * filled in, then this directory has more than one
443                  * hard link.  We assume the first link is correct,
444                  * and ask the user if he/she wants to clear this one.
445                  */
446                 if ((dot_state > 2) &&
447                     (ext2fs_test_inode_bitmap(inode_dir_map,
448                                               dirent->inode))) {
449                         subdir = get_dir_info(dirent->inode);
450                         if (!subdir) {
451                                 printf("INTERNAL ERROR: missing dir %u\n",
452                                        dirent->inode);
453                                 fatal_error(0);
454                         }
455                         if (subdir->parent) {
456                                 retval = ext2fs_get_pathname(fs, ino,
457                                                              0, &path1);
458                                 if (retval)
459                                         path1 = unknown_pathname;
460                                 retval = ext2fs_get_pathname(fs,
461                                                              subdir->parent,
462                                                              dirent->inode,
463                                                              &path2);
464                                 if (retval)
465                                         path2 = unknown_pathname;
466                                 printf("Entry '%s' in %s (%lu) is a link to directory %s (%u).\n",
467                                        name, path1, ino, path2, 
468                                        dirent->inode);
469                                 if (path1 != unknown_pathname)
470                                         free(path1);
471                                 if (path2 != unknown_pathname)
472                                         free(path2);
473                                 if (ask("Clear", 1)) {
474                                         dirent->inode = 0;
475                                         dir_modified++;
476                                         goto next;
477                                 } else
478                                         ext2fs_unmark_valid(fs);
479                         }
480                         subdir->parent = ino;
481                 }
482                 
483                 if (inode_count[dirent->inode]++ > 0)
484                         fs_links_count++;
485                 fs_total_count++;
486         next:
487                 offset += dirent->rec_len;
488         } while (offset < fs->blocksize);
489 #if 0
490         printf("\n");
491 #endif
492         if (offset != fs->blocksize) {
493                 printf("Final rec_len is %d, should be %d\n",
494                        dirent->rec_len,
495                        dirent->rec_len - fs->blocksize + offset);
496         }
497         if (dir_modified) {
498                 retval = ext2fs_write_dir_block(fs, block_nr, buf);
499                 if (retval) {
500                         com_err(program_name, retval,
501                                 "while writing directory block %d", block_nr);
502                 }
503                 ext2fs_mark_changed(fs);
504         }
505         return 0;
506 }
507
508 /*
509  * This function is called to deallocate a block, and is an interator
510  * functioned called by deallocate inode via ext2fs_iterate_block().
511  */
512 static int deallocate_inode_block(ext2_filsys fs,
513                              blk_t      *block_nr,
514                              int blockcnt,
515                              void *private)
516 {
517         if (!*block_nr)
518                 return 0;
519         ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
520         ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
521         return 0;
522 }
523                 
524 /*
525  * This fuction deallocates an inode
526  */
527 static void deallocate_inode(ext2_filsys fs, ino_t ino,
528                              char* block_buf)
529 {
530         errcode_t               retval;
531         struct ext2_inode       inode;
532
533         inode_link_info[ino] = 0;
534         e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
535         inode.i_links_count = 0;
536         inode.i_dtime = time(0);
537         e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
538
539         /*
540          * Fix up the bitmaps...
541          */
542         read_bitmaps(fs);
543         ext2fs_unmark_inode_bitmap(inode_used_map, ino);
544         ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
545         if (inode_bad_map)
546                 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
547         ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
548         ext2fs_mark_ib_dirty(fs);
549
550         if (!inode_has_valid_blocks(&inode))
551                 return;
552         
553         ext2fs_mark_bb_dirty(fs);
554         retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
555                                       deallocate_inode_block, 0);
556         if (retval)
557                 com_err("deallocate_inode", retval,
558                         "while calling ext2fs_block_iterate for inode %d",
559                         ino);
560 }
561
562 /*
563  * These two subroutines are used by process_bad_inode; it is used to
564  * make sure that certain reserved fields are really zero.  If not,
565  * prompt the user if he/she wants us to zeroize them.
566  */
567 static void check_for_zero_u32(ext2_filsys fs, ino_t ino, char *pathname,
568                                 const char *name, __u32 *val,
569                                 int *modified)
570 {
571         char prompt[80];
572         
573         if (*val) {
574                 printf("%s for inode %lu (%s) is %u, should be zero.\n",
575                        name, ino, pathname, *val);
576                 preenhalt(fs);
577                 sprintf(prompt, "Clear %s", name);
578                 if (ask(prompt, 1)) {
579                         *val = 0;
580                         *modified = 1;
581                 } else
582                         ext2fs_unmark_valid(fs);
583         }
584 }
585
586 static void check_for_zero_u8(ext2_filsys fs, ino_t ino, char *pathname,
587                                 const char *name, __u8 *val,
588                                 int *modified)
589 {
590         char prompt[80];
591         
592         if (*val) {
593                 printf("%s for inode %lu (%s) is %d, should be zero.\n",
594                        name, ino, pathname, *val);
595                 preenhalt(fs);
596                 sprintf(prompt, "Clear %s", name);
597                 if (ask(prompt, 1)) {
598                         *val = 0;
599                         *modified = 1;
600                 } else
601                         ext2fs_unmark_valid(fs);
602         }
603 }
604
605         
606
607 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
608 {
609         struct ext2_inode       inode;
610         errcode_t               retval;
611         int                     inode_modified = 0;
612         char                    *pathname;
613         unsigned char           *frag, *fsize;
614
615         e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
616         retval = ext2fs_get_pathname(fs, dir, ino, &pathname);
617         if (retval) {
618                 com_err("process_bad_inode", retval,
619                         "while getting pathname for inode %d",
620                         ino);
621                 return 0;
622         }
623         if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
624             !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
625             !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
626             !(LINUX_S_ISSOCK(inode.i_mode))) {
627                 printf("Inode %lu (%s) has a bad mode (0%o).\n",
628                        ino, pathname, inode.i_mode);
629                 preenhalt(fs);
630                 if (ask("Clear", 1)) {
631                         deallocate_inode(fs, ino, 0);
632                         free(pathname);
633                         return 1;
634                 } else
635                         ext2fs_unmark_valid(fs);
636         }
637         check_for_zero_u32(fs, ino, pathname, "i_faddr", &inode.i_faddr,
638                             &inode_modified);
639
640         switch (fs->super->s_creator_os) {
641             case EXT2_OS_LINUX:
642                 frag = &inode.osd2.linux2.l_i_frag;
643                 fsize = &inode.osd2.linux2.l_i_fsize;
644                 break;
645             case EXT2_OS_HURD:
646                 frag = &inode.osd2.hurd2.h_i_frag;
647                 fsize = &inode.osd2.hurd2.h_i_fsize;
648                 break;
649             case EXT2_OS_MASIX:
650                 frag = &inode.osd2.masix2.m_i_frag;
651                 fsize = &inode.osd2.masix2.m_i_fsize;
652                 break;
653             default:
654                 frag = fsize = 0;
655         }
656         if (frag)
657                 check_for_zero_u8(fs, ino, pathname, "i_frag", frag,
658                                   &inode_modified);
659         if (fsize)
660                 check_for_zero_u8(fs, ino, pathname, "i_fsize", fsize,
661                                   &inode_modified);
662
663         check_for_zero_u32(fs, ino, pathname, "i_file_acl", &inode.i_file_acl,
664                             &inode_modified);
665         check_for_zero_u32(fs, ino, pathname, "i_dir_acl", &inode.i_dir_acl,
666                             &inode_modified);
667         free(pathname);
668         if (inode_modified)
669                 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
670         return 0;
671 }
672
673
674 /*
675  * allocate_dir_block --- this function allocates a new directory
676  *      block for a particular inode; this is done if a directory has
677  *      a "hole" in it, or if a directory has a illegal block number
678  *      that was zeroed out and now needs to be replaced.
679  */
680 static int allocate_dir_block(ext2_filsys fs,
681                               struct dir_block_struct *db,
682                               char *buf)
683 {
684         blk_t                   blk;
685         char                    *block;
686         struct ext2_inode       inode;
687         errcode_t               retval;
688
689         printf("Directory inode %lu has a hole at block #%d\n",
690                db->ino, db->blockcnt);
691         if (ask("Allocate block", 1) == 0)
692                 return 1;
693
694         /*
695          * Read the inode and block bitmaps in; we'll be messing with
696          * them.
697          */
698         read_bitmaps(fs);
699         
700         /*
701          * First, find a free block
702          */
703         retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
704         if (retval) {
705                 com_err("allocate_dir_block", retval,
706                         "while trying to fill a hole in a directory inode");
707                 return 1;
708         }
709         ext2fs_mark_block_bitmap(block_found_map, blk);
710         ext2fs_mark_block_bitmap(fs->block_map, blk);
711         ext2fs_mark_bb_dirty(fs);
712
713         /*
714          * Now let's create the actual data block for the inode
715          */
716         if (db->blockcnt)
717                 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
718         else
719                 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
720                                               &block);
721
722         if (retval) {
723                 com_err("allocate_dir_block", retval,
724                         "while creating new directory block");
725                 return 1;
726         }
727
728         retval = ext2fs_write_dir_block(fs, blk, block);
729         free(block);
730         if (retval) {
731                 com_err("allocate_dir_block", retval,
732                         "while writing an empty directory block");
733                 return 1;
734         }
735
736         /*
737          * Update the inode block count
738          */
739         e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
740         inode.i_blocks += fs->blocksize / 512;
741         if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
742                 inode.i_size = (db->blockcnt+1) * fs->blocksize;
743         e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
744
745         /*
746          * Finally, update the block pointers for the inode
747          */
748         db->blk = blk;
749         retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
750                                       0, update_dir_block, db);
751         if (retval) {
752                 com_err("allocate_dir_block", retval,
753                         "while calling ext2fs_block_iterate");
754                 return 1;
755         }
756
757         return 0;
758 }
759
760 /*
761  * This is a helper function for allocate_dir_block().
762  */
763 static int update_dir_block(ext2_filsys fs,
764                             blk_t       *block_nr,
765                             int blockcnt,
766                             void *private)
767 {
768         struct dir_block_struct *db = private;
769
770         if (db->blockcnt == blockcnt) {
771                 *block_nr = db->blk;
772                 return BLOCK_CHANGED;
773         }
774         return 0;
775 }
776