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