/* * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2013, 2014 * The President and Fellows of Harvard College. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include /* for CHAR_BIT */ #include /* also for CHAR_BIT */ #include #include #include #include #include "compat.h" #include #include "utils.h" #include "sfs.h" #include "sb.h" #include "freemap.h" #include "main.h" static unsigned long blocksinuse = 0; static uint8_t *freemapdata; static uint8_t *tofreedata; /* * Allocate space to keep track of the free block bitmap. This is * called after the superblock is loaded so we can ask how big the * volume is. */ void freemap_setup(void) { size_t i, mapbytes; uint32_t fsblocks, mapblocks; fsblocks = sb_totalblocks(); mapblocks = sb_freemapblocks(); mapbytes = mapblocks * SFS_BLOCKSIZE; freemapdata = domalloc(mapbytes * sizeof(uint8_t)); tofreedata = domalloc(mapbytes * sizeof(uint8_t)); for (i=0; i INOMAX_D) got partially completed. */ void freemap_blockfree(uint32_t block) { unsigned index = block/8; uint8_t mask = ((uint8_t)1)<<(block%8); if (tofreedata[index] & mask) { /* already marked to free once, ignore */ return; } if (freemapdata[index] & mask) { /* block is used elsewhere, ignore */ return; } tofreedata[index] |= mask; } /* * Count the number of bits set. */ static int countbits(uint8_t val) { uint8_t x; int ct=0; for (x=1; x; x<<=1) { if (val & x) ct++; } return ct; } /* * Print a complaint about freemap bits being wrong. * * FREEMAPBLOCK is the block number within the freemap; BYTE is the * byte offset within that block; VAL is the byte value; WHAT is a * string indicating what happened. */ static void reportfreemap(uint32_t mapblock, uint32_t byte, uint8_t val, const char *what) { uint8_t x, y; uint32_t blocknum; for (x=1, y=0; x; x<<=1, y++) { if (val & x) { blocknum = mapblock*SFS_BITSPERBLOCK + byte*CHAR_BIT + y; warnx("Block %lu erroneously shown %s in freemap", (unsigned long) blocknum, what); } } } /* * Scan the freemap. * * This is called after (at the end of) pass 1, when we've recursively * found all the reachable blocks and marked them. */ void freemap_check(void) { uint8_t actual[SFS_BLOCKSIZE], *expected, *tofree, tmp; uint32_t alloccount=0, freecount=0, i, j; int bchanged; uint32_t bitblocks; bitblocks = sb_freemapblocks(); for (i=0; i 0) { warnx("%lu blocks erroneously shown free in freemap (fixed)", (unsigned long) alloccount); setbadness(EXIT_RECOV); } if (freecount > 0) { warnx("%lu blocks erroneously shown used in freemap (fixed)", (unsigned long) freecount); setbadness(EXIT_RECOV); } } /* * Return the total number of blocks in use, which we count during * pass 1. */ unsigned long freemap_blocksused(void) { return blocksinuse; }