blob: fb217273f6d56974b9fb1d616e8a9082856e74c1 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
3 *
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +00004 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
Theodore Ts'o21c84b71997-04-29 16:15:03 +00005 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'oefc6f622008-08-27 23:07:54 -040010 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000011 * 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".
Theodore Ts'oefc6f622008-08-27 23:07:54 -040017 *
Theodore Ts'o3839e651997-04-26 13:21:57 +000018 * 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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -040027 *
Theodore Ts'o08b21301997-11-03 19:42:40 +000028 * 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.
Theodore Ts'o3839e651997-04-26 13:21:57 +000032 *
33 * Pass 3 frees the following data structures:
34 * - The dirinfo directory information cache.
35 */
36
Theodore Ts'od1154eb2011-09-18 17:34:37 -040037#include "config.h"
Theodore Ts'o50e1e101997-04-26 13:58:21 +000038#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000041
42#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000043#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000044
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000045static void check_root(e2fsck_t ctx);
Theodore Ts'o28db82a2007-04-04 22:33:31 -040046static int check_directory(e2fsck_t ctx, ext2_ino_t ino,
Theodore Ts'o28ffafb2000-02-08 19:14:02 +000047 struct problem_context *pctx);
Theodore Ts'o28db82a2007-04-04 22:33:31 -040048static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000050static ext2fs_inode_bitmap inode_loop_detect = 0;
51static ext2fs_inode_bitmap inode_done_map = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040052
Theodore Ts'o08b21301997-11-03 19:42:40 +000053void e2fsck_pass3(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000054{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000055 ext2_filsys fs = ctx->fs;
Darrick J. Wongf0131bd2013-12-12 12:57:48 -050056 struct dir_info_iter *iter = NULL;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000057#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000058 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000059#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000060 struct problem_context pctx;
61 struct dir_info *dir;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000062 unsigned long maxdirs, count;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000063
Theodore Ts'o6d96b002007-08-03 20:07:09 -040064 init_resource_track(&rtrack, ctx->fs->io);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000065 clear_problem_context(&pctx);
66
Theodore Ts'o3839e651997-04-26 13:21:57 +000067#ifdef MTRACE
68 mtrace_print("Pass 3");
69#endif
70
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000071 if (!(ctx->options & E2F_OPT_PREEN))
72 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +000073
74 /*
75 * Allocate some bitmaps to do loop detection.
76 */
Theodore Ts'o830b44f2011-12-16 14:55:50 -050077 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("inode done bitmap"),
78 EXT2FS_BMAP64_AUTODIR,
79 "inode_done_map", &inode_done_map);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000080 if (pctx.errcode) {
81 pctx.num = 2;
82 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000083 ctx->flags |= E2F_FLAG_ABORT;
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000084 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 }
Ken Chen9facd072009-05-28 09:55:10 -040086 print_resource_track(ctx, _("Peak memory"), &ctx->global_rtrack, NULL);
Theodore Ts'o3839e651997-04-26 13:21:57 +000087
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000088 check_root(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +000089 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
90 goto abort_exit;
Theodore Ts'o08b21301997-11-03 19:42:40 +000091
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -040092 ext2fs_mark_inode_bitmap2(inode_done_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +000093
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000094 maxdirs = e2fsck_get_num_dirinfo(ctx);
Theodore Ts'of75c28d1998-08-01 04:18:06 +000095 count = 1;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000096
Theodore Ts'of75c28d1998-08-01 04:18:06 +000097 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000098 if ((ctx->progress)(ctx, 3, 0, maxdirs))
Theodore Ts'of75c28d1998-08-01 04:18:06 +000099 goto abort_exit;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400100
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400101 iter = e2fsck_dir_info_iter_begin(ctx);
102 while ((dir = e2fsck_dir_info_iter(ctx, iter)) != 0) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400103 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;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400107 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dir->ino))
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400108 if (check_directory(ctx, dir->ino, &pctx))
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000109 goto abort_exit;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 }
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000111
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000112 /*
113 * Force the creation of /lost+found if not present
114 */
Johan Erlandssone9a8c0c2013-12-19 13:55:51 -0500115 if ((ctx->options & E2F_OPT_READONLY) == 0)
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400116 e2fsck_get_lost_and_found(ctx, 1);
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000117
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400118 /*
119 * If there are any directories that need to be indexed or
120 * optimized, do it here.
121 */
122 e2fsck_rehash_directories(ctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400123
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000124abort_exit:
Darrick J. Wongf0131bd2013-12-12 12:57:48 -0500125 if (iter)
126 e2fsck_dir_info_iter_end(ctx, iter);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000127 e2fsck_free_dir_info(ctx);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000128 if (inode_loop_detect) {
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000129 ext2fs_free_inode_bitmap(inode_loop_detect);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000130 inode_loop_detect = 0;
131 }
132 if (inode_done_map) {
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000133 ext2fs_free_inode_bitmap(inode_done_map);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000134 inode_done_map = 0;
135 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400136
Ken Chen9facd072009-05-28 09:55:10 -0400137 print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138}
139
140/*
141 * This makes sure the root inode is present; if not, we ask if the
142 * user wants us to create it. Not creating it is a fatal error.
143 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000144static void check_root(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000145{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000146 ext2_filsys fs = ctx->fs;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400147 blk64_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148 struct ext2_inode inode;
149 char * block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000150 struct problem_context pctx;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400151
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000152 clear_problem_context(&pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400153
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400154 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 /*
Theodore Ts'o08b21301997-11-03 19:42:40 +0000156 * If the root inode is not a directory, die here. The
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 * user must have answered 'no' in pass1 when we
158 * offered to clear it.
159 */
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400160 if (!(ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000161 EXT2_ROOT_INO))) {
162 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
163 ctx->flags |= E2F_FLAG_ABORT;
164 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 return;
166 }
167
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000168 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
169 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
170 ctx->flags |= E2F_FLAG_ABORT;
171 return;
172 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000174 e2fsck_read_bitmaps(ctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400175
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176 /*
177 * First, find a free block
178 */
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400179 pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000180 if (pctx.errcode) {
181 pctx.str = "ext2fs_new_block";
182 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000183 ctx->flags |= E2F_FLAG_ABORT;
184 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000185 }
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400186 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
187 ext2fs_mark_block_bitmap2(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000188 ext2fs_mark_bb_dirty(fs);
189
190 /*
191 * Now let's create the actual data block for the inode
192 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000193 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
194 &block);
195 if (pctx.errcode) {
196 pctx.str = "ext2fs_new_dir_block";
197 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000198 ctx->flags |= E2F_FLAG_ABORT;
199 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 }
201
Kit Westneat7bef6d52013-12-02 19:11:52 -0500202 pctx.errcode = ext2fs_write_dir_block3(fs, blk, block, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000203 if (pctx.errcode) {
Kit Westneat7bef6d52013-12-02 19:11:52 -0500204 pctx.str = "ext2fs_write_dir_block3";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000205 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000206 ctx->flags |= E2F_FLAG_ABORT;
207 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 }
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400209 ext2fs_free_mem(&block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210
211 /*
212 * Set up the inode structure
213 */
214 memset(&inode, 0, sizeof(inode));
215 inode.i_mode = 040755;
216 inode.i_size = fs->blocksize;
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400217 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218 inode.i_links_count = 2;
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400219 ext2fs_iblk_set(fs, &inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 inode.i_block[0] = blk;
221
222 /*
223 * Write out the inode.
224 */
Theodore Ts'o030970e2005-03-20 20:05:22 -0500225 pctx.errcode = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000226 if (pctx.errcode) {
227 pctx.str = "ext2fs_write_inode";
228 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000229 ctx->flags |= E2F_FLAG_ABORT;
230 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000231 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400232
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 /*
234 * Miscellaneous bookkeeping...
235 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000236 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000237 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
238 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400240 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO);
241 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, EXT2_ROOT_INO);
242 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_ROOT_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 ext2fs_mark_ib_dirty(fs);
244}
245
246/*
247 * This subroutine is responsible for making sure that a particular
248 * directory is connected to the root; if it isn't we trace it up as
249 * far as we can go, and then offer to connect the resulting parent to
250 * the lost+found. We have to do loop detection; if we ever discover
251 * a loop, we treat that as a disconnected directory and offer to
252 * reparent it to lost+found.
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400253 *
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000254 * However, loop detection is expensive, because for very large
255 * filesystems, the inode_loop_detect bitmap is huge, and clearing it
256 * is non-trivial. Loops in filesystems are also a rare error case,
257 * and we shouldn't optimize for error cases. So we try two passes of
258 * the algorithm. The first time, we ignore loop detection and merely
259 * increment a counter; if the counter exceeds some extreme threshold,
260 * then we try again with the loop detection bitmap enabled.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261 */
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400262static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000263 struct problem_context *pctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264{
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000265 ext2_filsys fs = ctx->fs;
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400266 ext2_ino_t ino = dir, parent;
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000267 int loop_pass = 0, parent_count = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000269 while (1) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 /*
271 * Mark this inode as being "done"; by the time we
272 * return from this function, the inode we either be
273 * verified as being connected to the directory tree,
274 * or we will have offered to reconnect this to
275 * lost+found.
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000276 *
277 * If it was marked done already, then we've reached a
278 * parent we've already checked.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 */
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400280 if (ext2fs_mark_inode_bitmap2(inode_done_map, ino))
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000281 break;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000282
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400283 if (e2fsck_dir_info_get_parent(ctx, ino, &parent)) {
284 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
285 return 0;
286 }
287
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288 /*
289 * If this directory doesn't have a parent, or we've
290 * seen the parent once already, then offer to
291 * reparent it to lost+found
292 */
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400293 if (!parent ||
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400294 (loop_pass &&
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400295 (ext2fs_test_inode_bitmap2(inode_loop_detect,
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400296 parent)))) {
297 pctx->ino = ino;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000298 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
Theodore Ts'o2e5fcce2003-12-12 03:00:56 -0500299 if (e2fsck_reconnect_file(ctx, pctx->ino))
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000300 ext2fs_unmark_valid(fs);
301 else {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400302 fix_dotdot(ctx, pctx->ino,
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400303 ctx->lost_and_found);
304 parent = ctx->lost_and_found;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000305 }
306 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 break;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000308 }
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400309 ino = parent;
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000310 if (loop_pass) {
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400311 ext2fs_mark_inode_bitmap2(inode_loop_detect, ino);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000312 } else if (parent_count++ > 2048) {
313 /*
314 * If we've run into a path depth that's
315 * greater than 2048, try again with the inode
316 * loop bitmap turned on and start from the
317 * top.
318 */
319 loop_pass = 1;
320 if (inode_loop_detect)
321 ext2fs_clear_inode_bitmap(inode_loop_detect);
322 else {
Theodore Ts'o830b44f2011-12-16 14:55:50 -0500323 pctx->errcode = e2fsck_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), EXT2FS_BMAP64_AUTODIR, "inode_loop_detect", &inode_loop_detect);
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000324 if (pctx->errcode) {
325 pctx->num = 1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400326 fix_problem(ctx,
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000327 PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
328 ctx->flags |= E2F_FLAG_ABORT;
329 return -1;
330 }
331 }
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400332 ino = dir;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000333 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000334 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335
336 /*
337 * Make sure that .. and the parent directory are the same;
338 * offer to fix it if not.
339 */
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400340 pctx->ino = dir;
341 if (e2fsck_dir_info_get_dotdot(ctx, dir, &pctx->ino2) ||
342 e2fsck_dir_info_get_parent(ctx, dir, &pctx->dir)) {
343 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
344 return 0;
345 }
346 if (pctx->ino2 != pctx->dir) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000347 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400348 fix_dotdot(ctx, dir, pctx->dir);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349 }
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000350 return 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400351}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352
353/*
354 * This routine gets the lost_and_found inode, making it a directory
355 * if necessary
356 */
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400357ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000358{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000359 ext2_filsys fs = ctx->fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000360 ext2_ino_t ino;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400361 blk64_t blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000362 errcode_t retval;
363 struct ext2_inode inode;
364 char * block;
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000365 static const char name[] = "lost+found";
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000366 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000367
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400368 if (ctx->lost_and_found)
369 return ctx->lost_and_found;
370
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000371 clear_problem_context(&pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400372
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000373 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
374 sizeof(name)-1, 0, &ino);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400375 if (retval && !fix)
376 return 0;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000377 if (!retval) {
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400378 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino)) {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400379 ctx->lost_and_found = ino;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000380 return ino;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400381 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400382
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000383 /* Lost+found isn't a directory! */
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400384 if (!fix)
385 return 0;
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000386 pctx.ino = ino;
387 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
388 return 0;
389
Theodore Ts'oc54b3c31999-07-03 07:20:06 +0000390 /* OK, unlink the old /lost+found file. */
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000391 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
392 if (pctx.errcode) {
393 pctx.str = "ext2fs_unlink";
394 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
395 return 0;
396 }
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400397 (void) e2fsck_dir_info_set_parent(ctx, ino, 0);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500398 e2fsck_adjust_inode_count(ctx, ino, -1);
Theodore Ts'o4a9f5931999-03-16 19:32:52 +0000399 } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000400 pctx.errcode = retval;
401 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
402 }
403 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000404 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405
406 /*
407 * Read the inode and block bitmaps in; we'll be messing with
408 * them.
409 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000410 e2fsck_read_bitmaps(ctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400411
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 /*
413 * First, find a free block
414 */
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400415 retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000417 pctx.errcode = retval;
418 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 return 0;
420 }
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400421 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
Valerie Aurora Henson48f23052009-10-25 21:46:58 -0400422 ext2fs_block_alloc_stats2(fs, blk, +1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423
424 /*
425 * Next find a free inode.
426 */
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500427 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000428 ctx->inode_used_map, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000430 pctx.errcode = retval;
431 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 return 0;
433 }
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400434 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
435 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400436 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437
438 /*
439 * Now let's create the actual data block for the inode
440 */
441 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
442 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000443 pctx.errcode = retval;
444 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 return 0;
446 }
447
Kit Westneat7bef6d52013-12-02 19:11:52 -0500448 retval = ext2fs_write_dir_block3(fs, blk, block, 0);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400449 ext2fs_free_mem(&block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000451 pctx.errcode = retval;
452 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 return 0;
454 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455
456 /*
457 * Set up the inode structure
458 */
459 memset(&inode, 0, sizeof(inode));
Theodore Ts'o64aecc42002-10-11 17:44:12 -0400460 inode.i_mode = 040700;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 inode.i_size = fs->blocksize;
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400462 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 inode.i_links_count = 2;
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400464 ext2fs_iblk_set(fs, &inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 inode.i_block[0] = blk;
466
467 /*
468 * Next, write out the inode.
469 */
Theodore Ts'o030970e2005-03-20 20:05:22 -0500470 pctx.errcode = ext2fs_write_new_inode(fs, ino, &inode);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000471 if (pctx.errcode) {
472 pctx.str = "ext2fs_write_inode";
473 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474 return 0;
475 }
476 /*
477 * Finally, create the directory link
478 */
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000479 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000480 if (pctx.errcode) {
481 pctx.str = "ext2fs_link";
482 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483 return 0;
484 }
485
486 /*
487 * Miscellaneous bookkeeping that needs to be kept straight.
488 */
Theodore Ts'o08b21301997-11-03 19:42:40 +0000489 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500490 e2fsck_adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000491 ext2fs_icount_store(ctx->inode_count, ino, 2);
492 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400493 ctx->lost_and_found = ino;
Aditya Kali624e4a62011-07-20 11:40:06 -0700494 quota_data_add(ctx->qctx, &inode, ino, fs->blocksize);
495 quota_data_inodes(ctx->qctx, &inode, ino, +1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000497 printf("/lost+found created; inode #%lu\n", ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498#endif
499 return ino;
500}
501
502/*
503 * This routine will connect a file to lost+found
504 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000505int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000507 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 errcode_t retval;
509 char name[80];
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510 struct problem_context pctx;
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000511 struct ext2_inode inode;
512 int file_type = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000513
514 clear_problem_context(&pctx);
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000515 pctx.ino = ino;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000516
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400517 if (!ctx->bad_lost_and_found && !ctx->lost_and_found) {
518 if (e2fsck_get_lost_and_found(ctx, 1) == 0)
519 ctx->bad_lost_and_found++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000520 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400521 if (ctx->bad_lost_and_found) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000522 fix_problem(ctx, PR_3_NO_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523 return 1;
524 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400525
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000526 sprintf(name, "#%u", ino);
Theodore Ts'o6fdc7a31999-11-10 13:34:40 +0000527 if (ext2fs_read_inode(fs, ino, &inode) == 0)
528 file_type = ext2_file_type(inode.i_mode);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400529 retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000531 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 return 1;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400533 retval = e2fsck_expand_directory(ctx, ctx->lost_and_found,
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400534 1, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000536 pctx.errcode = retval;
537 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 return 1;
539 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400540 retval = ext2fs_link(fs, ctx->lost_and_found, name,
541 ino, file_type);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 }
543 if (retval) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000544 pctx.errcode = retval;
545 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 return 1;
547 }
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500548 e2fsck_adjust_inode_count(ctx, ino, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549
550 return 0;
551}
552
553/*
554 * Utility routine to adjust the inode counts on an inode.
555 */
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500556errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000558 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559 errcode_t retval;
560 struct ext2_inode inode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400561
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562 if (!ino)
563 return 0;
564
565 retval = ext2fs_read_inode(fs, ino, &inode);
566 if (retval)
567 return retval;
568
569#if 0
Theodore Ts'of3db3561997-04-26 13:34:30 +0000570 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 inode.i_links_count);
572#endif
573
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000574 if (adj == 1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000575 ext2fs_icount_increment(ctx->inode_count, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000576 if (inode.i_links_count == (__u16) ~0)
577 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000578 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000579 inode.i_links_count++;
580 } else if (adj == -1) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000581 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000582 if (inode.i_links_count == 0)
583 return 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000584 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000585 inode.i_links_count--;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000586 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400587
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 retval = ext2fs_write_inode(fs, ino, &inode);
589 if (retval)
590 return retval;
591
592 return 0;
593}
594
595/*
596 * Fix parent --- this routine fixes up the parent of a directory.
597 */
598struct fix_dotdot_struct {
599 ext2_filsys fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000600 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 int done;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000602 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603};
604
605static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -0500606 int offset EXT2FS_ATTR((unused)),
607 int blocksize EXT2FS_ATTR((unused)),
608 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000609 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000611 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000613 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614
Theodore Ts'ob6f79831998-03-09 13:10:37 +0000615 if ((dirent->name_len & 0xFF) != 2)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 return 0;
617 if (strncmp(dirent->name, "..", 2))
618 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000620 clear_problem_context(&pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400621
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500622 retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000623 if (retval) {
624 pctx.errcode = retval;
625 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
626 }
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500627 retval = e2fsck_adjust_inode_count(fp->ctx, fp->parent, 1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000628 if (retval) {
629 pctx.errcode = retval;
630 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
631 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 dirent->inode = fp->parent;
Theodore Ts'o56c8c592007-03-31 19:18:24 -0400633 if (fp->ctx->fs->super->s_feature_incompat &
634 EXT2_FEATURE_INCOMPAT_FILETYPE)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400635 dirent->name_len = (dirent->name_len & 0xFF) |
Theodore Ts'o56c8c592007-03-31 19:18:24 -0400636 (EXT2_FT_DIR << 8);
637 else
638 dirent->name_len = dirent->name_len & 0xFF;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639
640 fp->done++;
641 return DIRENT_ABORT | DIRENT_CHANGED;
642}
643
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400644static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000646 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 errcode_t retval;
648 struct fix_dotdot_struct fp;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000649 struct problem_context pctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650
651 fp.fs = fs;
652 fp.parent = parent;
653 fp.done = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000654 fp.ctx = ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655
656#if 0
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400657 printf("Fixing '..' of inode %lu to be %lu...\n", ino, parent);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400659
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400660 clear_problem_context(&pctx);
661 pctx.ino = ino;
662 retval = ext2fs_dir_iterate(fs, ino, DIRENT_FLAG_INCLUDE_EMPTY,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 0, fix_dotdot_proc, &fp);
664 if (retval || !fp.done) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000665 pctx.errcode = retval;
666 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
667 PR_3_FIX_PARENT_NOFIND, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 ext2fs_unmark_valid(fs);
669 }
Theodore Ts'o28db82a2007-04-04 22:33:31 -0400670 (void) e2fsck_dir_info_set_dotdot(ctx, ino, parent);
671 if (e2fsck_dir_info_set_parent(ctx, ino, ctx->lost_and_found))
672 fix_problem(ctx, PR_3_NO_DIRINFO, &pctx);
673
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 return;
675}
676
677/*
678 * These routines are responsible for expanding a /lost+found if it is
679 * too small.
680 */
681
682struct expand_dir_struct {
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400683 blk64_t num;
684 e2_blkcnt_t guaranteed_size;
685 blk64_t newblocks;
686 blk64_t last_block;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000687 errcode_t err;
688 e2fsck_t ctx;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000689};
690
691static int expand_dir_proc(ext2_filsys fs,
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400692 blk64_t *blocknr,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000693 e2_blkcnt_t blockcnt,
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400694 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500695 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000696 void *priv_data)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000697{
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000698 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400699 blk64_t new_blk;
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400700 static blk64_t last_blk = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000701 char *block;
702 errcode_t retval;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000703 e2fsck_t ctx;
704
705 ctx = es->ctx;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400706
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400707 if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
708 return BLOCK_ABORT;
709
710 if (blockcnt > 0)
711 es->last_block = blockcnt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 if (*blocknr) {
713 last_blk = *blocknr;
714 return 0;
715 }
Darrick J. Wong00472552013-12-15 23:54:07 -0500716
717 if (blockcnt &&
718 (EXT2FS_B2C(fs, last_blk) == EXT2FS_B2C(fs, last_blk + 1)))
719 new_blk = last_blk + 1;
720 else {
721 last_blk &= ~EXT2FS_CLUSTER_MASK(fs);
722 retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map,
723 &new_blk);
724 if (retval) {
725 es->err = retval;
726 return BLOCK_ABORT;
727 }
728 es->newblocks++;
729 ext2fs_block_alloc_stats2(fs, new_blk, +1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 }
Darrick J. Wong00472552013-12-15 23:54:07 -0500731 last_blk = new_blk;
732
Theodore Ts'o3839e651997-04-26 13:21:57 +0000733 if (blockcnt > 0) {
734 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
735 if (retval) {
736 es->err = retval;
737 return BLOCK_ABORT;
738 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400739 es->num--;
Kit Westneat7bef6d52013-12-02 19:11:52 -0500740 retval = ext2fs_write_dir_block3(fs, new_blk, block, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000741 } else {
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400742 retval = ext2fs_get_mem(fs->blocksize, &block);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000743 if (retval) {
744 es->err = retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745 return BLOCK_ABORT;
746 }
747 memset(block, 0, fs->blocksize);
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400748 retval = io_channel_write_blk64(fs->io, new_blk, 1, block);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400749 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000750 if (retval) {
751 es->err = retval;
752 return BLOCK_ABORT;
753 }
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400754 ext2fs_free_mem(&block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000755 *blocknr = new_blk;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -0400756 ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400757
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400758 if (es->num == 0)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000759 return (BLOCK_CHANGED | BLOCK_ABORT);
760 else
761 return BLOCK_CHANGED;
762}
763
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400764errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
765 int num, int guaranteed_size)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000767 ext2_filsys fs = ctx->fs;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000768 errcode_t retval;
769 struct expand_dir_struct es;
770 struct ext2_inode inode;
Darrick J. Wong4b58df12013-12-10 17:22:07 -0800771 blk64_t sz, before, after;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400772
Theodore Ts'o3839e651997-04-26 13:21:57 +0000773 if (!(fs->flags & EXT2_FLAG_RW))
774 return EXT2_ET_RO_FILSYS;
775
Theodore Ts'ob8647fa1997-11-20 21:52:43 +0000776 /*
777 * Read the inode and block bitmaps in; we'll be messing with
778 * them.
779 */
780 e2fsck_read_bitmaps(ctx);
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000781
Theodore Ts'o3839e651997-04-26 13:21:57 +0000782 retval = ext2fs_check_directory(fs, dir);
783 if (retval)
784 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400785
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400786 es.num = num;
787 es.guaranteed_size = guaranteed_size;
788 es.last_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000789 es.err = 0;
Theodore Ts'oc1faf9c1999-09-14 20:00:54 +0000790 es.newblocks = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000791 es.ctx = ctx;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400792
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400793 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND,
Theodore Ts'o133a56d2000-11-17 05:40:49 +0000794 0, expand_dir_proc, &es);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795
796 if (es.err)
797 return es.err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000798
799 /*
800 * Update the size and block count fields in the inode.
801 */
802 retval = ext2fs_read_inode(fs, dir, &inode);
803 if (retval)
804 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400805
Darrick J. Wong4dbfd792013-10-07 09:51:48 -0400806 sz = (es.last_block + 1) * fs->blocksize;
Darrick J. Wong97c607b2014-07-26 14:34:56 -0400807 retval = ext2fs_inode_size_set(fs, &inode, sz);
808 if (retval)
809 return retval;
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400810 ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
Aditya Kali624e4a62011-07-20 11:40:06 -0700811 quota_data_add(ctx->qctx, &inode, dir, es.newblocks * fs->blocksize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000812
Theodore Ts'o08b21301997-11-03 19:42:40 +0000813 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814
815 return 0;
816}
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400817