2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * - The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 /** @addtogroup lwext4
39 * @brief Superblock operations.
42 #include <ext4_config.h>
43 #include <ext4_super.h>
46 uint32_t ext4_block_group_cnt(struct ext4_sblock *s)
48 uint64_t blocks_count = ext4_sb_get_blocks_cnt(s);
49 uint32_t blocks_per_group = ext4_get32(s, blocks_per_group);
51 uint32_t block_groups_count = blocks_count / blocks_per_group;
53 if (blocks_count % blocks_per_group)
56 return block_groups_count;
59 uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
61 uint32_t block_group_count = ext4_block_group_cnt(s);
62 uint32_t blocks_per_group = ext4_get32(s, blocks_per_group);
63 uint64_t total_blocks = ext4_sb_get_blocks_cnt(s);
65 if (bgid < block_group_count - 1)
66 return blocks_per_group;
69 return (total_blocks - ((block_group_count - 1) * blocks_per_group));
72 uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid)
74 uint32_t block_group_count = ext4_block_group_cnt(s);
75 uint32_t inodes_per_group = ext4_get32(s, inodes_per_group);
76 uint32_t total_inodes = ext4_get32(s, inodes_count);
79 if (bgid < block_group_count - 1)
80 return inodes_per_group;
82 return (total_inodes - ((block_group_count - 1) * inodes_per_group));
85 int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s)
87 return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET,
88 s, EXT4_SUPERBLOCK_SIZE);
91 int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s)
93 return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET,
94 s, EXT4_SUPERBLOCK_SIZE);
97 bool ext4_sb_check(struct ext4_sblock *s)
99 if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC)
102 if (ext4_get32(s, inodes_count) == 0)
105 if (ext4_sb_get_blocks_cnt(s) == 0)
108 if (ext4_get32(s, blocks_per_group) == 0)
111 if (ext4_get32(s, inodes_per_group) == 0)
114 if (ext4_get16(s, inode_size) < 128)
117 if (ext4_get32(s, first_inode) < 11)
120 if (ext4_sb_get_desc_size(s) < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE)
123 if (ext4_sb_get_desc_size(s) > EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)