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