Whamcloud - gitweb
Fix edge case when i_size doesn't get fixed until second e2fsck run
[tools/e2fsprogs.git] / e2fsck / pass3.c
1 /*
2  * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
3  *
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 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 #3 assures that all directories are connected to the
12  * filesystem tree, using the following algorithm:
13  *
14  * First, the root directory is checked to make sure it exists; if
15  * not, e2fsck will offer to create a new one.  It is then marked as
16  * "done".
17  * 
18  * Then, pass3 interates over all directory inodes; for each directory
19  * it attempts to trace up the filesystem tree, using dirinfo.parent
20  * until it reaches a directory which has been marked "done".  If it
21  * can not do so, then the directory must be disconnected, and e2fsck
22  * will offer to reconnect it to /lost+found.  While it is chasing
23  * parent pointers up the filesystem tree, if pass3 sees a directory
24  * twice, then it has detected a filesystem loop, and it will again
25  * offer to reconnect the directory to /lost+found in to break the
26  * filesystem loop.
27  * 
28  * Pass 3 also contains the subroutine, e2fsck_reconnect_file() to
29  * reconnect inodes to /lost+found; this subroutine is also used by
30  * pass 4.  e2fsck_reconnect_file() calls get_lost_and_found(), which
31  * is responsible for creating /lost+found if it does not exist.
32  *
33  * Pass 3 frees the following data structures:
34  *      - The dirinfo directory information cache.
35  */
36
37 #ifdef HAVE_ERRNO_H
38 #include <errno.h>
39 #endif
40
41 #include "e2fsck.h"
42 #include "problem.h"
43
44 static void check_root(e2fsck_t ctx);
45 static int check_directory(e2fsck_t ctx, struct dir_info *dir,
46                            struct problem_context *pctx);
47 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent);
48
49 static ext2fs_inode_bitmap inode_loop_detect = 0;
50 static ext2fs_inode_bitmap inode_done_map = 0;
51         
52 void e2fsck_pass3(e2fsck_t ctx)
53 {
54         ext2_filsys fs = ctx->fs;
55         int             i;
56 #ifdef RESOURCE_TRACK
57         struct resource_track   rtrack;
58 #endif
59         struct problem_context  pctx;
60         struct dir_info *dir;
61         unsigned long maxdirs, count;
62
63 #ifdef RESOURCE_TRACK
64         init_resource_track(&rtrack);
65 #endif
66
67         clear_problem_context(&pctx);
68
69 #ifdef MTRACE
70         mtrace_print("Pass 3");
71 #endif
72
73         if (!(ctx->options & E2F_OPT_PREEN))
74                 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
75
76         /*
77          * Allocate some bitmaps to do loop detection.
78          */
79         pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
80                                                     &inode_done_map);
81         if (pctx.errcode) {
82                 pctx.num = 2;
83                 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
84                 ctx->flags |= E2F_FLAG_ABORT;
85                 goto abort_exit;
86         }
87 #ifdef RESOURCE_TRACK
88         if (ctx->options & E2F_OPT_TIME) {
89                 e2fsck_clear_progbar(ctx);
90                 print_resource_track(_("Peak memory"), &ctx->global_rtrack);
91         }
92 #endif
93
94         check_root(ctx);
95         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
96                 goto abort_exit;
97
98         ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
99
100         maxdirs = e2fsck_get_num_dirinfo(ctx);
101         count = 1;
102
103         if (ctx->progress)
104                 if ((ctx->progress)(ctx, 3, 0, maxdirs))
105                         goto abort_exit;
106         
107         for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
108                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
109                         goto abort_exit;
110                 if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
111                         goto abort_exit;
112                 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
113                         if (check_directory(ctx, dir, &pctx))
114                                 goto abort_exit;
115         }
116
117         /*
118          * Force the creation of /lost+found if not present
119          */
120         if ((ctx->flags & E2F_OPT_READONLY) == 0)
121                 e2fsck_get_lost_and_found(ctx, 1);
122
123         /*
124          * If there are any directories that need to be indexed or
125          * optimized, do it here.
126          */
127         e2fsck_rehash_directories(ctx);
128         
129 abort_exit:
130         e2fsck_free_dir_info(ctx);
131         if (inode_loop_detect) {
132                 ext2fs_free_inode_bitmap(inode_loop_detect);
133                 inode_loop_detect = 0;
134         }
135         if (inode_done_map) {
136                 ext2fs_free_inode_bitmap(inode_done_map);
137                 inode_done_map = 0;
138         }
139
140 #ifdef RESOURCE_TRACK
141         if (ctx->options & E2F_OPT_TIME2) {
142                 e2fsck_clear_progbar(ctx);
143                 print_resource_track(_("Pass 3"), &rtrack);
144         }
145 #endif
146 }
147
148 /*
149  * This makes sure the root inode is present; if not, we ask if the
150  * user wants us to create it.  Not creating it is a fatal error.
151  */
152 static void check_root(e2fsck_t ctx)
153 {
154         ext2_filsys fs = ctx->fs;
155         blk_t                   blk;
156         struct ext2_inode       inode;
157         char *                  block;
158         struct problem_context  pctx;
159         
160         clear_problem_context(&pctx);
161         
162         if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
163                 /*
164                  * If the root inode is not a directory, die here.  The
165                  * user must have answered 'no' in pass1 when we
166                  * offered to clear it.
167                  */
168                 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
169                                                EXT2_ROOT_INO))) {
170                         fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
171                         ctx->flags |= E2F_FLAG_ABORT;
172                 }
173                 return;
174         }
175
176         if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
177                 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
178                 ctx->flags |= E2F_FLAG_ABORT;
179                 return;
180         }
181
182         e2fsck_read_bitmaps(ctx);
183         
184         /*
185          * First, find a free block
186          */
187         pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
188         if (pctx.errcode) {
189                 pctx.str = "ext2fs_new_block";
190                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
191                 ctx->flags |= E2F_FLAG_ABORT;
192                 return;
193         }
194         ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
195         ext2fs_mark_block_bitmap(fs->block_map, blk);
196         ext2fs_mark_bb_dirty(fs);
197
198         /*
199          * Now let's create the actual data block for the inode
200          */
201         pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
202                                             &block);
203         if (pctx.errcode) {
204                 pctx.str = "ext2fs_new_dir_block";
205                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
206                 ctx->flags |= E2F_FLAG_ABORT;
207                 return;
208         }
209
210         pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
211         if (pctx.errcode) {
212                 pctx.str = "ext2fs_write_dir_block";
213                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
214                 ctx->flags |= E2F_FLAG_ABORT;
215                 return;
216         }
217         ext2fs_free_mem(&block);
218
219         /*
220          * Set up the inode structure
221          */
222         memset(&inode, 0, sizeof(inode));
223         inode.i_mode = 040755;
224         inode.i_size = fs->blocksize;
225         inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
226         inode.i_links_count = 2;
227         inode.i_blocks = fs->blocksize / 512;
228         inode.i_block[0] = blk;
229
230         /*
231          * Write out the inode.
232          */
233         pctx.errcode = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
234         if (pctx.errcode) {
235                 pctx.str = "ext2fs_write_inode";
236                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
237                 ctx->flags |= E2F_FLAG_ABORT;
238                 return;
239         }
240         
241         /*
242          * Miscellaneous bookkeeping...
243          */
244         e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
245         ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
246         ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
247
248         ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
249         ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
250         ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
251         ext2fs_mark_ib_dirty(fs);
252 }
253
254 /*
255  * This subroutine is responsible for making sure that a particular
256  * directory is connected to the root; if it isn't we trace it up as
257  * far as we can go, and then offer to connect the resulting parent to
258  * the lost+found.  We have to do loop detection; if we ever discover
259  * a loop, we treat that as a disconnected directory and offer to
260  * reparent it to lost+found.
261  * 
262  * However, loop detection is expensive, because for very large
263  * filesystems, the inode_loop_detect bitmap is huge, and clearing it
264  * is non-trivial.  Loops in filesystems are also a rare error case,
265  * and we shouldn't optimize for error cases.  So we try two passes of
266  * the algorithm.  The first time, we ignore loop detection and merely
267  * increment a counter; if the counter exceeds some extreme threshold,
268  * then we try again with the loop detection bitmap enabled.
269  */
270 static int check_directory(e2fsck_t ctx, struct dir_info *dir,
271                            struct problem_context *pctx)
272 {
273         ext2_filsys     fs = ctx->fs;
274         struct dir_info *p = dir;
275         int             loop_pass = 0, parent_count = 0;
276
277         if (!p)
278                 return 0;
279
280         while (1) {
281                 /*
282                  * Mark this inode as being "done"; by the time we
283                  * return from this function, the inode we either be
284                  * verified as being connected to the directory tree,
285                  * or we will have offered to reconnect this to
286                  * lost+found.
287                  *
288                  * If it was marked done already, then we've reached a
289                  * parent we've already checked.
290                  */
291                 if (ext2fs_mark_inode_bitmap(inode_done_map, p->ino))
292                         break;
293
294                 /*
295                  * If this directory doesn't have a parent, or we've
296                  * seen the parent once already, then offer to
297                  * reparent it to lost+found
298                  */
299                 if (!p->parent ||
300                     (loop_pass && 
301                      (ext2fs_test_inode_bitmap(inode_loop_detect,
302                                               p->parent)))) {
303                         pctx->ino = p->ino;
304                         if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
305                                 if (e2fsck_reconnect_file(ctx, pctx->ino))
306                                         ext2fs_unmark_valid(fs);
307                                 else {
308                                         p = e2fsck_get_dir_info(ctx, pctx->ino);
309                                         if (!p) {
310                                                 fix_problem(ctx, 
311                                                     PR_3_NO_DIRINFO, pctx);
312                                                 return 0;
313                                         }
314                                         p->parent = ctx->lost_and_found;
315                                         fix_dotdot(ctx, p, ctx->lost_and_found);
316                                 }
317                         }
318                         break;
319                 }
320                 p = e2fsck_get_dir_info(ctx, p->parent);
321                 if (!p) {
322                         pctx->ino = p->parent;
323                         fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
324                         return 0;
325                 }
326                 if (loop_pass) {
327                         ext2fs_mark_inode_bitmap(inode_loop_detect,
328                                                  p->ino);
329                 } else if (parent_count++ > 2048) {
330                         /*
331                          * If we've run into a path depth that's
332                          * greater than 2048, try again with the inode
333                          * loop bitmap turned on and start from the
334                          * top.
335                          */
336                         loop_pass = 1;
337                         if (inode_loop_detect)
338                                 ext2fs_clear_inode_bitmap(inode_loop_detect);
339                         else {
340                                 pctx->errcode = ext2fs_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), &inode_loop_detect);
341                                 if (pctx->errcode) {
342                                         pctx->num = 1;
343                                         fix_problem(ctx, 
344                                     PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
345                                         ctx->flags |= E2F_FLAG_ABORT;
346                                         return -1;
347                                 }
348                         }
349                         p = dir;
350                 }
351         }
352
353         /*
354          * Make sure that .. and the parent directory are the same;
355          * offer to fix it if not.
356          */
357         if (dir->parent != dir->dotdot) {
358                 pctx->ino = dir->ino;
359                 pctx->ino2 = dir->dotdot;
360                 pctx->dir = dir->parent;
361                 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
362                         fix_dotdot(ctx, dir, dir->parent);
363         }
364         return 0;
365 }
366
367 /*
368  * This routine gets the lost_and_found inode, making it a directory
369  * if necessary
370  */
371 ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
372 {
373         ext2_filsys fs = ctx->fs;
374         ext2_ino_t                      ino;
375         blk_t                   blk;
376         errcode_t               retval;
377         struct ext2_inode       inode;
378         char *                  block;
379         static const char       name[] = "lost+found";
380         struct  problem_context pctx;
381         struct dir_info         *dirinfo;
382
383         if (ctx->lost_and_found)
384                 return ctx->lost_and_found;
385
386         clear_problem_context(&pctx);
387         
388         retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
389                                sizeof(name)-1, 0, &ino);
390         if (retval && !fix)
391                 return 0;
392         if (!retval) {
393                 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino)) {
394                         ctx->lost_and_found = ino;
395                         return ino;
396                 }
397                                 
398                 /* Lost+found isn't a directory! */
399                 if (!fix)
400                         return 0;
401                 pctx.ino = ino;
402                 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
403                         return 0;
404
405                 /* OK, unlink the old /lost+found file. */
406                 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
407                 if (pctx.errcode) {
408                         pctx.str = "ext2fs_unlink";
409                         fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
410                         return 0;
411                 }
412                 dirinfo = e2fsck_get_dir_info(ctx, ino);
413                 if (dirinfo)
414                         dirinfo->parent = 0;
415                 e2fsck_adjust_inode_count(ctx, ino, -1);
416         } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
417                 pctx.errcode = retval;
418                 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
419         }
420         if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
421                 return 0;
422
423         /*
424          * Read the inode and block bitmaps in; we'll be messing with
425          * them.
426          */
427         e2fsck_read_bitmaps(ctx);
428         
429         /*
430          * First, find a free block
431          */
432         retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
433         if (retval) {
434                 pctx.errcode = retval;
435                 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
436                 return 0;
437         }
438         ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
439         ext2fs_block_alloc_stats(fs, blk, +1);
440
441         /*
442          * Next find a free inode.
443          */
444         retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700,
445                                   ctx->inode_used_map, &ino);
446         if (retval) {
447                 pctx.errcode = retval;
448                 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
449                 return 0;
450         }
451         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
452         ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
453         ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
454
455         /*
456          * Now let's create the actual data block for the inode
457          */
458         retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
459         if (retval) {
460                 pctx.errcode = retval;
461                 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
462                 return 0;
463         }
464
465         retval = ext2fs_write_dir_block(fs, blk, block);
466         ext2fs_free_mem(&block);
467         if (retval) {
468                 pctx.errcode = retval;
469                 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
470                 return 0;
471         }
472
473         /*
474          * Set up the inode structure
475          */
476         memset(&inode, 0, sizeof(inode));
477         inode.i_mode = 040700;
478         inode.i_size = fs->blocksize;
479         inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
480         inode.i_links_count = 2;
481         inode.i_blocks = fs->blocksize / 512;
482         inode.i_block[0] = blk;
483
484         /*
485          * Next, write out the inode.
486          */
487         pctx.errcode = ext2fs_write_new_inode(fs, ino, &inode);
488         if (pctx.errcode) {
489                 pctx.str = "ext2fs_write_inode";
490                 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
491                 return 0;
492         }
493         /*
494          * Finally, create the directory link
495          */
496         pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
497         if (pctx.errcode) {
498                 pctx.str = "ext2fs_link";
499                 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
500                 return 0;
501         }
502
503         /*
504          * Miscellaneous bookkeeping that needs to be kept straight.
505          */
506         e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
507         e2fsck_adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
508         ext2fs_icount_store(ctx->inode_count, ino, 2);
509         ext2fs_icount_store(ctx->inode_link_info, ino, 2);
510         ctx->lost_and_found = ino;
511 #if 0
512         printf("/lost+found created; inode #%lu\n", ino);
513 #endif
514         return ino;
515 }
516
517 /*
518  * This routine will connect a file to lost+found
519  */
520 int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
521 {
522         ext2_filsys fs = ctx->fs;
523         errcode_t       retval;
524         char            name[80];
525         struct problem_context  pctx;
526         struct ext2_inode       inode;
527         int             file_type = 0;
528
529         clear_problem_context(&pctx);
530         pctx.ino = ino;
531
532         if (!ctx->bad_lost_and_found && !ctx->lost_and_found) {
533                 if (e2fsck_get_lost_and_found(ctx, 1) == 0)
534                         ctx->bad_lost_and_found++;
535         }
536         if (ctx->bad_lost_and_found) {
537                 fix_problem(ctx, PR_3_NO_LPF, &pctx);
538                 return 1;
539         }
540         
541         sprintf(name, "#%u", ino);
542         if (ext2fs_read_inode(fs, ino, &inode) == 0)
543                 file_type = ext2_file_type(inode.i_mode);
544         retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type);
545         if (retval == EXT2_ET_DIR_NO_SPACE) {
546                 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
547                         return 1;
548                 retval = e2fsck_expand_directory(ctx, ctx->lost_and_found, 
549                                                  1, 0);
550                 if (retval) {
551                         pctx.errcode = retval;
552                         fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
553                         return 1;
554                 }
555                 retval = ext2fs_link(fs, ctx->lost_and_found, name,
556                                      ino, file_type);
557         }
558         if (retval) {
559                 pctx.errcode = retval;
560                 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
561                 return 1;
562         }
563         e2fsck_adjust_inode_count(ctx, ino, 1);
564
565         return 0;
566 }
567
568 /*
569  * Utility routine to adjust the inode counts on an inode.
570  */
571 errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
572 {
573         ext2_filsys fs = ctx->fs;
574         errcode_t               retval;
575         struct ext2_inode       inode;
576         
577         if (!ino)
578                 return 0;
579
580         retval = ext2fs_read_inode(fs, ino, &inode);
581         if (retval)
582                 return retval;
583
584 #if 0
585         printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
586                inode.i_links_count);
587 #endif
588
589         if (adj == 1) {
590                 ext2fs_icount_increment(ctx->inode_count, ino, 0);
591                 if (inode.i_links_count == (__u16) ~0)
592                         return 0;
593                 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
594                 inode.i_links_count++;
595         } else if (adj == -1) {
596                 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
597                 if (inode.i_links_count == 0)
598                         return 0;
599                 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
600                 inode.i_links_count--;
601         }
602         
603         retval = ext2fs_write_inode(fs, ino, &inode);
604         if (retval)
605                 return retval;
606
607         return 0;
608 }
609
610 /*
611  * Fix parent --- this routine fixes up the parent of a directory.
612  */
613 struct fix_dotdot_struct {
614         ext2_filsys     fs;
615         ext2_ino_t      parent;
616         int             done;
617         e2fsck_t        ctx;
618 };
619
620 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
621                            int  offset EXT2FS_ATTR((unused)),
622                            int  blocksize EXT2FS_ATTR((unused)),
623                            char *buf EXT2FS_ATTR((unused)),
624                            void *priv_data)
625 {
626         struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
627         errcode_t       retval;
628         struct problem_context pctx;
629
630         if ((dirent->name_len & 0xFF) != 2)
631                 return 0;
632         if (strncmp(dirent->name, "..", 2))
633                 return 0;
634
635         clear_problem_context(&pctx);
636         
637         retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1);
638         if (retval) {
639                 pctx.errcode = retval;
640                 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
641         }
642         retval = e2fsck_adjust_inode_count(fp->ctx, fp->parent, 1);
643         if (retval) {
644                 pctx.errcode = retval;
645                 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
646         }
647         dirent->inode = fp->parent;
648
649         fp->done++;
650         return DIRENT_ABORT | DIRENT_CHANGED;
651 }
652
653 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent)
654 {
655         ext2_filsys fs = ctx->fs;
656         errcode_t       retval;
657         struct fix_dotdot_struct fp;
658         struct problem_context pctx;
659
660         fp.fs = fs;
661         fp.parent = parent;
662         fp.done = 0;
663         fp.ctx = ctx;
664
665 #if 0
666         printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
667 #endif
668         
669         retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
670                                     0, fix_dotdot_proc, &fp);
671         if (retval || !fp.done) {
672                 clear_problem_context(&pctx);
673                 pctx.ino = dir->ino;
674                 pctx.errcode = retval;
675                 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
676                             PR_3_FIX_PARENT_NOFIND, &pctx);
677                 ext2fs_unmark_valid(fs);
678         }
679         dir->dotdot = parent;
680         
681         return;
682 }
683
684 /*
685  * These routines are responsible for expanding a /lost+found if it is
686  * too small.
687  */
688
689 struct expand_dir_struct {
690         int                     num;
691         int                     guaranteed_size;
692         int                     newblocks;
693         int                     last_block;
694         errcode_t               err;
695         e2fsck_t                ctx;
696 };
697
698 static int expand_dir_proc(ext2_filsys fs,
699                            blk_t        *blocknr,
700                            e2_blkcnt_t  blockcnt,
701                            blk_t ref_block EXT2FS_ATTR((unused)),
702                            int ref_offset EXT2FS_ATTR((unused)),
703                            void *priv_data)
704 {
705         struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
706         blk_t   new_blk;
707         static blk_t    last_blk = 0;
708         char            *block;
709         errcode_t       retval;
710         e2fsck_t        ctx;
711
712         ctx = es->ctx;
713         
714         if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
715                 return BLOCK_ABORT;
716
717         if (blockcnt > 0)
718                 es->last_block = blockcnt;
719         if (*blocknr) {
720                 last_blk = *blocknr;
721                 return 0;
722         }
723         retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
724                                   &new_blk);
725         if (retval) {
726                 es->err = retval;
727                 return BLOCK_ABORT;
728         }
729         if (blockcnt > 0) {
730                 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
731                 if (retval) {
732                         es->err = retval;
733                         return BLOCK_ABORT;
734                 }
735                 es->num--;
736                 retval = ext2fs_write_dir_block(fs, new_blk, block);
737         } else {
738                 retval = ext2fs_get_mem(fs->blocksize, &block);
739                 if (retval) {
740                         es->err = retval;
741                         return BLOCK_ABORT;
742                 }
743                 memset(block, 0, fs->blocksize);
744                 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
745         }       
746         if (retval) {
747                 es->err = retval;
748                 return BLOCK_ABORT;
749         }
750         ext2fs_free_mem(&block);
751         *blocknr = new_blk;
752         ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
753         ext2fs_block_alloc_stats(fs, new_blk, +1);
754         es->newblocks++;
755         
756         if (es->num == 0)
757                 return (BLOCK_CHANGED | BLOCK_ABORT);
758         else
759                 return BLOCK_CHANGED;
760 }
761
762 errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
763                                   int num, int guaranteed_size)
764 {
765         ext2_filsys fs = ctx->fs;
766         errcode_t       retval;
767         struct expand_dir_struct es;
768         struct ext2_inode       inode;
769         
770         if (!(fs->flags & EXT2_FLAG_RW))
771                 return EXT2_ET_RO_FILSYS;
772
773         /*
774          * Read the inode and block bitmaps in; we'll be messing with
775          * them.
776          */
777         e2fsck_read_bitmaps(ctx);
778
779         retval = ext2fs_check_directory(fs, dir);
780         if (retval)
781                 return retval;
782         
783         es.num = num;
784         es.guaranteed_size = guaranteed_size;
785         es.last_block = 0;
786         es.err = 0;
787         es.newblocks = 0;
788         es.ctx = ctx;
789         
790         retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
791                                        0, expand_dir_proc, &es);
792
793         if (es.err)
794                 return es.err;
795
796         /*
797          * Update the size and block count fields in the inode.
798          */
799         retval = ext2fs_read_inode(fs, dir, &inode);
800         if (retval)
801                 return retval;
802         
803         inode.i_size = (es.last_block + 1) * fs->blocksize;
804         inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
805
806         e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
807
808         return 0;
809 }
810