blob: f63e028c638c2426136e4c300ccc0ec02bce2358 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/bitmap.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 */
10
11#include <linux/buffer_head.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040012#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070013
Theodore Ts'of6fb99c2012-06-30 19:14:57 -040014unsigned int ext4_count_free(char *bitmap, unsigned int numchars)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070015{
Akinobu Mita6017b482012-07-30 14:41:08 -070016 return numchars * BITS_PER_BYTE - memweight(bitmap, numchars);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070017}
18
Darrick J. Wong41a246d2012-04-29 18:33:10 -040019int ext4_inode_bitmap_csum_verify(struct super_block *sb, ext4_group_t group,
20 struct ext4_group_desc *gdp,
21 struct buffer_head *bh, int sz)
22{
23 __u32 hi;
24 __u32 provided, calculated;
25 struct ext4_sb_info *sbi = EXT4_SB(sb);
26
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040027 if (!ext4_has_metadata_csum(sb))
Darrick J. Wong41a246d2012-04-29 18:33:10 -040028 return 1;
29
30 provided = le16_to_cpu(gdp->bg_inode_bitmap_csum_lo);
31 calculated = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
32 if (sbi->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END) {
33 hi = le16_to_cpu(gdp->bg_inode_bitmap_csum_hi);
34 provided |= (hi << 16);
35 } else
36 calculated &= 0xFFFF;
37
38 return provided == calculated;
39}
40
41void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
42 struct ext4_group_desc *gdp,
43 struct buffer_head *bh, int sz)
44{
45 __u32 csum;
46 struct ext4_sb_info *sbi = EXT4_SB(sb);
47
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040048 if (!ext4_has_metadata_csum(sb))
Darrick J. Wong41a246d2012-04-29 18:33:10 -040049 return;
50
51 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
52 gdp->bg_inode_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF);
53 if (sbi->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
54 gdp->bg_inode_bitmap_csum_hi = cpu_to_le16(csum >> 16);
55}
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040056
57int ext4_block_bitmap_csum_verify(struct super_block *sb, ext4_group_t group,
58 struct ext4_group_desc *gdp,
Tao Ma79f1ba42012-10-22 00:34:32 -040059 struct buffer_head *bh)
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040060{
61 __u32 hi;
62 __u32 provided, calculated;
63 struct ext4_sb_info *sbi = EXT4_SB(sb);
Tao Ma79f1ba42012-10-22 00:34:32 -040064 int sz = EXT4_CLUSTERS_PER_GROUP(sb) / 8;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040065
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040066 if (!ext4_has_metadata_csum(sb))
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040067 return 1;
68
69 provided = le16_to_cpu(gdp->bg_block_bitmap_csum_lo);
70 calculated = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
71 if (sbi->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END) {
72 hi = le16_to_cpu(gdp->bg_block_bitmap_csum_hi);
73 provided |= (hi << 16);
74 } else
75 calculated &= 0xFFFF;
76
77 if (provided == calculated)
78 return 1;
79
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040080 return 0;
81}
82
83void ext4_block_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
84 struct ext4_group_desc *gdp,
Tao Ma79f1ba42012-10-22 00:34:32 -040085 struct buffer_head *bh)
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040086{
Tao Ma79f1ba42012-10-22 00:34:32 -040087 int sz = EXT4_CLUSTERS_PER_GROUP(sb) / 8;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040088 __u32 csum;
89 struct ext4_sb_info *sbi = EXT4_SB(sb);
90
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040091 if (!ext4_has_metadata_csum(sb))
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -040092 return;
93
94 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)bh->b_data, sz);
95 gdp->bg_block_bitmap_csum_lo = cpu_to_le16(csum & 0xFFFF);
96 if (sbi->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_END)
97 gdp->bg_block_bitmap_csum_hi = cpu_to_le16(csum >> 16);
98}