blob: d4fea082e6f7500c9bf1693656aedc27efbf74e8 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
3 *
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +00004 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
Theodore Ts'o21c84b71997-04-29 16:15:03 +00005 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 *
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 *
Theodore Ts'o08b21301997-11-03 19:42:40 +000028 * 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.
Theodore Ts'o3839e651997-04-26 13:21:57 +000032 *
33 * Pass 3 frees the following data structures:
34 * - The dirinfo directory information cache.
35 */
36
Theodore Ts'o50e1e101997-04-26 13:58:21 +000037#ifdef HAVE_ERRNO_H
38#include <errno.h>
39#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000040
41#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000042#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000043
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000044static void check_root(e2fsck_t ctx);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +000045static int check_directory(e2fsck_t ctx, struct dir_info *dir,
46 struct problem_context *pctx);
Theodore Ts'o86c627e2001-01-11 15:12:14 +000047static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent);
48static errcode_t adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj);
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000050static ext2fs_inode_bitmap inode_loop_detect = 0;
51static ext2fs_inode_bitmap inode_done_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
Theodore Ts'o08b21301997-11-03 19:42:40 +000053void e2fsck_pass3(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000054{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000055 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000056 int i;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000057#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000058 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000059#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000060 struct problem_context pctx;
61 struct dir_info *dir;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000062 unsigned long maxdirs, count;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000063
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000064#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000065 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000066#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000067
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000068 clear_problem_context(&pctx);
69
Theodore Ts'o3839e651997-04-26 13:21:57 +000070#ifdef MTRACE
71 mtrace_print("Pass 3");
72#endif
73
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000074 if (!(ctx->options & E2F_OPT_PREEN))
75 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +000076
77 /*
78 * Allocate some bitmaps to do loop detection.
79 */
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000080 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000081 &inode_done_map);
82 if (pctx.errcode) {
83 pctx.num = 2;
84 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000085 ctx->flags |= E2F_FLAG_ABORT;
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000086 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000088#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +000089 if (ctx->options & E2F_OPT_TIME) {
90 e2fsck_clear_progbar(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000091 print_resource_track(_("Peak memory"), &ctx->global_rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +000092 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000093#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000094
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000095 check_root(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000096 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
97 goto abort_exit;
Theodore Ts'o08b21301997-11-03 19:42:40 +000098
Theodore Ts'of3db3561997-04-26 13:34:30 +000099 ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000101 maxdirs = e2fsck_get_num_dirinfo(ctx);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000102 count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000103
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000104 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000105 if ((ctx->progress)(ctx, 3, 0, maxdirs))
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000106 goto abort_exit;
107
Theodore Ts'o08b21301997-11-03 19:42:40 +0000108 for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400109 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
110 goto abort_exit;
111 if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
112 goto abort_exit;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000113 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000114 if (check_directory(ctx, dir, &pctx))
115 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000117
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000118 /*
119 * Force the creation of /lost+found if not present
120 */
121 if ((ctx->flags & E2F_OPT_READONLY) == 0)
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400122 e2fsck_get_lost_and_found(ctx, 1);
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000123
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400124 /*
125 * If there are any directories that need to be indexed or
126 * optimized, do it here.
127 */
128 e2fsck_rehash_directories(ctx);
129
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000130abort_exit:
Theodore Ts'o08b21301997-11-03 19:42:40 +0000131 e2fsck_free_dir_info(ctx);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000132 if (inode_loop_detect) {
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000133 ext2fs_free_inode_bitmap(inode_loop_detect);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000134 inode_loop_detect = 0;
135 }
136 if (inode_done_map) {
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000137 ext2fs_free_inode_bitmap(inode_done_map);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000138 inode_done_map = 0;
139 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400140
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000141#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +0000142 if (ctx->options & E2F_OPT_TIME2) {
143 e2fsck_clear_progbar(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000144 print_resource_track(_("Pass 3"), &rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000145 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000146#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147}
148
149/*
150 * This makes sure the root inode is present; if not, we ask if the
151 * user wants us to create it. Not creating it is a fatal error.
152 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000153static void check_root(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000155 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 blk_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 struct ext2_inode inode;
158 char * block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000159 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000161 clear_problem_context(&pctx);
162
163 if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164 /*
Theodore Ts'o08b21301997-11-03 19:42:40 +0000165 * If the root inode is not a directory, die here. The
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166 * user must have answered 'no' in pass1 when we
167 * offered to clear it.
168 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000169 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000170 EXT2_ROOT_INO))) {
171 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
172 ctx->flags |= E2F_FLAG_ABORT;
173 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 return;
175 }
176
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000177 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
178 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
179 ctx->flags |= E2F_FLAG_ABORT;
180 return;
181 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000183 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184
185 /*
186 * First, find a free block
187 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000188 pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
189 if (pctx.errcode) {
190 pctx.str = "ext2fs_new_block";
191 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000192 ctx->flags |= E2F_FLAG_ABORT;
193 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000195 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000196 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 ext2fs_mark_bb_dirty(fs);
198
199 /*
200 * Now let's create the actual data block for the inode
201 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000202 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
203 &block);
204 if (pctx.errcode) {
205 pctx.str = "ext2fs_new_dir_block";
206 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000207 ctx->flags |= E2F_FLAG_ABORT;
208 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 }
210
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000211 pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
212 if (pctx.errcode) {
213 pctx.str = "ext2fs_write_dir_block";
214 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000215 ctx->flags |= E2F_FLAG_ABORT;
216 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000217 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000218 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000219
220 /*
221 * Set up the inode structure
222 */
223 memset(&inode, 0, sizeof(inode));
224 inode.i_mode = 040755;
225 inode.i_size = fs->blocksize;
226 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
227 inode.i_links_count = 2;
228 inode.i_blocks = fs->blocksize / 512;
229 inode.i_block[0] = blk;
230
231 /*
232 * Write out the inode.
233 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000234 pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
235 if (pctx.errcode) {
236 pctx.str = "ext2fs_write_inode";
237 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000238 ctx->flags |= E2F_FLAG_ABORT;
239 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 }
241
242 /*
243 * Miscellaneous bookkeeping...
244 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000245 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000246 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
247 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000249 ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
250 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000251 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252 ext2fs_mark_ib_dirty(fs);
253}
254
255/*
256 * This subroutine is responsible for making sure that a particular
257 * directory is connected to the root; if it isn't we trace it up as
258 * far as we can go, and then offer to connect the resulting parent to
259 * the lost+found. We have to do loop detection; if we ever discover
260 * a loop, we treat that as a disconnected directory and offer to
261 * reparent it to lost+found.
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000262 *
263 * However, loop detection is expensive, because for very large
264 * filesystems, the inode_loop_detect bitmap is huge, and clearing it
265 * is non-trivial. Loops in filesystems are also a rare error case,
266 * and we shouldn't optimize for error cases. So we try two passes of
267 * the algorithm. The first time, we ignore loop detection and merely
268 * increment a counter; if the counter exceeds some extreme threshold,
269 * then we try again with the loop detection bitmap enabled.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 */
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000271static int check_directory(e2fsck_t ctx, struct dir_info *dir,
272 struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273{
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000274 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000275 struct dir_info *p = dir;
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000276 int loop_pass = 0, parent_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000278 if (!p)
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000279 return 0;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000280
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000281 while (1) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282 /*
283 * Mark this inode as being "done"; by the time we
284 * return from this function, the inode we either be
285 * verified as being connected to the directory tree,
286 * or we will have offered to reconnect this to
287 * lost+found.
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000288 *
289 * If it was marked done already, then we've reached a
290 * parent we've already checked.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 */
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000292 if (ext2fs_mark_inode_bitmap(inode_done_map, p->ino))
293 break;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000294
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295 /*
296 * If this directory doesn't have a parent, or we've
297 * seen the parent once already, then offer to
298 * reparent it to lost+found
299 */
300 if (!p->parent ||
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000301 (loop_pass &&
302 (ext2fs_test_inode_bitmap(inode_loop_detect,
303 p->parent)))) {
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000304 pctx->ino = p->ino;
305 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
306 if (e2fsck_reconnect_file(ctx, p->ino))
307 ext2fs_unmark_valid(fs);
308 else {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400309 p->parent = ctx->lost_and_found;
310 fix_dotdot(ctx, p, ctx->lost_and_found);
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000311 }
312 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 break;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000314 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000315 p = e2fsck_get_dir_info(ctx, p->parent);
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000316 if (!p) {
317 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000318 return 0;
319 }
320 if (loop_pass) {
321 ext2fs_mark_inode_bitmap(inode_loop_detect,
322 p->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 p = dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000345 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346
347 /*
348 * Make sure that .. and the parent directory are the same;
349 * offer to fix it if not.
350 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351 if (dir->parent != dir->dotdot) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000352 pctx->ino = dir->ino;
353 pctx->ino2 = dir->dotdot;
354 pctx->dir = dir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000355 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
356 fix_dotdot(ctx, dir, dir->parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000357 }
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000358 return 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400359}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360
361/*
362 * This routine gets the lost_and_found inode, making it a directory
363 * if necessary
364 */
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400365ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000367 ext2_filsys fs = ctx->fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000368 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 blk_t blk;
370 errcode_t retval;
371 struct ext2_inode inode;
372 char * block;
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000373 static const char name[] = "lost+found";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000374 struct problem_context pctx;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000375 struct dir_info *dirinfo;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000376
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400377 if (ctx->lost_and_found)
378 return ctx->lost_and_found;
379
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000380 clear_problem_context(&pctx);
381
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000382 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
383 sizeof(name)-1, 0, &ino);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400384 if (retval && !fix)
385 return 0;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000386 if (!retval) {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400387 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino)) {
388 ctx->lost_and_found = ino;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000389 return ino;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400390 }
391
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000392 /* Lost+found isn't a directory! */
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400393 if (!fix)
394 return 0;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000395 pctx.ino = ino;
396 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
397 return 0;
398
Theodore Ts'oc54b3c31999-07-03 07:20:06 +0000399 /* OK, unlink the old /lost+found file. */
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000400 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
401 if (pctx.errcode) {
402 pctx.str = "ext2fs_unlink";
403 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
404 return 0;
405 }
406 dirinfo = e2fsck_get_dir_info(ctx, ino);
407 if (dirinfo)
408 dirinfo->parent = 0;
409 adjust_inode_count(ctx, ino, -1);
410 } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000411 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))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416
417 /*
418 * Read the inode and block bitmaps in; we'll be messing with
419 * them.
420 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000421 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422
423 /*
424 * First, find a free block
425 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000426 retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000428 pctx.errcode = retval;
429 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 return 0;
431 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000432 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400433 ext2fs_block_alloc_stats(fs, blk, +1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434
435 /*
436 * Next find a free inode.
437 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000438 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755,
439 ctx->inode_used_map, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000441 pctx.errcode = retval;
442 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 return 0;
444 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000445 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
446 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400447 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448
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) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000454 pctx.errcode = retval;
455 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 return 0;
457 }
458
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000459 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000460 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000462 pctx.errcode = retval;
463 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464 return 0;
465 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466
467 /*
468 * Set up the inode structure
469 */
470 memset(&inode, 0, sizeof(inode));
471 inode.i_mode = 040755;
472 inode.i_size = fs->blocksize;
473 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
474 inode.i_links_count = 2;
475 inode.i_blocks = fs->blocksize / 512;
476 inode.i_block[0] = blk;
477
478 /*
479 * Next, write out the inode.
480 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000481 pctx.errcode = ext2fs_write_inode(fs, ino, &inode);
482 if (pctx.errcode) {
483 pctx.str = "ext2fs_write_inode";
484 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 return 0;
486 }
487 /*
488 * Finally, create the directory link
489 */
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000490 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000491 if (pctx.errcode) {
492 pctx.str = "ext2fs_link";
493 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494 return 0;
495 }
496
497 /*
498 * Miscellaneous bookkeeping that needs to be kept straight.
499 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000500 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000501 adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000502 ext2fs_icount_store(ctx->inode_count, ino, 2);
503 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400504 ctx->lost_and_found = ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000506 printf("/lost+found created; inode #%lu\n", ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507#endif
508 return ino;
509}
510
511/*
512 * This routine will connect a file to lost+found
513 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000514int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000516 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 errcode_t retval;
518 char name[80];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000519 struct problem_context pctx;
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000520 struct ext2_inode inode;
521 int file_type = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000522
523 clear_problem_context(&pctx);
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000524 pctx.ino = ino;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000525
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400526 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++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000529 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400530 if (ctx->bad_lost_and_found) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000531 fix_problem(ctx, PR_3_NO_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 return 1;
533 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000534
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000535 sprintf(name, "#%u", ino);
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000536 if (ext2fs_read_inode(fs, ino, &inode) == 0)
537 file_type = ext2_file_type(inode.i_mode);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400538 retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000540 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 return 1;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400542 retval = e2fsck_expand_directory(ctx, ctx->lost_and_found,
543 1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000545 pctx.errcode = retval;
546 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 return 1;
548 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400549 retval = ext2fs_link(fs, ctx->lost_and_found, name,
550 ino, file_type);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 }
552 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000553 pctx.errcode = retval;
554 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 return 1;
556 }
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000557 adjust_inode_count(ctx, ino, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558
559 return 0;
560}
561
562/*
563 * Utility routine to adjust the inode counts on an inode.
564 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000565static errcode_t adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000567 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568 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
Theodore Ts'of3db3561997-04-26 13:34:30 +0000579 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580 inode.i_links_count);
581#endif
582
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000583 if (adj == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000584 ext2fs_icount_increment(ctx->inode_count, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000585 if (inode.i_links_count == (__u16) ~0)
586 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000587 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000588 inode.i_links_count++;
589 } else if (adj == -1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000590 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000591 if (inode.i_links_count == 0)
592 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000593 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000594 inode.i_links_count--;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000595 }
596
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 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 */
607struct fix_dotdot_struct {
608 ext2_filsys fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000609 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610 int done;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000611 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612};
613
614static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
615 int offset,
616 int blocksize,
617 char *buf,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000618 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000620 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000622 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000624 if ((dirent->name_len & 0xFF) != 2)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625 return 0;
626 if (strncmp(dirent->name, "..", 2))
627 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 clear_problem_context(&pctx);
630
631 retval = 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 = 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 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641 dirent->inode = fp->parent;
642
643 fp->done++;
644 return DIRENT_ABORT | DIRENT_CHANGED;
645}
646
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000647static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000649 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650 errcode_t retval;
651 struct fix_dotdot_struct fp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000652 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653
654 fp.fs = fs;
655 fp.parent = parent;
656 fp.done = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000657 fp.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658
659#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000660 printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661#endif
662
663 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
664 0, fix_dotdot_proc, &fp);
665 if (retval || !fp.done) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000666 clear_problem_context(&pctx);
667 pctx.ino = dir->ino;
668 pctx.errcode = retval;
669 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
670 PR_3_FIX_PARENT_NOFIND, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 ext2fs_unmark_valid(fs);
672 }
673 dir->dotdot = parent;
674
675 return;
676}
677
678/*
679 * These routines are responsible for expanding a /lost+found if it is
680 * too small.
681 */
682
683struct expand_dir_struct {
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400684 int num;
685 int guaranteed_size;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000686 int newblocks;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400687 int last_block;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000688 errcode_t err;
689 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690};
691
692static int expand_dir_proc(ext2_filsys fs,
693 blk_t *blocknr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000694 e2_blkcnt_t blockcnt,
695 blk_t ref_block,
696 int ref_offset,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000697 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000699 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700 blk_t new_blk;
701 static blk_t last_blk = 0;
702 char *block;
703 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000704 e2fsck_t ctx;
705
706 ctx = es->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400708 if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
709 return BLOCK_ABORT;
710
711 if (blockcnt > 0)
712 es->last_block = blockcnt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000713 if (*blocknr) {
714 last_blk = *blocknr;
715 return 0;
716 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000717 retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
718 &new_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719 if (retval) {
720 es->err = retval;
721 return BLOCK_ABORT;
722 }
723 if (blockcnt > 0) {
724 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
725 if (retval) {
726 es->err = retval;
727 return BLOCK_ABORT;
728 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400729 es->num--;
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000730 retval = ext2fs_write_dir_block(fs, new_blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000731 } else {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000732 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
733 if (retval) {
734 es->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000735 return BLOCK_ABORT;
736 }
737 memset(block, 0, fs->blocksize);
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000738 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000739 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000740 if (retval) {
741 es->err = retval;
742 return BLOCK_ABORT;
743 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000744 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745 *blocknr = new_blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000746 ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400747 ext2fs_block_alloc_stats(fs, new_blk, +1);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000748 es->newblocks++;
749
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400750 if (es->num == 0)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000751 return (BLOCK_CHANGED | BLOCK_ABORT);
752 else
753 return BLOCK_CHANGED;
754}
755
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400756errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
757 int num, int guaranteed_size)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000759 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000760 errcode_t retval;
761 struct expand_dir_struct es;
762 struct ext2_inode inode;
763
764 if (!(fs->flags & EXT2_FLAG_RW))
765 return EXT2_ET_RO_FILSYS;
766
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000767 /*
768 * Read the inode and block bitmaps in; we'll be messing with
769 * them.
770 */
771 e2fsck_read_bitmaps(ctx);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000772
Theodore Ts'o3839e651997-04-26 13:21:57 +0000773 retval = ext2fs_check_directory(fs, dir);
774 if (retval)
775 return retval;
776
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400777 es.num = num;
778 es.guaranteed_size = guaranteed_size;
779 es.last_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000780 es.err = 0;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000781 es.newblocks = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000782 es.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000783
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000784 retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
785 0, expand_dir_proc, &es);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000786
787 if (es.err)
788 return es.err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000789
790 /*
791 * Update the size and block count fields in the inode.
792 */
793 retval = ext2fs_read_inode(fs, dir, &inode);
794 if (retval)
795 return retval;
796
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400797 inode.i_size = (es.last_block + 1) * fs->blocksize;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000798 inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799
Theodore Ts'o08b21301997-11-03 19:42:40 +0000800 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801
802 return 0;
803}
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400804