super.c revision b849f712000fa48e11e7ee8342362876783542ab
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/* 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * e2fsck.c - superblock checks 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o. 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * %Begin-Header% 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * This file may be redistributed under the terms of the GNU Public 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * License. 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * %End-Header% 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "config.h" 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifdef HAVE_ERRNO_H 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <errno.h> 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 17010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#ifndef EXT2_SKIP_UUID 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "uuid/uuid.h" 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "e2fsck.h" 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "problem.h" 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define MIN_CHECK 1 240de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)#define MAX_CHECK 2 250de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)static void check_super_value(e2fsck_t ctx, const char *descr, 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) unsigned long value, int flags, 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) unsigned long min_val, unsigned long max_val) 290de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles){ 300de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) struct problem_context pctx; 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (((flags & MIN_CHECK) && (value < min_val)) || 336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) ((flags & MAX_CHECK) && (value > max_val))) { 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) clear_problem_context(&pctx); 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pctx.num = value; 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pctx.str = descr; 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); 380de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ 390de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) } 400de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)} 410de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 420de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)/* 430de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) * helper function to release an inode 440de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) */ 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)struct process_block_struct { 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) e2fsck_t ctx; 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) char *buf; 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct problem_context *pctx; 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int truncating; 500de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) int truncate_offset; 510de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) e2_blkcnt_t truncate_block; 520de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) int truncated_blocks; 530de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) int abort; 540de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) errcode_t errcode; 550de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)}; 560de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 570de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)static int release_inode_block(ext2_filsys fs, 580de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) blk64_t *block_nr, 59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) e2_blkcnt_t blockcnt, 600de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) blk64_t ref_blk EXT2FS_ATTR((unused)), 610de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) int ref_offset EXT2FS_ATTR((unused)), 626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) void *priv_data) 630de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles){ 640de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) struct process_block_struct *pb; 650de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) e2fsck_t ctx; 666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) struct problem_context *pctx; 670de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) blk64_t blk = *block_nr; 680de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) int retval = 0; 690de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 700de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) pb = (struct process_block_struct *) priv_data; 710de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) ctx = pb->ctx; 720de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) pctx = pb->pctx; 730de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 740de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) pctx->blk = blk; 750de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) pctx->blkcount = blockcnt; 760de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (HOLE_BLKADDR(blk)) 780de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) return 0; 790de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if ((blk < fs->super->s_first_data_block) || 810de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) (blk >= ext2fs_blocks_count(fs->super))) { 820de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx); 830de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) return_abort: 840de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) pb->abort = 1; 850de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) return BLOCK_ABORT; 860de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) } 876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 880de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) { 890de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx); 900de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) goto return_abort; 910de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) } 920de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 930de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) /* 94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * If we are deleting an orphan, then we leave the fields alone. 95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * If we are truncating an orphan, then update the inode fields 96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * and clean up any partial block data. 97010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) */ 98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (pb->truncating) { 99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /* 100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * We only remove indirect blocks if they are 1016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) * completely empty. 1020de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) */ 103010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) if (blockcnt < 0) { 1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) int i, limit; 1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) blk_t *bp; 106010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 107010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) pb->errcode = io_channel_read_blk64(fs->io, blk, 1, 108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb->buf); 1096e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (pb->errcode) 1106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) goto return_abort; 111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 1120de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) limit = fs->blocksize >> 2; 113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (i = 0, bp = (blk_t *) pb->buf; 114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) i < limit; i++, bp++) 115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (*bp) 116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 0; 117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 1180de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) /* 119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) * We don't remove direct blocks until we've reached 1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) * the truncation block. 1216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) */ 1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (blockcnt >= 0 && blockcnt < pb->truncate_block) 123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 0; 124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) /* 1250de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) * If part of the last block needs truncating, we do 126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * it here. 127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if ((blockcnt == pb->truncate_block) && pb->truncate_offset) { 129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb->errcode = io_channel_read_blk64(fs->io, blk, 1, 130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb->buf); 131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (pb->errcode) 132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) goto return_abort; 133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) memset(pb->buf + pb->truncate_offset, 0, 134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) fs->blocksize - pb->truncate_offset); 135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb->errcode = io_channel_write_blk64(fs->io, blk, 1, 1366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) pb->buf); 1375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (pb->errcode) 1385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) goto return_abort; 139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 1405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) pb->truncated_blocks++; 1415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) *block_nr = 0; 142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) retval |= BLOCK_CHANGED; 143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 1445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 1455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ext2fs_block_alloc_stats2(fs, blk, -1); 146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ctx->free_blocks++; 147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return retval; 148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 1490de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 1500de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)/* 151c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch * This function releases an inode. Returns 1 if an inconsistency was 1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * found. If the inode has a link count, then it is being truncated and 1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * not deleted. 154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino, 156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct ext2_inode *inode, char *block_buf, 157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct problem_context *pctx) 158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles){ 159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct process_block_struct pb; 1606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) ext2_filsys fs = ctx->fs; 1616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) errcode_t retval; 162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) __u32 count; 163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (!ext2fs_inode_has_valid_blocks2(fs, inode)) 165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 0; 1666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb.buf = block_buf + 3 * ctx->fs->blocksize; 168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb.ctx = ctx; 169c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch pb.abort = 0; 1706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) pb.errcode = 0; 171c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch pb.pctx = pctx; 1726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (inode->i_links_count) { 173c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch pb.truncating = 1; 174c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch pb.truncate_block = (e2_blkcnt_t) 175c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch ((EXT2_I_SIZE(inode) + fs->blocksize - 1) / 1766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) fs->blocksize); 177c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch pb.truncate_offset = inode->i_size % fs->blocksize; 178c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch } else { 179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb.truncating = 0; 180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pb.truncate_block = 0; 1816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) pb.truncate_offset = 0; 182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 1836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) pb.truncated_blocks = 0; 1840de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE, 185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) block_buf, release_inode_block, &pb); 1865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (retval) { 1871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci com_err("release_inode_blocks", retval, 1881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci _("while calling ext2fs_block_iterate for inode %d"), 1895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ino); 1905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu return 1; 191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 1925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (pb.abort) 193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 1; 1946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 1956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) /* Refresh the inode since ext2fs_block_iterate may have changed it */ 196cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks"); 197cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 1985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (pb.truncated_blocks) 1995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks); 2005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 2016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (ext2fs_file_acl_block(fs, inode)) { 2025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) retval = ext2fs_adjust_ea_refcount2(fs, 203cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ext2fs_file_acl_block(fs, inode), 204cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) block_buf, -1, &count); 205cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) { 206cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) retval = 0; 207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) count = 1; 208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (retval) { 210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) com_err("release_inode_blocks", retval, 2116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) _("while calling ext2fs_adjust_ea_refcount2 for inode %d"), 212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ino); 213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 1; 214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 215effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch if (count == 0) { 216effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch ext2fs_block_alloc_stats2(fs, 217effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch ext2fs_file_acl_block(fs, inode), -1); 2185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ctx->free_blocks++; 2191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci } 220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ext2fs_file_acl_block_set(fs, inode, 0); 221a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 2225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu return 0; 223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/* 226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * This function releases all of the orphan inodes. It returns 1 if 227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * it hit some error, and 0 on success. 228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 2296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)static int release_orphan_inodes(e2fsck_t ctx) 230c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch{ 231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ext2_filsys fs = ctx->fs; 232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ext2_ino_t ino, next_ino; 233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct ext2_inode inode; 234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct problem_context pctx; 235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) char *block_buf; 236cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if ((ino = fs->super->s_last_orphan) == 0) 238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 0; 239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 240cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /* 2416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) * Win or lose, we won't be using the head of the orphan inode 242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * list again. 2430de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) */ 244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) fs->super->s_last_orphan = 0; 2450de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) ext2fs_mark_super_dirty(fs); 246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /* 248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * If the filesystem contains errors, don't run the orphan 2495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * list, since the orphan list can't be trusted; and we're 2506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) * going to be running a full e2fsck run anyway... 251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 2526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (fs->super->s_state & EXT2_ERROR_FS) 2536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) return 0; 2545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 2556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if ((ino < EXT2_FIRST_INODE(fs->super)) || 2565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) (ino > fs->super->s_inodes_count)) { 2576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) clear_problem_context(&pctx); 258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pctx.ino = ino; 259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx); 260a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 1; 261a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 263effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4, 264effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch "block iterate buffer"); 265effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch e2fsck_read_bitmaps(ctx); 266effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 267effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch while (ino) { 268effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes"); 2696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) clear_problem_context(&pctx); 2706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) pctx.ino = ino; 271a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pctx.inode = &inode; 272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pctx.str = inode.i_links_count ? _("Truncating") : 273a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) _("Clearing"); 274a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 275a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx); 276a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 277a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) next_ino = inode.i_dtime; 278a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (next_ino && 279a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ((next_ino < EXT2_FIRST_INODE(fs->super)) || 280a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) (next_ino > fs->super->s_inodes_count))) { 281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pctx.ino = next_ino; 282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx); 2836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) goto return_abort; 2846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) } 2856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 2866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx)) 2876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) goto return_abort; 2886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 2896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (!inode.i_links_count) { 2906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) ext2fs_inode_alloc_stats2(fs, ino, -1, 2916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) LINUX_S_ISDIR(inode.i_mode)); 2926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) ctx->free_inodes++; 2936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) inode.i_dtime = ctx->now; 2946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) } else { 295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) inode.i_dtime = 0; 296a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 297a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) e2fsck_write_inode(ctx, ino, &inode, "delete_file"); 298a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ino = next_ino; 299a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 300a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ext2fs_free_mem(&block_buf); 301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 0; 302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)return_abort: 303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ext2fs_free_mem(&block_buf); 304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return 1; 305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/* 308a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Check the resize inode to make sure it is sane. We check both for 309a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * the case where on-line resizing is not enabled (in which case the 310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * resize inode should be cleared) as well as the case where on-line 311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * resizing is enabled. 312a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 3135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void check_resize_inode(e2fsck_t ctx) 3145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles){ 3155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) ext2_filsys fs = ctx->fs; 3165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) struct ext2_inode inode; 317a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct problem_context pctx; 3185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) int i, gdt_off, ind_off; 3195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) dgrp_t j; 3206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) blk_t blk, pblk; 321a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) blk_t expect; /* for resize inode, which is 32-bit only */ 3221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci __u32 *dind_buf = 0, *ind_buf; 323cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) errcode_t retval; 324cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 3256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) clear_problem_context(&pctx); 3266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 327cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /* 3286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) * If the resize inode feature isn't set, then 329a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * s_reserved_gdt_blocks must be zero. 330a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 331cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (!(fs->super->s_feature_compat & 332cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXT2_FEATURE_COMPAT_RESIZE_INODE)) { 333cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (fs->super->s_reserved_gdt_blocks) { 334cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) pctx.num = fs->super->s_reserved_gdt_blocks; 3356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS, 336cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) &pctx)) { 337cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) fs->super->s_reserved_gdt_blocks = 0; 338cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ext2fs_mark_super_dirty(fs); 339cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 340cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 341cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 342cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 343cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /* Read the resize inode */ 344cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) pctx.ino = EXT2_RESIZE_INO; 345cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode); 346cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (retval) { 347cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (fs->super->s_feature_compat & 348cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXT2_FEATURE_COMPAT_RESIZE_INODE) 349cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ctx->flags |= E2F_FLAG_RESIZE_INODE; 350cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return; 351cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 352cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 3536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) /* 354cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * If the resize inode feature isn't set, check to make sure 3556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) * the resize inode is cleared; then we're done. 356cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) */ 357cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (!(fs->super->s_feature_compat & 358cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXT2_FEATURE_COMPAT_RESIZE_INODE)) { 359cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) for (i=0; i < EXT2_N_BLOCKS; i++) { 360cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (inode.i_block[i]) 361cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) break; 362cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 363cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if ((i < EXT2_N_BLOCKS) && 364cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) { 3656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) memset(&inode, 0, sizeof(inode)); 366cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode, 367cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) "clear_resize"); 368cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 369cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return; 370cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 371cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 372cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /* 373cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * The resize inode feature is enabled; check to make sure the 374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * only block in use is the double indirect block 375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */ 376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) blk = inode.i_block[EXT2_DIND_BLOCK]; 377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (i=0; i < EXT2_N_BLOCKS; i++) { 378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (i != EXT2_DIND_BLOCK && inode.i_block[i]) 379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) break; 380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 381a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count || 382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) !(inode.i_mode & LINUX_S_IFREG) || 383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) (blk < fs->super->s_first_data_block || 384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) blk >= ext2fs_blocks_count(fs->super))) { 385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) resize_inode_invalid: 3865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) { 3875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) memset(&inode, 0, sizeof(inode)); 388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode, 389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) "clear_resize"); 390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ctx->flags |= E2F_FLAG_RESIZE_INODE; 391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (!(ctx->options & E2F_OPT_READONLY)) { 3935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) fs->super->s_state &= ~EXT2_VALID_FS; 394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ext2fs_mark_super_dirty(fs); 395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) goto cleanup; 397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2, 399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) "resize dind buffer"); 400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize); 401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) retval = ext2fs_read_ind_block(fs, blk, dind_buf); 403a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (retval) 404a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) goto resize_inode_invalid; 405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) gdt_off = fs->desc_blocks; 407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks; 408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0) 409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pblk++; /* Deal with 1024 blocksize bigalloc fs */ 410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4; 411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) i++, gdt_off++, pblk++) { 412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) gdt_off %= fs->blocksize/4; 413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (dind_buf[gdt_off] != pblk) 414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) goto resize_inode_invalid; 415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) retval = ext2fs_read_ind_block(fs, pblk, ind_buf); 416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (retval) 417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) goto resize_inode_invalid; 418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ind_off = 0; 419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (j = 1; j < fs->group_desc_count; j++) { 4206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (!ext2fs_bg_has_super(fs, j)) 4210de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) continue; 4220de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) expect = pblk + (j * fs->super->s_blocks_per_group); 4236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (ind_buf[ind_off] != expect) 4246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) goto resize_inode_invalid; 425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ind_off++; 426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 4296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)cleanup: 430a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (dind_buf) 431a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ext2fs_free_mem(&dind_buf); 432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 433a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 434a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 435a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/* 4365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * This function checks the dirhash signed/unsigned hint if necessary. 4376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) */ 438a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)static void e2fsck_fix_dirhash_hint(e2fsck_t ctx) 439a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles){ 440a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) struct ext2_super_block *sb = ctx->fs->super; 4416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) struct problem_context pctx; 442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) char c; 443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 444cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if ((ctx->options & E2F_OPT_READONLY) || 445cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) || 446cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH))) 447cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) return; 448cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 449cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) c = (char) 255; 450cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 451cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) clear_problem_context(&pctx); 452cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) { 453cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (((int) c) == -1) { 454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) sb->s_flags |= EXT2_FLAGS_SIGNED_HASH; 455cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } else { 456cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH; 4576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) } 4586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) ext2fs_mark_super_dirty(ctx->fs); 4596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) } 4606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)} 4616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 4626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 4636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void check_super_block(e2fsck_t ctx) 4646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles){ 4656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) ext2_filsys fs = ctx->fs; 4666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) blk64_t first_block, last_block; 4676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) struct ext2_super_block *sb = fs->super; 4686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) unsigned int ipg_max; 4696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) problem_t problem; 4706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) blk64_t blocks_per_group = fs->super->s_blocks_per_group; 4716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) __u32 bpg_max, cpg_max; 4726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) int inodes_per_block; 4736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) int inode_size; 4746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) int accept_time_fudge; 4756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) int broken_system_clock; 4766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) dgrp_t i; 477a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) blk64_t should_be; 4785c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu struct problem_context pctx; 4796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) blk64_t free_blocks = 0; 480a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ino_t free_inodes = 0; 481a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int csum_flag, clear_test_fs_flag; 4826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 4830de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super); 4840de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) ipg_max = inodes_per_block * (blocks_per_group - 4); 485a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb)) 486a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ipg_max = EXT2_MAX_INODES_PER_GROUP(sb); 4876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) cpg_max = 8 * EXT2_BLOCK_SIZE(sb); 4886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb)) 489a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb); 4906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs); 4910de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb)) 4920de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb); 493010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 494010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, 4950de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap"); 496a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, 4975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) sizeof(int) * fs->group_desc_count, "invalid_block_bitmap"); 498a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx, 499010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) sizeof(int) * fs->group_desc_count, "invalid_inode_table"); 5000de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) 5010de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) clear_problem_context(&pctx); 502a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 503010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) /* 504010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) * Verify the super block constants... 505010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) */ 506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) check_super_value(ctx, "inodes_count", sb->s_inodes_count, 507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) MIN_CHECK, 1, 0); 5086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb), 509010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) MIN_CHECK, 1, 0); 510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) check_super_value(ctx, "first_data_block", sb->s_first_data_block, 5116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) MAX_CHECK, 0, ext2fs_blocks_count(sb)); 512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) check_super_value(ctx, "log_block_size", sb->s_log_block_size, 513a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) MIN_CHECK | MAX_CHECK, 0, 514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE); 515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) check_super_value(ctx, "log_cluster_size", 5160de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) sb->s_log_cluster_size, 5170de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles) MIN_CHECK | MAX_CHECK, sb->s_log_block_size, 518a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) (EXT2_MAX_CLUSTER_LOG_SIZE - 519a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXT2_MIN_CLUSTER_LOG_SIZE)); 520a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group, 521a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) MIN_CHECK | MAX_CHECK, 8, cpg_max); 522a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group, 523 MIN_CHECK | MAX_CHECK, 8, bpg_max); 524 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group, 525 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max); 526 check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb), 527 MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2); 528 check_super_value(ctx, "reserved_gdt_blocks", 529 sb->s_reserved_gdt_blocks, MAX_CHECK, 0, 530 fs->blocksize/4); 531 if (sb->s_rev_level > EXT2_GOOD_OLD_REV) 532 check_super_value(ctx, "first_ino", sb->s_first_ino, 533 MIN_CHECK | MAX_CHECK, 534 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count); 535 inode_size = EXT2_INODE_SIZE(sb); 536 check_super_value(ctx, "inode_size", 537 inode_size, MIN_CHECK | MAX_CHECK, 538 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize); 539 if (sb->s_blocks_per_group != (sb->s_clusters_per_group * 540 EXT2FS_CLUSTER_RATIO(fs))) { 541 pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs); 542 pctx.str = "block_size"; 543 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); 544 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ 545 return; 546 } 547 if (inode_size & (inode_size - 1)) { 548 pctx.num = inode_size; 549 pctx.str = "inode_size"; 550 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); 551 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ 552 return; 553 } 554 555 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) && 556 (ctx->num_blocks < ext2fs_blocks_count(sb))) { 557 pctx.blk = ext2fs_blocks_count(sb); 558 pctx.blk2 = ctx->num_blocks; 559 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) { 560 ctx->flags |= E2F_FLAG_ABORT; 561 return; 562 } 563 } 564 565 should_be = (sb->s_log_block_size == 0 && 566 EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0; 567 if (sb->s_first_data_block != should_be) { 568 pctx.blk = sb->s_first_data_block; 569 pctx.blk2 = should_be; 570 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx); 571 ctx->flags |= E2F_FLAG_ABORT; 572 return; 573 } 574 575 should_be = sb->s_inodes_per_group * fs->group_desc_count; 576 if (sb->s_inodes_count != should_be) { 577 pctx.ino = sb->s_inodes_count; 578 pctx.ino2 = should_be; 579 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) { 580 sb->s_inodes_count = should_be; 581 ext2fs_mark_super_dirty(fs); 582 } 583 } 584 585 /* Is 64bit set and extents unset? */ 586 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, 587 EXT4_FEATURE_INCOMPAT_64BIT) && 588 !EXT2_HAS_INCOMPAT_FEATURE(fs->super, 589 EXT3_FEATURE_INCOMPAT_EXTENTS) && 590 fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) { 591 fs->super->s_feature_incompat |= 592 EXT3_FEATURE_INCOMPAT_EXTENTS; 593 ext2fs_mark_super_dirty(fs); 594 } 595 596 /* 597 * Verify the group descriptors.... 598 */ 599 first_block = sb->s_first_data_block; 600 last_block = ext2fs_blocks_count(sb)-1; 601 602 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 603 EXT4_FEATURE_RO_COMPAT_GDT_CSUM); 604 for (i = 0; i < fs->group_desc_count; i++) { 605 pctx.group = i; 606 607 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super, 608 EXT4_FEATURE_INCOMPAT_FLEX_BG)) { 609 first_block = ext2fs_group_first_block2(fs, i); 610 last_block = ext2fs_group_last_block2(fs, i); 611 } 612 613 if ((ext2fs_block_bitmap_loc(fs, i) < first_block) || 614 (ext2fs_block_bitmap_loc(fs, i) > last_block)) { 615 pctx.blk = ext2fs_block_bitmap_loc(fs, i); 616 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) 617 ext2fs_block_bitmap_loc_set(fs, i, 0); 618 } 619 if (ext2fs_block_bitmap_loc(fs, i) == 0) { 620 ctx->invalid_block_bitmap_flag[i]++; 621 ctx->invalid_bitmaps++; 622 } 623 if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) || 624 (ext2fs_inode_bitmap_loc(fs, i) > last_block)) { 625 pctx.blk = ext2fs_inode_bitmap_loc(fs, i); 626 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) 627 ext2fs_inode_bitmap_loc_set(fs, i, 0); 628 } 629 if (ext2fs_inode_bitmap_loc(fs, i) == 0) { 630 ctx->invalid_inode_bitmap_flag[i]++; 631 ctx->invalid_bitmaps++; 632 } 633 if ((ext2fs_inode_table_loc(fs, i) < first_block) || 634 ((ext2fs_inode_table_loc(fs, i) + 635 fs->inode_blocks_per_group - 1) > last_block)) { 636 pctx.blk = ext2fs_inode_table_loc(fs, i); 637 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) 638 ext2fs_inode_table_loc_set(fs, i, 0); 639 } 640 if (ext2fs_inode_table_loc(fs, i) == 0) { 641 ctx->invalid_inode_table_flag[i]++; 642 ctx->invalid_bitmaps++; 643 } 644 free_blocks += ext2fs_bg_free_blocks_count(fs, i); 645 free_inodes += ext2fs_bg_free_inodes_count(fs, i); 646 647 if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) || 648 (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) || 649 (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group)) 650 ext2fs_unmark_valid(fs); 651 652 should_be = 0; 653 if (!ext2fs_group_desc_csum_verify(fs, i)) { 654 pctx.csum1 = ext2fs_bg_checksum(fs, i); 655 pctx.csum2 = ext2fs_group_desc_csum(fs, i); 656 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) { 657 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); 658 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT); 659 ext2fs_bg_itable_unused_set(fs, i, 0); 660 should_be = 1; 661 } 662 ext2fs_unmark_valid(fs); 663 } 664 665 if (!csum_flag && 666 (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) || 667 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) || 668 ext2fs_bg_itable_unused(fs, i) != 0)) { 669 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) { 670 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); 671 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT); 672 ext2fs_bg_itable_unused_set(fs, i, 0); 673 should_be = 1; 674 } 675 ext2fs_unmark_valid(fs); 676 } 677 678 if (i == fs->group_desc_count - 1 && 679 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) { 680 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) { 681 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); 682 should_be = 1; 683 } 684 ext2fs_unmark_valid(fs); 685 } 686 687 if (csum_flag && 688 (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) || 689 ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) { 690 pctx.blk = ext2fs_bg_itable_unused(fs, i); 691 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) { 692 ext2fs_bg_itable_unused_set(fs, i, 0); 693 should_be = 1; 694 } 695 ext2fs_unmark_valid(fs); 696 } 697 698 if (should_be) 699 ext2fs_group_desc_csum_set(fs, i); 700 /* If the user aborts e2fsck by typing ^C, stop right away */ 701 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 702 return; 703 } 704 705 ctx->free_blocks = EXT2FS_C2B(fs, free_blocks); 706 ctx->free_inodes = free_inodes; 707 708 if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) || 709 (sb->s_free_inodes_count > sb->s_inodes_count)) 710 ext2fs_unmark_valid(fs); 711 712 713 /* 714 * If we have invalid bitmaps, set the error state of the 715 * filesystem. 716 */ 717 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) { 718 sb->s_state &= ~EXT2_VALID_FS; 719 ext2fs_mark_super_dirty(fs); 720 } 721 722 clear_problem_context(&pctx); 723 724#ifndef EXT2_SKIP_UUID 725 /* 726 * If the UUID field isn't assigned, assign it. 727 */ 728 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) { 729 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) { 730 uuid_generate(sb->s_uuid); 731 fs->flags |= EXT2_FLAG_DIRTY; 732 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 733 } 734 } 735#endif 736 737 /* 738 * Check to see if we should disable the test_fs flag 739 */ 740 profile_get_boolean(ctx->profile, "options", 741 "clear_test_fs_flag", 0, 1, 742 &clear_test_fs_flag); 743 if (!(ctx->options & E2F_OPT_READONLY) && 744 clear_test_fs_flag && 745 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) && 746 (fs_proc_check("ext4") || check_for_modules("ext4"))) { 747 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) { 748 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS; 749 fs->flags |= EXT2_FLAG_DIRTY; 750 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 751 } 752 } 753 754 /* 755 * For the Hurd, check to see if the filetype option is set, 756 * since it doesn't support it. 757 */ 758 if (!(ctx->options & E2F_OPT_READONLY) && 759 fs->super->s_creator_os == EXT2_OS_HURD && 760 (fs->super->s_feature_incompat & 761 EXT2_FEATURE_INCOMPAT_FILETYPE)) { 762 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) { 763 fs->super->s_feature_incompat &= 764 ~EXT2_FEATURE_INCOMPAT_FILETYPE; 765 ext2fs_mark_super_dirty(fs); 766 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 767 } 768 } 769 770 /* 771 * If we have any of the compatibility flags set, we need to have a 772 * revision 1 filesystem. Most kernels will not check the flags on 773 * a rev 0 filesystem and we may have corruption issues because of 774 * the incompatible changes to the filesystem. 775 */ 776 if (!(ctx->options & E2F_OPT_READONLY) && 777 fs->super->s_rev_level == EXT2_GOOD_OLD_REV && 778 (fs->super->s_feature_compat || 779 fs->super->s_feature_ro_compat || 780 fs->super->s_feature_incompat) && 781 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) { 782 ext2fs_update_dynamic_rev(fs); 783 ext2fs_mark_super_dirty(fs); 784 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 785 } 786 787 /* 788 * Clean up any orphan inodes, if present. 789 */ 790 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) { 791 fs->super->s_state &= ~EXT2_VALID_FS; 792 ext2fs_mark_super_dirty(fs); 793 } 794 795 /* 796 * Unfortunately, due to Windows' unfortunate design decision 797 * to configure the hardware clock to tick localtime, instead 798 * of the more proper and less error-prone UTC time, many 799 * users end up in the situation where the system clock is 800 * incorrectly set at the time when e2fsck is run. 801 * 802 * Historically this was usually due to some distributions 803 * having buggy init scripts and/or installers that didn't 804 * correctly detect this case and take appropriate 805 * countermeasures. However, it's still possible, despite the 806 * best efforts of init script and installer authors to not be 807 * able to detect this misconfiguration, usually due to a 808 * buggy or misconfigured virtualization manager or the 809 * installer not having access to a network time server during 810 * the installation process. So by default, we allow the 811 * superblock times to be fudged by up to 24 hours. This can 812 * be disabled by setting options.accept_time_fudge to the 813 * boolean value of false in e2fsck.conf. We also support 814 * options.buggy_init_scripts for backwards compatibility. 815 */ 816 profile_get_boolean(ctx->profile, "options", "accept_time_fudge", 817 0, 1, &accept_time_fudge); 818 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts", 819 0, accept_time_fudge, &accept_time_fudge); 820 ctx->time_fudge = accept_time_fudge ? 86400 : 0; 821 822 profile_get_boolean(ctx->profile, "options", "broken_system_clock", 823 0, 0, &broken_system_clock); 824 825 /* 826 * Check to see if the superblock last mount time or last 827 * write time is in the future. 828 */ 829 if (!broken_system_clock && 830 !(ctx->flags & E2F_FLAG_TIME_INSANE) && 831 fs->super->s_mtime > (__u32) ctx->now) { 832 pctx.num = fs->super->s_mtime; 833 problem = PR_0_FUTURE_SB_LAST_MOUNT; 834 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge) 835 problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED; 836 if (fix_problem(ctx, problem, &pctx)) { 837 fs->super->s_mtime = ctx->now; 838 fs->flags |= EXT2_FLAG_DIRTY; 839 } 840 } 841 if (!broken_system_clock && 842 !(ctx->flags & E2F_FLAG_TIME_INSANE) && 843 fs->super->s_wtime > (__u32) ctx->now) { 844 pctx.num = fs->super->s_wtime; 845 problem = PR_0_FUTURE_SB_LAST_WRITE; 846 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge) 847 problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED; 848 if (fix_problem(ctx, problem, &pctx)) { 849 fs->super->s_wtime = ctx->now; 850 fs->flags |= EXT2_FLAG_DIRTY; 851 } 852 } 853 854 /* 855 * Move the ext3 journal file, if necessary. 856 */ 857 e2fsck_move_ext3_journal(ctx); 858 859 /* 860 * Fix journal hint, if necessary 861 */ 862 e2fsck_fix_ext3_journal_hint(ctx); 863 864 /* 865 * Add dirhash hint if necessary 866 */ 867 e2fsck_fix_dirhash_hint(ctx); 868 869 /* 870 * Hide quota inodes if necessary. 871 */ 872 e2fsck_hide_quota(ctx); 873 874 return; 875} 876 877/* 878 * Check to see if we should backup the master sb to the backup super 879 * blocks. Returns non-zero if the sb should be backed up. 880 */ 881 882/* 883 * A few flags are set on the fly by the kernel, but only in the 884 * primary superblock. This is actually a bad thing, and we should 885 * try to discourage it in the future. In particular, for the newer 886 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and 887 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the 888 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when 889 * copying the primary superblock during online resize. 890 * 891 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but 892 * unfortunately, we shouldn't ignore it since if it's not set in the 893 * backup, the extended attributes in the filesystem will be stripped 894 * away. 895 */ 896#define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \ 897 EXT4_FEATURE_RO_COMPAT_DIR_NLINK) 898#define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \ 899 EXT3_FEATURE_INCOMPAT_RECOVER) 900 901int check_backup_super_block(e2fsck_t ctx) 902{ 903 ext2_filsys fs = ctx->fs; 904 errcode_t retval; 905 dgrp_t g; 906 blk64_t sb; 907 int ret = 0; 908 char buf[SUPERBLOCK_SIZE]; 909 struct ext2_super_block *backup_sb; 910 911 /* 912 * If we are already writing out the backup blocks, then we 913 * don't need to test. Also, if the filesystem is invalid, or 914 * the check was aborted or cancelled, we also don't want to 915 * do the backup. If the filesystem was opened read-only then 916 * we can't do the backup. 917 */ 918 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) || 919 !ext2fs_test_valid(fs) || 920 (fs->super->s_state & EXT2_ERROR_FS) || 921 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) || 922 (ctx->options & E2F_OPT_READONLY)) 923 return 0; 924 925 for (g = 1; g < fs->group_desc_count; g++) { 926 if (!ext2fs_bg_has_super(fs, g)) 927 continue; 928 929 sb = ext2fs_group_first_block2(fs, g); 930 931 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE, 932 buf); 933 if (retval) 934 continue; 935 backup_sb = (struct ext2_super_block *) buf; 936#ifdef WORDS_BIGENDIAN 937 ext2fs_swap_super(backup_sb); 938#endif 939 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) || 940 (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) || 941 ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) > 942 EXT2_MAX_BLOCK_LOG_SIZE) || 943 (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE)) 944 continue; 945 946#define SUPER_INCOMPAT_DIFFERENT(x) \ 947 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \ 948 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE)) 949#define SUPER_RO_COMPAT_DIFFERENT(x) \ 950 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \ 951 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE)) 952#define SUPER_DIFFERENT(x) \ 953 (fs->super->x != backup_sb->x) 954 955 if (SUPER_DIFFERENT(s_feature_compat) || 956 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) || 957 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) || 958 SUPER_DIFFERENT(s_blocks_count) || 959 SUPER_DIFFERENT(s_inodes_count) || 960 memcmp(fs->super->s_uuid, backup_sb->s_uuid, 961 sizeof(fs->super->s_uuid))) 962 ret = 1; 963 break; 964 } 965 return ret; 966} 967