Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/bitmap.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | */ |
| 9 | |
| 10 | #include <linux/buffer_head.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 11 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 12 | |
Theodore Ts'o | f6fb99c | 2012-06-30 19:14:57 -0400 | [diff] [blame] | 13 | unsigned int ext4_count_free(char *bitmap, unsigned int numchars) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 14 | { |
Akinobu Mita | 6017b48 | 2012-07-30 14:41:08 -0700 | [diff] [blame] | 15 | return numchars * BITS_PER_BYTE - memweight(bitmap, numchars); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 16 | } |
| 17 | |
Darrick J. Wong | 41a246d | 2012-04-29 18:33:10 -0400 | [diff] [blame] | 18 | int ext4_inode_bitmap_csum_verify(struct super_block *sb, ext4_group_t group, |
| 19 | struct ext4_group_desc *gdp, |
| 20 | struct buffer_head *bh, int sz) |
| 21 | { |
| 22 | __u32 hi; |
| 23 | __u32 provided, calculated; |
| 24 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 25 | |
Dmitry Monakhov | 9aa5d32 | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 26 | if (!ext4_has_metadata_csum(sb)) |
Darrick J. Wong | 41a246d | 2012-04-29 18:33:10 -0400 | [diff] [blame] | 27 | return 1; |
| 28 | |
| 29 | provided = le16_to_cpu(gdp->bg_inode_bitmap_csum_lo); |
| 30 | calculated = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz); |
| 31 | if (sbi->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END) { |
| 32 | hi = le16_to_cpu(gdp->bg_inode_bitmap_csum_hi); |
| 33 | provided |= (hi << 16); |
| 34 | } else |
| 35 | calculated &= 0xFFFF; |
| 36 | |
| 37 | return provided == calculated; |
| 38 | } |
| 39 | |
| 40 | void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group, |
| 41 | struct ext4_group_desc *gdp, |
| 42 | struct buffer_head *bh, int sz) |
| 43 | { |
| 44 | __u32 csum; |
| 45 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 46 | |
Dmitry Monakhov | 9aa5d32 | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 47 | if (!ext4_has_metadata_csum(sb)) |
Darrick J. Wong | 41a246d | 2012-04-29 18:33:10 -0400 | [diff] [blame] | 48 | return; |
| 49 | |
| 50 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz); |
| 51 | gdp->bg_inode_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF); |
| 52 | if (sbi->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END) |
| 53 | gdp->bg_inode_bitmap_csum_hi = cpu_to_le16(csum >> 16); |
| 54 | } |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 55 | |
| 56 | int ext4_block_bitmap_csum_verify(struct super_block *sb, ext4_group_t group, |
| 57 | struct ext4_group_desc *gdp, |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 58 | struct buffer_head *bh) |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 59 | { |
| 60 | __u32 hi; |
| 61 | __u32 provided, calculated; |
| 62 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 63 | int sz = EXT4_CLUSTERS_PER_GROUP(sb) / 8; |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 64 | |
Dmitry Monakhov | 9aa5d32 | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 65 | if (!ext4_has_metadata_csum(sb)) |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 66 | return 1; |
| 67 | |
| 68 | provided = le16_to_cpu(gdp->bg_block_bitmap_csum_lo); |
| 69 | calculated = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz); |
| 70 | if (sbi->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END) { |
| 71 | hi = le16_to_cpu(gdp->bg_block_bitmap_csum_hi); |
| 72 | provided |= (hi << 16); |
| 73 | } else |
| 74 | calculated &= 0xFFFF; |
| 75 | |
| 76 | if (provided == calculated) |
| 77 | return 1; |
| 78 | |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | void ext4_block_bitmap_csum_set(struct super_block *sb, ext4_group_t group, |
| 83 | struct ext4_group_desc *gdp, |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 84 | struct buffer_head *bh) |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 85 | { |
Tao Ma | 79f1ba4 | 2012-10-22 00:34:32 -0400 | [diff] [blame] | 86 | int sz = EXT4_CLUSTERS_PER_GROUP(sb) / 8; |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 87 | __u32 csum; |
| 88 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 89 | |
Dmitry Monakhov | 9aa5d32 | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 90 | if (!ext4_has_metadata_csum(sb)) |
Darrick J. Wong | fa77dcf | 2012-04-29 18:35:10 -0400 | [diff] [blame] | 91 | return; |
| 92 | |
| 93 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz); |
| 94 | gdp->bg_block_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF); |
| 95 | if (sbi->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END) |
| 96 | gdp->bg_block_bitmap_csum_hi = cpu_to_le16(csum >> 16); |
| 97 | } |