diff --git a/sys/gnu/ext2fs/alpha-bitops.h b/sys/gnu/ext2fs/alpha-bitops.h new file mode 100644 index 000000000000..3910da8a323b --- /dev/null +++ b/sys/gnu/ext2fs/alpha-bitops.h @@ -0,0 +1,255 @@ +/* $FreeBSD$ */ +#ifndef _ALPHA_BITOPS_H +#define _ALPHA_BITOPS_H + +/* + * Copyright 1994, Linus Torvalds. + */ + +/* + * These have to be done with inline assembly: that way the bit-setting + * is guaranteed to be atomic. All bit operations return 0 if the bit + * was cleared before the operation and != 0 if it was not. + * + * To get proper branch prediction for the main line, we must branch + * forward to code at the end of this object's .text section, then + * branch back to restart the operation. + * + * bit 0 is the LSB of addr; bit 64 is the LSB of (addr+1). + */ +static __inline unsigned int set_bit(unsigned long, volatile void *); +static __inline unsigned int clear_bit(unsigned long, volatile void *); +static __inline unsigned int change_bit(unsigned long, volatile void *); +static __inline unsigned int test_bit(int, volatile void *); +static __inline unsigned long ffz_b(unsigned long x); +static __inline unsigned long ffz(unsigned long); +/* static __inline int ffs(int); */ +static __inline void * memscan(void *, int, size_t); +#ifdef __alpha_cix__ +static __inline unsigned long hweight64(unsigned long); +#endif +static __inline unsigned long +find_next_zero_bit(void *, unsigned long, unsigned long); + +static __inline unsigned int set_bit(unsigned long nr, volatile void * addr) +{ + unsigned long oldbit; + unsigned long temp; + volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5); + + __asm__ __volatile__( + "1: ldl_l %0,%1\n" + " and %0,%3,%2\n" + " bne %2,2f\n" + " xor %0,%3,%0\n" + " stl_c %0,%1\n" + " beq %0,3f\n" + "2:\n" + ".section .text2,\"ax\"\n" + "3: br 1b\n" + ".previous" + :"=&r" (temp), "=m" (*m), "=&r" (oldbit) + :"Ir" (1UL << (nr & 31)), "m" (*m)); + return oldbit; +} + +static __inline unsigned int clear_bit(unsigned long nr, volatile void * addr) +{ + unsigned long oldbit; + unsigned long temp; + volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5); + + __asm__ __volatile__( + "1: ldl_l %0,%1\n" + " and %0,%3,%2\n" + " beq %2,2f\n" + " xor %0,%3,%0\n" + " stl_c %0,%1\n" + " beq %0,3f\n" + "2:\n" + ".section .text2,\"ax\"\n" + "3: br 1b\n" + ".previous" + :"=&r" (temp), "=m" (*m), "=&r" (oldbit) + :"Ir" (1UL << (nr & 31)), "m" (*m)); + return oldbit; +} + +static __inline unsigned int change_bit(unsigned long nr, volatile void * addr) +{ + unsigned long oldbit; + unsigned long temp; + volatile unsigned int *m = ((volatile unsigned int *) addr) + (nr >> 5); + + __asm__ __volatile__( + "1: ldl_l %0,%1\n" + " xor %0,%2,%0\n" + " stl_c %0,%1\n" + " beq %0,3f\n" + ".section .text2,\"ax\"\n" + "3: br 1b\n" + ".previous" + :"=&r" (temp), "=m" (*m), "=&r" (oldbit) + :"Ir" (1UL << (nr & 31)), "m" (*m)); + return oldbit; +} + +static __inline unsigned int test_bit(int nr, volatile void * addr) +{ + return 1UL & (((volatile int *) addr)[nr >> 5] >> (nr & 31)); +} + +/* + * ffz = Find First Zero in word. Undefined if no zero exists, + * so code should check against ~0UL first.. + * + * Do a binary search on the bits. Due to the nature of large + * constants on the alpha, it is worthwhile to split the search. + */ +static __inline unsigned long ffz_b(unsigned long x) +{ + unsigned long sum = 0; + + x = ~x & -~x; /* set first 0 bit, clear others */ + if (x & 0xF0) sum += 4; + if (x & 0xCC) sum += 2; + if (x & 0xAA) sum += 1; + + return sum; +} + +static __inline unsigned long ffz(unsigned long word) +{ +#ifdef __alpha_cix__ + /* Whee. EV6 can calculate it directly. */ + unsigned long result; + __asm__("ctlz %1,%0" : "=r"(result) : "r"(~word)); + return result; +#else + unsigned long bits, qofs, bofs; + + __asm__("cmpbge %1,%2,%0" : "=r"(bits) : "r"(word), "r"(~0UL)); + qofs = ffz_b(bits); + __asm__("extbl %1,%2,%0" : "=r"(bits) : "r"(word), "r"(qofs)); + bofs = ffz_b(bits); + + return qofs*8 + bofs; +#endif +} + +#ifdef __KERNEL__ +#if 0 +/* + * ffs: find first bit set. This is defined the same way as + * the libc and compiler builtin ffs routines, therefore + * differs in spirit from the above ffz (man ffs). + */ + +static __inline int ffs(int word) +{ + int result = ffz(~word); + return word ? result+1 : 0; +} +#endif + +/* + * hweightN: returns the hamming weight (i.e. the number + * of bits set) of a N-bit word + */ + +#ifdef __alpha_cix__ +/* Whee. EV6 can calculate it directly. */ +static __inline unsigned long hweight64(unsigned long w) +{ + unsigned long result; + __asm__("ctpop %1,%0" : "=r"(result) : "r"(w)); + return result; +} + +#define hweight32(x) hweight64((x) & 0xfffffffful) +#define hweight16(x) hweight64((x) & 0xfffful) +#define hweight8(x) hweight64((x) & 0xfful) +#else +#define hweight32(x) generic_hweight32(x) +#define hweight16(x) generic_hweight16(x) +#define hweight8(x) generic_hweight8(x) +#endif + +#endif /* __alpha_cix__ */ + +/* from lib/string.c */ +static __inline void * memscan(void * addr, int c, size_t size) +{ + unsigned char * p = (unsigned char *) addr; + + while (size) { + if (*p == c) + return (void *) p; + p++; + size--; + } + return (void *) p; +} + + +/* + * Find next zero bit in a bitmap reasonably efficiently.. + */ +static __inline unsigned long find_next_zero_bit(void * addr, unsigned long size, unsigned long offset) +{ + unsigned long * p = ((unsigned long *) addr) + (offset >> 6); + unsigned long result = offset & ~63UL; + unsigned long tmp; + + if (offset >= size) + return size; + size -= result; + offset &= 63UL; + if (offset) { + tmp = *(p++); + tmp |= ~0UL >> (64-offset); + if (size < 64) + goto found_first; + if (~tmp) + goto found_middle; + size -= 64; + result += 64; + } + while (size & ~63UL) { + if (~(tmp = *(p++))) + goto found_middle; + result += 64; + size -= 64; + } + if (!size) + return result; + tmp = *p; +found_first: + tmp |= ~0UL << size; +found_middle: + return result + ffz(tmp); +} + +/* + * The optimizer actually does good code for this case.. + */ +#define find_first_zero_bit(addr, size) \ + find_next_zero_bit((addr), (size), 0) + +#ifdef __KERNEL__ + +#define ext2_set_bit test_and_set_bit +#define ext2_clear_bit test_and_clear_bit +#define ext2_test_bit test_bit +#define ext2_find_first_zero_bit find_first_zero_bit +#define ext2_find_next_zero_bit find_next_zero_bit + +/* Bitmap functions for the minix filesystem. */ +#define minix_set_bit(nr,addr) test_and_set_bit(nr,addr) +#define minix_clear_bit(nr,addr) test_and_clear_bit(nr,addr) +#define minix_test_bit(nr,addr) test_bit(nr,addr) +#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size) + +#endif /* __KERNEL__ */ + +#endif /* _ALPHA_BITOPS_H */ diff --git a/sys/gnu/ext2fs/ext2_linux_balloc.c b/sys/gnu/ext2fs/ext2_linux_balloc.c index bc5f08dfb19e..fc723114dfa6 100644 --- a/sys/gnu/ext2fs/ext2_linux_balloc.c +++ b/sys/gnu/ext2fs/ext2_linux_balloc.c @@ -1,609 +1,611 @@ /* * modified for Lites 1.1 * * Aug 1995, Godmar Back (gback@cs.utah.edu) * University of Utah, Department of Computer Science * * $FreeBSD$ */ /* * 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 */ /* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __i386__ #include +#elif __alpha__ +#include #else #error Provide an bitops.h file, please ! #endif #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) /* got rid of get_group_desc since it can already be found in * ext2_linux_ialloc.c */ static void read_block_bitmap (struct mount * mp, unsigned int block_group, unsigned long bitmap_nr) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct ext2_group_desc * gdp; struct buffer_head * bh; int error; gdp = get_group_desc (mp, block_group, NULL); if ((error = bread (VFSTOUFS(mp)->um_devvp, fsbtodb(sb, gdp->bg_block_bitmap),sb->s_blocksize, NOCRED, &bh)) != 0) panic ( "read_block_bitmap: " "Cannot read block bitmap - " "block_group = %d, block_bitmap = %lu", block_group, (unsigned long) gdp->bg_block_bitmap); sb->s_block_bitmap_number[bitmap_nr] = block_group; sb->s_block_bitmap[bitmap_nr] = bh; LCK_BUF(bh) } /* * 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. */ static int load__block_bitmap (struct mount * mp, unsigned int block_group) { int i, j; struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; unsigned long block_bitmap_number; struct buffer_head * block_bitmap; if (block_group >= sb->s_groups_count) panic ( "load_block_bitmap: " "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->s_groups_count); if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) { if (sb->s_block_bitmap[block_group]) { if (sb->s_block_bitmap_number[block_group] != block_group) panic ( "load_block_bitmap: " "block_group != block_bitmap_number"); else return block_group; } else { read_block_bitmap (mp, block_group, block_group); return block_group; } } for (i = 0; i < sb->s_loaded_block_bitmaps && sb->s_block_bitmap_number[i] != block_group; i++) ; if (i < sb->s_loaded_block_bitmaps && sb->s_block_bitmap_number[i] == block_group) { block_bitmap_number = sb->s_block_bitmap_number[i]; block_bitmap = sb->s_block_bitmap[i]; for (j = i; j > 0; j--) { sb->s_block_bitmap_number[j] = sb->s_block_bitmap_number[j - 1]; sb->s_block_bitmap[j] = sb->s_block_bitmap[j - 1]; } sb->s_block_bitmap_number[0] = block_bitmap_number; sb->s_block_bitmap[0] = block_bitmap; } else { if (sb->s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED) sb->s_loaded_block_bitmaps++; else ULCK_BUF(sb->s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1]) for (j = sb->s_loaded_block_bitmaps - 1; j > 0; j--) { sb->s_block_bitmap_number[j] = sb->s_block_bitmap_number[j - 1]; sb->s_block_bitmap[j] = sb->s_block_bitmap[j - 1]; } read_block_bitmap (mp, block_group, 0); } return 0; } static __inline int load_block_bitmap (struct mount * mp, unsigned int block_group) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; if (sb->s_loaded_block_bitmaps > 0 && sb->s_block_bitmap_number[0] == block_group) return 0; if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED && sb->s_block_bitmap_number[block_group] == block_group && sb->s_block_bitmap[block_group]) return block_group; return load__block_bitmap (mp, block_group); } void ext2_free_blocks (struct mount * mp, unsigned long block, unsigned long count) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct buffer_head * bh; struct buffer_head * bh2; unsigned long block_group; unsigned long bit; unsigned long i; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_super_block * es = sb->s_es; if (!sb) { printf ("ext2_free_blocks: nonexistent device"); return; } lock_super (VFSTOUFS(mp)->um_devvp); if (block < es->s_first_data_block || (block + count) > es->s_blocks_count) { printf ( "ext2_free_blocks: " "Freeing blocks not in datazone - " "block = %lu, count = %lu", block, count); unlock_super (VFSTOUFS(mp)->um_devvp); return; } ext2_debug ("freeing blocks %lu to %lu\n", block, block+count-1); 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)) panic ( "ext2_free_blocks: " "Freeing blocks across group boundary - " "Block = %lu, count = %lu", block, count); bitmap_nr = load_block_bitmap (mp, block_group); bh = sb->s_block_bitmap[bitmap_nr]; gdp = get_group_desc (mp, block_group, &bh2); if (/* test_opt (sb, CHECK_STRICT) && assume always 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->s_itb_per_group) || in_range (block + count - 1, gdp->bg_inode_table, sb->s_itb_per_group))) panic ( "ext2_free_blocks: " "Freeing blocks in system zones - " "Block = %lu, count = %lu", block, count); for (i = 0; i < count; i++) { if (!clear_bit (bit + i, bh->b_data)) printf ("ext2_free_blocks: " "bit already cleared for block %lu", block); else { gdp->bg_free_blocks_count++; es->s_free_blocks_count++; } } mark_buffer_dirty(bh2); mark_buffer_dirty(bh); /**** if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } ****/ sb->s_dirt = 1; unlock_super (VFSTOUFS(mp)->um_devvp); 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 (struct mount * mp, unsigned long goal, u_int32_t * prealloc_count, u_int32_t * prealloc_block) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct buffer_head * bh; struct buffer_head * bh2; char * p, * r; int i, j, k, tmp; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_super_block * es = sb->s_es; #ifdef EXT2FS_DEBUG static int goal_hits = 0, goal_attempts = 0; #endif if (!sb) { printf ("ext2_new_block: nonexistent device"); return 0; } lock_super (VFSTOUFS(mp)->um_devvp); ext2_debug ("goal=%lu.\n", goal); repeat: /* * First, test whether the goal block is free. */ if (goal < es->s_first_data_block || goal >= es->s_blocks_count) goal = es->s_first_data_block; i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb); gdp = get_group_desc (mp, i, &bh2); if (gdp->bg_free_blocks_count > 0) { j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb)); #ifdef EXT2FS_DEBUG if (j) goal_attempts++; #endif bitmap_nr = load_block_bitmap (mp, i); bh = sb->s_block_bitmap[bitmap_nr]; ext2_debug ("goal is at %d:%d.\n", i, j); if (!test_bit(j, bh->b_data)) { #ifdef EXT2FS_DEBUG goal_hits++; ext2_debug ("goal bit allocated.\n"); #endif goto got_block; } if (j) { /* * The goal was occupied; search forward for a free * block within the next XX blocks. * * end_goal is more or less random, but it has to be * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the * next 64-bit boundary is simple.. */ int end_goal = (j + 63) & ~63; j = find_next_zero_bit(bh->b_data, end_goal, j); if (j < end_goal) goto got_block; } ext2_debug ("Bit not found near goal\n"); /* * There has been no free block found in the near vicinity * of the goal: do a search forward through the block groups, * searching in each group first for an entire free byte in * the bitmap and then for any free bit. * * Search first in the remainder of the current group; then, * cyclicly search through the rest of the groups. */ p = ((char *) bh->b_data) + (j >> 3); r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3); k = (r - ((char *) bh->b_data)) << 3; if (k < EXT2_BLOCKS_PER_GROUP(sb)) { j = k; goto search_back; } k = find_next_zero_bit ((unsigned long *) bh->b_data, EXT2_BLOCKS_PER_GROUP(sb), j); if (k < EXT2_BLOCKS_PER_GROUP(sb)) { j = k; goto got_block; } } ext2_debug ("Bit not found in block group %d.\n", i); /* * Now search the rest of the groups. We assume that * i and gdp correctly point to the last group visited. */ for (k = 0; k < sb->s_groups_count; k++) { i++; if (i >= sb->s_groups_count) i = 0; gdp = get_group_desc (mp, i, &bh2); if (gdp->bg_free_blocks_count > 0) break; } if (k >= sb->s_groups_count) { unlock_super (VFSTOUFS(mp)->um_devvp); return 0; } bitmap_nr = load_block_bitmap (mp, i); bh = sb->s_block_bitmap[bitmap_nr]; r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3); j = (r - bh->b_data) << 3; if (j < EXT2_BLOCKS_PER_GROUP(sb)) goto search_back; else j = find_first_zero_bit ((unsigned long *) bh->b_data, EXT2_BLOCKS_PER_GROUP(sb)); if (j >= EXT2_BLOCKS_PER_GROUP(sb)) { printf ( "ext2_new_block: " "Free blocks count corrupted for block group %d", i); unlock_super (VFSTOUFS(mp)->um_devvp); return 0; } search_back: /* * We have succeeded in finding a free byte in the block * bitmap. Now search backwards up to 7 bits to find the * start of this group of free blocks. */ for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--); got_block: ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count); tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block; if (/* test_opt (sb, CHECK_STRICT) && we are always strict. */ (tmp == gdp->bg_block_bitmap || tmp == gdp->bg_inode_bitmap || in_range (tmp, gdp->bg_inode_table, sb->s_itb_per_group))) panic ( "ext2_new_block: " "Allocating block in system zone - " "%dth block = %u in group %u", j, tmp, i); if (set_bit (j, bh->b_data)) { printf ( "ext2_new_block: " "bit already set for block %d", j); goto repeat; } ext2_debug ("found bit %d\n", j); /* * Do block preallocation now if required. */ #ifdef EXT2_PREALLOCATE if (prealloc_block) { *prealloc_count = 0; *prealloc_block = tmp + 1; for (k = 1; k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) { if (set_bit (j + k, bh->b_data)) break; (*prealloc_count)++; } gdp->bg_free_blocks_count -= *prealloc_count; es->s_free_blocks_count -= *prealloc_count; ext2_debug ("Preallocated a further %lu bits.\n", *prealloc_count); } #endif j = tmp; mark_buffer_dirty(bh); /**** if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } ****/ if (j >= es->s_blocks_count) { printf ( "ext2_new_block: " "block >= blocks count - " "block_group = %d, block=%d", i, j); unlock_super (VFSTOUFS(mp)->um_devvp); return 0; } ext2_debug ("allocating block %d. " "Goal hits %d of %d.\n", j, goal_hits, goal_attempts); gdp->bg_free_blocks_count--; mark_buffer_dirty(bh2); es->s_free_blocks_count--; sb->s_dirt = 1; unlock_super (VFSTOUFS(mp)->um_devvp); return j; } #ifdef unused static unsigned long ext2_count_free_blocks (struct mount * mp) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; #ifdef EXT2FS_DEBUG struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; int bitmap_nr; struct ext2_group_desc * gdp; int i; lock_super (VFSTOUFS(mp)->um_devvp); es = sb->s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; for (i = 0; i < sb->s_groups_count; i++) { gdp = get_group_desc (mp, i, NULL); desc_count += gdp->bg_free_blocks_count; bitmap_nr = load_block_bitmap (mp, i); x = ext2_count_free (sb->s_block_bitmap[bitmap_nr], sb->s_blocksize); ext2_debug ("group %d: stored = %d, counted = %lu\n", i, gdp->bg_free_blocks_count, x); bitmap_count += x; } ext2_debug( "stored = %lu, computed = %lu, %lu\n", es->s_free_blocks_count, desc_count, bitmap_count); unlock_super (VFSTOUFS(mp)->um_devvp); return bitmap_count; #else return sb->s_es->s_free_blocks_count; #endif } #endif /* unused */ static __inline int block_in_use (unsigned long block, struct ext2_sb_info * sb, unsigned char * map) { return test_bit ((block - sb->s_es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb), map); } static int test_root(int a, int b) { if (a == 0) return 1; while (1) { if (a == 1) return 1; if (a % b) return 0; a = a / b; } } int ext2_group_sparse(int group) { return (test_root(group, 3) || test_root(group, 5) || test_root(group, 7)); } #ifdef unused static void ext2_check_blocks_bitmap (struct mount * mp) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct buffer_head * bh; struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; unsigned long desc_blocks; int bitmap_nr; struct ext2_group_desc * gdp; int i, j; lock_super (VFSTOUFS(mp)->um_devvp); es = sb->s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; desc_blocks = (sb->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) / EXT2_DESC_PER_BLOCK(sb); for (i = 0; i < sb->s_groups_count; i++) { gdp = get_group_desc (mp, i, NULL); desc_count += gdp->bg_free_blocks_count; bitmap_nr = load_block_bitmap (mp, i); bh = sb->s_block_bitmap[bitmap_nr]; if (!(es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) || ext2_group_sparse(i)) { if (!test_bit (0, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Superblock in group %d " "is marked free", i); for (j = 0; j < desc_blocks; j++) if (!test_bit (j + 1, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Descriptor block #%d in group " "%d is marked free", j, i); } if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Block bitmap for group %d is marked free", i); if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Inode bitmap for group %d is marked free", i); for (j = 0; j < sb->s_itb_per_group; j++) if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Block #%d of the inode table in " "group %d is marked free", j, i); x = ext2_count_free (bh, sb->s_blocksize); if (gdp->bg_free_blocks_count != x) printf ("ext2_check_blocks_bitmap: " "Wrong free blocks count for group %d, " "stored = %d, counted = %lu", i, gdp->bg_free_blocks_count, x); bitmap_count += x; } if (es->s_free_blocks_count != bitmap_count) printf ("ext2_check_blocks_bitmap: " "Wrong free blocks count in super block, " "stored = %lu, counted = %lu", (unsigned long) es->s_free_blocks_count, bitmap_count); unlock_super (VFSTOUFS(mp)->um_devvp); } #endif /* unused */ /* * this function is taken from * linux/fs/ext2/bitmap.c */ static int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0}; unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars) { unsigned int i; unsigned long sum = 0; if (!map) return (0); for (i = 0; i < numchars; i++) sum += nibblemap[map->b_data[i] & 0xf] + nibblemap[(map->b_data[i] >> 4) & 0xf]; return (sum); } diff --git a/sys/gnu/ext2fs/ext2_linux_ialloc.c b/sys/gnu/ext2fs/ext2_linux_ialloc.c index 3ba49b0c9090..01ae55b078dc 100644 --- a/sys/gnu/ext2fs/ext2_linux_ialloc.c +++ b/sys/gnu/ext2fs/ext2_linux_ialloc.c @@ -1,514 +1,516 @@ /* * modified for Lites 1.1 * * Aug 1995, Godmar Back (gback@cs.utah.edu) * University of Utah, Department of Computer Science * * $FreeBSD$ */ /* * linux/fs/ext2/ialloc.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) * * BSD ufs-inspired inode and directory allocation by * Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 */ /* * The free inodes 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __i386__ #include +#elif __alpha__ +#include #else #error please provide bit operation functions #endif /* this is supposed to mark a buffer dirty on ready for delayed writing */ void mark_buffer_dirty(struct buf *bh) { int s; s = splbio(); bh->b_flags |= B_DIRTY; splx(s); } struct ext2_group_desc * get_group_desc (struct mount * mp, unsigned int block_group, struct buffer_head ** bh) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; unsigned long group_desc; unsigned long desc; struct ext2_group_desc * gdp; if (block_group >= sb->s_groups_count) panic ("get_group_desc: " "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->s_groups_count); group_desc = block_group / EXT2_DESC_PER_BLOCK(sb); desc = block_group % EXT2_DESC_PER_BLOCK(sb); if (!sb->s_group_desc[group_desc]) panic ( "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->s_group_desc[group_desc]->b_data; if (bh) *bh = sb->s_group_desc[group_desc]; return gdp + desc; } static void read_inode_bitmap (struct mount * mp, unsigned long block_group, unsigned int bitmap_nr) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct ext2_group_desc * gdp; struct buffer_head * bh; int error; gdp = get_group_desc (mp, block_group, NULL); if ((error = bread (VFSTOUFS(mp)->um_devvp, fsbtodb(sb, gdp->bg_inode_bitmap), sb->s_blocksize, NOCRED, &bh)) != 0) panic ( "read_inode_bitmap:" "Cannot read inode bitmap - " "block_group = %lu, inode_bitmap = %lu", block_group, (unsigned long) gdp->bg_inode_bitmap); sb->s_inode_bitmap_number[bitmap_nr] = block_group; sb->s_inode_bitmap[bitmap_nr] = bh; LCK_BUF(bh) } /* * load_inode_bitmap loads the inode 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. */ static int load_inode_bitmap (struct mount * mp, unsigned int block_group) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; int i, j; unsigned long inode_bitmap_number; struct buffer_head * inode_bitmap; if (block_group >= sb->s_groups_count) panic ("load_inode_bitmap:" "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->s_groups_count); if (sb->s_loaded_inode_bitmaps > 0 && sb->s_inode_bitmap_number[0] == block_group) return 0; if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) { if (sb->s_inode_bitmap[block_group]) { if (sb->s_inode_bitmap_number[block_group] != block_group) panic ( "load_inode_bitmap:" "block_group != inode_bitmap_number"); else return block_group; } else { read_inode_bitmap (mp, block_group, block_group); return block_group; } } for (i = 0; i < sb->s_loaded_inode_bitmaps && sb->s_inode_bitmap_number[i] != block_group; i++) ; if (i < sb->s_loaded_inode_bitmaps && sb->s_inode_bitmap_number[i] == block_group) { inode_bitmap_number = sb->s_inode_bitmap_number[i]; inode_bitmap = sb->s_inode_bitmap[i]; for (j = i; j > 0; j--) { sb->s_inode_bitmap_number[j] = sb->s_inode_bitmap_number[j - 1]; sb->s_inode_bitmap[j] = sb->s_inode_bitmap[j - 1]; } sb->s_inode_bitmap_number[0] = inode_bitmap_number; sb->s_inode_bitmap[0] = inode_bitmap; } else { if (sb->s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED) sb->s_loaded_inode_bitmaps++; else ULCK_BUF(sb->s_inode_bitmap[EXT2_MAX_GROUP_LOADED - 1]) for (j = sb->s_loaded_inode_bitmaps - 1; j > 0; j--) { sb->s_inode_bitmap_number[j] = sb->s_inode_bitmap_number[j - 1]; sb->s_inode_bitmap[j] = sb->s_inode_bitmap[j - 1]; } read_inode_bitmap (mp, block_group, 0); } return 0; } void ext2_free_inode (struct inode * inode) { struct ext2_sb_info * sb; struct buffer_head * bh; struct buffer_head * bh2; unsigned long block_group; unsigned long bit; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_super_block * es; if (!inode) return; if (inode->i_nlink) { printf ("ext2_free_inode: inode has nlink=%d\n", inode->i_nlink); return; } ext2_debug ("freeing inode %lu\n", inode->i_number); sb = inode->i_e2fs; lock_super (DEVVP(inode)); if (inode->i_number < EXT2_FIRST_INO || inode->i_number > sb->s_es->s_inodes_count) { printf ("free_inode reserved inode or nonexistent inode"); unlock_super (DEVVP(inode)); return; } es = sb->s_es; block_group = (inode->i_number - 1) / EXT2_INODES_PER_GROUP(sb); bit = (inode->i_number - 1) % EXT2_INODES_PER_GROUP(sb); bitmap_nr = load_inode_bitmap (ITOV(inode)->v_mount, block_group); bh = sb->s_inode_bitmap[bitmap_nr]; if (!clear_bit (bit, bh->b_data)) printf ( "ext2_free_inode:" "bit already cleared for inode %lu", (unsigned long)inode->i_number); else { gdp = get_group_desc (ITOV(inode)->v_mount, block_group, &bh2); gdp->bg_free_inodes_count++; if (S_ISDIR(inode->i_mode)) gdp->bg_used_dirs_count--; mark_buffer_dirty(bh2); es->s_free_inodes_count++; } mark_buffer_dirty(bh); /*** XXX if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } ***/ sb->s_dirt = 1; unlock_super (DEVVP(inode)); } #if linux /* * This function increments the inode version number * * This may be used one day by the NFS server */ static void inc_inode_version (struct inode * inode, struct ext2_group_desc *gdp, int mode) { unsigned long inode_block; struct buffer_head * bh; struct ext2_inode * raw_inode; inode_block = gdp->bg_inode_table + (((inode->i_number - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) / EXT2_INODES_PER_BLOCK(inode->i_sb)); bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize); if (!bh) { printf ("inc_inode_version Cannot load inode table block - " "inode=%lu, inode_block=%lu\n", inode->i_number, inode_block); inode->u.ext2_i.i_version = 1; return; } raw_inode = ((struct ext2_inode *) bh->b_data) + (((inode->i_number - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) % EXT2_INODES_PER_BLOCK(inode->i_sb)); raw_inode->i_version++; inode->u.ext2_i.i_version = raw_inode->i_version; bdwrite (bh); } #endif /* linux */ /* * There are two policies for allocating an inode. If the new inode is * a directory, then a forward search is made for a block group with both * free space and a low directory-to-inode ratio; if that fails, then of * the groups with above-average free space, that group with the fewest * directories already is chosen. * * For other inodes, search forward from the parent directory\'s block * group to find a free inode. */ /* * this functino has been reduced to the actual 'find the inode number' part */ ino_t ext2_new_inode (const struct inode * dir, int mode) { struct ext2_sb_info * sb; struct buffer_head * bh; struct buffer_head * bh2; int i, j, avefreei; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_group_desc * tmp; struct ext2_super_block * es; if (!dir) return 0; sb = dir->i_e2fs; lock_super (DEVVP(dir)); es = sb->s_es; repeat: gdp = NULL; i=0; if (S_ISDIR(mode)) { avefreei = es->s_free_inodes_count / sb->s_groups_count; /* I am not yet convinced that this next bit is necessary. i = dir->u.ext2_i.i_block_group; for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) { tmp = get_group_desc (sb, i, &bh2); if ((tmp->bg_used_dirs_count << 8) < tmp->bg_free_inodes_count) { gdp = tmp; break; } else i = ++i % sb->u.ext2_sb.s_groups_count; } */ if (!gdp) { for (j = 0; j < sb->s_groups_count; j++) { tmp = get_group_desc(ITOV(dir)->v_mount,j,&bh2); if (tmp->bg_free_inodes_count && tmp->bg_free_inodes_count >= avefreei) { if (!gdp || (tmp->bg_free_blocks_count > gdp->bg_free_blocks_count)) { i = j; gdp = tmp; } } } } } else { /* * Try to place the inode in its parent directory */ i = dir->i_block_group; tmp = get_group_desc (ITOV(dir)->v_mount, i, &bh2); if (tmp->bg_free_inodes_count) gdp = tmp; else { /* * Use a quadratic hash to find a group with a * free inode */ for (j = 1; j < sb->s_groups_count; j <<= 1) { i += j; if (i >= sb->s_groups_count) i -= sb->s_groups_count; tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2); if (tmp->bg_free_inodes_count) { gdp = tmp; break; } } } if (!gdp) { /* * That failed: try linear search for a free inode */ i = dir->i_block_group + 1; for (j = 2; j < sb->s_groups_count; j++) { if (++i >= sb->s_groups_count) i = 0; tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2); if (tmp->bg_free_inodes_count) { gdp = tmp; break; } } } } if (!gdp) { unlock_super (DEVVP(dir)); return 0; } bitmap_nr = load_inode_bitmap (ITOV(dir)->v_mount, i); bh = sb->s_inode_bitmap[bitmap_nr]; if ((j = find_first_zero_bit ((unsigned long *) bh->b_data, EXT2_INODES_PER_GROUP(sb))) < EXT2_INODES_PER_GROUP(sb)) { if (set_bit (j, bh->b_data)) { printf ( "ext2_new_inode:" "bit already set for inode %d", j); goto repeat; } /* Linux now does the following: mark_buffer_dirty(bh); if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } */ mark_buffer_dirty(bh); } else { if (gdp->bg_free_inodes_count != 0) { printf ( "ext2_new_inode:" "Free inodes count corrupted in group %d", i); unlock_super (DEVVP(dir)); return 0; } goto repeat; } j += i * EXT2_INODES_PER_GROUP(sb) + 1; if (j < EXT2_FIRST_INO || j > es->s_inodes_count) { printf ( "ext2_new_inode:" "reserved inode or inode > inodes count - " "block_group = %d,inode=%d", i, j); unlock_super (DEVVP(dir)); return 0; } gdp->bg_free_inodes_count--; if (S_ISDIR(mode)) gdp->bg_used_dirs_count++; mark_buffer_dirty(bh2); es->s_free_inodes_count--; /* mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1); */ sb->s_dirt = 1; unlock_super (DEVVP(dir)); return j; } #ifdef unused static unsigned long ext2_count_free_inodes (struct mount * mp) { #ifdef EXT2FS_DEBUG struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; int bitmap_nr; struct ext2_group_desc * gdp; int i; lock_super (VFSTOUFS(mp)->um_devvp); es = sb->s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; for (i = 0; i < sb->s_groups_count; i++) { gdp = get_group_desc (mp, i, NULL); desc_count += gdp->bg_free_inodes_count; bitmap_nr = load_inode_bitmap (mp, i); x = ext2_count_free (sb->s_inode_bitmap[bitmap_nr], EXT2_INODES_PER_GROUP(sb) / 8); ext2_debug ("group %d: stored = %d, counted = %lu\n", i, gdp->bg_free_inodes_count, x); bitmap_count += x; } ext2_debug("stored = %lu, computed = %lu, %lu\n", es->s_free_inodes_count, desc_count, bitmap_count); unlock_super (VFSTOUFS(mp)->um_devvp); return desc_count; #else return VFSTOUFS(mp)->um_e2fsb->s_free_inodes_count; #endif } #endif /* unused */ #ifdef LATER void ext2_check_inodes_bitmap (struct mount * mp) { struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; int bitmap_nr; struct ext2_group_desc * gdp; int i; lock_super (sb); es = sb->u.ext2_sb.s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) { gdp = get_group_desc (sb, i, NULL); desc_count += gdp->bg_free_inodes_count; bitmap_nr = load_inode_bitmap (sb, i); x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr], EXT2_INODES_PER_GROUP(sb) / 8); if (gdp->bg_free_inodes_count != x) printf ( "ext2_check_inodes_bitmap:" "Wrong free inodes count in group %d, " "stored = %d, counted = %lu", i, gdp->bg_free_inodes_count, x); bitmap_count += x; } if (es->s_free_inodes_count != bitmap_count) printf ( "ext2_check_inodes_bitmap:" "Wrong free inodes count in super block, " "stored = %lu, counted = %lu", (unsigned long) es->s_free_inodes_count, bitmap_count); unlock_super (sb); } #endif diff --git a/sys/gnu/fs/ext2fs/ext2_linux_balloc.c b/sys/gnu/fs/ext2fs/ext2_linux_balloc.c index bc5f08dfb19e..fc723114dfa6 100644 --- a/sys/gnu/fs/ext2fs/ext2_linux_balloc.c +++ b/sys/gnu/fs/ext2fs/ext2_linux_balloc.c @@ -1,609 +1,611 @@ /* * modified for Lites 1.1 * * Aug 1995, Godmar Back (gback@cs.utah.edu) * University of Utah, Department of Computer Science * * $FreeBSD$ */ /* * 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 */ /* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __i386__ #include +#elif __alpha__ +#include #else #error Provide an bitops.h file, please ! #endif #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) /* got rid of get_group_desc since it can already be found in * ext2_linux_ialloc.c */ static void read_block_bitmap (struct mount * mp, unsigned int block_group, unsigned long bitmap_nr) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct ext2_group_desc * gdp; struct buffer_head * bh; int error; gdp = get_group_desc (mp, block_group, NULL); if ((error = bread (VFSTOUFS(mp)->um_devvp, fsbtodb(sb, gdp->bg_block_bitmap),sb->s_blocksize, NOCRED, &bh)) != 0) panic ( "read_block_bitmap: " "Cannot read block bitmap - " "block_group = %d, block_bitmap = %lu", block_group, (unsigned long) gdp->bg_block_bitmap); sb->s_block_bitmap_number[bitmap_nr] = block_group; sb->s_block_bitmap[bitmap_nr] = bh; LCK_BUF(bh) } /* * 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. */ static int load__block_bitmap (struct mount * mp, unsigned int block_group) { int i, j; struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; unsigned long block_bitmap_number; struct buffer_head * block_bitmap; if (block_group >= sb->s_groups_count) panic ( "load_block_bitmap: " "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->s_groups_count); if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) { if (sb->s_block_bitmap[block_group]) { if (sb->s_block_bitmap_number[block_group] != block_group) panic ( "load_block_bitmap: " "block_group != block_bitmap_number"); else return block_group; } else { read_block_bitmap (mp, block_group, block_group); return block_group; } } for (i = 0; i < sb->s_loaded_block_bitmaps && sb->s_block_bitmap_number[i] != block_group; i++) ; if (i < sb->s_loaded_block_bitmaps && sb->s_block_bitmap_number[i] == block_group) { block_bitmap_number = sb->s_block_bitmap_number[i]; block_bitmap = sb->s_block_bitmap[i]; for (j = i; j > 0; j--) { sb->s_block_bitmap_number[j] = sb->s_block_bitmap_number[j - 1]; sb->s_block_bitmap[j] = sb->s_block_bitmap[j - 1]; } sb->s_block_bitmap_number[0] = block_bitmap_number; sb->s_block_bitmap[0] = block_bitmap; } else { if (sb->s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED) sb->s_loaded_block_bitmaps++; else ULCK_BUF(sb->s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1]) for (j = sb->s_loaded_block_bitmaps - 1; j > 0; j--) { sb->s_block_bitmap_number[j] = sb->s_block_bitmap_number[j - 1]; sb->s_block_bitmap[j] = sb->s_block_bitmap[j - 1]; } read_block_bitmap (mp, block_group, 0); } return 0; } static __inline int load_block_bitmap (struct mount * mp, unsigned int block_group) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; if (sb->s_loaded_block_bitmaps > 0 && sb->s_block_bitmap_number[0] == block_group) return 0; if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED && sb->s_block_bitmap_number[block_group] == block_group && sb->s_block_bitmap[block_group]) return block_group; return load__block_bitmap (mp, block_group); } void ext2_free_blocks (struct mount * mp, unsigned long block, unsigned long count) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct buffer_head * bh; struct buffer_head * bh2; unsigned long block_group; unsigned long bit; unsigned long i; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_super_block * es = sb->s_es; if (!sb) { printf ("ext2_free_blocks: nonexistent device"); return; } lock_super (VFSTOUFS(mp)->um_devvp); if (block < es->s_first_data_block || (block + count) > es->s_blocks_count) { printf ( "ext2_free_blocks: " "Freeing blocks not in datazone - " "block = %lu, count = %lu", block, count); unlock_super (VFSTOUFS(mp)->um_devvp); return; } ext2_debug ("freeing blocks %lu to %lu\n", block, block+count-1); 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)) panic ( "ext2_free_blocks: " "Freeing blocks across group boundary - " "Block = %lu, count = %lu", block, count); bitmap_nr = load_block_bitmap (mp, block_group); bh = sb->s_block_bitmap[bitmap_nr]; gdp = get_group_desc (mp, block_group, &bh2); if (/* test_opt (sb, CHECK_STRICT) && assume always 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->s_itb_per_group) || in_range (block + count - 1, gdp->bg_inode_table, sb->s_itb_per_group))) panic ( "ext2_free_blocks: " "Freeing blocks in system zones - " "Block = %lu, count = %lu", block, count); for (i = 0; i < count; i++) { if (!clear_bit (bit + i, bh->b_data)) printf ("ext2_free_blocks: " "bit already cleared for block %lu", block); else { gdp->bg_free_blocks_count++; es->s_free_blocks_count++; } } mark_buffer_dirty(bh2); mark_buffer_dirty(bh); /**** if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } ****/ sb->s_dirt = 1; unlock_super (VFSTOUFS(mp)->um_devvp); 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 (struct mount * mp, unsigned long goal, u_int32_t * prealloc_count, u_int32_t * prealloc_block) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct buffer_head * bh; struct buffer_head * bh2; char * p, * r; int i, j, k, tmp; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_super_block * es = sb->s_es; #ifdef EXT2FS_DEBUG static int goal_hits = 0, goal_attempts = 0; #endif if (!sb) { printf ("ext2_new_block: nonexistent device"); return 0; } lock_super (VFSTOUFS(mp)->um_devvp); ext2_debug ("goal=%lu.\n", goal); repeat: /* * First, test whether the goal block is free. */ if (goal < es->s_first_data_block || goal >= es->s_blocks_count) goal = es->s_first_data_block; i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb); gdp = get_group_desc (mp, i, &bh2); if (gdp->bg_free_blocks_count > 0) { j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb)); #ifdef EXT2FS_DEBUG if (j) goal_attempts++; #endif bitmap_nr = load_block_bitmap (mp, i); bh = sb->s_block_bitmap[bitmap_nr]; ext2_debug ("goal is at %d:%d.\n", i, j); if (!test_bit(j, bh->b_data)) { #ifdef EXT2FS_DEBUG goal_hits++; ext2_debug ("goal bit allocated.\n"); #endif goto got_block; } if (j) { /* * The goal was occupied; search forward for a free * block within the next XX blocks. * * end_goal is more or less random, but it has to be * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the * next 64-bit boundary is simple.. */ int end_goal = (j + 63) & ~63; j = find_next_zero_bit(bh->b_data, end_goal, j); if (j < end_goal) goto got_block; } ext2_debug ("Bit not found near goal\n"); /* * There has been no free block found in the near vicinity * of the goal: do a search forward through the block groups, * searching in each group first for an entire free byte in * the bitmap and then for any free bit. * * Search first in the remainder of the current group; then, * cyclicly search through the rest of the groups. */ p = ((char *) bh->b_data) + (j >> 3); r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3); k = (r - ((char *) bh->b_data)) << 3; if (k < EXT2_BLOCKS_PER_GROUP(sb)) { j = k; goto search_back; } k = find_next_zero_bit ((unsigned long *) bh->b_data, EXT2_BLOCKS_PER_GROUP(sb), j); if (k < EXT2_BLOCKS_PER_GROUP(sb)) { j = k; goto got_block; } } ext2_debug ("Bit not found in block group %d.\n", i); /* * Now search the rest of the groups. We assume that * i and gdp correctly point to the last group visited. */ for (k = 0; k < sb->s_groups_count; k++) { i++; if (i >= sb->s_groups_count) i = 0; gdp = get_group_desc (mp, i, &bh2); if (gdp->bg_free_blocks_count > 0) break; } if (k >= sb->s_groups_count) { unlock_super (VFSTOUFS(mp)->um_devvp); return 0; } bitmap_nr = load_block_bitmap (mp, i); bh = sb->s_block_bitmap[bitmap_nr]; r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3); j = (r - bh->b_data) << 3; if (j < EXT2_BLOCKS_PER_GROUP(sb)) goto search_back; else j = find_first_zero_bit ((unsigned long *) bh->b_data, EXT2_BLOCKS_PER_GROUP(sb)); if (j >= EXT2_BLOCKS_PER_GROUP(sb)) { printf ( "ext2_new_block: " "Free blocks count corrupted for block group %d", i); unlock_super (VFSTOUFS(mp)->um_devvp); return 0; } search_back: /* * We have succeeded in finding a free byte in the block * bitmap. Now search backwards up to 7 bits to find the * start of this group of free blocks. */ for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--); got_block: ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count); tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block; if (/* test_opt (sb, CHECK_STRICT) && we are always strict. */ (tmp == gdp->bg_block_bitmap || tmp == gdp->bg_inode_bitmap || in_range (tmp, gdp->bg_inode_table, sb->s_itb_per_group))) panic ( "ext2_new_block: " "Allocating block in system zone - " "%dth block = %u in group %u", j, tmp, i); if (set_bit (j, bh->b_data)) { printf ( "ext2_new_block: " "bit already set for block %d", j); goto repeat; } ext2_debug ("found bit %d\n", j); /* * Do block preallocation now if required. */ #ifdef EXT2_PREALLOCATE if (prealloc_block) { *prealloc_count = 0; *prealloc_block = tmp + 1; for (k = 1; k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) { if (set_bit (j + k, bh->b_data)) break; (*prealloc_count)++; } gdp->bg_free_blocks_count -= *prealloc_count; es->s_free_blocks_count -= *prealloc_count; ext2_debug ("Preallocated a further %lu bits.\n", *prealloc_count); } #endif j = tmp; mark_buffer_dirty(bh); /**** if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } ****/ if (j >= es->s_blocks_count) { printf ( "ext2_new_block: " "block >= blocks count - " "block_group = %d, block=%d", i, j); unlock_super (VFSTOUFS(mp)->um_devvp); return 0; } ext2_debug ("allocating block %d. " "Goal hits %d of %d.\n", j, goal_hits, goal_attempts); gdp->bg_free_blocks_count--; mark_buffer_dirty(bh2); es->s_free_blocks_count--; sb->s_dirt = 1; unlock_super (VFSTOUFS(mp)->um_devvp); return j; } #ifdef unused static unsigned long ext2_count_free_blocks (struct mount * mp) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; #ifdef EXT2FS_DEBUG struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; int bitmap_nr; struct ext2_group_desc * gdp; int i; lock_super (VFSTOUFS(mp)->um_devvp); es = sb->s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; for (i = 0; i < sb->s_groups_count; i++) { gdp = get_group_desc (mp, i, NULL); desc_count += gdp->bg_free_blocks_count; bitmap_nr = load_block_bitmap (mp, i); x = ext2_count_free (sb->s_block_bitmap[bitmap_nr], sb->s_blocksize); ext2_debug ("group %d: stored = %d, counted = %lu\n", i, gdp->bg_free_blocks_count, x); bitmap_count += x; } ext2_debug( "stored = %lu, computed = %lu, %lu\n", es->s_free_blocks_count, desc_count, bitmap_count); unlock_super (VFSTOUFS(mp)->um_devvp); return bitmap_count; #else return sb->s_es->s_free_blocks_count; #endif } #endif /* unused */ static __inline int block_in_use (unsigned long block, struct ext2_sb_info * sb, unsigned char * map) { return test_bit ((block - sb->s_es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb), map); } static int test_root(int a, int b) { if (a == 0) return 1; while (1) { if (a == 1) return 1; if (a % b) return 0; a = a / b; } } int ext2_group_sparse(int group) { return (test_root(group, 3) || test_root(group, 5) || test_root(group, 7)); } #ifdef unused static void ext2_check_blocks_bitmap (struct mount * mp) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct buffer_head * bh; struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; unsigned long desc_blocks; int bitmap_nr; struct ext2_group_desc * gdp; int i, j; lock_super (VFSTOUFS(mp)->um_devvp); es = sb->s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; desc_blocks = (sb->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) / EXT2_DESC_PER_BLOCK(sb); for (i = 0; i < sb->s_groups_count; i++) { gdp = get_group_desc (mp, i, NULL); desc_count += gdp->bg_free_blocks_count; bitmap_nr = load_block_bitmap (mp, i); bh = sb->s_block_bitmap[bitmap_nr]; if (!(es->s_feature_ro_compat & EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) || ext2_group_sparse(i)) { if (!test_bit (0, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Superblock in group %d " "is marked free", i); for (j = 0; j < desc_blocks; j++) if (!test_bit (j + 1, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Descriptor block #%d in group " "%d is marked free", j, i); } if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Block bitmap for group %d is marked free", i); if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Inode bitmap for group %d is marked free", i); for (j = 0; j < sb->s_itb_per_group; j++) if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data)) printf ("ext2_check_blocks_bitmap: " "Block #%d of the inode table in " "group %d is marked free", j, i); x = ext2_count_free (bh, sb->s_blocksize); if (gdp->bg_free_blocks_count != x) printf ("ext2_check_blocks_bitmap: " "Wrong free blocks count for group %d, " "stored = %d, counted = %lu", i, gdp->bg_free_blocks_count, x); bitmap_count += x; } if (es->s_free_blocks_count != bitmap_count) printf ("ext2_check_blocks_bitmap: " "Wrong free blocks count in super block, " "stored = %lu, counted = %lu", (unsigned long) es->s_free_blocks_count, bitmap_count); unlock_super (VFSTOUFS(mp)->um_devvp); } #endif /* unused */ /* * this function is taken from * linux/fs/ext2/bitmap.c */ static int nibblemap[] = {4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0}; unsigned long ext2_count_free (struct buffer_head * map, unsigned int numchars) { unsigned int i; unsigned long sum = 0; if (!map) return (0); for (i = 0; i < numchars; i++) sum += nibblemap[map->b_data[i] & 0xf] + nibblemap[(map->b_data[i] >> 4) & 0xf]; return (sum); } diff --git a/sys/gnu/fs/ext2fs/ext2_linux_ialloc.c b/sys/gnu/fs/ext2fs/ext2_linux_ialloc.c index 3ba49b0c9090..01ae55b078dc 100644 --- a/sys/gnu/fs/ext2fs/ext2_linux_ialloc.c +++ b/sys/gnu/fs/ext2fs/ext2_linux_ialloc.c @@ -1,514 +1,516 @@ /* * modified for Lites 1.1 * * Aug 1995, Godmar Back (gback@cs.utah.edu) * University of Utah, Department of Computer Science * * $FreeBSD$ */ /* * linux/fs/ext2/ialloc.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) * * BSD ufs-inspired inode and directory allocation by * Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 */ /* * The free inodes 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __i386__ #include +#elif __alpha__ +#include #else #error please provide bit operation functions #endif /* this is supposed to mark a buffer dirty on ready for delayed writing */ void mark_buffer_dirty(struct buf *bh) { int s; s = splbio(); bh->b_flags |= B_DIRTY; splx(s); } struct ext2_group_desc * get_group_desc (struct mount * mp, unsigned int block_group, struct buffer_head ** bh) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; unsigned long group_desc; unsigned long desc; struct ext2_group_desc * gdp; if (block_group >= sb->s_groups_count) panic ("get_group_desc: " "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->s_groups_count); group_desc = block_group / EXT2_DESC_PER_BLOCK(sb); desc = block_group % EXT2_DESC_PER_BLOCK(sb); if (!sb->s_group_desc[group_desc]) panic ( "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->s_group_desc[group_desc]->b_data; if (bh) *bh = sb->s_group_desc[group_desc]; return gdp + desc; } static void read_inode_bitmap (struct mount * mp, unsigned long block_group, unsigned int bitmap_nr) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct ext2_group_desc * gdp; struct buffer_head * bh; int error; gdp = get_group_desc (mp, block_group, NULL); if ((error = bread (VFSTOUFS(mp)->um_devvp, fsbtodb(sb, gdp->bg_inode_bitmap), sb->s_blocksize, NOCRED, &bh)) != 0) panic ( "read_inode_bitmap:" "Cannot read inode bitmap - " "block_group = %lu, inode_bitmap = %lu", block_group, (unsigned long) gdp->bg_inode_bitmap); sb->s_inode_bitmap_number[bitmap_nr] = block_group; sb->s_inode_bitmap[bitmap_nr] = bh; LCK_BUF(bh) } /* * load_inode_bitmap loads the inode 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. */ static int load_inode_bitmap (struct mount * mp, unsigned int block_group) { struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; int i, j; unsigned long inode_bitmap_number; struct buffer_head * inode_bitmap; if (block_group >= sb->s_groups_count) panic ("load_inode_bitmap:" "block_group >= groups_count - " "block_group = %d, groups_count = %lu", block_group, sb->s_groups_count); if (sb->s_loaded_inode_bitmaps > 0 && sb->s_inode_bitmap_number[0] == block_group) return 0; if (sb->s_groups_count <= EXT2_MAX_GROUP_LOADED) { if (sb->s_inode_bitmap[block_group]) { if (sb->s_inode_bitmap_number[block_group] != block_group) panic ( "load_inode_bitmap:" "block_group != inode_bitmap_number"); else return block_group; } else { read_inode_bitmap (mp, block_group, block_group); return block_group; } } for (i = 0; i < sb->s_loaded_inode_bitmaps && sb->s_inode_bitmap_number[i] != block_group; i++) ; if (i < sb->s_loaded_inode_bitmaps && sb->s_inode_bitmap_number[i] == block_group) { inode_bitmap_number = sb->s_inode_bitmap_number[i]; inode_bitmap = sb->s_inode_bitmap[i]; for (j = i; j > 0; j--) { sb->s_inode_bitmap_number[j] = sb->s_inode_bitmap_number[j - 1]; sb->s_inode_bitmap[j] = sb->s_inode_bitmap[j - 1]; } sb->s_inode_bitmap_number[0] = inode_bitmap_number; sb->s_inode_bitmap[0] = inode_bitmap; } else { if (sb->s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED) sb->s_loaded_inode_bitmaps++; else ULCK_BUF(sb->s_inode_bitmap[EXT2_MAX_GROUP_LOADED - 1]) for (j = sb->s_loaded_inode_bitmaps - 1; j > 0; j--) { sb->s_inode_bitmap_number[j] = sb->s_inode_bitmap_number[j - 1]; sb->s_inode_bitmap[j] = sb->s_inode_bitmap[j - 1]; } read_inode_bitmap (mp, block_group, 0); } return 0; } void ext2_free_inode (struct inode * inode) { struct ext2_sb_info * sb; struct buffer_head * bh; struct buffer_head * bh2; unsigned long block_group; unsigned long bit; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_super_block * es; if (!inode) return; if (inode->i_nlink) { printf ("ext2_free_inode: inode has nlink=%d\n", inode->i_nlink); return; } ext2_debug ("freeing inode %lu\n", inode->i_number); sb = inode->i_e2fs; lock_super (DEVVP(inode)); if (inode->i_number < EXT2_FIRST_INO || inode->i_number > sb->s_es->s_inodes_count) { printf ("free_inode reserved inode or nonexistent inode"); unlock_super (DEVVP(inode)); return; } es = sb->s_es; block_group = (inode->i_number - 1) / EXT2_INODES_PER_GROUP(sb); bit = (inode->i_number - 1) % EXT2_INODES_PER_GROUP(sb); bitmap_nr = load_inode_bitmap (ITOV(inode)->v_mount, block_group); bh = sb->s_inode_bitmap[bitmap_nr]; if (!clear_bit (bit, bh->b_data)) printf ( "ext2_free_inode:" "bit already cleared for inode %lu", (unsigned long)inode->i_number); else { gdp = get_group_desc (ITOV(inode)->v_mount, block_group, &bh2); gdp->bg_free_inodes_count++; if (S_ISDIR(inode->i_mode)) gdp->bg_used_dirs_count--; mark_buffer_dirty(bh2); es->s_free_inodes_count++; } mark_buffer_dirty(bh); /*** XXX if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } ***/ sb->s_dirt = 1; unlock_super (DEVVP(inode)); } #if linux /* * This function increments the inode version number * * This may be used one day by the NFS server */ static void inc_inode_version (struct inode * inode, struct ext2_group_desc *gdp, int mode) { unsigned long inode_block; struct buffer_head * bh; struct ext2_inode * raw_inode; inode_block = gdp->bg_inode_table + (((inode->i_number - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) / EXT2_INODES_PER_BLOCK(inode->i_sb)); bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize); if (!bh) { printf ("inc_inode_version Cannot load inode table block - " "inode=%lu, inode_block=%lu\n", inode->i_number, inode_block); inode->u.ext2_i.i_version = 1; return; } raw_inode = ((struct ext2_inode *) bh->b_data) + (((inode->i_number - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) % EXT2_INODES_PER_BLOCK(inode->i_sb)); raw_inode->i_version++; inode->u.ext2_i.i_version = raw_inode->i_version; bdwrite (bh); } #endif /* linux */ /* * There are two policies for allocating an inode. If the new inode is * a directory, then a forward search is made for a block group with both * free space and a low directory-to-inode ratio; if that fails, then of * the groups with above-average free space, that group with the fewest * directories already is chosen. * * For other inodes, search forward from the parent directory\'s block * group to find a free inode. */ /* * this functino has been reduced to the actual 'find the inode number' part */ ino_t ext2_new_inode (const struct inode * dir, int mode) { struct ext2_sb_info * sb; struct buffer_head * bh; struct buffer_head * bh2; int i, j, avefreei; int bitmap_nr; struct ext2_group_desc * gdp; struct ext2_group_desc * tmp; struct ext2_super_block * es; if (!dir) return 0; sb = dir->i_e2fs; lock_super (DEVVP(dir)); es = sb->s_es; repeat: gdp = NULL; i=0; if (S_ISDIR(mode)) { avefreei = es->s_free_inodes_count / sb->s_groups_count; /* I am not yet convinced that this next bit is necessary. i = dir->u.ext2_i.i_block_group; for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) { tmp = get_group_desc (sb, i, &bh2); if ((tmp->bg_used_dirs_count << 8) < tmp->bg_free_inodes_count) { gdp = tmp; break; } else i = ++i % sb->u.ext2_sb.s_groups_count; } */ if (!gdp) { for (j = 0; j < sb->s_groups_count; j++) { tmp = get_group_desc(ITOV(dir)->v_mount,j,&bh2); if (tmp->bg_free_inodes_count && tmp->bg_free_inodes_count >= avefreei) { if (!gdp || (tmp->bg_free_blocks_count > gdp->bg_free_blocks_count)) { i = j; gdp = tmp; } } } } } else { /* * Try to place the inode in its parent directory */ i = dir->i_block_group; tmp = get_group_desc (ITOV(dir)->v_mount, i, &bh2); if (tmp->bg_free_inodes_count) gdp = tmp; else { /* * Use a quadratic hash to find a group with a * free inode */ for (j = 1; j < sb->s_groups_count; j <<= 1) { i += j; if (i >= sb->s_groups_count) i -= sb->s_groups_count; tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2); if (tmp->bg_free_inodes_count) { gdp = tmp; break; } } } if (!gdp) { /* * That failed: try linear search for a free inode */ i = dir->i_block_group + 1; for (j = 2; j < sb->s_groups_count; j++) { if (++i >= sb->s_groups_count) i = 0; tmp = get_group_desc(ITOV(dir)->v_mount,i,&bh2); if (tmp->bg_free_inodes_count) { gdp = tmp; break; } } } } if (!gdp) { unlock_super (DEVVP(dir)); return 0; } bitmap_nr = load_inode_bitmap (ITOV(dir)->v_mount, i); bh = sb->s_inode_bitmap[bitmap_nr]; if ((j = find_first_zero_bit ((unsigned long *) bh->b_data, EXT2_INODES_PER_GROUP(sb))) < EXT2_INODES_PER_GROUP(sb)) { if (set_bit (j, bh->b_data)) { printf ( "ext2_new_inode:" "bit already set for inode %d", j); goto repeat; } /* Linux now does the following: mark_buffer_dirty(bh); if (sb->s_flags & MS_SYNCHRONOUS) { ll_rw_block (WRITE, 1, &bh); wait_on_buffer (bh); } */ mark_buffer_dirty(bh); } else { if (gdp->bg_free_inodes_count != 0) { printf ( "ext2_new_inode:" "Free inodes count corrupted in group %d", i); unlock_super (DEVVP(dir)); return 0; } goto repeat; } j += i * EXT2_INODES_PER_GROUP(sb) + 1; if (j < EXT2_FIRST_INO || j > es->s_inodes_count) { printf ( "ext2_new_inode:" "reserved inode or inode > inodes count - " "block_group = %d,inode=%d", i, j); unlock_super (DEVVP(dir)); return 0; } gdp->bg_free_inodes_count--; if (S_ISDIR(mode)) gdp->bg_used_dirs_count++; mark_buffer_dirty(bh2); es->s_free_inodes_count--; /* mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1); */ sb->s_dirt = 1; unlock_super (DEVVP(dir)); return j; } #ifdef unused static unsigned long ext2_count_free_inodes (struct mount * mp) { #ifdef EXT2FS_DEBUG struct ext2_sb_info *sb = VFSTOUFS(mp)->um_e2fs; struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; int bitmap_nr; struct ext2_group_desc * gdp; int i; lock_super (VFSTOUFS(mp)->um_devvp); es = sb->s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; for (i = 0; i < sb->s_groups_count; i++) { gdp = get_group_desc (mp, i, NULL); desc_count += gdp->bg_free_inodes_count; bitmap_nr = load_inode_bitmap (mp, i); x = ext2_count_free (sb->s_inode_bitmap[bitmap_nr], EXT2_INODES_PER_GROUP(sb) / 8); ext2_debug ("group %d: stored = %d, counted = %lu\n", i, gdp->bg_free_inodes_count, x); bitmap_count += x; } ext2_debug("stored = %lu, computed = %lu, %lu\n", es->s_free_inodes_count, desc_count, bitmap_count); unlock_super (VFSTOUFS(mp)->um_devvp); return desc_count; #else return VFSTOUFS(mp)->um_e2fsb->s_free_inodes_count; #endif } #endif /* unused */ #ifdef LATER void ext2_check_inodes_bitmap (struct mount * mp) { struct ext2_super_block * es; unsigned long desc_count, bitmap_count, x; int bitmap_nr; struct ext2_group_desc * gdp; int i; lock_super (sb); es = sb->u.ext2_sb.s_es; desc_count = 0; bitmap_count = 0; gdp = NULL; for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) { gdp = get_group_desc (sb, i, NULL); desc_count += gdp->bg_free_inodes_count; bitmap_nr = load_inode_bitmap (sb, i); x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr], EXT2_INODES_PER_GROUP(sb) / 8); if (gdp->bg_free_inodes_count != x) printf ( "ext2_check_inodes_bitmap:" "Wrong free inodes count in group %d, " "stored = %d, counted = %lu", i, gdp->bg_free_inodes_count, x); bitmap_count += x; } if (es->s_free_inodes_count != bitmap_count) printf ( "ext2_check_inodes_bitmap:" "Wrong free inodes count in super block, " "stored = %lu, counted = %lu", (unsigned long) es->s_free_inodes_count, bitmap_count); unlock_super (sb); } #endif