]> 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 goto abort_exit;
105 if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
106 goto abort_exit;
107 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dir->ino))
108 if (check_directory(ctx, dir->ino, &pctx))
109 goto abort_exit;
110 }
111
112 /*
113 * Force the creation of /lost+found if not present
114 */
115 if ((ctx->options & E2F_OPT_READONLY) == 0)
116 e2fsck_get_lost_and_found(ctx, 1);
117
118 /*
119 * If there are any directories that need to be indexed or
120 * optimized, do it here.
121 */
122 e2fsck_rehash_directories(ctx);
123
124 abort_exit:
125 if (iter)
126 e2fsck_dir_info_iter_end(ctx, iter);
127 e2fsck_free_dir_info(ctx);
128 if (inode_loop_detect) {
129 ext2fs_free_inode_bitmap(inode_loop_detect);
130 inode_loop_detect = 0;
131 }
132 if (inode_done_map) {
133 ext2fs_free_inode_bitmap(inode_done_map);
134 inode_done_map = 0;
135 }
136
137 print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io);
138 }
139
140 /*
141 * This makes sure the root inode is present; if not, we ask if the
142 * user wants us to create it. Not creating it is a fatal error.
143 */
144 static void check_root(e2fsck_t ctx)
145 {
146 ext2_filsys fs = ctx->fs;
147 blk64_t blk;
148 struct ext2_inode inode;
149 char * block;
150 struct problem_context pctx;
151
152 clear_problem_context(&pctx);
153
154 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO)) {
155 /*
156 * If the root inode is not a directory, die here. The
157 * user must have answered 'no' in pass1 when we
158 * offered to clear it.
159 */
160 if (!(ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
161 EXT2_ROOT_INO))) {
162 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
163 ctx->flags |= E2F_FLAG_ABORT;
164 }
165 return;
166 }
167
168 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
169 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
170 ctx->flags |= E2F_FLAG_ABORT;
171 return;
172 }
173
174 e2fsck_read_bitmaps(ctx);
175
176 /*
177 * First, find a free block
178 */
179 pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
180 if (pctx.errcode) {
181 pctx.str = "ext2fs_new_block";
182 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
183 ctx->flags |= E2F_FLAG_ABORT;
184 return;
185 }
186 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
187 ext2fs_mark_block_bitmap2(fs->block_map, blk);
188 ext2fs_mark_bb_dirty(fs);
189
190 /*
191 * Now let's create the actual data block for the inode
192 */
193 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
194 &block);
195 if (pctx.errcode) {
196 pctx.str = "ext2fs_new_dir_block";
197 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
198 ctx->flags |= E2F_FLAG_ABORT;
199 return;
200 }
201
202 pctx.errcode = ext2fs_write_dir_block4(fs, blk, block, 0,
203 EXT2_ROOT_INO);
204 if (pctx.errcode) {
205 pctx.str = "ext2fs_write_dir_block4";
206 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
207 ctx->flags |= E2F_FLAG_ABORT;
208 return;
209 }
210 ext2fs_free_mem(&block);
211
212 /*
213 * Set up the inode structure
214 */
215 memset(&inode, 0, sizeof(inode));
216 inode.i_mode = 040755;
217 inode.i_size = fs->blocksize;
218 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
219 inode.i_links_count = 2;
220 ext2fs_iblk_set(fs, &inode, 1);
221 inode.i_block[0] = blk;
222
223 /*
224 * Write out the inode.
225 */
226 pctx.errcode = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
227 if (pctx.errcode) {
228 pctx.str = "ext2fs_write_inode";
229 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
230 ctx->flags |= E2F_FLAG_ABORT;
231 return;
232 }
233
234 /*
235 * Miscellaneous bookkeeping...
236 */
237 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
238 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
239 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
240
241 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO);
242 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, EXT2_ROOT_INO);
243 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_ROOT_INO);
244 ext2fs_mark_ib_dirty(fs);
245 }
246
247 /*
248 * This subroutine is responsible for making sure that a particular
249 * directory is connected to the root; if it isn't we trace it up as
250 * far as we can go, and then offer to connect the resulting parent to
251 * the lost+found. We have to do loop detection; if we ever discover
252 * a loop, we treat that as a disconnected directory and offer to
253 * reparent it to lost+found.
254 *
255 * However, loop detection is expensive, because for very large
256 * filesystems, the inode_loop_detect bitmap is huge, and clearing it
257 * is non-trivial. Loops in filesystems are also a rare error case,
258 * and we shouldn't optimize for error cases. So we try two passes of
259 * the algorithm. The first time, we ignore loop detection and merely
260 * increment a counter; if the counter exceeds some extreme threshold,
261 * then we try again with the loop detection bitmap enabled.
262 */
263 static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
264 struct problem_context *pctx)
265 {
266 ext2_filsys fs = ctx->fs;
267 ext2_ino_t ino = dir, parent;
268 int loop_pass = 0, parent_count = 0;
269
270 while (1) {
271 /*
272 * Mark this inode as being "done"; by the time we
273 * return from this function, the inode we either be
274 * verified as being connected to the directory tree,
275 * or we will have offered to reconnect this to
276 * lost+found.
277 *
278 * If it was marked done already, then we've reached a
279 * parent we've already checked.
280 */
281 if (ext2fs_mark_inode_bitmap2(inode_done_map, ino))
282 break;
283
284 if (e2fsck_dir_info_get_parent(ctx, ino, &parent)) {
285 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
286 return 0;
287 }
288
289 /*
290 * If this directory doesn't have a parent, or we've
291 * seen the parent once already, then offer to
292 * reparent it to lost+found
293 */
294 if (!parent ||
295 (loop_pass &&
296 (ext2fs_test_inode_bitmap2(inode_loop_detect,
297 parent)))) {
298 pctx->ino = ino;
299 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
300 if (e2fsck_reconnect_file(ctx, pctx->ino))
301 ext2fs_unmark_valid(fs);
302 else {
303 fix_dotdot(ctx, pctx->ino,
304 ctx->lost_and_found);
305 parent = ctx->lost_and_found;
306 }
307 }
308 break;
309 }
310 ino = parent;
311 if (loop_pass) {
312 ext2fs_mark_inode_bitmap2(inode_loop_detect, ino);
313 } else if (parent_count++ > 2048) {
314 /*
315 * If we've run into a path depth that's
316 * greater than 2048, try again with the inode
317 * loop bitmap turned on and start from the
318 * top.
319 */
320 loop_pass = 1;
321 if (inode_loop_detect)
322 ext2fs_clear_inode_bitmap(inode_loop_detect);
323 else {
324 pctx->errcode = e2fsck_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), EXT2FS_BMAP64_AUTODIR, "inode_loop_detect", &inode_loop_detect);
325 if (pctx->errcode) {
326 pctx->num = 1;
327 fix_problem(ctx,
328 PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
329 ctx->flags |= E2F_FLAG_ABORT;
330 return -1;
331 }
332 }
333 ino = dir;
334 }
335 }
336
337 /*
338 * Make sure that .. and the parent directory are the same;
339 * offer to fix it if not.
340 */
341 pctx->ino = dir;
342 if (e2fsck_dir_info_get_dotdot(ctx, dir, &pctx->ino2) ||
343 e2fsck_dir_info_get_parent(ctx, dir, &pctx->dir)) {
344 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
345 return 0;
346 }
347 if (pctx->ino2 != pctx->dir) {
348 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
349 fix_dotdot(ctx, dir, pctx->dir);
350 }
351 return 0;
352 }
353
354 /*
355 * This routine gets the lost_and_found inode, making it a directory
356 * if necessary
357 */
358 ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
359 {
360 ext2_filsys fs = ctx->fs;
361 ext2_ino_t ino;
362 blk64_t blk;
363 errcode_t retval;
364 struct ext2_inode inode;
365 char * block;
366 static const char name[] = "lost+found";
367 struct problem_context pctx;
368
369 if (ctx->lost_and_found)
370 return ctx->lost_and_found;
371
372 clear_problem_context(&pctx);
373
374 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
375 sizeof(name)-1, 0, &ino);
376 if (retval && !fix)
377 return 0;
378 if (!retval) {
379 /* Lost+found shouldn't have inline data */
380 retval = ext2fs_read_inode(fs, ino, &inode);
381 if (fix && retval)
382 return 0;
383
384 if (fix && (inode.i_flags & EXT4_INLINE_DATA_FL)) {
385 if (!fix_problem(ctx, PR_3_LPF_INLINE_DATA, &pctx))
386 return 0;
387 goto unlink;
388 }
389
390 if (ext2fs_check_directory(fs, ino) == 0) {
391 ctx->lost_and_found = ino;
392 return ino;
393 }
394
395 /* Lost+found isn't a directory! */
396 if (!fix)
397 return 0;
398 pctx.ino = ino;
399 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
400 return 0;
401
402 unlink:
403 /* OK, unlink the old /lost+found file. */
404 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
405 if (pctx.errcode) {
406 pctx.str = "ext2fs_unlink";
407 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
408 return 0;
409 }
410 (void) e2fsck_dir_info_set_parent(ctx, ino, 0);
411 e2fsck_adjust_inode_count(ctx, ino, -1);
412 } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
413 pctx.errcode = retval;
414 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
415 }
416 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
417 return 0;
418
419 /*
420 * Read the inode and block bitmaps in; we'll be messing with
421 * them.
422 */
423 e2fsck_read_bitmaps(ctx);
424
425 /*
426 * First, find a free block
427 */
428 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
429 if (retval) {
430 pctx.errcode = retval;
431 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
432 return 0;
433 }
434 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
435 ext2fs_block_alloc_stats2(fs, blk, +1);
436
437 /*
438 * Next find a free inode.
439 */
440 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700,
441 ctx->inode_used_map, &ino);
442 if (retval) {
443 pctx.errcode = retval;
444 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
445 return 0;
446 }
447 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
448 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
449 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
450
451 /*
452 * Now let's create the actual data block for the inode
453 */
454 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
455 if (retval) {
456 pctx.errcode = retval;
457 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
458 return 0;
459 }
460
461 retval = ext2fs_write_dir_block4(fs, blk, block, 0, ino);
462 ext2fs_free_mem(&block);
463 if (retval) {
464 pctx.errcode = retval;
465 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
466 return 0;
467 }
468
469 /*
470 * Set up the inode structure
471 */
472 memset(&inode, 0, sizeof(inode));
473 inode.i_mode = 040700;
474 inode.i_size = fs->blocksize;
475 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
476 inode.i_links_count = 2;
477 ext2fs_iblk_set(fs, &inode, 1);
478 inode.i_block[0] = blk;
479
480 /*
481 * Next, write out the inode.
482 */
483 pctx.errcode = ext2fs_write_new_inode(fs, ino, &inode);
484 if (pctx.errcode) {
485 pctx.str = "ext2fs_write_inode";
486 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
487 return 0;
488 }
489 /*
490 * Finally, create the directory link
491 */
492 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
493 if (pctx.errcode) {
494 pctx.str = "ext2fs_link";
495 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
496 return 0;
497 }
498
499 /*
500 * Miscellaneous bookkeeping that needs to be kept straight.
501 */
502 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
503 e2fsck_adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
504 ext2fs_icount_store(ctx->inode_count, ino, 2);
505 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
506 ctx->lost_and_found = ino;
507 quota_data_add(ctx->qctx, &inode, ino, fs->blocksize);
508 quota_data_inodes(ctx->qctx, &inode, ino, +1);
509 #if 0
510 printf("/lost+found created; inode #%lu\n", ino);
511 #endif
512 return ino;
513 }
514
515 /*
516 * This routine will connect a file to lost+found
517 */
518 int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
519 {
520 ext2_filsys fs = ctx->fs;
521 errcode_t retval;
522 char name[80];
523 struct problem_context pctx;
524 struct ext2_inode inode;
525 int file_type = 0;
526
527 clear_problem_context(&pctx);
528 pctx.ino = ino;
529
530 if (!ctx->bad_lost_and_found && !ctx->lost_and_found) {
531 if (e2fsck_get_lost_and_found(ctx, 1) == 0)
532 ctx->bad_lost_and_found++;
533 }
534 if (ctx->bad_lost_and_found) {
535 fix_problem(ctx, PR_3_NO_LPF, &pctx);
536 return 1;
537 }
538
539 sprintf(name, "#%u", ino);
540 if (ext2fs_read_inode(fs, ino, &inode) == 0)
541 file_type = ext2_file_type(inode.i_mode);
542 retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type);
543 if (retval == EXT2_ET_DIR_NO_SPACE) {
544 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
545 return 1;
546 retval = e2fsck_expand_directory(ctx, ctx->lost_and_found,
547 1, 0);
548 if (retval) {
549 pctx.errcode = retval;
550 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
551 return 1;
552 }
553 retval = ext2fs_link(fs, ctx->lost_and_found, name,
554 ino, file_type);
555 }
556 if (retval) {
557 pctx.errcode = retval;
558 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
559 return 1;
560 }
561 e2fsck_adjust_inode_count(ctx, ino, 1);
562
563 return 0;
564 }
565
566 /*
567 * Utility routine to adjust the inode counts on an inode.
568 */
569 errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
570 {
571 ext2_filsys fs = ctx->fs;
572 errcode_t retval;
573 struct ext2_inode inode;
574
575 if (!ino)
576 return 0;
577
578 retval = ext2fs_read_inode(fs, ino, &inode);
579 if (retval)
580 return retval;
581
582 #if 0
583 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
584 inode.i_links_count);
585 #endif
586
587 if (adj == 1) {
588 ext2fs_icount_increment(ctx->inode_count, ino, 0);
589 if (inode.i_links_count == (__u16) ~0)
590 return 0;
591 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
592 inode.i_links_count++;
593 } else if (adj == -1) {
594 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
595 if (inode.i_links_count == 0)
596 return 0;
597 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
598 inode.i_links_count--;
599 }
600
601 retval = ext2fs_write_inode(fs, ino, &inode);
602 if (retval)
603 return retval;
604
605 return 0;
606 }
607
608 /*
609 * Fix parent --- this routine fixes up the parent of a directory.
610 */
611 struct fix_dotdot_struct {
612 ext2_filsys fs;
613 ext2_ino_t parent;
614 int done;
615 e2fsck_t ctx;
616 };
617
618 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
619 int offset EXT2FS_ATTR((unused)),
620 int blocksize EXT2FS_ATTR((unused)),
621 char *buf EXT2FS_ATTR((unused)),
622 void *priv_data)
623 {
624 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
625 errcode_t retval;
626 struct problem_context pctx;
627
628 if (ext2fs_dirent_name_len(dirent) != 2)
629 return 0;
630 if (strncmp(dirent->name, "..", 2))
631 return 0;
632
633 clear_problem_context(&pctx);
634
635 retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1);
636 if (retval) {
637 pctx.errcode = retval;
638 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
639 }
640 retval = e2fsck_adjust_inode_count(fp->ctx, fp->parent, 1);
641 if (retval) {
642 pctx.errcode = retval;
643 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
644 }
645 dirent->inode = fp->parent;
646 if (fp->ctx->fs->super->s_feature_incompat &
647 EXT2_FEATURE_INCOMPAT_FILETYPE)
648 ext2fs_dirent_set_file_type(dirent, EXT2_FT_DIR);
649 else
650 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
651
652 fp->done++;
653 return DIRENT_ABORT | DIRENT_CHANGED;
654 }
655
656 static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
657 {
658 ext2_filsys fs = ctx->fs;
659 errcode_t retval;
660 struct fix_dotdot_struct fp;
661 struct problem_context pctx;
662
663 fp.fs = fs;
664 fp.parent = parent;
665 fp.done = 0;
666 fp.ctx = ctx;
667
668 #if 0
669 printf("Fixing '..' of inode %lu to be %lu...\n", ino, parent);
670 #endif
671
672 clear_problem_context(&pctx);
673 pctx.ino = ino;
674 if (e2fsck_dir_will_be_rehashed(ctx, ino))
675 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
676 retval = ext2fs_dir_iterate(fs, ino, DIRENT_FLAG_INCLUDE_EMPTY,
677 0, fix_dotdot_proc, &fp);
678 if (e2fsck_dir_will_be_rehashed(ctx, ino))
679 ctx->fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
680 if (retval || !fp.done) {
681 pctx.errcode = retval;
682 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
683 PR_3_FIX_PARENT_NOFIND, &pctx);
684 ext2fs_unmark_valid(fs);
685 }
686 (void) e2fsck_dir_info_set_dotdot(ctx, ino, parent);
687 if (e2fsck_dir_info_set_parent(ctx, ino, ctx->lost_and_found))
688 fix_problem(ctx, PR_3_NO_DIRINFO, &pctx);
689
690 return;
691 }
692
693 /*
694 * These routines are responsible for expanding a /lost+found if it is
695 * too small.
696 */
697
698 struct expand_dir_struct {
699 blk64_t num;
700 e2_blkcnt_t guaranteed_size;
701 blk64_t newblocks;
702 blk64_t last_block;
703 errcode_t err;
704 e2fsck_t ctx;
705 ext2_ino_t dir;
706 };
707
708 static int expand_dir_proc(ext2_filsys fs,
709 blk64_t *blocknr,
710 e2_blkcnt_t blockcnt,
711 blk64_t ref_block EXT2FS_ATTR((unused)),
712 int ref_offset EXT2FS_ATTR((unused)),
713 void *priv_data)
714 {
715 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
716 blk64_t new_blk;
717 static blk64_t last_blk = 0;
718 char *block;
719 errcode_t retval;
720 e2fsck_t ctx;
721
722 ctx = es->ctx;
723
724 if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
725 return BLOCK_ABORT;
726
727 if (blockcnt > 0)
728 es->last_block = blockcnt;
729 if (*blocknr) {
730 last_blk = *blocknr;
731 return 0;
732 }
733
734 if (blockcnt &&
735 (EXT2FS_B2C(fs, last_blk) == EXT2FS_B2C(fs, last_blk + 1)))
736 new_blk = last_blk + 1;
737 else {
738 last_blk &= ~EXT2FS_CLUSTER_MASK(fs);
739 retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map,
740 &new_blk);
741 if (retval) {
742 es->err = retval;
743 return BLOCK_ABORT;
744 }
745 es->newblocks++;
746 ext2fs_block_alloc_stats2(fs, new_blk, +1);
747 }
748 last_blk = new_blk;
749
750 if (blockcnt > 0) {
751 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
752 if (retval) {
753 es->err = retval;
754 return BLOCK_ABORT;
755 }
756 es->num--;
757 retval = ext2fs_write_dir_block4(fs, new_blk, block, 0,
758 es->dir);
759 } else {
760 retval = ext2fs_get_mem(fs->blocksize, &block);
761 if (retval) {
762 es->err = retval;
763 return BLOCK_ABORT;
764 }
765 memset(block, 0, fs->blocksize);
766 retval = io_channel_write_blk64(fs->io, new_blk, 1, block);
767 }
768 if (retval) {
769 es->err = retval;
770 return BLOCK_ABORT;
771 }
772 ext2fs_free_mem(&block);
773 *blocknr = new_blk;
774 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk);
775
776 if (es->num == 0)
777 return (BLOCK_CHANGED | BLOCK_ABORT);
778 else
779 return BLOCK_CHANGED;
780 }
781
782 /*
783 * Ensure that all blocks are marked in the block_found_map, since it's
784 * possible that the library allocated an extent node block or a block map
785 * block during the directory rebuilding; these new allocations are not
786 * captured in block_found_map. This is bad since we could later use
787 * block_found_map to allocate more blocks.
788 */
789 static int find_new_blocks_proc(ext2_filsys fs,
790 blk64_t *blocknr,
791 e2_blkcnt_t blockcnt,
792 blk64_t ref_block EXT2FS_ATTR((unused)),
793 int ref_offset EXT2FS_ATTR((unused)),
794 void *priv_data)
795 {
796 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
797 e2fsck_t ctx = es->ctx;
798
799 ext2fs_mark_block_bitmap2(ctx->block_found_map, *blocknr);
800 return 0;
801 }
802
803 errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
804 int num, int guaranteed_size)
805 {
806 ext2_filsys fs = ctx->fs;
807 errcode_t retval;
808 struct expand_dir_struct es;
809 struct ext2_inode inode;
810 blk64_t sz, before, after;
811
812 if (!(fs->flags & EXT2_FLAG_RW))
813 return EXT2_ET_RO_FILSYS;
814
815 /*
816 * Read the inode and block bitmaps in; we'll be messing with
817 * them.
818 */
819 e2fsck_read_bitmaps(ctx);
820
821 retval = ext2fs_check_directory(fs, dir);
822 if (retval)
823 return retval;
824
825 es.num = num;
826 es.guaranteed_size = guaranteed_size;
827 es.last_block = 0;
828 es.err = 0;
829 es.newblocks = 0;
830 es.ctx = ctx;
831 es.dir = dir;
832
833 before = ext2fs_free_blocks_count(fs->super);
834 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND,
835 0, expand_dir_proc, &es);
836
837 if (es.err)
838 return es.err;
839 after = ext2fs_free_blocks_count(fs->super);
840
841 /*
842 * If the free block count has dropped by more than the blocks we
843 * allocated ourselves, then we must've allocated some extent/map
844 * blocks. Therefore, we must iterate this dir's blocks again to
845 * ensure that all newly allocated blocks are captured in
846 * block_found_map.
847 */
848 if ((before - after) > es.newblocks) {
849 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY,
850 0, find_new_blocks_proc, &es);
851 if (es.err)
852 return es.err;
853 }
854
855 /*
856 * Update the size and block count fields in the inode.
857 */
858 retval = ext2fs_read_inode(fs, dir, &inode);
859 if (retval)
860 return retval;
861
862 sz = (es.last_block + 1) * fs->blocksize;
863 inode.i_size = sz;
864 inode.i_size_high = sz >> 32;
865 ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
866 quota_data_add(ctx->qctx, &inode, dir, es.newblocks * fs->blocksize);
867
868 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
869
870 return 0;
871 }
872