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