📄 balloc.c
字号:
/* * linux/fs/ext2/balloc.c * * Copyright (C) 1992, 1993, 1994, 1995 * Remy Card (card@masi.ibp.fr) * Laboratoire MASI - Institut Blaise Pascal * Universite Pierre et Marie Curie (Paris VI) * * Enhanced block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 *//* * balloc.c contains the blocks allocation and deallocation routines *//* * The free blocks are managed by bitmaps. A file system contains several * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap * block for inodes, N blocks for the inode table and data blocks. * * The file system contains group descriptors which are located after the * super block. Each descriptor contains the number of the bitmap block and * the free blocks count in the block. The descriptors are loaded in memory * when a file system is mounted (see ext2_read_super). */#include <linux/fs.h>#include <linux/ext2_fs.h>#include <linux/stat.h>#include <linux/sched.h>#include <linux/string.h>#include <linux/locks.h>#include <asm/bitops.h>#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)static struct ext2_group_desc * get_group_desc (struct super_block * sb, unsigned int block_group, struct buffer_head ** bh){ unsigned long group_desc; unsigned long desc; struct ext2_group_desc * gdp; /* * This panic should never trigger on a bad filesystem: the caller * should have verified the block/group number already. */ if (block_group >= sb->u.ext2_sb.s_groups_count) ext2_panic (sb, "get_group_desc", "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->u.ext2_sb.s_groups_count); group_desc = block_group / EXT2_DESC_PER_BLOCK(sb); desc = block_group % EXT2_DESC_PER_BLOCK(sb); if (!sb->u.ext2_sb.s_group_desc[group_desc]) ext2_panic (sb, "get_group_desc", "Group descriptor not loaded - " "block_group = %d, group_desc = %lu, desc = %lu", block_group, group_desc, desc); gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[group_desc]->b_data; if (bh) *bh = sb->u.ext2_sb.s_group_desc[group_desc]; return gdp + desc;}/* * Read the bitmap for a given block_group, reading into the specified * slot in the superblock's bitmap cache. * * Return >=0 on success or a -ve error code. */static int read_block_bitmap (struct super_block * sb, unsigned int block_group, unsigned long bitmap_nr){ struct ext2_group_desc * gdp; struct buffer_head * bh; int retval = 0; gdp = get_group_desc (sb, block_group, NULL); bh = bread (sb->s_dev, gdp->bg_block_bitmap, sb->s_blocksize); if (!bh) { ext2_error (sb, "read_block_bitmap", "Cannot read block bitmap - " "block_group = %d, block_bitmap = %lu", block_group, (unsigned long) gdp->bg_block_bitmap); retval = -EIO; } /* * On IO error, just leave a zero in the superblock's block pointer for * this group. The IO will be retried next time. */ sb->u.ext2_sb.s_block_bitmap_number[bitmap_nr] = block_group; sb->u.ext2_sb.s_block_bitmap[bitmap_nr] = bh; return retval;}/* * load_block_bitmap loads the block bitmap for a blocks group * * It maintains a cache for the last bitmaps loaded. This cache is managed * with a LRU algorithm. * * Notes: * 1/ There is one cache per mounted file system. * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups, * this function reads the bitmap without maintaining a LRU cache. * * Return the slot used to store the bitmap, or a -ve error code. */static int load__block_bitmap (struct super_block * sb, unsigned int block_group){ int i, j, retval = 0; unsigned long block_bitmap_number; struct buffer_head * block_bitmap; if (block_group >= sb->u.ext2_sb.s_groups_count) ext2_panic (sb, "load_block_bitmap", "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->u.ext2_sb.s_groups_count); if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED) { if (sb->u.ext2_sb.s_block_bitmap[block_group]) { if (sb->u.ext2_sb.s_block_bitmap_number[block_group] != block_group) ext2_panic (sb, "load_block_bitmap", "block_group != block_bitmap_number"); else return block_group; } else { retval = read_block_bitmap (sb, block_group, block_group); if (retval < 0) return retval; return block_group; } } for (i = 0; i < sb->u.ext2_sb.s_loaded_block_bitmaps && sb->u.ext2_sb.s_block_bitmap_number[i] != block_group; i++) ; if (i < sb->u.ext2_sb.s_loaded_block_bitmaps && sb->u.ext2_sb.s_block_bitmap_number[i] == block_group) { block_bitmap_number = sb->u.ext2_sb.s_block_bitmap_number[i]; block_bitmap = sb->u.ext2_sb.s_block_bitmap[i]; for (j = i; j > 0; j--) { sb->u.ext2_sb.s_block_bitmap_number[j] = sb->u.ext2_sb.s_block_bitmap_number[j - 1]; sb->u.ext2_sb.s_block_bitmap[j] = sb->u.ext2_sb.s_block_bitmap[j - 1]; } sb->u.ext2_sb.s_block_bitmap_number[0] = block_bitmap_number; sb->u.ext2_sb.s_block_bitmap[0] = block_bitmap; /* * There's still one special case here --- if block_bitmap == 0 * then our last attempt to read the bitmap failed and we have * just ended up caching that failure. Try again to read it. */ if (!block_bitmap) retval = read_block_bitmap (sb, block_group, 0); } else { if (sb->u.ext2_sb.s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED) sb->u.ext2_sb.s_loaded_block_bitmaps++; else brelse (sb->u.ext2_sb.s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1]); for (j = sb->u.ext2_sb.s_loaded_block_bitmaps - 1; j > 0; j--) { sb->u.ext2_sb.s_block_bitmap_number[j] = sb->u.ext2_sb.s_block_bitmap_number[j - 1]; sb->u.ext2_sb.s_block_bitmap[j] = sb->u.ext2_sb.s_block_bitmap[j - 1]; } retval = read_block_bitmap (sb, block_group, 0); } return retval;}/* * Load the block bitmap for a given block group. First of all do a couple * of fast lookups for common cases and then pass the request onto the guts * of the bitmap loader. * * Return the slot number of the group in the superblock bitmap cache's on * success, or a -ve error code. * * There is still one inconsistancy here --- if the number of groups in this * filesystems is <= EXT2_MAX_GROUP_LOADED, then we have no way of * differentiating between a group for which we have never performed a bitmap * IO request, and a group for which the last bitmap read request failed. */static inline int load_block_bitmap (struct super_block * sb, unsigned int block_group){ int slot; /* * Do the lookup for the slot. First of all, check if we're asking * for the same slot as last time, and did we succeed that last time? */ if (sb->u.ext2_sb.s_loaded_block_bitmaps > 0 && sb->u.ext2_sb.s_block_bitmap_number[0] == block_group && sb->u.ext2_sb.s_block_bitmap[block_group]) { slot = 0; } /* * Or can we do a fast lookup based on a loaded group on a filesystem * small enough to be mapped directly into the superblock? */ else if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED && sb->u.ext2_sb.s_block_bitmap_number[block_group] == block_group && sb->u.ext2_sb.s_block_bitmap[block_group]) { slot = block_group; } /* * If not, then do a full lookup for this block group. */ else { slot = load__block_bitmap (sb, block_group); } /* * <0 means we just got an error */ if (slot < 0) return slot; /* * If it's a valid slot, we may still have cached a previous IO error, * in which case the bh in the superblock cache will be zero. */ if (!sb->u.ext2_sb.s_block_bitmap[slot]) return -EIO; /* * Must have been read in OK to get this far. */ return slot;}void ext2_free_blocks (const struct inode * inode, unsigned long block, unsigned long count){ struct buffer_head * bh; struct buffer_head * bh2; unsigned long block_group; unsigned long bit; unsigned long i; int bitmap_nr; struct super_block * sb; struct ext2_group_desc * gdp; struct ext2_super_block * es; sb = inode->i_sb; if (!sb) { printk ("ext2_free_blocks: nonexistent device"); return; } lock_super (sb); es = sb->u.ext2_sb.s_es; if (block < es->s_first_data_block || (block + count) > es->s_blocks_count) { ext2_error (sb, "ext2_free_blocks", "Freeing blocks not in datazone - " "block = %lu, count = %lu", block, count); unlock_super (sb); return; } ext2_debug ("freeing block %lu\n", block); block_group = (block - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb); bit = (block - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb); if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) { ext2_error (sb, "ext2_free_blocks", "Freeing blocks across group boundary - " "Block = %lu, count = %lu", block, count); unlock_super (sb); return; } bitmap_nr = load_block_bitmap (sb, block_group); if (bitmap_nr < 0) goto error_return; bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr]; gdp = get_group_desc (sb, block_group, &bh2); if (test_opt (sb, CHECK_STRICT) && (in_range (gdp->bg_block_bitmap, block, count) || in_range (gdp->bg_inode_bitmap, block, count) || in_range (block, gdp->bg_inode_table, sb->u.ext2_sb.s_itb_per_group) || in_range (block + count - 1, gdp->bg_inode_table, sb->u.ext2_sb.s_itb_per_group))) { ext2_error (sb, "ext2_free_blocks", "Freeing blocks in system zones - " "Block = %lu, count = %lu", block, count); unlock_super (sb); return; } for (i = 0; i < count; i++) { if (!clear_bit (bit + i, bh->b_data)) ext2_warning (sb, "ext2_free_blocks", "bit already cleared for block %lu", block); else { if (sb->dq_op) sb->dq_op->free_block(inode, fs_to_dq_blocks(1, sb->s_blocksize)); gdp->bg_free_blocks_count++; es->s_free_blocks_count++; } } mark_buffer_dirty(bh2, 1); mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1); mark_buffer_dirty(bh, 1); if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } sb->s_dirt = 1;error_return: unlock_super (sb); return;}/* * ext2_new_block uses a goal block to assist allocation. If the goal is * free, or there is a free block within 32 blocks of the goal, that block * is allocated. Otherwise a forward search is made for a free block; within * each block group the search first looks for an entire free byte in the block * bitmap, and then for any free bit if that fails. */int ext2_new_block (const struct inode * inode, unsigned long goal, u32 * prealloc_count, u32 * prealloc_block, int * err){ struct buffer_head * bh; struct buffer_head * bh2; char * p, * r; int i, j, k, tmp; int bitmap_nr; struct super_block * sb; struct ext2_group_desc * gdp; struct ext2_super_block * es; *err = -ENOSPC;#ifdef EXT2FS_DEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -