]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/pass3.c
Merge branch 'maint' into next
[thirdparty/e2fsprogs.git] / e2fsck / pass3.c
CommitLineData
3839e657
TT
1/*
2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
3 *
c1faf9cc 4 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
21c84b71
TT
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
efc6f628 10 *
3839e657
TT
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".
efc6f628 17 *
055866d8 18 * Then, pass3 iterates over all directory inodes; for each directory
3839e657
TT
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.
efc6f628 27 *
08b21301
TT
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.
3839e657
TT
32 *
33 * Pass 3 frees the following data structures:
34 * - The dirinfo directory information cache.
35 */
36
d1154eb4 37#include "config.h"
50e1e10f
TT
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
3839e657
TT
41
42#include "e2fsck.h"
21c84b71 43#include "problem.h"
3839e657 44
1b6bf175 45static void check_root(e2fsck_t ctx);
28db82a8 46static int check_directory(e2fsck_t ctx, ext2_ino_t ino,
28ffafb0 47 struct problem_context *pctx);
28db82a8 48static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent);
3839e657 49
a02ce9df
TT
50static ext2fs_inode_bitmap inode_loop_detect = 0;
51static ext2fs_inode_bitmap inode_done_map = 0;
efc6f628 52
08b21301 53void e2fsck_pass3(e2fsck_t ctx)
3839e657 54{
1b6bf175 55 ext2_filsys fs = ctx->fs;
f0131bdc 56 struct dir_info_iter *iter = NULL;
8bf191e8 57#ifdef RESOURCE_TRACK
3839e657 58 struct resource_track rtrack;
8bf191e8 59#endif
21c84b71
TT
60 struct problem_context pctx;
61 struct dir_info *dir;
7f813ba3 62 unsigned long maxdirs, count;
f8188fff 63
6d96b00d 64 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175
TT
65 clear_problem_context(&pctx);
66
3839e657
TT
67#ifdef MTRACE
68 mtrace_print("Pass 3");
69#endif
70
1b6bf175
TT
71 if (!(ctx->options & E2F_OPT_PREEN))
72 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
3839e657
TT
73
74 /*
75 * Allocate some bitmaps to do loop detection.
76 */
830b44f4
TT
77 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("inode done bitmap"),
78 EXT2FS_BMAP64_AUTODIR,
79 "inode_done_map", &inode_done_map);
1b6bf175
TT
80 if (pctx.errcode) {
81 pctx.num = 2;
82 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
08b21301 83 ctx->flags |= E2F_FLAG_ABORT;
a02ce9df 84 goto abort_exit;
3839e657 85 }
9facd076 86 print_resource_track(ctx, _("Peak memory"), &ctx->global_rtrack, NULL);
3839e657 87
1b6bf175 88 check_root(ctx);
a02ce9df
TT
89 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
90 goto abort_exit;
08b21301 91
c5d2f50d 92 ext2fs_mark_inode_bitmap2(inode_done_map, EXT2_ROOT_INO);
3839e657 93
7f813ba3 94 maxdirs = e2fsck_get_num_dirinfo(ctx);
f75c28de 95 count = 1;
f8188fff 96
f75c28de 97 if (ctx->progress)
7f813ba3 98 if ((ctx->progress)(ctx, 3, 0, maxdirs))
f75c28de 99 goto abort_exit;
efc6f628 100
28db82a8
TT
101 iter = e2fsck_dir_info_iter_begin(ctx);
102 while ((dir = e2fsck_dir_info_iter(ctx, iter)) != 0) {
4ecd63d7
TT
103 if (ctx->flags & E2F_FLAG_SIGNAL_MASK ||
104 ctx->flags & E2F_FLAG_RESTART)
4cae0452
TT
105 goto abort_exit;
106 if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
107 goto abort_exit;
c5d2f50d 108 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dir->ino))
28db82a8 109 if (check_directory(ctx, dir->ino, &pctx))
28ffafb0 110 goto abort_exit;
3839e657 111 }
a02ce9df 112
5a679c8f
TT
113 /*
114 * Force the creation of /lost+found if not present
115 */
e9a8c0c2 116 if ((ctx->options & E2F_OPT_READONLY) == 0)
850d05e9 117 e2fsck_get_lost_and_found(ctx, 1);
5a679c8f 118
850d05e9
TT
119 /*
120 * If there are any directories that need to be indexed or
121 * optimized, do it here.
122 */
123 e2fsck_rehash_directories(ctx);
efc6f628 124
a02ce9df 125abort_exit:
f0131bdc
DW
126 if (iter)
127 e2fsck_dir_info_iter_end(ctx, iter);
08b21301 128 e2fsck_free_dir_info(ctx);
28ffafb0 129 if (inode_loop_detect) {
a02ce9df 130 ext2fs_free_inode_bitmap(inode_loop_detect);
28ffafb0
TT
131 inode_loop_detect = 0;
132 }
133 if (inode_done_map) {
a02ce9df 134 ext2fs_free_inode_bitmap(inode_done_map);
28ffafb0
TT
135 inode_done_map = 0;
136 }
b7a00563 137
b729b7df
DW
138 if (ctx->lnf_repair_block) {
139 ext2fs_unmark_block_bitmap2(ctx->block_found_map,
140 ctx->lnf_repair_block);
141 ctx->lnf_repair_block = 0;
142 }
143 if (ctx->root_repair_block) {
144 ext2fs_unmark_block_bitmap2(ctx->block_found_map,
145 ctx->root_repair_block);
146 ctx->root_repair_block = 0;
147 }
148
9facd076 149 print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io);
3839e657
TT
150}
151
152/*
153 * This makes sure the root inode is present; if not, we ask if the
154 * user wants us to create it. Not creating it is a fatal error.
155 */
1b6bf175 156static void check_root(e2fsck_t ctx)
3839e657 157{
1b6bf175 158 ext2_filsys fs = ctx->fs;
c5d2f50d 159 blk64_t blk;
3839e657
TT
160 struct ext2_inode inode;
161 char * block;
1b6bf175 162 struct problem_context pctx;
efc6f628 163
1b6bf175 164 clear_problem_context(&pctx);
efc6f628 165
c5d2f50d 166 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO)) {
3839e657 167 /*
08b21301 168 * If the root inode is not a directory, die here. The
3839e657
TT
169 * user must have answered 'no' in pass1 when we
170 * offered to clear it.
171 */
c5d2f50d 172 if (!(ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
f8188fff
TT
173 EXT2_ROOT_INO))) {
174 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
175 ctx->flags |= E2F_FLAG_ABORT;
176 }
3839e657
TT
177 return;
178 }
179
f8188fff
TT
180 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
181 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
182 ctx->flags |= E2F_FLAG_ABORT;
183 return;
184 }
3839e657 185
f8188fff 186 e2fsck_read_bitmaps(ctx);
efc6f628 187
3839e657
TT
188 /*
189 * First, find a free block
190 */
b729b7df
DW
191 if (ctx->root_repair_block) {
192 blk = ctx->root_repair_block;
193 ctx->root_repair_block = 0;
194 goto skip_new_block;
195 }
c5d2f50d 196 pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1b6bf175
TT
197 if (pctx.errcode) {
198 pctx.str = "ext2fs_new_block";
199 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
08b21301
TT
200 ctx->flags |= E2F_FLAG_ABORT;
201 return;
3839e657 202 }
c5d2f50d 203 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
b729b7df 204skip_new_block:
c5d2f50d 205 ext2fs_mark_block_bitmap2(fs->block_map, blk);
3839e657
TT
206 ext2fs_mark_bb_dirty(fs);
207
3839e657
TT
208 /*
209 * Set up the inode structure
210 */
211 memset(&inode, 0, sizeof(inode));
212 inode.i_mode = 040755;
213 inode.i_size = fs->blocksize;
1f3ad14a 214 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
3839e657 215 inode.i_links_count = 2;
1ca1059f 216 ext2fs_iblk_set(fs, &inode, 1);
3839e657
TT
217 inode.i_block[0] = blk;
218
219 /*
220 * Write out the inode.
221 */
030970ed 222 pctx.errcode = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
1b6bf175
TT
223 if (pctx.errcode) {
224 pctx.str = "ext2fs_write_inode";
225 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
08b21301
TT
226 ctx->flags |= E2F_FLAG_ABORT;
227 return;
3839e657 228 }
efc6f628 229
eb89a628
DW
230 /*
231 * Now let's create the actual data block for the inode.
232 * Due to metadata_csum, we must write the dir blocks AFTER
233 * the inode has been written to disk!
234 */
235 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
236 &block);
237 if (pctx.errcode) {
238 pctx.str = "ext2fs_new_dir_block";
239 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
240 ctx->flags |= E2F_FLAG_ABORT;
241 return;
242 }
243
244 pctx.errcode = ext2fs_write_dir_block4(fs, blk, block, 0,
245 EXT2_ROOT_INO);
246 ext2fs_free_mem(&block);
247 if (pctx.errcode) {
248 pctx.str = "ext2fs_write_dir_block4";
249 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
250 ctx->flags |= E2F_FLAG_ABORT;
251 return;
252 }
253
3839e657
TT
254 /*
255 * Miscellaneous bookkeeping...
256 */
08b21301 257 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
1b6bf175
TT
258 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
259 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
3839e657 260
c5d2f50d
VAH
261 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO);
262 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, EXT2_ROOT_INO);
263 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_ROOT_INO);
3839e657
TT
264 ext2fs_mark_ib_dirty(fs);
265}
266
267/*
268 * This subroutine is responsible for making sure that a particular
269 * directory is connected to the root; if it isn't we trace it up as
270 * far as we can go, and then offer to connect the resulting parent to
271 * the lost+found. We have to do loop detection; if we ever discover
272 * a loop, we treat that as a disconnected directory and offer to
273 * reparent it to lost+found.
efc6f628 274 *
28ffafb0
TT
275 * However, loop detection is expensive, because for very large
276 * filesystems, the inode_loop_detect bitmap is huge, and clearing it
277 * is non-trivial. Loops in filesystems are also a rare error case,
278 * and we shouldn't optimize for error cases. So we try two passes of
279 * the algorithm. The first time, we ignore loop detection and merely
280 * increment a counter; if the counter exceeds some extreme threshold,
281 * then we try again with the loop detection bitmap enabled.
3839e657 282 */
28db82a8 283static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
28ffafb0 284 struct problem_context *pctx)
3839e657 285{
28ffafb0 286 ext2_filsys fs = ctx->fs;
28db82a8 287 ext2_ino_t ino = dir, parent;
28ffafb0 288 int loop_pass = 0, parent_count = 0;
3839e657 289
28ffafb0 290 while (1) {
3839e657
TT
291 /*
292 * Mark this inode as being "done"; by the time we
293 * return from this function, the inode we either be
294 * verified as being connected to the directory tree,
295 * or we will have offered to reconnect this to
296 * lost+found.
28ffafb0
TT
297 *
298 * If it was marked done already, then we've reached a
299 * parent we've already checked.
3839e657 300 */
c5d2f50d 301 if (ext2fs_mark_inode_bitmap2(inode_done_map, ino))
28ffafb0 302 break;
7f813ba3 303
28db82a8
TT
304 if (e2fsck_dir_info_get_parent(ctx, ino, &parent)) {
305 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
306 return 0;
307 }
308
3839e657
TT
309 /*
310 * If this directory doesn't have a parent, or we've
311 * seen the parent once already, then offer to
312 * reparent it to lost+found
313 */
28db82a8 314 if (!parent ||
efc6f628 315 (loop_pass &&
c5d2f50d 316 (ext2fs_test_inode_bitmap2(inode_loop_detect,
28db82a8
TT
317 parent)))) {
318 pctx->ino = ino;
7f813ba3 319 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
2e5fcce0 320 if (e2fsck_reconnect_file(ctx, pctx->ino))
7f813ba3
TT
321 ext2fs_unmark_valid(fs);
322 else {
efc6f628 323 fix_dotdot(ctx, pctx->ino,
28db82a8
TT
324 ctx->lost_and_found);
325 parent = ctx->lost_and_found;
7f813ba3
TT
326 }
327 }
3839e657 328 break;
7f813ba3 329 }
28db82a8 330 ino = parent;
28ffafb0 331 if (loop_pass) {
c5d2f50d 332 ext2fs_mark_inode_bitmap2(inode_loop_detect, ino);
28ffafb0
TT
333 } else if (parent_count++ > 2048) {
334 /*
335 * If we've run into a path depth that's
336 * greater than 2048, try again with the inode
337 * loop bitmap turned on and start from the
338 * top.
339 */
340 loop_pass = 1;
341 if (inode_loop_detect)
342 ext2fs_clear_inode_bitmap(inode_loop_detect);
343 else {
830b44f4 344 pctx->errcode = e2fsck_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), EXT2FS_BMAP64_AUTODIR, "inode_loop_detect", &inode_loop_detect);
28ffafb0
TT
345 if (pctx->errcode) {
346 pctx->num = 1;
efc6f628 347 fix_problem(ctx,
28ffafb0
TT
348 PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
349 ctx->flags |= E2F_FLAG_ABORT;
350 return -1;
351 }
352 }
28db82a8 353 ino = dir;
3839e657 354 }
21c84b71 355 }
3839e657
TT
356
357 /*
358 * Make sure that .. and the parent directory are the same;
359 * offer to fix it if not.
360 */
28db82a8
TT
361 pctx->ino = dir;
362 if (e2fsck_dir_info_get_dotdot(ctx, dir, &pctx->ino2) ||
363 e2fsck_dir_info_get_parent(ctx, dir, &pctx->dir)) {
364 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
365 return 0;
366 }
367 if (pctx->ino2 != pctx->dir) {
1b6bf175 368 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
28db82a8 369 fix_dotdot(ctx, dir, pctx->dir);
3839e657 370 }
28ffafb0 371 return 0;
b7a00563 372}
3839e657
TT
373
374/*
375 * This routine gets the lost_and_found inode, making it a directory
376 * if necessary
377 */
850d05e9 378ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
3839e657 379{
1b6bf175 380 ext2_filsys fs = ctx->fs;
86c627ec 381 ext2_ino_t ino;
c5d2f50d 382 blk64_t blk;
3839e657 383 errcode_t retval;
bc1ec4b4 384 struct ext2_inode_large inode;
3839e657 385 char * block;
53ef44c4 386 static const char name[] = "lost+found";
1b6bf175 387 struct problem_context pctx;
506fc404 388 int will_rehash, flags;
3839e657 389
850d05e9
TT
390 if (ctx->lost_and_found)
391 return ctx->lost_and_found;
392
1b6bf175 393 clear_problem_context(&pctx);
efc6f628 394
506fc404
DW
395 will_rehash = e2fsck_dir_will_be_rehashed(ctx, EXT2_ROOT_INO);
396 if (will_rehash) {
397 flags = ctx->fs->flags;
398 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
399 }
21c84b71
TT
400 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
401 sizeof(name)-1, 0, &ino);
506fc404
DW
402 if (will_rehash)
403 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
404 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
850d05e9
TT
405 if (retval && !fix)
406 return 0;
4a9f5936 407 if (!retval) {
9676f3a9 408 /* Lost+found shouldn't have inline data */
bc1ec4b4
TT
409 retval = ext2fs_read_inode_full(fs, ino, EXT2_INODE(&inode),
410 sizeof(inode));
9676f3a9
ZL
411 if (fix && retval)
412 return 0;
413
414 if (fix && (inode.i_flags & EXT4_INLINE_DATA_FL)) {
415 if (!fix_problem(ctx, PR_3_LPF_INLINE_DATA, &pctx))
416 return 0;
417 goto unlink;
418 }
419
4ecd63d7
TT
420 if (fix && (inode.i_flags & EXT4_ENCRYPT_FL)) {
421 if (!fix_problem(ctx, PR_3_LPF_ENCRYPTED, &pctx))
422 return 0;
423 goto unlink;
424 }
425
82372e32 426 if (ext2fs_check_directory(fs, ino) == 0) {
850d05e9 427 ctx->lost_and_found = ino;
4a9f5936 428 return ino;
850d05e9 429 }
efc6f628 430
4a9f5936 431 /* Lost+found isn't a directory! */
850d05e9
TT
432 if (!fix)
433 return 0;
4a9f5936
TT
434 pctx.ino = ino;
435 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
436 return 0;
437
9676f3a9 438unlink:
c54b3c3c 439 /* OK, unlink the old /lost+found file. */
4a9f5936
TT
440 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
441 if (pctx.errcode) {
442 pctx.str = "ext2fs_unlink";
443 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
444 return 0;
445 }
28db82a8 446 (void) e2fsck_dir_info_set_parent(ctx, ino, 0);
b0700a1b 447 e2fsck_adjust_inode_count(ctx, ino, -1);
4ecd63d7
TT
448 /*
449 * If the old lost+found was a directory, we've just
450 * disconnected it from the directory tree, which
451 * means we need to restart the directory tree scan.
452 * The simplest way to do this is restart the whole
453 * e2fsck operation.
454 */
455 if (LINUX_S_ISDIR(inode.i_mode))
456 ctx->flags |= E2F_FLAG_RESTART;
4a9f5936 457 } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
1b6bf175
TT
458 pctx.errcode = retval;
459 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
460 }
461 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
3839e657 462 return 0;
3839e657
TT
463
464 /*
465 * Read the inode and block bitmaps in; we'll be messing with
466 * them.
467 */
f8188fff 468 e2fsck_read_bitmaps(ctx);
efc6f628 469
3839e657
TT
470 /*
471 * First, find a free block
472 */
b729b7df
DW
473 if (ctx->lnf_repair_block) {
474 blk = ctx->lnf_repair_block;
475 ctx->lnf_repair_block = 0;
476 goto skip_new_block;
477 }
c5d2f50d 478 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
09918967
DW
479 if (retval == EXT2_ET_BLOCK_ALLOC_FAIL &&
480 fix_problem(ctx, PR_3_LPF_NO_SPACE, &pctx)) {
d5da89e8 481 fix_problem(ctx, PR_3_NO_SPACE_TO_RECOVER, &pctx);
09918967
DW
482 ctx->lost_and_found = EXT2_ROOT_INO;
483 return 0;
484 }
3839e657 485 if (retval) {
1b6bf175
TT
486 pctx.errcode = retval;
487 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
3839e657
TT
488 return 0;
489 }
c5d2f50d 490 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
b729b7df 491skip_new_block:
48f23054 492 ext2fs_block_alloc_stats2(fs, blk, +1);
3839e657
TT
493
494 /*
495 * Next find a free inode.
496 */
b0700a1b 497 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700,
1b6bf175 498 ctx->inode_used_map, &ino);
09918967
DW
499 if (retval == EXT2_ET_INODE_ALLOC_FAIL &&
500 fix_problem(ctx, PR_3_LPF_NO_SPACE, &pctx)) {
d5da89e8 501 fix_problem(ctx, PR_3_NO_SPACE_TO_RECOVER, &pctx);
09918967
DW
502 ctx->lost_and_found = EXT2_ROOT_INO;
503 return 0;
504 }
3839e657 505 if (retval) {
1b6bf175
TT
506 pctx.errcode = retval;
507 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
3839e657
TT
508 return 0;
509 }
c5d2f50d
VAH
510 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
511 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
0684a4f3 512 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
3839e657 513
3839e657
TT
514 /*
515 * Set up the inode structure
516 */
517 memset(&inode, 0, sizeof(inode));
64aecc4d 518 inode.i_mode = 040700;
3839e657 519 inode.i_size = fs->blocksize;
1f3ad14a 520 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
3839e657 521 inode.i_links_count = 2;
bc1ec4b4 522 ext2fs_iblk_set(fs, EXT2_INODE(&inode), 1);
3839e657
TT
523 inode.i_block[0] = blk;
524
525 /*
526 * Next, write out the inode.
527 */
bc1ec4b4 528 pctx.errcode = ext2fs_write_new_inode(fs, ino, EXT2_INODE(&inode));
1b6bf175
TT
529 if (pctx.errcode) {
530 pctx.str = "ext2fs_write_inode";
531 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
3839e657
TT
532 return 0;
533 }
eb89a628
DW
534
535 /*
536 * Now let's create the actual data block for the inode.
537 * Due to metadata_csum, the directory block MUST be written
538 * after the inode is written to disk!
539 */
540 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
541 if (retval) {
542 pctx.errcode = retval;
543 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
544 return 0;
545 }
546
547 retval = ext2fs_write_dir_block4(fs, blk, block, 0, ino);
548 ext2fs_free_mem(&block);
549 if (retval) {
550 pctx.errcode = retval;
551 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
552 return 0;
553 }
554
3839e657
TT
555 /*
556 * Finally, create the directory link
557 */
6fdc7a32 558 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
f0770b16
DW
559 if (pctx.errcode == EXT2_ET_DIR_NO_SPACE) {
560 pctx.errcode = ext2fs_expand_dir(fs, EXT2_ROOT_INO);
561 if (pctx.errcode)
562 goto link_error;
563 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino,
564 EXT2_FT_DIR);
565 }
1b6bf175 566 if (pctx.errcode) {
f0770b16 567link_error:
1b6bf175
TT
568 pctx.str = "ext2fs_link";
569 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
3839e657
TT
570 return 0;
571 }
572
573 /*
574 * Miscellaneous bookkeeping that needs to be kept straight.
575 */
08b21301 576 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
b0700a1b 577 e2fsck_adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
1b6bf175
TT
578 ext2fs_icount_store(ctx->inode_count, ino, 2);
579 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
850d05e9 580 ctx->lost_and_found = ino;
97126931 581 quota_data_add(ctx->qctx, &inode, ino, EXT2_CLUSTER_SIZE(fs->super));
624e4a64 582 quota_data_inodes(ctx->qctx, &inode, ino, +1);
3839e657 583#if 0
f3db3566 584 printf("/lost+found created; inode #%lu\n", ino);
3839e657
TT
585#endif
586 return ino;
587}
588
589/*
590 * This routine will connect a file to lost+found
591 */
86c627ec 592int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
3839e657 593{
1b6bf175 594 ext2_filsys fs = ctx->fs;
3839e657
TT
595 errcode_t retval;
596 char name[80];
1b6bf175 597 struct problem_context pctx;
6fdc7a32
TT
598 struct ext2_inode inode;
599 int file_type = 0;
1b6bf175
TT
600
601 clear_problem_context(&pctx);
6fdc7a32 602 pctx.ino = ino;
1b6bf175 603
850d05e9
TT
604 if (!ctx->bad_lost_and_found && !ctx->lost_and_found) {
605 if (e2fsck_get_lost_and_found(ctx, 1) == 0)
606 ctx->bad_lost_and_found++;
1b6bf175 607 }
850d05e9 608 if (ctx->bad_lost_and_found) {
1b6bf175 609 fix_problem(ctx, PR_3_NO_LPF, &pctx);
3839e657
TT
610 return 1;
611 }
efc6f628 612
86c627ec 613 sprintf(name, "#%u", ino);
6fdc7a32
TT
614 if (ext2fs_read_inode(fs, ino, &inode) == 0)
615 file_type = ext2_file_type(inode.i_mode);
850d05e9 616 retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type);
3839e657 617 if (retval == EXT2_ET_DIR_NO_SPACE) {
1b6bf175 618 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
3839e657 619 return 1;
efc6f628 620 retval = e2fsck_expand_directory(ctx, ctx->lost_and_found,
850d05e9 621 1, 0);
3839e657 622 if (retval) {
1b6bf175
TT
623 pctx.errcode = retval;
624 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
3839e657
TT
625 return 1;
626 }
850d05e9
TT
627 retval = ext2fs_link(fs, ctx->lost_and_found, name,
628 ino, file_type);
3839e657
TT
629 }
630 if (retval) {
1b6bf175
TT
631 pctx.errcode = retval;
632 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
3839e657
TT
633 return 1;
634 }
b0700a1b 635 e2fsck_adjust_inode_count(ctx, ino, 1);
3839e657
TT
636
637 return 0;
638}
639
640/*
641 * Utility routine to adjust the inode counts on an inode.
642 */
b0700a1b 643errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
3839e657 644{
1b6bf175 645 ext2_filsys fs = ctx->fs;
3839e657
TT
646 errcode_t retval;
647 struct ext2_inode inode;
efc6f628 648
3839e657
TT
649 if (!ino)
650 return 0;
651
652 retval = ext2fs_read_inode(fs, ino, &inode);
653 if (retval)
654 return retval;
655
656#if 0
f3db3566 657 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
3839e657
TT
658 inode.i_links_count);
659#endif
660
21c84b71 661 if (adj == 1) {
1b6bf175 662 ext2fs_icount_increment(ctx->inode_count, ino, 0);
c1faf9cc
TT
663 if (inode.i_links_count == (__u16) ~0)
664 return 0;
1b6bf175 665 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
c1faf9cc
TT
666 inode.i_links_count++;
667 } else if (adj == -1) {
1b6bf175 668 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
c1faf9cc
TT
669 if (inode.i_links_count == 0)
670 return 0;
1b6bf175 671 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
c1faf9cc 672 inode.i_links_count--;
21c84b71 673 }
efc6f628 674
3839e657
TT
675 retval = ext2fs_write_inode(fs, ino, &inode);
676 if (retval)
677 return retval;
678
679 return 0;
680}
681
682/*
683 * Fix parent --- this routine fixes up the parent of a directory.
684 */
685struct fix_dotdot_struct {
686 ext2_filsys fs;
86c627ec 687 ext2_ino_t parent;
3839e657 688 int done;
1b6bf175 689 e2fsck_t ctx;
3839e657
TT
690};
691
692static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
54434927
TT
693 int offset EXT2FS_ATTR((unused)),
694 int blocksize EXT2FS_ATTR((unused)),
695 char *buf EXT2FS_ATTR((unused)),
54dc7ca2 696 void *priv_data)
3839e657 697{
54dc7ca2 698 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
3839e657 699 errcode_t retval;
1b6bf175 700 struct problem_context pctx;
3839e657 701
70f4632b 702 if (ext2fs_dirent_name_len(dirent) != 2)
3839e657
TT
703 return 0;
704 if (strncmp(dirent->name, "..", 2))
705 return 0;
3839e657 706
1b6bf175 707 clear_problem_context(&pctx);
efc6f628 708
b0700a1b 709 retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1);
1b6bf175
TT
710 if (retval) {
711 pctx.errcode = retval;
712 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
713 }
b0700a1b 714 retval = e2fsck_adjust_inode_count(fp->ctx, fp->parent, 1);
1b6bf175
TT
715 if (retval) {
716 pctx.errcode = retval;
717 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
718 }
3839e657 719 dirent->inode = fp->parent;
86f3b6cf 720 if (ext2fs_has_feature_filetype(fp->ctx->fs->super))
70f4632b 721 ext2fs_dirent_set_file_type(dirent, EXT2_FT_DIR);
56c8c592 722 else
70f4632b 723 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
3839e657
TT
724
725 fp->done++;
726 return DIRENT_ABORT | DIRENT_CHANGED;
727}
728
28db82a8 729static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
3839e657 730{
1b6bf175 731 ext2_filsys fs = ctx->fs;
3839e657
TT
732 errcode_t retval;
733 struct fix_dotdot_struct fp;
1b6bf175 734 struct problem_context pctx;
2e9d8391 735 int flags, will_rehash;
3839e657
TT
736
737 fp.fs = fs;
738 fp.parent = parent;
739 fp.done = 0;
1b6bf175 740 fp.ctx = ctx;
3839e657
TT
741
742#if 0
28db82a8 743 printf("Fixing '..' of inode %lu to be %lu...\n", ino, parent);
3839e657 744#endif
efc6f628 745
28db82a8
TT
746 clear_problem_context(&pctx);
747 pctx.ino = ino;
2e9d8391
DW
748 will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino);
749 if (will_rehash) {
750 flags = ctx->fs->flags;
e8548796 751 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2e9d8391 752 }
28db82a8 753 retval = ext2fs_dir_iterate(fs, ino, DIRENT_FLAG_INCLUDE_EMPTY,
3839e657 754 0, fix_dotdot_proc, &fp);
2e9d8391
DW
755 if (will_rehash)
756 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
757 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3839e657 758 if (retval || !fp.done) {
1b6bf175
TT
759 pctx.errcode = retval;
760 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
761 PR_3_FIX_PARENT_NOFIND, &pctx);
3839e657
TT
762 ext2fs_unmark_valid(fs);
763 }
28db82a8
TT
764 (void) e2fsck_dir_info_set_dotdot(ctx, ino, parent);
765 if (e2fsck_dir_info_set_parent(ctx, ino, ctx->lost_and_found))
766 fix_problem(ctx, PR_3_NO_DIRINFO, &pctx);
767
3839e657
TT
768 return;
769}
770
771/*
772 * These routines are responsible for expanding a /lost+found if it is
773 * too small.
774 */
775
776struct expand_dir_struct {
6dc64392
VAH
777 blk64_t num;
778 e2_blkcnt_t guaranteed_size;
779 blk64_t newblocks;
780 blk64_t last_block;
c1faf9cc
TT
781 errcode_t err;
782 e2fsck_t ctx;
81683c6a 783 ext2_ino_t dir;
3839e657
TT
784};
785
786static int expand_dir_proc(ext2_filsys fs,
6dc64392 787 blk64_t *blocknr,
133a56dc 788 e2_blkcnt_t blockcnt,
6dc64392 789 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 790 int ref_offset EXT2FS_ATTR((unused)),
54dc7ca2 791 void *priv_data)
3839e657 792{
54dc7ca2 793 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
c5d2f50d 794 blk64_t new_blk;
6dc64392 795 static blk64_t last_blk = 0;
3839e657
TT
796 char *block;
797 errcode_t retval;
1b6bf175
TT
798 e2fsck_t ctx;
799
800 ctx = es->ctx;
efc6f628 801
b7a00563
TT
802 if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
803 return BLOCK_ABORT;
804
805 if (blockcnt > 0)
806 es->last_block = blockcnt;
3839e657
TT
807 if (*blocknr) {
808 last_blk = *blocknr;
809 return 0;
810 }
0047255f
DW
811
812 if (blockcnt &&
813 (EXT2FS_B2C(fs, last_blk) == EXT2FS_B2C(fs, last_blk + 1)))
814 new_blk = last_blk + 1;
815 else {
816 last_blk &= ~EXT2FS_CLUSTER_MASK(fs);
817 retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map,
818 &new_blk);
819 if (retval) {
820 es->err = retval;
821 return BLOCK_ABORT;
822 }
823 es->newblocks++;
824 ext2fs_block_alloc_stats2(fs, new_blk, +1);
3839e657 825 }
0047255f
DW
826 last_blk = new_blk;
827
3839e657
TT
828 if (blockcnt > 0) {
829 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
830 if (retval) {
831 es->err = retval;
832 return BLOCK_ABORT;
833 }
b7a00563 834 es->num--;
81683c6a
DW
835 retval = ext2fs_write_dir_block4(fs, new_blk, block, 0,
836 es->dir);
08c8e319
DW
837 ext2fs_free_mem(&block);
838 } else
839 retval = ext2fs_zero_blocks2(fs, new_blk, 1, NULL, NULL);
3839e657
TT
840 if (retval) {
841 es->err = retval;
842 return BLOCK_ABORT;
843 }
3839e657 844 *blocknr = new_blk;
c5d2f50d 845 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk);
efc6f628 846
b7a00563 847 if (es->num == 0)
3839e657
TT
848 return (BLOCK_CHANGED | BLOCK_ABORT);
849 else
850 return BLOCK_CHANGED;
851}
852
b7a00563
TT
853errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
854 int num, int guaranteed_size)
3839e657 855{
1b6bf175 856 ext2_filsys fs = ctx->fs;
3839e657
TT
857 errcode_t retval;
858 struct expand_dir_struct es;
bc1ec4b4 859 struct ext2_inode_large inode;
c4c9bc59 860 blk64_t sz;
efc6f628 861
3839e657
TT
862 if (!(fs->flags & EXT2_FLAG_RW))
863 return EXT2_ET_RO_FILSYS;
864
b8647faa
TT
865 /*
866 * Read the inode and block bitmaps in; we'll be messing with
867 * them.
868 */
869 e2fsck_read_bitmaps(ctx);
c1faf9cc 870
3839e657
TT
871 retval = ext2fs_check_directory(fs, dir);
872 if (retval)
873 return retval;
efc6f628 874
b7a00563
TT
875 es.num = num;
876 es.guaranteed_size = guaranteed_size;
877 es.last_block = 0;
3839e657 878 es.err = 0;
c1faf9cc 879 es.newblocks = 0;
1b6bf175 880 es.ctx = ctx;
81683c6a 881 es.dir = dir;
efc6f628 882
6dc64392 883 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND,
133a56dc 884 0, expand_dir_proc, &es);
3839e657
TT
885
886 if (es.err)
887 return es.err;
3839e657
TT
888
889 /*
890 * Update the size and block count fields in the inode.
891 */
bc1ec4b4
TT
892 retval = ext2fs_read_inode_full(fs, dir,
893 EXT2_INODE(&inode), sizeof(inode));
3839e657
TT
894 if (retval)
895 return retval;
efc6f628 896
4dbfd79d 897 sz = (es.last_block + 1) * fs->blocksize;
bc1ec4b4 898 retval = ext2fs_inode_size_set(fs, EXT2_INODE(&inode), sz);
97c607b1
DW
899 if (retval)
900 return retval;
bc1ec4b4 901 ext2fs_iblk_add_blocks(fs, EXT2_INODE(&inode), es.newblocks);
97126931
EW
902 quota_data_add(ctx->qctx, &inode, dir,
903 es.newblocks * EXT2_CLUSTER_SIZE(fs->super));
3839e657 904
bc1ec4b4
TT
905 e2fsck_write_inode_full(ctx, dir, EXT2_INODE(&inode),
906 sizeof(inode), "expand_directory");
3839e657
TT
907
908 return 0;
909}
b7a00563 910