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