2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2017, Fedor Uporov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/types.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/vnode.h>
39 #include <sys/endian.h>
41 #include <sys/gsb_crc32.h>
42 #include <sys/crc16.h>
43 #include <sys/mount.h>
45 #include <fs/ext2fs/fs.h>
46 #include <fs/ext2fs/ext2fs.h>
47 #include <fs/ext2fs/ext2_dinode.h>
48 #include <fs/ext2fs/inode.h>
49 #include <fs/ext2fs/ext2_dir.h>
50 #include <fs/ext2fs/htree.h>
51 #include <fs/ext2fs/ext2_extattr.h>
52 #include <fs/ext2fs/ext2_extern.h>
54 SDT_PROVIDER_DECLARE(ext2fs);
57 * arg0: verbosity. Higher numbers give more verbose messages
58 * arg1: Textual message
60 SDT_PROBE_DEFINE2(ext2fs, , trace, csum, "int", "char*");
62 #define EXT2_BG_INODE_BITMAP_CSUM_HI_END \
63 (offsetof(struct ext2_gd, ext4bgd_i_bmap_csum_hi) + \
66 #define EXT2_INODE_CSUM_HI_EXTRA_END \
67 (offsetof(struct ext2fs_dinode, e2di_chksum_hi) + sizeof(uint16_t) - \
70 #define EXT2_BG_BLOCK_BITMAP_CSUM_HI_LOCATION \
71 (offsetof(struct ext2_gd, ext4bgd_b_bmap_csum_hi) + \
75 ext2_sb_csum_set_seed(struct m_ext2fs *fs)
78 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_CSUM_SEED))
79 fs->e2fs_csum_seed = le32toh(fs->e2fs->e4fs_chksum_seed);
80 else if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
81 fs->e2fs_csum_seed = calculate_crc32c(~0, fs->e2fs->e2fs_uuid,
82 sizeof(fs->e2fs->e2fs_uuid));
85 fs->e2fs_csum_seed = 0;
89 ext2_sb_csum_verify(struct m_ext2fs *fs)
92 if (fs->e2fs->e4fs_chksum_type != EXT4_CRC32C_CHKSUM) {
94 "WARNING: mount of %s denied due bad sb csum type\n", fs->e2fs_fsmnt);
97 if (le32toh(fs->e2fs->e4fs_sbchksum) !=
98 calculate_crc32c(~0, (const char *)fs->e2fs,
99 offsetof(struct ext2fs, e4fs_sbchksum))) {
101 "WARNING: mount of %s denied due bad sb csum=0x%x, expected=0x%x - run fsck\n",
102 fs->e2fs_fsmnt, le32toh(fs->e2fs->e4fs_sbchksum),
103 calculate_crc32c(~0, (const char *)fs->e2fs,
104 offsetof(struct ext2fs, e4fs_sbchksum)));
112 ext2_sb_csum_set(struct m_ext2fs *fs)
115 fs->e2fs->e4fs_sbchksum =
116 htole32(calculate_crc32c(~0, (const char *)fs->e2fs,
117 offsetof(struct ext2fs, e4fs_sbchksum)));
121 ext2_extattr_blk_csum(struct inode *ip, uint64_t facl,
122 struct ext2fs_extattr_header *header)
125 uint32_t crc, dummy_crc = 0;
126 uint64_t facl_bn = htole64(facl);
127 int offset = offsetof(struct ext2fs_extattr_header, h_checksum);
131 crc = calculate_crc32c(fs->e2fs_csum_seed, (uint8_t *)&facl_bn,
133 crc = calculate_crc32c(crc, (uint8_t *)header, offset);
134 crc = calculate_crc32c(crc, (uint8_t *)&dummy_crc,
136 offset += sizeof(dummy_crc);
137 crc = calculate_crc32c(crc, (uint8_t *)header + offset,
138 fs->e2fs_bsize - offset);
140 return (htole32(crc));
144 ext2_extattr_blk_csum_verify(struct inode *ip, struct buf *bp)
146 struct ext2fs_extattr_header *header;
148 header = (struct ext2fs_extattr_header *)bp->b_data;
150 if (EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_METADATA_CKSUM) &&
151 (header->h_checksum != ext2_extattr_blk_csum(ip, ip->i_facl, header))) {
152 SDT_PROBE2(ext2fs, , trace, csum, 1, "bad extattr csum detected");
160 ext2_extattr_blk_csum_set(struct inode *ip, struct buf *bp)
162 struct ext2fs_extattr_header *header;
164 if (!EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
167 header = (struct ext2fs_extattr_header *)bp->b_data;
168 header->h_checksum = ext2_extattr_blk_csum(ip, ip->i_facl, header);
172 ext2_init_dirent_tail(struct ext2fs_direct_tail *tp)
174 memset(tp, 0, sizeof(struct ext2fs_direct_tail));
175 tp->e2dt_rec_len = le16toh(sizeof(struct ext2fs_direct_tail));
176 tp->e2dt_reserved_ft = EXT2_FT_DIR_CSUM;
180 ext2_is_dirent_tail(struct inode *ip, struct ext2fs_direct_2 *ep)
183 struct ext2fs_direct_tail *tp;
187 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
190 tp = (struct ext2fs_direct_tail *)ep;
191 if (tp->e2dt_reserved_zero1 == 0 &&
192 le16toh(tp->e2dt_rec_len) == sizeof(struct ext2fs_direct_tail) &&
193 tp->e2dt_reserved_zero2 == 0 &&
194 tp->e2dt_reserved_ft == EXT2_FT_DIR_CSUM)
200 struct ext2fs_direct_tail *
201 ext2_dirent_get_tail(struct inode *ip, struct ext2fs_direct_2 *ep)
203 struct ext2fs_direct_2 *dep;
205 unsigned int rec_len;
208 top = EXT2_DIRENT_TAIL(ep, ip->i_e2fs->e2fs_bsize);
209 rec_len = le16toh(dep->e2d_reclen);
211 while (rec_len && !(rec_len & 0x3)) {
212 dep = (struct ext2fs_direct_2 *)(((char *)dep) + rec_len);
213 if ((void *)dep >= top)
215 rec_len = le16toh(dep->e2d_reclen);
221 if (ext2_is_dirent_tail(ip, dep))
222 return ((struct ext2fs_direct_tail *)dep);
228 ext2_dirent_csum(struct inode *ip, struct ext2fs_direct_2 *ep, int size)
232 uint32_t inum, gen, crc;
238 inum = htole32(ip->i_number);
239 gen = htole32(ip->i_gen);
240 crc = calculate_crc32c(fs->e2fs_csum_seed, (uint8_t *)&inum, sizeof(inum));
241 crc = calculate_crc32c(crc, (uint8_t *)&gen, sizeof(gen));
242 crc = calculate_crc32c(crc, (uint8_t *)buf, size);
248 ext2_dirent_csum_verify(struct inode *ip, struct ext2fs_direct_2 *ep)
251 struct ext2fs_direct_tail *tp;
253 tp = ext2_dirent_get_tail(ip, ep);
257 calculated = ext2_dirent_csum(ip, ep, (char *)tp - (char *)ep);
258 if (calculated != le32toh(tp->e2dt_checksum))
264 static struct ext2fs_htree_count *
265 ext2_get_dx_count(struct inode *ip, struct ext2fs_direct_2 *ep, int *offset)
267 struct ext2fs_direct_2 *dp;
268 struct ext2fs_htree_root_info *root;
271 if (le16toh(ep->e2d_reclen) == EXT2_BLOCK_SIZE(ip->i_e2fs))
273 else if (le16toh(ep->e2d_reclen) == 12) {
274 dp = (struct ext2fs_direct_2 *)(((char *)ep) + 12);
275 if (le16toh(dp->e2d_reclen) != EXT2_BLOCK_SIZE(ip->i_e2fs) - 12)
278 root = (struct ext2fs_htree_root_info *)(((char *)dp + 12));
279 if (root->h_reserved1 ||
280 root->h_info_len != sizeof(struct ext2fs_htree_root_info))
288 *offset = count_offset;
290 return ((struct ext2fs_htree_count *)(((char *)ep) + count_offset));
294 ext2_dx_csum(struct inode *ip, struct ext2fs_direct_2 *ep, int count_offset,
295 int count, struct ext2fs_htree_tail *tp)
300 uint32_t inum, old_csum, gen, crc;
306 size = count_offset + (count * sizeof(struct ext2fs_htree_entry));
307 old_csum = tp->ht_checksum;
310 inum = htole32(ip->i_number);
311 gen = htole32(ip->i_gen);
312 crc = calculate_crc32c(fs->e2fs_csum_seed, (uint8_t *)&inum, sizeof(inum));
313 crc = calculate_crc32c(crc, (uint8_t *)&gen, sizeof(gen));
314 crc = calculate_crc32c(crc, (uint8_t *)buf, size);
315 crc = calculate_crc32c(crc, (uint8_t *)tp, sizeof(struct ext2fs_htree_tail));
316 tp->ht_checksum = old_csum;
322 ext2_dx_csum_verify(struct inode *ip, struct ext2fs_direct_2 *ep)
325 struct ext2fs_htree_count *cp;
326 struct ext2fs_htree_tail *tp;
327 int count_offset, limit, count;
329 cp = ext2_get_dx_count(ip, ep, &count_offset);
333 limit = le16toh(cp->h_entries_max);
334 count = le16toh(cp->h_entries_num);
335 if (count_offset + (limit * sizeof(struct ext2fs_htree_entry)) >
336 ip->i_e2fs->e2fs_bsize - sizeof(struct ext2fs_htree_tail))
339 tp = (struct ext2fs_htree_tail *)(((struct ext2fs_htree_entry *)cp) + limit);
340 calculated = ext2_dx_csum(ip, ep, count_offset, count, tp);
342 if (tp->ht_checksum != calculated)
349 ext2_dir_blk_csum_verify(struct inode *ip, struct buf *bp)
352 struct ext2fs_direct_2 *ep;
357 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
360 ep = (struct ext2fs_direct_2 *)bp->b_data;
362 if (ext2_dirent_get_tail(ip, ep) != NULL)
363 error = ext2_dirent_csum_verify(ip, ep);
364 else if (ext2_get_dx_count(ip, ep, NULL) != NULL)
365 error = ext2_dx_csum_verify(ip, ep);
368 SDT_PROBE2(ext2fs, , trace, csum, 1, "bad directory csum detected");
374 ext2_dirent_csum_set(struct inode *ip, struct ext2fs_direct_2 *ep)
377 struct ext2fs_direct_tail *tp;
381 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
384 tp = ext2_dirent_get_tail(ip, ep);
389 htole32(ext2_dirent_csum(ip, ep, (char *)tp - (char *)ep));
393 ext2_dx_csum_set(struct inode *ip, struct ext2fs_direct_2 *ep)
396 struct ext2fs_htree_count *cp;
397 struct ext2fs_htree_tail *tp;
398 int count_offset, limit, count;
402 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
405 cp = ext2_get_dx_count(ip, ep, &count_offset);
409 limit = le16toh(cp->h_entries_max);
410 count = le16toh(cp->h_entries_num);
411 if (count_offset + (limit * sizeof(struct ext2fs_htree_entry)) >
412 ip->i_e2fs->e2fs_bsize - sizeof(struct ext2fs_htree_tail))
415 tp = (struct ext2fs_htree_tail *)(((struct ext2fs_htree_entry *)cp) + limit);
416 tp->ht_checksum = ext2_dx_csum(ip, ep, count_offset, count, tp);
420 ext2_extent_blk_csum(struct inode *ip, struct ext4_extent_header *ehp)
424 uint32_t inum, gen, crc;
428 size = EXT4_EXTENT_TAIL_OFFSET(ehp) +
429 offsetof(struct ext4_extent_tail, et_checksum);
431 inum = htole32(ip->i_number);
432 gen = htole32(ip->i_gen);
433 crc = calculate_crc32c(fs->e2fs_csum_seed, (uint8_t *)&inum, sizeof(inum));
434 crc = calculate_crc32c(crc, (uint8_t *)&gen, sizeof(gen));
435 crc = calculate_crc32c(crc, (uint8_t *)ehp, size);
441 ext2_extent_blk_csum_verify(struct inode *ip, void *data)
444 struct ext4_extent_header *ehp;
445 struct ext4_extent_tail *etp;
446 uint32_t provided, calculated;
450 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
453 ehp = (struct ext4_extent_header *)data;
454 etp = (struct ext4_extent_tail *)(((char *)ehp) +
455 EXT4_EXTENT_TAIL_OFFSET(ehp));
457 provided = le32toh(etp->et_checksum);
458 calculated = ext2_extent_blk_csum(ip, ehp);
460 if (provided != calculated) {
461 SDT_PROBE2(ext2fs, , trace, csum, 1, "bad extent csum detected");
469 ext2_extent_blk_csum_set(struct inode *ip, void *data)
472 struct ext4_extent_header *ehp;
473 struct ext4_extent_tail *etp;
477 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
480 ehp = (struct ext4_extent_header *)data;
481 etp = (struct ext4_extent_tail *)(((char *)data) +
482 EXT4_EXTENT_TAIL_OFFSET(ehp));
484 etp->et_checksum = htole32(ext2_extent_blk_csum(ip,
485 (struct ext4_extent_header *)data));
489 ext2_gd_i_bitmap_csum_verify(struct m_ext2fs *fs, int cg, struct buf *bp)
491 uint32_t hi, provided, calculated;
493 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
496 provided = le16toh(fs->e2fs_gd[cg].ext4bgd_i_bmap_csum);
497 calculated = calculate_crc32c(fs->e2fs_csum_seed, bp->b_data,
499 if (le16toh(fs->e2fs->e3fs_desc_size) >=
500 EXT2_BG_INODE_BITMAP_CSUM_HI_END) {
501 hi = le16toh(fs->e2fs_gd[cg].ext4bgd_i_bmap_csum_hi);
502 provided |= (hi << 16);
504 calculated &= 0xFFFF;
506 if (provided != calculated) {
507 SDT_PROBE2(ext2fs, , trace, csum, 1, "bad inode bitmap csum detected");
515 ext2_gd_i_bitmap_csum_set(struct m_ext2fs *fs, int cg, struct buf *bp)
519 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
522 csum = calculate_crc32c(fs->e2fs_csum_seed, bp->b_data,
524 fs->e2fs_gd[cg].ext4bgd_i_bmap_csum = htole16(csum & 0xFFFF);
525 if (le16toh(fs->e2fs->e3fs_desc_size) >= EXT2_BG_INODE_BITMAP_CSUM_HI_END)
526 fs->e2fs_gd[cg].ext4bgd_i_bmap_csum_hi = htole16(csum >> 16);
530 ext2_gd_b_bitmap_csum_verify(struct m_ext2fs *fs, int cg, struct buf *bp)
532 uint32_t hi, provided, calculated, size;
534 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
537 size = fs->e2fs_fpg / 8;
538 provided = le16toh(fs->e2fs_gd[cg].ext4bgd_b_bmap_csum);
539 calculated = calculate_crc32c(fs->e2fs_csum_seed, bp->b_data, size);
540 if (le16toh(fs->e2fs->e3fs_desc_size) >=
541 EXT2_BG_BLOCK_BITMAP_CSUM_HI_LOCATION) {
542 hi = le16toh(fs->e2fs_gd[cg].ext4bgd_b_bmap_csum_hi);
543 provided |= (hi << 16);
545 calculated &= 0xFFFF;
547 if (provided != calculated) {
548 SDT_PROBE2(ext2fs, , trace, csum, 1, "bad block bitmap csum detected");
556 ext2_gd_b_bitmap_csum_set(struct m_ext2fs *fs, int cg, struct buf *bp)
560 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
563 size = fs->e2fs_fpg / 8;
564 csum = calculate_crc32c(fs->e2fs_csum_seed, bp->b_data, size);
565 fs->e2fs_gd[cg].ext4bgd_b_bmap_csum = htole16(csum & 0xFFFF);
566 if (le16toh(fs->e2fs->e3fs_desc_size) >= EXT2_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
567 fs->e2fs_gd[cg].ext4bgd_b_bmap_csum_hi = htole16(csum >> 16);
571 ext2_ei_csum(struct inode *ip, struct ext2fs_dinode *ei)
574 uint32_t inode_csum_seed, inum, gen, crc;
575 uint16_t dummy_csum = 0;
576 unsigned int offset, csum_size;
579 offset = offsetof(struct ext2fs_dinode, e2di_chksum_lo);
580 csum_size = sizeof(dummy_csum);
581 inum = htole32(ip->i_number);
582 crc = calculate_crc32c(fs->e2fs_csum_seed,
583 (uint8_t *)&inum, sizeof(inum));
584 gen = htole32(ip->i_gen);
585 inode_csum_seed = calculate_crc32c(crc,
586 (uint8_t *)&gen, sizeof(gen));
588 crc = calculate_crc32c(inode_csum_seed, (uint8_t *)ei, offset);
589 crc = calculate_crc32c(crc, (uint8_t *)&dummy_csum, csum_size);
591 crc = calculate_crc32c(crc, (uint8_t *)ei + offset,
592 E2FS_REV0_INODE_SIZE - offset);
594 if (EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE) {
595 offset = offsetof(struct ext2fs_dinode, e2di_chksum_hi);
596 crc = calculate_crc32c(crc, (uint8_t *)ei +
597 E2FS_REV0_INODE_SIZE, offset - E2FS_REV0_INODE_SIZE);
599 if ((EXT2_INODE_SIZE(ip->i_e2fs) > E2FS_REV0_INODE_SIZE &&
600 le16toh(ei->e2di_extra_isize) >=
601 EXT2_INODE_CSUM_HI_EXTRA_END)) {
602 crc = calculate_crc32c(crc, (uint8_t *)&dummy_csum,
607 crc = calculate_crc32c(crc, (uint8_t *)ei + offset,
608 EXT2_INODE_SIZE(fs) - offset);
615 ext2_ei_csum_verify(struct inode *ip, struct ext2fs_dinode *ei)
618 const static struct ext2fs_dinode ei_zero;
619 uint32_t hi, provided, calculated;
623 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
626 provided = le16toh(ei->e2di_chksum_lo);
627 calculated = ext2_ei_csum(ip, ei);
629 if ((EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE &&
630 le16toh(ei->e2di_extra_isize) >= EXT2_INODE_CSUM_HI_EXTRA_END)) {
631 hi = le16toh(ei->e2di_chksum_hi);
632 provided |= hi << 16;
634 calculated &= 0xFFFF;
636 if (provided != calculated) {
638 * If it is first time used dinode,
639 * it is expected that it will be zeroed
640 * and we will not return checksum error in this case.
642 if (!memcmp(ei, &ei_zero, sizeof(struct ext2fs_dinode)))
645 SDT_PROBE2(ext2fs, , trace, csum, 1, "bad inode csum");
654 ext2_ei_csum_set(struct inode *ip, struct ext2fs_dinode *ei)
661 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM))
664 crc = ext2_ei_csum(ip, ei);
666 ei->e2di_chksum_lo = htole16(crc & 0xFFFF);
667 if ((EXT2_INODE_SIZE(fs) > E2FS_REV0_INODE_SIZE &&
668 le16toh(ei->e2di_extra_isize) >= EXT2_INODE_CSUM_HI_EXTRA_END))
669 ei->e2di_chksum_hi = htole16(crc >> 16);
673 ext2_gd_csum(struct m_ext2fs *fs, uint32_t block_group, struct ext2_gd *gd)
677 uint16_t crc, dummy_csum;
679 offset = offsetof(struct ext2_gd, ext4bgd_csum);
681 block_group = htole32(block_group);
683 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
684 csum32 = calculate_crc32c(fs->e2fs_csum_seed,
685 (uint8_t *)&block_group, sizeof(block_group));
686 csum32 = calculate_crc32c(csum32, (uint8_t *)gd, offset);
688 csum32 = calculate_crc32c(csum32, (uint8_t *)&dummy_csum,
690 offset += sizeof(dummy_csum);
691 if (offset < le16toh(fs->e2fs->e3fs_desc_size))
692 csum32 = calculate_crc32c(csum32, (uint8_t *)gd + offset,
693 le16toh(fs->e2fs->e3fs_desc_size) - offset);
695 crc = csum32 & 0xFFFF;
696 return (htole16(crc));
697 } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM)) {
698 crc = crc16(~0, fs->e2fs->e2fs_uuid,
699 sizeof(fs->e2fs->e2fs_uuid));
700 crc = crc16(crc, (uint8_t *)&block_group,
701 sizeof(block_group));
702 crc = crc16(crc, (uint8_t *)gd, offset);
703 offset += sizeof(gd->ext4bgd_csum); /* skip checksum */
704 if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_64BIT) &&
705 offset < le16toh(fs->e2fs->e3fs_desc_size))
706 crc = crc16(crc, (uint8_t *)gd + offset,
707 le16toh(fs->e2fs->e3fs_desc_size) - offset);
708 return (htole16(crc));
715 ext2_gd_csum_verify(struct m_ext2fs *fs, struct cdev *dev)
720 for (i = 0; i < fs->e2fs_gcount; i++) {
721 if (fs->e2fs_gd[i].ext4bgd_csum !=
722 ext2_gd_csum(fs, i, &fs->e2fs_gd[i])) {
724 "WARNING: mount of %s denied due bad gd=%d csum=0x%x, expected=0x%x - run fsck\n",
725 devtoname(dev), i, fs->e2fs_gd[i].ext4bgd_csum,
726 ext2_gd_csum(fs, i, &fs->e2fs_gd[i]));
736 ext2_gd_csum_set(struct m_ext2fs *fs)
740 for (i = 0; i < fs->e2fs_gcount; i++)
741 fs->e2fs_gd[i].ext4bgd_csum = ext2_gd_csum(fs, i, &fs->e2fs_gd[i]);