blob: d05c90e5c45562b66a8a6aa71aa757f908849d01 [file] [log] [blame]
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001/*
2 * e2fsck.c - superblock checks
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00004 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000012#ifdef HAVE_ERRNO_H
13#include <errno.h>
14#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000015
Theodore Ts'o54be2cc1998-02-01 12:58:48 +000016#ifndef EXT2_SKIP_UUID
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000017#include "uuid/uuid.h"
Theodore Ts'o54be2cc1998-02-01 12:58:48 +000018#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000019#include "e2fsck.h"
20#include "problem.h"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000021
22#define MIN_CHECK 1
23#define MAX_CHECK 2
JP Abgralle0ed7402014-03-19 19:08:39 -070024#define LOG2_CHECK 4
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000025
26static void check_super_value(e2fsck_t ctx, const char *descr,
27 unsigned long value, int flags,
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000028 unsigned long min_val, unsigned long max_val)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000029{
30 struct problem_context pctx;
31
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000032 if (((flags & MIN_CHECK) && (value < min_val)) ||
JP Abgralle0ed7402014-03-19 19:08:39 -070033 ((flags & MAX_CHECK) && (value > max_val)) ||
34 ((flags & LOG2_CHECK) && (value & (value - 1) != 0))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000035 clear_problem_context(&pctx);
36 pctx.num = value;
37 pctx.str = descr;
38 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +000039 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000040 }
41}
42
Theodore Ts'o43139321998-02-16 22:34:46 +000043/*
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000044 * helper function to release an inode
45 */
46struct process_block_struct {
Theodore Ts'o83949022000-10-25 01:38:50 +000047 e2fsck_t ctx;
48 char *buf;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000049 struct problem_context *pctx;
Theodore Ts'o83949022000-10-25 01:38:50 +000050 int truncating;
51 int truncate_offset;
Theodore Ts'o54434922003-12-07 01:28:50 -050052 e2_blkcnt_t truncate_block;
Theodore Ts'o83949022000-10-25 01:38:50 +000053 int truncated_blocks;
54 int abort;
55 errcode_t errcode;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000056};
57
58static int release_inode_block(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -070059 blk64_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -050060 e2_blkcnt_t blockcnt,
JP Abgralle0ed7402014-03-19 19:08:39 -070061 blk64_t ref_blk EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -050062 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000063 void *priv_data)
64{
65 struct process_block_struct *pb;
Theodore Ts'o83949022000-10-25 01:38:50 +000066 e2fsck_t ctx;
67 struct problem_context *pctx;
JP Abgralle0ed7402014-03-19 19:08:39 -070068 blk64_t blk = *block_nr;
Theodore Ts'o83949022000-10-25 01:38:50 +000069 int retval = 0;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000070
71 pb = (struct process_block_struct *) priv_data;
72 ctx = pb->ctx;
73 pctx = pb->pctx;
74
75 pctx->blk = blk;
76 pctx->blkcount = blockcnt;
77
78 if (HOLE_BLKADDR(blk))
79 return 0;
80
81 if ((blk < fs->super->s_first_data_block) ||
JP Abgralle0ed7402014-03-19 19:08:39 -070082 (blk >= ext2fs_blocks_count(fs->super))) {
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000083 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000084 return_abort:
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000085 pb->abort = 1;
86 return BLOCK_ABORT;
87 }
88
JP Abgralle0ed7402014-03-19 19:08:39 -070089 if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
Theodore Ts'o80bfaa32000-08-18 15:08:37 +000090 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000091 goto return_abort;
Theodore Ts'o83949022000-10-25 01:38:50 +000092 }
93
94 /*
95 * If we are deleting an orphan, then we leave the fields alone.
96 * If we are truncating an orphan, then update the inode fields
97 * and clean up any partial block data.
98 */
99 if (pb->truncating) {
100 /*
101 * We only remove indirect blocks if they are
102 * completely empty.
103 */
104 if (blockcnt < 0) {
105 int i, limit;
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000106 blk_t *bp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400107
JP Abgralle0ed7402014-03-19 19:08:39 -0700108 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
Theodore Ts'o83949022000-10-25 01:38:50 +0000109 pb->buf);
110 if (pb->errcode)
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000111 goto return_abort;
Theodore Ts'o83949022000-10-25 01:38:50 +0000112
113 limit = fs->blocksize >> 2;
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000114 for (i = 0, bp = (blk_t *) pb->buf;
115 i < limit; i++, bp++)
116 if (*bp)
Theodore Ts'o83949022000-10-25 01:38:50 +0000117 return 0;
118 }
119 /*
120 * We don't remove direct blocks until we've reached
121 * the truncation block.
122 */
123 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
124 return 0;
125 /*
126 * If part of the last block needs truncating, we do
127 * it here.
128 */
129 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700130 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
Theodore Ts'o83949022000-10-25 01:38:50 +0000131 pb->buf);
132 if (pb->errcode)
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000133 goto return_abort;
Theodore Ts'o83949022000-10-25 01:38:50 +0000134 memset(pb->buf + pb->truncate_offset, 0,
135 fs->blocksize - pb->truncate_offset);
JP Abgralle0ed7402014-03-19 19:08:39 -0700136 pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
Theodore Ts'o83949022000-10-25 01:38:50 +0000137 pb->buf);
138 if (pb->errcode)
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000139 goto return_abort;
Theodore Ts'o83949022000-10-25 01:38:50 +0000140 }
141 pb->truncated_blocks++;
142 *block_nr = 0;
143 retval |= BLOCK_CHANGED;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000144 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400145
JP Abgralle0ed7402014-03-19 19:08:39 -0700146 ext2fs_block_alloc_stats2(fs, blk, -1);
147 ctx->free_blocks++;
Theodore Ts'o83949022000-10-25 01:38:50 +0000148 return retval;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000149}
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400150
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000151/*
152 * This function releases an inode. Returns 1 if an inconsistency was
Theodore Ts'o83949022000-10-25 01:38:50 +0000153 * found. If the inode has a link count, then it is being truncated and
154 * not deleted.
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000155 */
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000156static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
Theodore Ts'o721edd02001-01-12 21:05:57 +0000157 struct ext2_inode *inode, char *block_buf,
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000158 struct problem_context *pctx)
159{
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400160 struct process_block_struct pb;
Theodore Ts'o83949022000-10-25 01:38:50 +0000161 ext2_filsys fs = ctx->fs;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000162 errcode_t retval;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400163 __u32 count;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000164
JP Abgralle0ed7402014-03-19 19:08:39 -0700165 if (!ext2fs_inode_has_valid_blocks2(fs, inode))
Theodore Ts'o42475e22001-06-22 20:35:38 -0400166 return 0;
167
Theodore Ts'o83949022000-10-25 01:38:50 +0000168 pb.buf = block_buf + 3 * ctx->fs->blocksize;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000169 pb.ctx = ctx;
170 pb.abort = 0;
Theodore Ts'o83949022000-10-25 01:38:50 +0000171 pb.errcode = 0;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000172 pb.pctx = pctx;
Theodore Ts'o83949022000-10-25 01:38:50 +0000173 if (inode->i_links_count) {
174 pb.truncating = 1;
Theodore Ts'o54434922003-12-07 01:28:50 -0500175 pb.truncate_block = (e2_blkcnt_t)
JP Abgralle0ed7402014-03-19 19:08:39 -0700176 ((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
Theodore Ts'o83949022000-10-25 01:38:50 +0000177 fs->blocksize);
178 pb.truncate_offset = inode->i_size % fs->blocksize;
179 } else {
180 pb.truncating = 0;
181 pb.truncate_block = 0;
182 pb.truncate_offset = 0;
183 }
184 pb.truncated_blocks = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700185 retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
Theodore Ts'o83949022000-10-25 01:38:50 +0000186 block_buf, release_inode_block, &pb);
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000187 if (retval) {
Theodore Ts'o83949022000-10-25 01:38:50 +0000188 com_err("release_inode_blocks", retval,
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000189 _("while calling ext2fs_block_iterate for inode %d"),
190 ino);
191 return 1;
192 }
193 if (pb.abort)
194 return 1;
195
Theodore Ts'o83949022000-10-25 01:38:50 +0000196 /* Refresh the inode since ext2fs_block_iterate may have changed it */
197 e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks");
198
199 if (pb.truncated_blocks)
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400200 ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks);
Theodore Ts'o83949022000-10-25 01:38:50 +0000201
JP Abgralle0ed7402014-03-19 19:08:39 -0700202 if (ext2fs_file_acl_block(fs, inode)) {
203 retval = ext2fs_adjust_ea_refcount2(fs,
204 ext2fs_file_acl_block(fs, inode),
205 block_buf, -1, &count);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400206 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
207 retval = 0;
208 count = 1;
209 }
210 if (retval) {
211 com_err("release_inode_blocks", retval,
JP Abgralle0ed7402014-03-19 19:08:39 -0700212 _("while calling ext2fs_adjust_ea_refcount2 for inode %d"),
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400213 ino);
214 return 1;
215 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700216 if (count == 0) {
217 ext2fs_block_alloc_stats2(fs,
218 ext2fs_file_acl_block(fs, inode), -1);
219 ctx->free_blocks++;
220 }
221 ext2fs_file_acl_block_set(fs, inode, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400222 }
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000223 return 0;
224}
225
226/*
227 * This function releases all of the orphan inodes. It returns 1 if
228 * it hit some error, and 0 on success.
229 */
230static int release_orphan_inodes(e2fsck_t ctx)
231{
232 ext2_filsys fs = ctx->fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +0000233 ext2_ino_t ino, next_ino;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000234 struct ext2_inode inode;
235 struct problem_context pctx;
236 char *block_buf;
237
238 if ((ino = fs->super->s_last_orphan) == 0)
239 return 0;
240
Theodore Ts'o25c63ba2000-08-18 15:31:37 +0000241 /*
242 * Win or lose, we won't be using the head of the orphan inode
243 * list again.
244 */
245 fs->super->s_last_orphan = 0;
246 ext2fs_mark_super_dirty(fs);
Theodore Ts'oeb4ab512001-08-13 10:58:41 -0400247
248 /*
249 * If the filesystem contains errors, don't run the orphan
250 * list, since the orphan list can't be trusted; and we're
251 * going to be running a full e2fsck run anyway...
252 */
253 if (fs->super->s_state & EXT2_ERROR_FS)
254 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400255
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000256 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
257 (ino > fs->super->s_inodes_count)) {
258 clear_problem_context(&pctx);
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000259 pctx.ino = ino;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000260 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
261 return 1;
262 }
263
Theodore Ts'o83949022000-10-25 01:38:50 +0000264 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
Theodore Ts'o9b565752000-12-13 18:50:22 +0000265 "block iterate buffer");
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000266 e2fsck_read_bitmaps(ctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400267
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000268 while (ino) {
Theodore Ts'o83949022000-10-25 01:38:50 +0000269 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000270 clear_problem_context(&pctx);
Theodore Ts'oecf1b772000-08-20 22:06:31 +0000271 pctx.ino = ino;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000272 pctx.inode = &inode;
Theodore Ts'of0b8c872001-05-09 06:03:58 +0000273 pctx.str = inode.i_links_count ? _("Truncating") :
274 _("Clearing");
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000275
Theodore Ts'o83949022000-10-25 01:38:50 +0000276 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
Theodore Ts'oecf1b772000-08-20 22:06:31 +0000277
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000278 next_ino = inode.i_dtime;
279 if (next_ino &&
Theodore Ts'o99a2cc92000-08-22 21:41:52 +0000280 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
281 (next_ino > fs->super->s_inodes_count))) {
282 pctx.ino = next_ino;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000283 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000284 goto return_abort;
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000285 }
286
Theodore Ts'o83949022000-10-25 01:38:50 +0000287 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000288 goto return_abort;
Theodore Ts'oecf1b772000-08-20 22:06:31 +0000289
Theodore Ts'o83949022000-10-25 01:38:50 +0000290 if (!inode.i_links_count) {
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400291 ext2fs_inode_alloc_stats2(fs, ino, -1,
292 LINUX_S_ISDIR(inode.i_mode));
JP Abgralle0ed7402014-03-19 19:08:39 -0700293 ctx->free_inodes++;
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400294 inode.i_dtime = ctx->now;
Stephen Tweedieeb16f862002-08-14 11:00:44 +0100295 } else {
296 inode.i_dtime = 0;
Theodore Ts'o83949022000-10-25 01:38:50 +0000297 }
298 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000299 ino = next_ino;
300 }
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400301 ext2fs_free_mem(&block_buf);
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000302 return 0;
Theodore Ts'o53ef44c2001-01-06 05:55:58 +0000303return_abort:
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400304 ext2fs_free_mem(&block_buf);
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000305 return 1;
306}
307
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500308/*
309 * Check the resize inode to make sure it is sane. We check both for
310 * the case where on-line resizing is not enabled (in which case the
311 * resize inode should be cleared) as well as the case where on-line
312 * resizing is enabled.
313 */
Theodore Ts'o69d0edf2009-04-23 00:39:06 -0400314void check_resize_inode(e2fsck_t ctx)
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500315{
316 ext2_filsys fs = ctx->fs;
317 struct ext2_inode inode;
318 struct problem_context pctx;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500319 int i, gdt_off, ind_off;
320 dgrp_t j;
JP Abgralle0ed7402014-03-19 19:08:39 -0700321 blk_t blk, pblk;
322 blk_t expect; /* for resize inode, which is 32-bit only */
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500323 __u32 *dind_buf = 0, *ind_buf;
324 errcode_t retval;
325
326 clear_problem_context(&pctx);
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500327
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400328 /*
329 * If the resize inode feature isn't set, then
Theodore Ts'oea774312005-01-28 11:45:28 -0500330 * s_reserved_gdt_blocks must be zero.
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500331 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400332 if (!(fs->super->s_feature_compat &
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500333 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
334 if (fs->super->s_reserved_gdt_blocks) {
335 pctx.num = fs->super->s_reserved_gdt_blocks;
336 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
337 &pctx)) {
338 fs->super->s_reserved_gdt_blocks = 0;
339 ext2fs_mark_super_dirty(fs);
340 }
341 }
Theodore Ts'oea774312005-01-28 11:45:28 -0500342 }
343
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400344 /* Read the resize inode */
Theodore Ts'oea774312005-01-28 11:45:28 -0500345 pctx.ino = EXT2_RESIZE_INO;
346 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
347 if (retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400348 if (fs->super->s_feature_compat &
Theodore Ts'oea774312005-01-28 11:45:28 -0500349 EXT2_FEATURE_COMPAT_RESIZE_INODE)
350 ctx->flags |= E2F_FLAG_RESIZE_INODE;
351 return;
352 }
353
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400354 /*
355 * If the resize inode feature isn't set, check to make sure
Theodore Ts'oea774312005-01-28 11:45:28 -0500356 * the resize inode is cleared; then we're done.
357 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400358 if (!(fs->super->s_feature_compat &
Theodore Ts'oea774312005-01-28 11:45:28 -0500359 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500360 for (i=0; i < EXT2_N_BLOCKS; i++) {
361 if (inode.i_block[i])
362 break;
363 }
364 if ((i < EXT2_N_BLOCKS) &&
365 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
366 memset(&inode, 0, sizeof(inode));
367 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
368 "clear_resize");
369 }
370 return;
371 }
Theodore Ts'oea774312005-01-28 11:45:28 -0500372
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400373 /*
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500374 * The resize inode feature is enabled; check to make sure the
375 * only block in use is the double indirect block
376 */
377 blk = inode.i_block[EXT2_DIND_BLOCK];
378 for (i=0; i < EXT2_N_BLOCKS; i++) {
379 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
380 break;
381 }
Theodore Ts'od4dc0a92005-01-27 18:47:51 -0500382 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
383 !(inode.i_mode & LINUX_S_IFREG) ||
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500384 (blk < fs->super->s_first_data_block ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700385 blk >= ext2fs_blocks_count(fs->super))) {
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500386 resize_inode_invalid:
387 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
388 memset(&inode, 0, sizeof(inode));
389 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
390 "clear_resize");
391 ctx->flags |= E2F_FLAG_RESIZE_INODE;
392 }
393 if (!(ctx->options & E2F_OPT_READONLY)) {
394 fs->super->s_state &= ~EXT2_VALID_FS;
395 ext2fs_mark_super_dirty(fs);
396 }
397 goto cleanup;
398 }
399 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
400 "resize dind buffer");
401 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
402
Theodore Ts'odc8ce342005-01-06 00:04:24 -0500403 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500404 if (retval)
405 goto resize_inode_invalid;
406
407 gdt_off = fs->desc_blocks;
408 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
JP Abgralle0ed7402014-03-19 19:08:39 -0700409 if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
410 pblk++; /* Deal with 1024 blocksize bigalloc fs */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400411 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500412 i++, gdt_off++, pblk++) {
413 gdt_off %= fs->blocksize/4;
414 if (dind_buf[gdt_off] != pblk)
415 goto resize_inode_invalid;
Theodore Ts'odc8ce342005-01-06 00:04:24 -0500416 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400417 if (retval)
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500418 goto resize_inode_invalid;
419 ind_off = 0;
420 for (j = 1; j < fs->group_desc_count; j++) {
421 if (!ext2fs_bg_has_super(fs, j))
422 continue;
423 expect = pblk + (j * fs->super->s_blocks_per_group);
424 if (ind_buf[ind_off] != expect)
425 goto resize_inode_invalid;
426 ind_off++;
427 }
428 }
429
430cleanup:
431 if (dind_buf)
432 ext2fs_free_mem(&dind_buf);
433
434 }
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000435
Theodore Ts'of77704e2006-11-11 22:32:35 -0500436/*
437 * This function checks the dirhash signed/unsigned hint if necessary.
438 */
Theodore Ts'o642935c2006-11-14 23:38:17 -0500439static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
Theodore Ts'of77704e2006-11-11 22:32:35 -0500440{
441 struct ext2_super_block *sb = ctx->fs->super;
442 struct problem_context pctx;
Theodore Ts'of77704e2006-11-11 22:32:35 -0500443 char c;
444
445 if ((ctx->options & E2F_OPT_READONLY) ||
446 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
447 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
448 return;
449
450 c = (char) 255;
451
452 clear_problem_context(&pctx);
453 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
454 if (((int) c) == -1) {
455 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
456 } else {
457 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
458 }
459 ext2fs_mark_super_dirty(ctx->fs);
460 }
461}
462
463
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000464void check_super_block(e2fsck_t ctx)
465{
466 ext2_filsys fs = ctx->fs;
JP Abgralle0ed7402014-03-19 19:08:39 -0700467 blk64_t first_block, last_block;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000468 struct ext2_super_block *sb = fs->super;
JP Abgralle0ed7402014-03-19 19:08:39 -0700469 unsigned int ipg_max;
Theodore Ts'o26ea4892009-07-16 23:44:50 -0400470 problem_t problem;
JP Abgralle0ed7402014-03-19 19:08:39 -0700471 blk64_t blocks_per_group = fs->super->s_blocks_per_group;
472 __u32 bpg_max, cpg_max;
Theodore Ts'o78cf0541999-10-20 18:29:18 +0000473 int inodes_per_block;
Theodore Ts'o89db86d2005-03-20 22:47:54 -0500474 int inode_size;
Theodore Ts'oba5131f2009-10-16 20:46:45 -0400475 int accept_time_fudge;
Theodore Ts'o177839e2010-05-13 17:36:36 -0400476 int broken_system_clock;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000477 dgrp_t i;
JP Abgralle0ed7402014-03-19 19:08:39 -0700478 blk64_t should_be;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000479 struct problem_context pctx;
JP Abgralle0ed7402014-03-19 19:08:39 -0700480 blk64_t free_blocks = 0;
Valerie Clement5d38ef12007-08-30 17:33:40 +0200481 ino_t free_inodes = 0;
Theodore Ts'o80875db2008-10-12 23:09:26 -0400482 int csum_flag, clear_test_fs_flag;
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600483
484 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
485 ipg_max = inodes_per_block * (blocks_per_group - 4);
486 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
487 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
JP Abgralle0ed7402014-03-19 19:08:39 -0700488 cpg_max = 8 * EXT2_BLOCK_SIZE(sb);
489 if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb))
490 cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb);
491 bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs);
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600492 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
493 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000494
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000495 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000496 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000497 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000498 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000499 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000500 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600501
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000502 clear_problem_context(&pctx);
503
504 /*
505 * Verify the super block constants...
506 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000507 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000508 MIN_CHECK, 1, 0);
JP Abgralle0ed7402014-03-19 19:08:39 -0700509 check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000510 MIN_CHECK, 1, 0);
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000511 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
JP Abgralle0ed7402014-03-19 19:08:39 -0700512 MAX_CHECK, 0, ext2fs_blocks_count(sb));
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000513 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
Theodore Ts'o31e29a12002-05-17 10:53:07 -0400514 MIN_CHECK | MAX_CHECK, 0,
515 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
JP Abgralle0ed7402014-03-19 19:08:39 -0700516 check_super_value(ctx, "log_cluster_size",
517 sb->s_log_cluster_size,
518 MIN_CHECK | MAX_CHECK, sb->s_log_block_size,
519 (EXT2_MAX_CLUSTER_LOG_SIZE -
520 EXT2_MIN_CLUSTER_LOG_SIZE));
521 check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group,
522 MIN_CHECK | MAX_CHECK, 8, cpg_max);
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000523 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600524 MIN_CHECK | MAX_CHECK, 8, bpg_max);
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000525 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
Andreas Dilgerb21bf262002-06-10 11:05:56 -0600526 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
JP Abgralle0ed7402014-03-19 19:08:39 -0700527 check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
528 MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400529 check_super_value(ctx, "reserved_gdt_blocks",
Theodore Ts'oc3ffaf82004-12-24 01:42:22 -0500530 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
JP Abgralle0ed7402014-03-19 19:08:39 -0700531 fs->blocksize / sizeof(__u32));
532 check_super_value(ctx, "desc_size",
533 sb->s_desc_size, MAX_CHECK | LOG2_CHECK, 0,
534 EXT2_MAX_DESC_SIZE);
Theodore Ts'o1f790a72009-01-22 15:52:50 -0500535 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
536 check_super_value(ctx, "first_ino", sb->s_first_ino,
537 MIN_CHECK | MAX_CHECK,
538 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
Theodore Ts'o89db86d2005-03-20 22:47:54 -0500539 inode_size = EXT2_INODE_SIZE(sb);
540 check_super_value(ctx, "inode_size",
JP Abgralle0ed7402014-03-19 19:08:39 -0700541 inode_size, MIN_CHECK | MAX_CHECK | LOG2_CHECK,
Theodore Ts'o89db86d2005-03-20 22:47:54 -0500542 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
JP Abgralle0ed7402014-03-19 19:08:39 -0700543 if (sb->s_blocks_per_group != (sb->s_clusters_per_group *
544 EXT2FS_CLUSTER_RATIO(fs))) {
545 pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs);
546 pctx.str = "block_size";
Theodore Ts'o44fe08f2011-06-10 18:58:16 -0400547 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
548 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
549 return;
550 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400551
Theodore Ts'od2af1bd2007-06-04 01:14:52 -0400552 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700553 (ctx->num_blocks < ext2fs_blocks_count(sb))) {
554 pctx.blk = ext2fs_blocks_count(sb);
Theodore Ts'od2af1bd2007-06-04 01:14:52 -0400555 pctx.blk2 = ctx->num_blocks;
556 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000557 ctx->flags |= E2F_FLAG_ABORT;
558 return;
559 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000560 }
561
JP Abgralle0ed7402014-03-19 19:08:39 -0700562 should_be = (sb->s_log_block_size == 0 &&
563 EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000564 if (sb->s_first_data_block != should_be) {
565 pctx.blk = sb->s_first_data_block;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000566 pctx.blk2 = should_be;
567 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +0000568 ctx->flags |= E2F_FLAG_ABORT;
569 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000570 }
571
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000572 should_be = sb->s_inodes_per_group * fs->group_desc_count;
573 if (sb->s_inodes_count != should_be) {
574 pctx.ino = sb->s_inodes_count;
Theodore Ts'od4b0ce01999-06-18 01:09:29 +0000575 pctx.ino2 = should_be;
576 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000577 sb->s_inodes_count = should_be;
Theodore Ts'od4b0ce01999-06-18 01:09:29 +0000578 ext2fs_mark_super_dirty(fs);
579 }
580 }
581
JP Abgralle0ed7402014-03-19 19:08:39 -0700582 /* Is 64bit set and extents unset? */
583 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
584 EXT4_FEATURE_INCOMPAT_64BIT) &&
585 !EXT2_HAS_INCOMPAT_FEATURE(fs->super,
586 EXT3_FEATURE_INCOMPAT_EXTENTS) &&
587 fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) {
588 fs->super->s_feature_incompat |=
589 EXT3_FEATURE_INCOMPAT_EXTENTS;
590 ext2fs_mark_super_dirty(fs);
591 }
592
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000593 /*
594 * Verify the group descriptors....
595 */
Jose R. Santos88b34b12007-08-13 23:33:03 -0500596 first_block = sb->s_first_data_block;
JP Abgralle0ed7402014-03-19 19:08:39 -0700597 last_block = ext2fs_blocks_count(sb)-1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000598
Jose R. Santos49a73602007-10-21 21:04:03 -0500599 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
600 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
JP Abgralle0ed7402014-03-19 19:08:39 -0700601 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000602 pctx.group = i;
Eric Sandeenabf23432006-09-12 14:56:16 -0400603
Jose R. Santos88b34b12007-08-13 23:33:03 -0500604 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
605 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700606 first_block = ext2fs_group_first_block2(fs, i);
607 last_block = ext2fs_group_last_block2(fs, i);
Jose R. Santos88b34b12007-08-13 23:33:03 -0500608 }
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400609
JP Abgralle0ed7402014-03-19 19:08:39 -0700610 if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
611 (ext2fs_block_bitmap_loc(fs, i) > last_block)) {
612 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500613 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
JP Abgralle0ed7402014-03-19 19:08:39 -0700614 ext2fs_block_bitmap_loc_set(fs, i, 0);
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500615 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700616 if (ext2fs_block_bitmap_loc(fs, i) == 0) {
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500617 ctx->invalid_block_bitmap_flag[i]++;
618 ctx->invalid_bitmaps++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000619 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700620 if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) ||
621 (ext2fs_inode_bitmap_loc(fs, i) > last_block)) {
622 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500623 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
JP Abgralle0ed7402014-03-19 19:08:39 -0700624 ext2fs_inode_bitmap_loc_set(fs, i, 0);
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500625 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700626 if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500627 ctx->invalid_inode_bitmap_flag[i]++;
628 ctx->invalid_bitmaps++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700630 if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
631 ((ext2fs_inode_table_loc(fs, i) +
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400632 fs->inode_blocks_per_group - 1) > last_block)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700633 pctx.blk = ext2fs_inode_table_loc(fs, i);
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500634 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
JP Abgralle0ed7402014-03-19 19:08:39 -0700635 ext2fs_inode_table_loc_set(fs, i, 0);
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500636 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700637 if (ext2fs_inode_table_loc(fs, i) == 0) {
Theodore Ts'o24bfb442001-11-26 15:51:14 -0500638 ctx->invalid_inode_table_flag[i]++;
639 ctx->invalid_bitmaps++;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000640 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700641 free_blocks += ext2fs_bg_free_blocks_count(fs, i);
642 free_inodes += ext2fs_bg_free_inodes_count(fs, i);
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500643
JP Abgralle0ed7402014-03-19 19:08:39 -0700644 if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) ||
645 (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) ||
646 (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group))
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500647 ext2fs_unmark_valid(fs);
648
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400649 should_be = 0;
Jose R. Santos49a73602007-10-21 21:04:03 -0500650 if (!ext2fs_group_desc_csum_verify(fs, i)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700651 pctx.csum1 = ext2fs_bg_checksum(fs, i);
652 pctx.csum2 = ext2fs_group_desc_csum(fs, i);
Jose R. Santos49a73602007-10-21 21:04:03 -0500653 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700654 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
655 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
656 ext2fs_bg_itable_unused_set(fs, i, 0);
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400657 should_be = 1;
Jose R. Santos49a73602007-10-21 21:04:03 -0500658 }
659 ext2fs_unmark_valid(fs);
660 }
661
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400662 if (!csum_flag &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700663 (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
664 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
665 ext2fs_bg_itable_unused(fs, i) != 0)) {
Jose R. Santos49a73602007-10-21 21:04:03 -0500666 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700667 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
668 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
669 ext2fs_bg_itable_unused_set(fs, i, 0);
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400670 should_be = 1;
Jose R. Santos49a73602007-10-21 21:04:03 -0500671 }
672 ext2fs_unmark_valid(fs);
673 }
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400674
675 if (i == fs->group_desc_count - 1 &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700676 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400677 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700678 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400679 should_be = 1;
680 }
Jose R. Santos49a73602007-10-21 21:04:03 -0500681 ext2fs_unmark_valid(fs);
682 }
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400683
Jose R. Santos49a73602007-10-21 21:04:03 -0500684 if (csum_flag &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700685 (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
686 ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
687 pctx.blk = ext2fs_bg_itable_unused(fs, i);
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400688 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700689 ext2fs_bg_itable_unused_set(fs, i, 0);
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400690 should_be = 1;
691 }
Jose R. Santos49a73602007-10-21 21:04:03 -0500692 ext2fs_unmark_valid(fs);
693 }
Andreas Dilger0d5439c2008-03-31 12:14:22 -0400694
695 if (should_be)
696 ext2fs_group_desc_csum_set(fs, i);
JP Abgralle0ed7402014-03-19 19:08:39 -0700697 /* If the user aborts e2fsck by typing ^C, stop right away */
698 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
699 return;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000700 }
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500701
JP Abgralle0ed7402014-03-19 19:08:39 -0700702 ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
703 ctx->free_inodes = free_inodes;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400704
JP Abgralle0ed7402014-03-19 19:08:39 -0700705 if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500706 (sb->s_free_inodes_count > sb->s_inodes_count))
707 ext2fs_unmark_valid(fs);
708
709
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000710 /*
711 * If we have invalid bitmaps, set the error state of the
712 * filesystem.
713 */
714 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500715 sb->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000716 ext2fs_mark_super_dirty(fs);
717 }
718
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000719 clear_problem_context(&pctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400720
Theodore Ts'o54be2cc1998-02-01 12:58:48 +0000721#ifndef EXT2_SKIP_UUID
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000722 /*
723 * If the UUID field isn't assigned, assign it.
724 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000725 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000726 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000727 uuid_generate(sb->s_uuid);
JP Abgralle0ed7402014-03-19 19:08:39 -0700728 fs->flags |= EXT2_FLAG_DIRTY;
Theodore Ts'o299d7422002-11-08 11:10:28 -0500729 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000730 }
731 }
Theodore Ts'o54be2cc1998-02-01 12:58:48 +0000732#endif
Theodore Ts'o2a77a782003-04-05 22:50:44 -0500733
734 /*
Theodore Ts'o80875db2008-10-12 23:09:26 -0400735 * Check to see if we should disable the test_fs flag
736 */
737 profile_get_boolean(ctx->profile, "options",
738 "clear_test_fs_flag", 0, 1,
739 &clear_test_fs_flag);
740 if (!(ctx->options & E2F_OPT_READONLY) &&
741 clear_test_fs_flag &&
742 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
743 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
744 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
745 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
JP Abgralle0ed7402014-03-19 19:08:39 -0700746 fs->flags |= EXT2_FLAG_DIRTY;
Theodore Ts'o80875db2008-10-12 23:09:26 -0400747 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
748 }
749 }
750
751 /*
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000752 * For the Hurd, check to see if the filetype option is set,
753 * since it doesn't support it.
754 */
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000755 if (!(ctx->options & E2F_OPT_READONLY) &&
756 fs->super->s_creator_os == EXT2_OS_HURD &&
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000757 (fs->super->s_feature_incompat &
758 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
759 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
760 fs->super->s_feature_incompat &=
761 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
762 ext2fs_mark_super_dirty(fs);
Theodore Ts'o0cfce7f2007-10-06 12:37:08 -0400763 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'o4ea0a112000-05-08 13:33:17 +0000764 }
765 }
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000766
767 /*
Theodore Ts'o7b59f1e2000-12-13 18:11:44 +0000768 * If we have any of the compatibility flags set, we need to have a
769 * revision 1 filesystem. Most kernels will not check the flags on
770 * a rev 0 filesystem and we may have corruption issues because of
771 * the incompatible changes to the filesystem.
772 */
773 if (!(ctx->options & E2F_OPT_READONLY) &&
774 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
775 (fs->super->s_feature_compat ||
776 fs->super->s_feature_ro_compat ||
777 fs->super->s_feature_incompat) &&
778 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
Theodore Ts'oa917d1c2000-12-13 18:36:23 +0000779 ext2fs_update_dynamic_rev(fs);
Theodore Ts'o7b59f1e2000-12-13 18:11:44 +0000780 ext2fs_mark_super_dirty(fs);
Theodore Ts'o0cfce7f2007-10-06 12:37:08 -0400781 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'o7b59f1e2000-12-13 18:11:44 +0000782 }
783
784 /*
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000785 * Clean up any orphan inodes, if present.
786 */
787 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
788 fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o7b59f1e2000-12-13 18:11:44 +0000789 ext2fs_mark_super_dirty(fs);
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000790 }
791
Theodore Ts'o60702c22007-09-22 20:42:04 -0400792 /*
Theodore Ts'oba5131f2009-10-16 20:46:45 -0400793 * Unfortunately, due to Windows' unfortunate design decision
794 * to configure the hardware clock to tick localtime, instead
795 * of the more proper and less error-prone UTC time, many
796 * users end up in the situation where the system clock is
797 * incorrectly set at the time when e2fsck is run.
798 *
799 * Historically this was usually due to some distributions
800 * having buggy init scripts and/or installers that didn't
801 * correctly detect this case and take appropriate
802 * countermeasures. However, it's still possible, despite the
803 * best efforts of init script and installer authors to not be
804 * able to detect this misconfiguration, usually due to a
805 * buggy or misconfigured virtualization manager or the
806 * installer not having access to a network time server during
807 * the installation process. So by default, we allow the
808 * superblock times to be fudged by up to 24 hours. This can
809 * be disabled by setting options.accept_time_fudge to the
810 * boolean value of false in e2fsck.conf. We also support
811 * options.buggy_init_scripts for backwards compatibility.
Theodore Ts'o60702c22007-09-22 20:42:04 -0400812 */
Theodore Ts'oba5131f2009-10-16 20:46:45 -0400813 profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
814 0, 1, &accept_time_fudge);
Theodore Ts'o60702c22007-09-22 20:42:04 -0400815 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
Theodore Ts'oba5131f2009-10-16 20:46:45 -0400816 0, accept_time_fudge, &accept_time_fudge);
817 ctx->time_fudge = accept_time_fudge ? 86400 : 0;
Theodore Ts'o60702c22007-09-22 20:42:04 -0400818
Theodore Ts'o177839e2010-05-13 17:36:36 -0400819 profile_get_boolean(ctx->profile, "options", "broken_system_clock",
820 0, 0, &broken_system_clock);
821
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400822 /*
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400823 * Check to see if the superblock last mount time or last
824 * write time is in the future.
825 */
Theodore Ts'o177839e2010-05-13 17:36:36 -0400826 if (!broken_system_clock &&
827 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
828 fs->super->s_mtime > (__u32) ctx->now) {
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400829 pctx.num = fs->super->s_mtime;
Theodore Ts'o26ea4892009-07-16 23:44:50 -0400830 problem = PR_0_FUTURE_SB_LAST_MOUNT;
831 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
832 problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
833 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400834 fs->super->s_mtime = ctx->now;
JP Abgralle0ed7402014-03-19 19:08:39 -0700835 fs->flags |= EXT2_FLAG_DIRTY;
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400836 }
837 }
Theodore Ts'o177839e2010-05-13 17:36:36 -0400838 if (!broken_system_clock &&
839 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
840 fs->super->s_wtime > (__u32) ctx->now) {
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400841 pctx.num = fs->super->s_wtime;
Theodore Ts'ofe26a552009-08-08 10:14:48 -0400842 problem = PR_0_FUTURE_SB_LAST_WRITE;
Theodore Ts'o26ea4892009-07-16 23:44:50 -0400843 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
Theodore Ts'o61a9d2b2009-10-03 23:17:45 -0400844 problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
Theodore Ts'o26ea4892009-07-16 23:44:50 -0400845 if (fix_problem(ctx, problem, &pctx)) {
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400846 fs->super->s_wtime = ctx->now;
JP Abgralle0ed7402014-03-19 19:08:39 -0700847 fs->flags |= EXT2_FLAG_DIRTY;
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400848 }
849 }
850
Theodore Ts'o773fd8a2001-10-06 21:26:27 -0400851 /*
852 * Move the ext3 journal file, if necessary.
853 */
854 e2fsck_move_ext3_journal(ctx);
Theodore Ts'ob1c52b22006-03-10 15:25:59 -0500855
856 /*
857 * Fix journal hint, if necessary
858 */
859 e2fsck_fix_ext3_journal_hint(ctx);
860
Theodore Ts'of77704e2006-11-11 22:32:35 -0500861 /*
862 * Add dirhash hint if necessary
863 */
864 e2fsck_fix_dirhash_hint(ctx);
865
JP Abgralle0ed7402014-03-19 19:08:39 -0700866 /*
867 * Hide quota inodes if necessary.
868 */
869 e2fsck_hide_quota(ctx);
870
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000871 return;
872}
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400873
874/*
875 * Check to see if we should backup the master sb to the backup super
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500876 * blocks. Returns non-zero if the sb should be backed up.
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400877 */
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500878
879/*
880 * A few flags are set on the fly by the kernel, but only in the
881 * primary superblock. This is actually a bad thing, and we should
882 * try to discourage it in the future. In particular, for the newer
883 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
884 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
Eric Sandeend9ff4742008-11-22 09:02:48 -0600885 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
886 * copying the primary superblock during online resize.
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500887 *
888 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
889 * unfortunately, we shouldn't ignore it since if it's not set in the
890 * backup, the extended attributes in the filesystem will be stripped
891 * away.
892 */
893#define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
894 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
Eric Sandeend9ff4742008-11-22 09:02:48 -0600895#define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
896 EXT3_FEATURE_INCOMPAT_RECOVER)
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500897
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400898int check_backup_super_block(e2fsck_t ctx)
899{
900 ext2_filsys fs = ctx->fs;
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400901 errcode_t retval;
902 dgrp_t g;
JP Abgralle0ed7402014-03-19 19:08:39 -0700903 blk64_t sb;
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400904 int ret = 0;
Theodore Ts'o982dd302009-04-23 01:27:07 -0400905 char buf[SUPERBLOCK_SIZE];
906 struct ext2_super_block *backup_sb;
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400907
908 /*
909 * If we are already writing out the backup blocks, then we
910 * don't need to test. Also, if the filesystem is invalid, or
911 * the check was aborted or cancelled, we also don't want to
912 * do the backup. If the filesystem was opened read-only then
913 * we can't do the backup.
914 */
915 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
916 !ext2fs_test_valid(fs) ||
917 (fs->super->s_state & EXT2_ERROR_FS) ||
918 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
919 (ctx->options & E2F_OPT_READONLY))
920 return 0;
921
922 for (g = 1; g < fs->group_desc_count; g++) {
923 if (!ext2fs_bg_has_super(fs, g))
924 continue;
925
JP Abgralle0ed7402014-03-19 19:08:39 -0700926 sb = ext2fs_group_first_block2(fs, g);
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400927
Theodore Ts'o982dd302009-04-23 01:27:07 -0400928 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
929 buf);
930 if (retval)
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400931 continue;
Theodore Ts'o982dd302009-04-23 01:27:07 -0400932 backup_sb = (struct ext2_super_block *) buf;
933#ifdef WORDS_BIGENDIAN
934 ext2fs_swap_super(backup_sb);
935#endif
936 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
937 (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
938 ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
939 EXT2_MAX_BLOCK_LOG_SIZE) ||
940 (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
941 continue;
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400942
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500943#define SUPER_INCOMPAT_DIFFERENT(x) \
Theodore Ts'o982dd302009-04-23 01:27:07 -0400944 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
945 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500946#define SUPER_RO_COMPAT_DIFFERENT(x) \
Theodore Ts'o982dd302009-04-23 01:27:07 -0400947 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
948 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500949#define SUPER_DIFFERENT(x) \
Theodore Ts'o982dd302009-04-23 01:27:07 -0400950 (fs->super->x != backup_sb->x)
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500951
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400952 if (SUPER_DIFFERENT(s_feature_compat) ||
Theodore Ts'oa8cde732008-01-26 23:17:50 -0500953 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
954 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400955 SUPER_DIFFERENT(s_blocks_count) ||
JP Abgralle0ed7402014-03-19 19:08:39 -0700956 SUPER_DIFFERENT(s_blocks_count_hi) ||
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400957 SUPER_DIFFERENT(s_inodes_count) ||
Theodore Ts'o982dd302009-04-23 01:27:07 -0400958 memcmp(fs->super->s_uuid, backup_sb->s_uuid,
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400959 sizeof(fs->super->s_uuid)))
960 ret = 1;
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400961 break;
962 }
963 return ret;
964}