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