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