blob: 7fdd814627bdaae37aacc4cfb8d85f9c924683af [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 ext2_ino_t get_lost_and_found(e2fsck_t ctx);
48static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent);
49static errcode_t adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj);
Theodore Ts'o3839e651997-04-26 13:21:57 +000050
Theodore Ts'o86c627e2001-01-11 15:12:14 +000051static ext2_ino_t lost_and_found = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052static int bad_lost_and_found = 0;
53
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000054static ext2fs_inode_bitmap inode_loop_detect = 0;
55static ext2fs_inode_bitmap inode_done_map = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'o08b21301997-11-03 19:42:40 +000057void e2fsck_pass3(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000058{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000059 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 int i;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000061#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000062 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000063#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000064 struct problem_context pctx;
65 struct dir_info *dir;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000066 unsigned long maxdirs, count;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000067
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000068#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000069 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000070#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000071
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000072 clear_problem_context(&pctx);
73
Theodore Ts'o3839e651997-04-26 13:21:57 +000074#ifdef MTRACE
75 mtrace_print("Pass 3");
76#endif
77
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000078 if (!(ctx->options & E2F_OPT_PREEN))
79 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +000080
81 /*
82 * Allocate some bitmaps to do loop detection.
83 */
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000084 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000085 &inode_done_map);
86 if (pctx.errcode) {
87 pctx.num = 2;
88 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000089 ctx->flags |= E2F_FLAG_ABORT;
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000090 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000092#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +000093 if (ctx->options & E2F_OPT_TIME) {
94 e2fsck_clear_progbar(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000095 print_resource_track(_("Peak memory"), &ctx->global_rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +000096 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000097#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000098
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000099 check_root(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000100 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
101 goto abort_exit;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000102
Theodore Ts'of3db3561997-04-26 13:34:30 +0000103 ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000105 maxdirs = e2fsck_get_num_dirinfo(ctx);
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000106 count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000107
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000108 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000109 if ((ctx->progress)(ctx, 3, 0, maxdirs))
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000110 goto abort_exit;
111
Theodore Ts'o08b21301997-11-03 19:42:40 +0000112 for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000113 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000114 if ((ctx->progress)(ctx, 3, count++, maxdirs))
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000115 goto abort_exit;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000116 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000117 if (check_directory(ctx, dir, &pctx))
118 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000120
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000121 /*
122 * Force the creation of /lost+found if not present
123 */
124 if ((ctx->flags & E2F_OPT_READONLY) == 0)
125 get_lost_and_found(ctx);
126
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000127abort_exit:
Theodore Ts'o08b21301997-11-03 19:42:40 +0000128 e2fsck_free_dir_info(ctx);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000129 if (inode_loop_detect) {
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000130 ext2fs_free_inode_bitmap(inode_loop_detect);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000131 inode_loop_detect = 0;
132 }
133 if (inode_done_map) {
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000134 ext2fs_free_inode_bitmap(inode_done_map);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000135 inode_done_map = 0;
136 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400137
138 /* If there are any directories that need to be indexed, do it here. */
139 if (ctx->dirs_to_hash)
140 e2fsck_rehash_directories(ctx);
141
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000142#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +0000143 if (ctx->options & E2F_OPT_TIME2) {
144 e2fsck_clear_progbar(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000145 print_resource_track(_("Pass 3"), &rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000146 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000147#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148}
149
150/*
151 * This makes sure the root inode is present; if not, we ask if the
152 * user wants us to create it. Not creating it is a fatal error.
153 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000154static void check_root(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000156 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 blk_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158 struct ext2_inode inode;
159 char * block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000160 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000162 clear_problem_context(&pctx);
163
164 if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 /*
Theodore Ts'o08b21301997-11-03 19:42:40 +0000166 * If the root inode is not a directory, die here. The
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 * user must have answered 'no' in pass1 when we
168 * offered to clear it.
169 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000170 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000171 EXT2_ROOT_INO))) {
172 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
173 ctx->flags |= E2F_FLAG_ABORT;
174 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 return;
176 }
177
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000178 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
179 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
180 ctx->flags |= E2F_FLAG_ABORT;
181 return;
182 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000184 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185
186 /*
187 * First, find a free block
188 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000189 pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
190 if (pctx.errcode) {
191 pctx.str = "ext2fs_new_block";
192 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000193 ctx->flags |= E2F_FLAG_ABORT;
194 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000196 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000197 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000198 ext2fs_mark_bb_dirty(fs);
199
200 /*
201 * Now let's create the actual data block for the inode
202 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000203 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
204 &block);
205 if (pctx.errcode) {
206 pctx.str = "ext2fs_new_dir_block";
207 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000208 ctx->flags |= E2F_FLAG_ABORT;
209 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 }
211
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000212 pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
213 if (pctx.errcode) {
214 pctx.str = "ext2fs_write_dir_block";
215 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000216 ctx->flags |= E2F_FLAG_ABORT;
217 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000219 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220
221 /*
222 * Set up the inode structure
223 */
224 memset(&inode, 0, sizeof(inode));
225 inode.i_mode = 040755;
226 inode.i_size = fs->blocksize;
227 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
228 inode.i_links_count = 2;
229 inode.i_blocks = fs->blocksize / 512;
230 inode.i_block[0] = blk;
231
232 /*
233 * Write out the inode.
234 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000235 pctx.errcode = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
236 if (pctx.errcode) {
237 pctx.str = "ext2fs_write_inode";
238 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000239 ctx->flags |= E2F_FLAG_ABORT;
240 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 }
242
243 /*
244 * Miscellaneous bookkeeping...
245 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000246 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000247 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
248 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000250 ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
251 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000252 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253 ext2fs_mark_ib_dirty(fs);
254}
255
256/*
257 * This subroutine is responsible for making sure that a particular
258 * directory is connected to the root; if it isn't we trace it up as
259 * far as we can go, and then offer to connect the resulting parent to
260 * the lost+found. We have to do loop detection; if we ever discover
261 * a loop, we treat that as a disconnected directory and offer to
262 * reparent it to lost+found.
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000263 *
264 * However, loop detection is expensive, because for very large
265 * filesystems, the inode_loop_detect bitmap is huge, and clearing it
266 * is non-trivial. Loops in filesystems are also a rare error case,
267 * and we shouldn't optimize for error cases. So we try two passes of
268 * the algorithm. The first time, we ignore loop detection and merely
269 * increment a counter; if the counter exceeds some extreme threshold,
270 * then we try again with the loop detection bitmap enabled.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000271 */
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000272static int check_directory(e2fsck_t ctx, struct dir_info *dir,
273 struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274{
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000275 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000276 struct dir_info *p = dir;
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000277 int loop_pass = 0, parent_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000279 if (!p)
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000280 return 0;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000281
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000282 while (1) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283 /*
284 * Mark this inode as being "done"; by the time we
285 * return from this function, the inode we either be
286 * verified as being connected to the directory tree,
287 * or we will have offered to reconnect this to
288 * lost+found.
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000289 *
290 * If it was marked done already, then we've reached a
291 * parent we've already checked.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292 */
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000293 if (ext2fs_mark_inode_bitmap(inode_done_map, p->ino))
294 break;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000295
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296 /*
297 * If this directory doesn't have a parent, or we've
298 * seen the parent once already, then offer to
299 * reparent it to lost+found
300 */
301 if (!p->parent ||
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000302 (loop_pass &&
303 (ext2fs_test_inode_bitmap(inode_loop_detect,
304 p->parent)))) {
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000305 pctx->ino = p->ino;
306 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
307 if (e2fsck_reconnect_file(ctx, p->ino))
308 ext2fs_unmark_valid(fs);
309 else {
310 p->parent = lost_and_found;
311 fix_dotdot(ctx, p, lost_and_found);
312 }
313 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 break;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000315 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000316 p = e2fsck_get_dir_info(ctx, p->parent);
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000317 if (!p) {
318 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000319 return 0;
320 }
321 if (loop_pass) {
322 ext2fs_mark_inode_bitmap(inode_loop_detect,
323 p->ino);
324 } else if (parent_count++ > 2048) {
325 /*
326 * If we've run into a path depth that's
327 * greater than 2048, try again with the inode
328 * loop bitmap turned on and start from the
329 * top.
330 */
331 loop_pass = 1;
332 if (inode_loop_detect)
333 ext2fs_clear_inode_bitmap(inode_loop_detect);
334 else {
335 pctx->errcode = ext2fs_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), &inode_loop_detect);
336 if (pctx->errcode) {
337 pctx->num = 1;
338 fix_problem(ctx,
339 PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
340 ctx->flags |= E2F_FLAG_ABORT;
341 return -1;
342 }
343 }
344 p = dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000346 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347
348 /*
349 * Make sure that .. and the parent directory are the same;
350 * offer to fix it if not.
351 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352 if (dir->parent != dir->dotdot) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000353 pctx->ino = dir->ino;
354 pctx->ino2 = dir->dotdot;
355 pctx->dir = dir->parent;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000356 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
357 fix_dotdot(ctx, dir, dir->parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000358 }
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000359 return 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400360}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000361
362/*
363 * This routine gets the lost_and_found inode, making it a directory
364 * if necessary
365 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000366static ext2_ino_t get_lost_and_found(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000368 ext2_filsys fs = ctx->fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000369 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000370 blk_t blk;
371 errcode_t retval;
372 struct ext2_inode inode;
373 char * block;
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000374 static const char name[] = "lost+found";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000375 struct problem_context pctx;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000376 struct dir_info *dirinfo;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000378 clear_problem_context(&pctx);
379
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000380 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
381 sizeof(name)-1, 0, &ino);
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000382 if (!retval) {
383 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino))
384 return ino;
385 /* Lost+found isn't a directory! */
386 pctx.ino = ino;
387 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
388 return 0;
389
Theodore Ts'oc54b3c31999-07-03 07:20:06 +0000390 /* OK, unlink the old /lost+found file. */
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000391 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
392 if (pctx.errcode) {
393 pctx.str = "ext2fs_unlink";
394 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
395 return 0;
396 }
397 dirinfo = e2fsck_get_dir_info(ctx, ino);
398 if (dirinfo)
399 dirinfo->parent = 0;
400 adjust_inode_count(ctx, ino, -1);
401 } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000402 pctx.errcode = retval;
403 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
404 }
405 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407
408 /*
409 * Read the inode and block bitmaps in; we'll be messing with
410 * them.
411 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000412 e2fsck_read_bitmaps(ctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413
414 /*
415 * First, find a free block
416 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000417 retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000419 pctx.errcode = retval;
420 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 return 0;
422 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000423 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000424 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425 ext2fs_mark_bb_dirty(fs);
426
427 /*
428 * Next find a free inode.
429 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000430 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755,
431 ctx->inode_used_map, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000433 pctx.errcode = retval;
434 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 return 0;
436 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000437 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
438 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000439 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000440 ext2fs_mark_ib_dirty(fs);
441
442 /*
443 * Now let's create the actual data block for the inode
444 */
445 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
446 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000447 pctx.errcode = retval;
448 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 return 0;
450 }
451
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000452 retval = ext2fs_write_dir_block(fs, blk, block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000453 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000455 pctx.errcode = retval;
456 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457 return 0;
458 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459
460 /*
461 * Set up the inode structure
462 */
463 memset(&inode, 0, sizeof(inode));
464 inode.i_mode = 040755;
465 inode.i_size = fs->blocksize;
466 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
467 inode.i_links_count = 2;
468 inode.i_blocks = fs->blocksize / 512;
469 inode.i_block[0] = blk;
470
471 /*
472 * Next, write out the inode.
473 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000474 pctx.errcode = ext2fs_write_inode(fs, ino, &inode);
475 if (pctx.errcode) {
476 pctx.str = "ext2fs_write_inode";
477 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 return 0;
479 }
480 /*
481 * Finally, create the directory link
482 */
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000483 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000484 if (pctx.errcode) {
485 pctx.str = "ext2fs_link";
486 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 return 0;
488 }
489
490 /*
491 * Miscellaneous bookkeeping that needs to be kept straight.
492 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000493 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000494 adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000495 ext2fs_icount_store(ctx->inode_count, ino, 2);
496 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000498 printf("/lost+found created; inode #%lu\n", ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499#endif
500 return ino;
501}
502
503/*
504 * This routine will connect a file to lost+found
505 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000506int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000508 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 errcode_t retval;
510 char name[80];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000511 struct problem_context pctx;
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000512 struct ext2_inode inode;
513 int file_type = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000514
515 clear_problem_context(&pctx);
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000516 pctx.ino = ino;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000517
518 if (!bad_lost_and_found && !lost_and_found) {
519 lost_and_found = get_lost_and_found(ctx);
520 if (!lost_and_found)
521 bad_lost_and_found++;
522 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523 if (bad_lost_and_found) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000524 fix_problem(ctx, PR_3_NO_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 return 1;
526 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000527
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000528 sprintf(name, "#%u", ino);
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000529 if (ext2fs_read_inode(fs, ino, &inode) == 0)
530 file_type = ext2_file_type(inode.i_mode);
531 retval = ext2fs_link(fs, lost_and_found, name, ino, file_type);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000533 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 return 1;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400535 retval = e2fsck_expand_directory(ctx, lost_and_found, 1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000537 pctx.errcode = retval;
538 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 return 1;
540 }
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000541 retval = ext2fs_link(fs, lost_and_found, name, ino, file_type);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 }
543 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000544 pctx.errcode = retval;
545 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 return 1;
547 }
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000548 adjust_inode_count(ctx, ino, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549
550 return 0;
551}
552
553/*
554 * Utility routine to adjust the inode counts on an inode.
555 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000556static errcode_t adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000558 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559 errcode_t retval;
560 struct ext2_inode inode;
561
562 if (!ino)
563 return 0;
564
565 retval = ext2fs_read_inode(fs, ino, &inode);
566 if (retval)
567 return retval;
568
569#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000570 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 inode.i_links_count);
572#endif
573
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000574 if (adj == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000575 ext2fs_icount_increment(ctx->inode_count, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000576 if (inode.i_links_count == (__u16) ~0)
577 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000578 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000579 inode.i_links_count++;
580 } else if (adj == -1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000581 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000582 if (inode.i_links_count == 0)
583 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000584 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000585 inode.i_links_count--;
586 } else {
587 /* Should never happen */
Theodore Ts'o99a2cc92000-08-22 21:41:52 +0000588 fatal_error(ctx, _("Debug error in e2fsck adjust_inode_count, "
589 "should never happen.\n"));
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000590 }
591
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 retval = ext2fs_write_inode(fs, ino, &inode);
593 if (retval)
594 return retval;
595
596 return 0;
597}
598
599/*
600 * Fix parent --- this routine fixes up the parent of a directory.
601 */
602struct fix_dotdot_struct {
603 ext2_filsys fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000604 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605 int done;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000606 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607};
608
609static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
610 int offset,
611 int blocksize,
612 char *buf,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000613 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000615 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000617 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000619 if ((dirent->name_len & 0xFF) != 2)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000620 return 0;
621 if (strncmp(dirent->name, "..", 2))
622 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000624 clear_problem_context(&pctx);
625
626 retval = adjust_inode_count(fp->ctx, dirent->inode, -1);
627 if (retval) {
628 pctx.errcode = retval;
629 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
630 }
631 retval = adjust_inode_count(fp->ctx, fp->parent, 1);
632 if (retval) {
633 pctx.errcode = retval;
634 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
635 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 dirent->inode = fp->parent;
637
638 fp->done++;
639 return DIRENT_ABORT | DIRENT_CHANGED;
640}
641
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000642static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000644 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645 errcode_t retval;
646 struct fix_dotdot_struct fp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000647 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648
649 fp.fs = fs;
650 fp.parent = parent;
651 fp.done = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000652 fp.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653
654#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000655 printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656#endif
657
658 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
659 0, fix_dotdot_proc, &fp);
660 if (retval || !fp.done) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000661 clear_problem_context(&pctx);
662 pctx.ino = dir->ino;
663 pctx.errcode = retval;
664 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
665 PR_3_FIX_PARENT_NOFIND, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 ext2fs_unmark_valid(fs);
667 }
668 dir->dotdot = parent;
669
670 return;
671}
672
673/*
674 * These routines are responsible for expanding a /lost+found if it is
675 * too small.
676 */
677
678struct expand_dir_struct {
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400679 int num;
680 int guaranteed_size;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000681 int newblocks;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400682 int last_block;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000683 errcode_t err;
684 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685};
686
687static int expand_dir_proc(ext2_filsys fs,
688 blk_t *blocknr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000689 e2_blkcnt_t blockcnt,
690 blk_t ref_block,
691 int ref_offset,
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000692 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000693{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000694 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 blk_t new_blk;
696 static blk_t last_blk = 0;
697 char *block;
698 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000699 e2fsck_t ctx;
700
701 ctx = es->ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000702
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400703 if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
704 return BLOCK_ABORT;
705
706 if (blockcnt > 0)
707 es->last_block = blockcnt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000708 if (*blocknr) {
709 last_blk = *blocknr;
710 return 0;
711 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000712 retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
713 &new_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 if (retval) {
715 es->err = retval;
716 return BLOCK_ABORT;
717 }
718 if (blockcnt > 0) {
719 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
720 if (retval) {
721 es->err = retval;
722 return BLOCK_ABORT;
723 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400724 es->num--;
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000725 retval = ext2fs_write_dir_block(fs, new_blk, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 } else {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000727 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
728 if (retval) {
729 es->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 return BLOCK_ABORT;
731 }
732 memset(block, 0, fs->blocksize);
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000733 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000734 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000735 if (retval) {
736 es->err = retval;
737 return BLOCK_ABORT;
738 }
Theodore Ts'o08b21301997-11-03 19:42:40 +0000739 ext2fs_free_mem((void **) &block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000740 *blocknr = new_blk;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000741 ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400742 ext2fs_block_alloc_stats(fs, new_blk, +1);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000743 es->newblocks++;
744
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400745 if (es->num == 0)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000746 return (BLOCK_CHANGED | BLOCK_ABORT);
747 else
748 return BLOCK_CHANGED;
749}
750
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400751errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
752 int num, int guaranteed_size)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000753{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000754 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000755 errcode_t retval;
756 struct expand_dir_struct es;
757 struct ext2_inode inode;
758
759 if (!(fs->flags & EXT2_FLAG_RW))
760 return EXT2_ET_RO_FILSYS;
761
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000762 /*
763 * Read the inode and block bitmaps in; we'll be messing with
764 * them.
765 */
766 e2fsck_read_bitmaps(ctx);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000767
Theodore Ts'o3839e651997-04-26 13:21:57 +0000768 retval = ext2fs_check_directory(fs, dir);
769 if (retval)
770 return retval;
771
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400772 es.num = num;
773 es.guaranteed_size = guaranteed_size;
774 es.last_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000775 es.err = 0;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000776 es.newblocks = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000777 es.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000779 retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
780 0, expand_dir_proc, &es);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781
782 if (es.err)
783 return es.err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000784
785 /*
786 * Update the size and block count fields in the inode.
787 */
788 retval = ext2fs_read_inode(fs, dir, &inode);
789 if (retval)
790 return retval;
791
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400792 inode.i_size = (es.last_block + 1) * fs->blocksize;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000793 inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000794
Theodore Ts'o08b21301997-11-03 19:42:40 +0000795 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796
797 return 0;
798}
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400799