Whamcloud - gitweb
ChangeLog, e2fsck.8.in, e2fsck.h, pass3.c, pass4.c, super.c:
[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 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 void check_directory(e2fsck_t ctx, struct dir_info *dir,
46                             struct problem_context *pctx);
47 static ino_t get_lost_and_found(e2fsck_t ctx);
48 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent);
49 static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj);
50 static errcode_t expand_directory(e2fsck_t ctx, ino_t dir);
51
52 static ino_t lost_and_found = 0;
53 static int bad_lost_and_found = 0;
54
55 static ext2fs_inode_bitmap inode_loop_detect;
56 static ext2fs_inode_bitmap inode_done_map;
57         
58 void e2fsck_pass3(e2fsck_t ctx)
59 {
60         ext2_filsys fs = ctx->fs;
61         int             i;
62 #ifdef RESOURCE_TRACK
63         struct resource_track   rtrack;
64 #endif
65         struct problem_context  pctx;
66         struct dir_info *dir;
67         unsigned long max, count;
68
69 #ifdef RESOURCE_TRACK
70         init_resource_track(&rtrack);
71 #endif
72
73         clear_problem_context(&pctx);
74
75 #ifdef MTRACE
76         mtrace_print("Pass 3");
77 #endif
78
79         if (!(ctx->options & E2F_OPT_PREEN))
80                 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
81
82         /*
83          * Allocate some bitmaps to do loop detection.
84          */
85         pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
86                     "inode loop detection bitmap", &inode_loop_detect);
87         if (pctx.errcode) {
88                 pctx.num = 1;
89                 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
90                 ctx->flags |= E2F_FLAG_ABORT;
91                 return;
92         }
93         pctx.errcode = ext2fs_allocate_inode_bitmap(fs, "inode done bitmap",
94                                                     &inode_done_map);
95         if (pctx.errcode) {
96                 pctx.num = 2;
97                 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
98                 ctx->flags |= E2F_FLAG_ABORT;
99                 return;
100         }
101 #ifdef RESOURCE_TRACK
102         if (ctx->options & E2F_OPT_TIME)
103                 print_resource_track("Peak memory", &ctx->global_rtrack);
104 #endif
105
106         check_root(ctx);
107         if (ctx->flags & E2F_FLAG_ABORT)
108                 return;
109
110         ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
111
112         max = e2fsck_get_num_dirinfo(ctx);
113         count = 0;
114
115         for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
116                 if (ctx->progress)
117                         (ctx->progress)(ctx, 3, count++, max);
118                 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
119                         check_directory(ctx, dir, &pctx);
120         }
121         if (ctx->progress)
122                 (ctx->progress)(ctx, 3, max, max);
123         
124         e2fsck_free_dir_info(ctx);
125         ext2fs_free_inode_bitmap(inode_loop_detect);
126         ext2fs_free_inode_bitmap(inode_done_map);
127 #ifdef RESOURCE_TRACK
128         if (ctx->options & E2F_OPT_TIME2)
129                 print_resource_track("Pass 3", &rtrack);
130 #endif
131 }
132
133 /*
134  * This makes sure the root inode is present; if not, we ask if the
135  * user wants us to create it.  Not creating it is a fatal error.
136  */
137 static void check_root(e2fsck_t ctx)
138 {
139         ext2_filsys fs = ctx->fs;
140         blk_t                   blk;
141         struct ext2_inode       inode;
142         char *                  block;
143         struct problem_context  pctx;
144         
145         clear_problem_context(&pctx);
146         
147         if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
148                 /*
149                  * If the root inode is not a directory, die here.  The
150                  * user must have answered 'no' in pass1 when we
151                  * offered to clear it.
152                  */
153                 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
154                                                EXT2_ROOT_INO))) {
155                         fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
156                         ctx->flags |= E2F_FLAG_ABORT;
157                 }
158                 return;
159         }
160
161         if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
162                 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
163                 ctx->flags |= E2F_FLAG_ABORT;
164                 return;
165         }
166
167         e2fsck_read_bitmaps(ctx);
168         
169         /*
170          * First, find a free block
171          */
172         pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
173         if (pctx.errcode) {
174                 pctx.str = "ext2fs_new_block";
175                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
176                 ctx->flags |= E2F_FLAG_ABORT;
177                 return;
178         }
179         ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
180         ext2fs_mark_block_bitmap(fs->block_map, blk);
181         ext2fs_mark_bb_dirty(fs);
182
183         /*
184          * Now let's create the actual data block for the inode
185          */
186         pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
187                                             &block);
188         if (pctx.errcode) {
189                 pctx.str = "ext2fs_new_dir_block";
190                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
191                 ctx->flags |= E2F_FLAG_ABORT;
192                 return;
193         }
194
195         pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
196         if (pctx.errcode) {
197                 pctx.str = "ext2fs_write_dir_block";
198                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
199                 ctx->flags |= E2F_FLAG_ABORT;
200                 return;
201         }
202         ext2fs_free_mem((void **) &block);
203
204         /*
205          * Set up the inode structure
206          */
207         memset(&inode, 0, sizeof(inode));
208         inode.i_mode = 040755;
209         inode.i_size = fs->blocksize;
210         inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
211         inode.i_links_count = 2;
212         inode.i_blocks = fs->blocksize / 512;
213         inode.i_block[0] = blk;
214
215         /*
216          * Write out the inode.
217          */
218         pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
219         if (pctx.errcode) {
220                 pctx.str = "ext2fs_write_inode";
221                 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
222                 ctx->flags |= E2F_FLAG_ABORT;
223                 return;
224         }
225         
226         /*
227          * Miscellaneous bookkeeping...
228          */
229         e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
230         ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
231         ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
232
233         ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
234         ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
235         ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
236         ext2fs_mark_ib_dirty(fs);
237 }
238
239 /*
240  * This subroutine is responsible for making sure that a particular
241  * directory is connected to the root; if it isn't we trace it up as
242  * far as we can go, and then offer to connect the resulting parent to
243  * the lost+found.  We have to do loop detection; if we ever discover
244  * a loop, we treat that as a disconnected directory and offer to
245  * reparent it to lost+found.
246  */
247 static void check_directory(e2fsck_t ctx, struct dir_info *dir,
248                             struct problem_context *pctx)
249 {
250         ext2_filsys fs = ctx->fs;
251         struct dir_info *p = dir;
252
253         ext2fs_clear_inode_bitmap(inode_loop_detect);
254         while (p) {
255                 /*
256                  * If we find a parent which we've already checked,
257                  * then stop; we know it's either already connected to
258                  * the directory tree, or it isn't but the user has
259                  * already told us he doesn't want us to reconnect the
260                  * disconnected subtree.
261                  */
262                 if (ext2fs_test_inode_bitmap(inode_done_map, p->ino))
263                         goto check_dot_dot;
264                 /*
265                  * Mark this inode as being "done"; by the time we
266                  * return from this function, the inode we either be
267                  * verified as being connected to the directory tree,
268                  * or we will have offered to reconnect this to
269                  * lost+found.
270                  */
271                 ext2fs_mark_inode_bitmap(inode_done_map, p->ino);
272                 /*
273                  * If this directory doesn't have a parent, or we've
274                  * seen the parent once already, then offer to
275                  * reparent it to lost+found
276                  */
277                 if (!p->parent ||
278                     (ext2fs_test_inode_bitmap(inode_loop_detect,
279                                               p->parent)))
280                         break;
281                 ext2fs_mark_inode_bitmap(inode_loop_detect,
282                                          p->parent);
283                 p = e2fsck_get_dir_info(ctx, p->parent);
284         }
285         /*
286          * If we've reached here, we've hit a detached directory
287          * inode; offer to reconnect it to lost+found.
288          */
289         pctx->ino = p->ino;
290         if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
291                 if (e2fsck_reconnect_file(ctx, p->ino))
292                         ext2fs_unmark_valid(fs);
293                 else {
294                         p->parent = lost_and_found;
295                         fix_dotdot(ctx, p, lost_and_found);
296                 }
297         }
298
299         /*
300          * Make sure that .. and the parent directory are the same;
301          * offer to fix it if not.
302          */
303 check_dot_dot:
304         if (dir->parent != dir->dotdot) {
305                 pctx->ino = dir->ino;
306                 pctx->ino2 = dir->dotdot;
307                 pctx->dir = dir->parent;
308                 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
309                         fix_dotdot(ctx, dir, dir->parent);
310         }
311 }       
312
313 /*
314  * This routine gets the lost_and_found inode, making it a directory
315  * if necessary
316  */
317 ino_t get_lost_and_found(e2fsck_t ctx)
318 {
319         ext2_filsys fs = ctx->fs;
320         ino_t                   ino;
321         blk_t                   blk;
322         errcode_t               retval;
323         struct ext2_inode       inode;
324         char *                  block;
325         const char              name[] = "lost+found";
326         struct  problem_context pctx;
327
328         clear_problem_context(&pctx);
329         
330         retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
331                                sizeof(name)-1, 0, &ino);
332         if (!retval)
333                 return ino;
334         if (retval != EXT2_ET_FILE_NOT_FOUND) {
335                 pctx.errcode = retval;
336                 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
337         }
338         if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
339                 return 0;
340
341         /*
342          * Read the inode and block bitmaps in; we'll be messing with
343          * them.
344          */
345         e2fsck_read_bitmaps(ctx);
346         
347         /*
348          * First, find a free block
349          */
350         retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
351         if (retval) {
352                 pctx.errcode = retval;
353                 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
354                 return 0;
355         }
356         ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
357         ext2fs_mark_block_bitmap(fs->block_map, blk);
358         ext2fs_mark_bb_dirty(fs);
359
360         /*
361          * Next find a free inode.
362          */
363         retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755,
364                                   ctx->inode_used_map, &ino);
365         if (retval) {
366                 pctx.errcode = retval;
367                 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
368                 return 0;
369         }
370         ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
371         ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
372         ext2fs_mark_inode_bitmap(fs->inode_map, ino);
373         ext2fs_mark_ib_dirty(fs);
374
375         /*
376          * Now let's create the actual data block for the inode
377          */
378         retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
379         if (retval) {
380                 pctx.errcode = retval;
381                 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
382                 return 0;
383         }
384
385         retval = ext2fs_write_dir_block(fs, blk, block);
386         ext2fs_free_mem((void **) &block);
387         if (retval) {
388                 pctx.errcode = retval;
389                 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
390                 return 0;
391         }
392
393         /*
394          * Set up the inode structure
395          */
396         memset(&inode, 0, sizeof(inode));
397         inode.i_mode = 040755;
398         inode.i_size = fs->blocksize;
399         inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
400         inode.i_links_count = 2;
401         inode.i_blocks = fs->blocksize / 512;
402         inode.i_block[0] = blk;
403
404         /*
405          * Next, write out the inode.
406          */
407         pctx.errcode = ext2fs_write_inode(fs, ino, &inode);
408         if (pctx.errcode) {
409                 pctx.str = "ext2fs_write_inode";
410                 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
411                 return 0;
412         }
413         /*
414          * Finally, create the directory link
415          */
416         pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0);
417         if (pctx.errcode) {
418                 pctx.str = "ext2fs_link";
419                 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
420                 return 0;
421         }
422
423         /*
424          * Miscellaneous bookkeeping that needs to be kept straight.
425          */
426         e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
427         adjust_inode_count(ctx, EXT2_ROOT_INO, +1);
428         ext2fs_icount_store(ctx->inode_count, ino, 2);
429         ext2fs_icount_store(ctx->inode_link_info, ino, 2);
430 #if 0
431         printf("/lost+found created; inode #%lu\n", ino);
432 #endif
433         return ino;
434 }
435
436 /*
437  * This routine will connect a file to lost+found
438  */
439 int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode)
440 {
441         ext2_filsys fs = ctx->fs;
442         errcode_t       retval;
443         char            name[80];
444         struct problem_context  pctx;
445
446         clear_problem_context(&pctx);
447         pctx.ino = inode;
448
449         if (!bad_lost_and_found && !lost_and_found) {
450                 lost_and_found = get_lost_and_found(ctx);
451                 if (!lost_and_found)
452                         bad_lost_and_found++;
453         }
454         if (bad_lost_and_found) {
455                 fix_problem(ctx, PR_3_NO_LPF, &pctx);
456                 return 1;
457         }
458         
459         sprintf(name, "#%lu", inode);
460         retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
461         if (retval == EXT2_ET_DIR_NO_SPACE) {
462                 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
463                         return 1;
464                 retval = expand_directory(ctx, lost_and_found);
465                 if (retval) {
466                         pctx.errcode = retval;
467                         fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
468                         return 1;
469                 }
470                 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
471         }
472         if (retval) {
473                 pctx.errcode = retval;
474                 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
475                 return 1;
476         }
477         adjust_inode_count(ctx, inode, +1);
478
479         return 0;
480 }
481
482 /*
483  * Utility routine to adjust the inode counts on an inode.
484  */
485 static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj)
486 {
487         ext2_filsys fs = ctx->fs;
488         errcode_t               retval;
489         struct ext2_inode       inode;
490         
491         if (!ino)
492                 return 0;
493
494         retval = ext2fs_read_inode(fs, ino, &inode);
495         if (retval)
496                 return retval;
497
498 #if 0
499         printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
500                inode.i_links_count);
501 #endif
502
503         inode.i_links_count += adj;
504         if (adj == 1) {
505                 ext2fs_icount_increment(ctx->inode_count, ino, 0);
506                 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
507         } else {
508                 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
509                 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
510         }
511         
512
513         retval = ext2fs_write_inode(fs, ino, &inode);
514         if (retval)
515                 return retval;
516
517         return 0;
518 }
519
520 /*
521  * Fix parent --- this routine fixes up the parent of a directory.
522  */
523 struct fix_dotdot_struct {
524         ext2_filsys     fs;
525         ino_t           parent;
526         int             done;
527         e2fsck_t        ctx;
528 };
529
530 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
531                            int  offset,
532                            int  blocksize,
533                            char *buf,
534                            void *priv_data)
535 {
536         struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
537         errcode_t       retval;
538         struct problem_context pctx;
539
540         if (dirent->name_len != 2)
541                 return 0;
542         if (strncmp(dirent->name, "..", 2))
543                 return 0;
544
545         clear_problem_context(&pctx);
546         
547         retval = adjust_inode_count(fp->ctx, dirent->inode, -1);
548         if (retval) {
549                 pctx.errcode = retval;
550                 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
551         }
552         retval = adjust_inode_count(fp->ctx, fp->parent, 1);
553         if (retval) {
554                 pctx.errcode = retval;
555                 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
556         }
557         dirent->inode = fp->parent;
558
559         fp->done++;
560         return DIRENT_ABORT | DIRENT_CHANGED;
561 }
562
563 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent)
564 {
565         ext2_filsys fs = ctx->fs;
566         errcode_t       retval;
567         struct fix_dotdot_struct fp;
568         struct problem_context pctx;
569
570         fp.fs = fs;
571         fp.parent = parent;
572         fp.done = 0;
573         fp.ctx = ctx;
574
575 #if 0
576         printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
577 #endif
578         
579         retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
580                                     0, fix_dotdot_proc, &fp);
581         if (retval || !fp.done) {
582                 clear_problem_context(&pctx);
583                 pctx.ino = dir->ino;
584                 pctx.errcode = retval;
585                 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
586                             PR_3_FIX_PARENT_NOFIND, &pctx);
587                 ext2fs_unmark_valid(fs);
588         }
589         dir->dotdot = parent;
590         
591         return;
592 }
593
594 /*
595  * These routines are responsible for expanding a /lost+found if it is
596  * too small.
597  */
598
599 struct expand_dir_struct {
600         int     done;
601         errcode_t       err;
602         e2fsck_t        ctx;
603 };
604
605 static int expand_dir_proc(ext2_filsys fs,
606                            blk_t        *blocknr,
607                            int  blockcnt,
608                            void *priv_data)
609 {
610         struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
611         blk_t   new_blk;
612         static blk_t    last_blk = 0;
613         char            *block;
614         errcode_t       retval;
615         e2fsck_t        ctx;
616
617         ctx = es->ctx;
618         
619         if (*blocknr) {
620                 last_blk = *blocknr;
621                 return 0;
622         }
623         retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
624                                   &new_blk);
625         if (retval) {
626                 es->err = retval;
627                 return BLOCK_ABORT;
628         }
629         if (blockcnt > 0) {
630                 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
631                 if (retval) {
632                         es->err = retval;
633                         return BLOCK_ABORT;
634                 }
635                 es->done = 1;
636                 retval = ext2fs_write_dir_block(fs, new_blk, block);
637         } else {
638                 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
639                 if (retval) {
640                         es->err = retval;
641                         return BLOCK_ABORT;
642                 }
643                 memset(block, 0, fs->blocksize);
644                 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
645         }       
646         if (retval) {
647                 es->err = retval;
648                 return BLOCK_ABORT;
649         }
650         ext2fs_free_mem((void **) &block);
651         *blocknr = new_blk;
652         ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
653         ext2fs_mark_block_bitmap(fs->block_map, new_blk);
654         ext2fs_mark_bb_dirty(fs);
655         if (es->done)
656                 return (BLOCK_CHANGED | BLOCK_ABORT);
657         else
658                 return BLOCK_CHANGED;
659 }
660
661 static errcode_t expand_directory(e2fsck_t ctx, ino_t dir)
662 {
663         ext2_filsys fs = ctx->fs;
664         errcode_t       retval;
665         struct expand_dir_struct es;
666         struct ext2_inode       inode;
667         
668         if (!(fs->flags & EXT2_FLAG_RW))
669                 return EXT2_ET_RO_FILSYS;
670
671         /*
672          * Read the inode and block bitmaps in; we'll be messing with
673          * them.
674          */
675         e2fsck_read_bitmaps(ctx);
676         
677         retval = ext2fs_check_directory(fs, dir);
678         if (retval)
679                 return retval;
680         
681         es.done = 0;
682         es.err = 0;
683         es.ctx = ctx;
684         
685         retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
686                                       0, expand_dir_proc, &es);
687
688         if (es.err)
689                 return es.err;
690         if (!es.done)
691                 return EXT2_ET_EXPAND_DIR_ERR;
692
693         /*
694          * Update the size and block count fields in the inode.
695          */
696         retval = ext2fs_read_inode(fs, dir, &inode);
697         if (retval)
698                 return retval;
699         
700         inode.i_size += fs->blocksize;
701         inode.i_blocks += fs->blocksize / 512;
702
703         e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
704
705         return 0;
706 }