Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011 - 2012 Samsung Electronics |
| 4 | * EXT4 filesystem implementation in Uboot by |
| 5 | * Uma Shankar <uma.shankar@samsung.com> |
| 6 | * Manjunatha C Achar <a.manjunatha@samsung.com> |
| 7 | * |
| 8 | * ext4ls and ext4load : Based on ext2 ls load support in Uboot. |
| 9 | * |
| 10 | * (C) Copyright 2004 |
| 11 | * esd gmbh <www.esd-electronics.com> |
| 12 | * Reinhard Arlt <reinhard.arlt@esd-electronics.com> |
| 13 | * |
| 14 | * based on code from grub2 fs/ext2.c and fs/fshelp.c by |
| 15 | * GRUB -- GRand Unified Bootloader |
| 16 | * Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
| 17 | * |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 18 | * ext4write : Based on generic ext4 protocol. |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 19 | */ |
| 20 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 21 | #include <blk.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 22 | #include <ext_common.h> |
| 23 | #include <ext4fs.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 24 | #include <log.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 25 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 26 | #include <memalign.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 27 | #include <part.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 28 | #include <stddef.h> |
| 29 | #include <linux/stat.h> |
| 30 | #include <linux/time.h> |
| 31 | #include <asm/byteorder.h> |
| 32 | #include "ext4_common.h" |
| 33 | |
| 34 | struct ext2_data *ext4fs_root; |
| 35 | struct ext2fs_node *ext4fs_file; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 36 | __le32 *ext4fs_indir1_block; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 37 | int ext4fs_indir1_size; |
| 38 | int ext4fs_indir1_blkno = -1; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 39 | __le32 *ext4fs_indir2_block; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 40 | int ext4fs_indir2_size; |
| 41 | int ext4fs_indir2_blkno = -1; |
| 42 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 43 | __le32 *ext4fs_indir3_block; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 44 | int ext4fs_indir3_size; |
| 45 | int ext4fs_indir3_blkno = -1; |
| 46 | struct ext2_inode *g_parent_inode; |
| 47 | static int symlinknest; |
| 48 | |
Stephen Warren | 4f8662d | 2012-10-22 06:43:50 +0000 | [diff] [blame] | 49 | #if defined(CONFIG_EXT4_WRITE) |
Stefan Brüns | 387f866 | 2016-09-20 01:12:42 +0200 | [diff] [blame] | 50 | struct ext2_block_group *ext4fs_get_group_descriptor |
| 51 | (const struct ext_filesystem *fs, uint32_t bg_idx) |
| 52 | { |
| 53 | return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize)); |
| 54 | } |
| 55 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 56 | static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb) |
| 57 | { |
| 58 | sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1); |
| 59 | } |
| 60 | |
| 61 | static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb) |
| 62 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 63 | uint64_t free_blocks = le32_to_cpu(sb->free_blocks); |
| 64 | free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32; |
| 65 | free_blocks--; |
| 66 | |
| 67 | sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff); |
| 68 | sb->free_blocks_high = cpu_to_le16(free_blocks >> 32); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 71 | static inline void ext4fs_bg_free_inodes_dec |
| 72 | (struct ext2_block_group *bg, const struct ext_filesystem *fs) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 73 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 74 | uint32_t free_inodes = le16_to_cpu(bg->free_inodes); |
| 75 | if (fs->gdsize == 64) |
| 76 | free_inodes += le16_to_cpu(bg->free_inodes_high) << 16; |
| 77 | free_inodes--; |
| 78 | |
| 79 | bg->free_inodes = cpu_to_le16(free_inodes & 0xffff); |
| 80 | if (fs->gdsize == 64) |
| 81 | bg->free_inodes_high = cpu_to_le16(free_inodes >> 16); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 84 | static inline void ext4fs_bg_free_blocks_dec |
| 85 | (struct ext2_block_group *bg, const struct ext_filesystem *fs) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 86 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 87 | uint32_t free_blocks = le16_to_cpu(bg->free_blocks); |
| 88 | if (fs->gdsize == 64) |
| 89 | free_blocks += le16_to_cpu(bg->free_blocks_high) << 16; |
| 90 | free_blocks--; |
| 91 | |
| 92 | bg->free_blocks = cpu_to_le16(free_blocks & 0xffff); |
| 93 | if (fs->gdsize == 64) |
| 94 | bg->free_blocks_high = cpu_to_le16(free_blocks >> 16); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 97 | static inline void ext4fs_bg_itable_unused_dec |
| 98 | (struct ext2_block_group *bg, const struct ext_filesystem *fs) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 99 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 100 | uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused); |
| 101 | if (fs->gdsize == 64) |
| 102 | free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16; |
| 103 | free_inodes--; |
| 104 | |
| 105 | bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff); |
| 106 | if (fs->gdsize == 64) |
| 107 | bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Stefan Brüns | 387f866 | 2016-09-20 01:12:42 +0200 | [diff] [blame] | 110 | uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb) |
| 111 | { |
| 112 | uint64_t free_blocks = le32_to_cpu(sb->free_blocks); |
| 113 | free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32; |
| 114 | return free_blocks; |
| 115 | } |
| 116 | |
| 117 | void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks) |
| 118 | { |
| 119 | sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff); |
| 120 | sb->free_blocks_high = cpu_to_le16(free_blocks >> 32); |
| 121 | } |
| 122 | |
| 123 | uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg, |
| 124 | const struct ext_filesystem *fs) |
| 125 | { |
| 126 | uint32_t free_blocks = le16_to_cpu(bg->free_blocks); |
| 127 | if (fs->gdsize == 64) |
| 128 | free_blocks += le16_to_cpu(bg->free_blocks_high) << 16; |
| 129 | return free_blocks; |
| 130 | } |
| 131 | |
| 132 | static inline |
| 133 | uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg, |
| 134 | const struct ext_filesystem *fs) |
| 135 | { |
| 136 | uint32_t free_inodes = le16_to_cpu(bg->free_inodes); |
| 137 | if (fs->gdsize == 64) |
| 138 | free_inodes += le16_to_cpu(bg->free_inodes_high) << 16; |
| 139 | return free_inodes; |
| 140 | } |
| 141 | |
| 142 | static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg) |
| 143 | { |
| 144 | return le16_to_cpu(bg->bg_flags); |
| 145 | } |
| 146 | |
| 147 | static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg, |
| 148 | uint16_t flags) |
| 149 | { |
| 150 | bg->bg_flags = cpu_to_le16(flags); |
| 151 | } |
| 152 | |
| 153 | /* Block number of the block bitmap */ |
| 154 | uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg, |
| 155 | const struct ext_filesystem *fs) |
| 156 | { |
| 157 | uint64_t block_nr = le32_to_cpu(bg->block_id); |
| 158 | if (fs->gdsize == 64) |
| 159 | block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32; |
| 160 | return block_nr; |
| 161 | } |
| 162 | |
| 163 | /* Block number of the inode bitmap */ |
| 164 | uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg, |
| 165 | const struct ext_filesystem *fs) |
| 166 | { |
| 167 | uint64_t block_nr = le32_to_cpu(bg->inode_id); |
| 168 | if (fs->gdsize == 64) |
| 169 | block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32; |
| 170 | return block_nr; |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | /* Block number of the inode table */ |
| 175 | uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg, |
| 176 | const struct ext_filesystem *fs) |
| 177 | { |
| 178 | uint64_t block_nr = le32_to_cpu(bg->inode_table_id); |
| 179 | if (fs->gdsize == 64) |
| 180 | block_nr += |
| 181 | (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32; |
| 182 | return block_nr; |
| 183 | } |
| 184 | |
| 185 | #if defined(CONFIG_EXT4_WRITE) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 186 | uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n) |
| 187 | { |
| 188 | uint32_t res = size / n; |
| 189 | if (res * n != size) |
| 190 | res++; |
| 191 | |
| 192 | return res; |
| 193 | } |
| 194 | |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 195 | void put_ext4(uint64_t off, const void *buf, uint32_t size) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 196 | { |
| 197 | uint64_t startblock; |
| 198 | uint64_t remainder; |
| 199 | unsigned char *temp_ptr = NULL; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 200 | struct ext_filesystem *fs = get_fs(); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 201 | int log2blksz = fs->dev_desc->log2blksz; |
| 202 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 203 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 204 | startblock = off >> log2blksz; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 205 | startblock += part_offset; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 206 | remainder = off & (uint64_t)(fs->dev_desc->blksz - 1); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 207 | |
| 208 | if (fs->dev_desc == NULL) |
| 209 | return; |
| 210 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 211 | if ((startblock + (size >> log2blksz)) > |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 212 | (part_offset + fs->total_sect)) { |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 213 | printf("part_offset is " LBAFU "\n", part_offset); |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 214 | printf("total_sector is %llu\n", fs->total_sect); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 215 | printf("error: overflow occurs\n"); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | if (remainder) { |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 220 | blk_dread(fs->dev_desc, startblock, 1, sec_buf); |
| 221 | temp_ptr = sec_buf; |
| 222 | memcpy((temp_ptr + remainder), (unsigned char *)buf, size); |
| 223 | blk_dwrite(fs->dev_desc, startblock, 1, sec_buf); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 224 | } else { |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 225 | if (size >> log2blksz != 0) { |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 226 | blk_dwrite(fs->dev_desc, startblock, size >> log2blksz, |
| 227 | (unsigned long *)buf); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 228 | } else { |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 229 | blk_dread(fs->dev_desc, startblock, 1, sec_buf); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 230 | temp_ptr = sec_buf; |
| 231 | memcpy(temp_ptr, buf, size); |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 232 | blk_dwrite(fs->dev_desc, startblock, 1, |
| 233 | (unsigned long *)sec_buf); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static int _get_new_inode_no(unsigned char *buffer) |
| 239 | { |
| 240 | struct ext_filesystem *fs = get_fs(); |
| 241 | unsigned char input; |
| 242 | int operand, status; |
| 243 | int count = 1; |
| 244 | int j = 0; |
| 245 | |
| 246 | /* get the blocksize of the filesystem */ |
| 247 | unsigned char *ptr = buffer; |
| 248 | while (*ptr == 255) { |
| 249 | ptr++; |
| 250 | count += 8; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 251 | if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group)) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | for (j = 0; j < fs->blksz; j++) { |
| 256 | input = *ptr; |
| 257 | int i = 0; |
| 258 | while (i <= 7) { |
| 259 | operand = 1 << i; |
| 260 | status = input & operand; |
| 261 | if (status) { |
| 262 | i++; |
| 263 | count++; |
| 264 | } else { |
| 265 | *ptr |= operand; |
| 266 | return count; |
| 267 | } |
| 268 | } |
| 269 | ptr = ptr + 1; |
| 270 | } |
| 271 | |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | static int _get_new_blk_no(unsigned char *buffer) |
| 276 | { |
Stefan Brüns | 6303191 | 2016-09-06 04:36:50 +0200 | [diff] [blame] | 277 | int operand; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 278 | int count = 0; |
Stefan Brüns | 6303191 | 2016-09-06 04:36:50 +0200 | [diff] [blame] | 279 | int i; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 280 | unsigned char *ptr = buffer; |
| 281 | struct ext_filesystem *fs = get_fs(); |
| 282 | |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 283 | while (*ptr == 255) { |
| 284 | ptr++; |
| 285 | count += 8; |
| 286 | if (count == (fs->blksz * 8)) |
| 287 | return -1; |
| 288 | } |
| 289 | |
Stefan Brüns | 6303191 | 2016-09-06 04:36:50 +0200 | [diff] [blame] | 290 | if (fs->blksz == 1024) |
| 291 | count += 1; |
| 292 | |
| 293 | for (i = 0; i <= 7; i++) { |
| 294 | operand = 1 << i; |
| 295 | if (*ptr & operand) { |
| 296 | count++; |
| 297 | } else { |
| 298 | *ptr |= operand; |
| 299 | return count; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 300 | } |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return -1; |
| 304 | } |
| 305 | |
| 306 | int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index) |
| 307 | { |
| 308 | int i, remainder, status; |
| 309 | unsigned char *ptr = buffer; |
| 310 | unsigned char operand; |
| 311 | i = blockno / 8; |
| 312 | remainder = blockno % 8; |
| 313 | int blocksize = EXT2_BLOCK_SIZE(ext4fs_root); |
| 314 | |
| 315 | i = i - (index * blocksize); |
| 316 | if (blocksize != 1024) { |
| 317 | ptr = ptr + i; |
| 318 | operand = 1 << remainder; |
| 319 | status = *ptr & operand; |
| 320 | if (status) |
| 321 | return -1; |
| 322 | |
| 323 | *ptr = *ptr | operand; |
| 324 | return 0; |
| 325 | } else { |
| 326 | if (remainder == 0) { |
| 327 | ptr = ptr + i - 1; |
| 328 | operand = (1 << 7); |
| 329 | } else { |
| 330 | ptr = ptr + i; |
| 331 | operand = (1 << (remainder - 1)); |
| 332 | } |
| 333 | status = *ptr & operand; |
| 334 | if (status) |
| 335 | return -1; |
| 336 | |
| 337 | *ptr = *ptr | operand; |
| 338 | return 0; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index) |
| 343 | { |
| 344 | int i, remainder, status; |
| 345 | unsigned char *ptr = buffer; |
| 346 | unsigned char operand; |
| 347 | i = blockno / 8; |
| 348 | remainder = blockno % 8; |
| 349 | int blocksize = EXT2_BLOCK_SIZE(ext4fs_root); |
| 350 | |
| 351 | i = i - (index * blocksize); |
| 352 | if (blocksize != 1024) { |
| 353 | ptr = ptr + i; |
| 354 | operand = (1 << remainder); |
| 355 | status = *ptr & operand; |
| 356 | if (status) |
| 357 | *ptr = *ptr & ~(operand); |
| 358 | } else { |
| 359 | if (remainder == 0) { |
| 360 | ptr = ptr + i - 1; |
| 361 | operand = (1 << 7); |
| 362 | } else { |
| 363 | ptr = ptr + i; |
| 364 | operand = (1 << (remainder - 1)); |
| 365 | } |
| 366 | status = *ptr & operand; |
| 367 | if (status) |
| 368 | *ptr = *ptr & ~(operand); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index) |
| 373 | { |
| 374 | int i, remainder, status; |
| 375 | unsigned char *ptr = buffer; |
| 376 | unsigned char operand; |
| 377 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 378 | inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group)); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 379 | i = inode_no / 8; |
| 380 | remainder = inode_no % 8; |
| 381 | if (remainder == 0) { |
| 382 | ptr = ptr + i - 1; |
| 383 | operand = (1 << 7); |
| 384 | } else { |
| 385 | ptr = ptr + i; |
| 386 | operand = (1 << (remainder - 1)); |
| 387 | } |
| 388 | status = *ptr & operand; |
| 389 | if (status) |
| 390 | return -1; |
| 391 | |
| 392 | *ptr = *ptr | operand; |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index) |
| 398 | { |
| 399 | int i, remainder, status; |
| 400 | unsigned char *ptr = buffer; |
| 401 | unsigned char operand; |
| 402 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 403 | inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group)); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 404 | i = inode_no / 8; |
| 405 | remainder = inode_no % 8; |
| 406 | if (remainder == 0) { |
| 407 | ptr = ptr + i - 1; |
| 408 | operand = (1 << 7); |
| 409 | } else { |
| 410 | ptr = ptr + i; |
| 411 | operand = (1 << (remainder - 1)); |
| 412 | } |
| 413 | status = *ptr & operand; |
| 414 | if (status) |
| 415 | *ptr = *ptr & ~(operand); |
| 416 | } |
| 417 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 418 | uint16_t ext4fs_checksum_update(uint32_t i) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 419 | { |
| 420 | struct ext2_block_group *desc; |
| 421 | struct ext_filesystem *fs = get_fs(); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 422 | uint16_t crc = 0; |
| 423 | __le32 le32_i = cpu_to_le32(i); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 424 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 425 | desc = ext4fs_get_group_descriptor(fs, i); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 426 | if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 427 | int offset = offsetof(struct ext2_block_group, bg_checksum); |
| 428 | |
Pali Rohár | be4ee4e | 2022-04-12 11:20:43 +0200 | [diff] [blame] | 429 | crc = crc16(~0, (__u8 *)fs->sb->unique_id, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 430 | sizeof(fs->sb->unique_id)); |
Pali Rohár | be4ee4e | 2022-04-12 11:20:43 +0200 | [diff] [blame] | 431 | crc = crc16(crc, (__u8 *)&le32_i, sizeof(le32_i)); |
| 432 | crc = crc16(crc, (__u8 *)desc, offset); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 433 | offset += sizeof(desc->bg_checksum); /* skip checksum */ |
| 434 | assert(offset == sizeof(*desc)); |
Tuomas Tynkkynen | 48fbf25 | 2017-09-25 22:06:31 +0300 | [diff] [blame] | 435 | if (offset < fs->gdsize) { |
Pali Rohár | be4ee4e | 2022-04-12 11:20:43 +0200 | [diff] [blame] | 436 | crc = crc16(crc, (__u8 *)desc + offset, |
Tuomas Tynkkynen | 48fbf25 | 2017-09-25 22:06:31 +0300 | [diff] [blame] | 437 | fs->gdsize - offset); |
| 438 | } |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | return crc; |
| 442 | } |
| 443 | |
| 444 | static int check_void_in_dentry(struct ext2_dirent *dir, char *filename) |
| 445 | { |
| 446 | int dentry_length; |
| 447 | int sizeof_void_space; |
| 448 | int new_entry_byte_reqd; |
| 449 | short padding_factor = 0; |
| 450 | |
| 451 | if (dir->namelen % 4 != 0) |
| 452 | padding_factor = 4 - (dir->namelen % 4); |
| 453 | |
| 454 | dentry_length = sizeof(struct ext2_dirent) + |
| 455 | dir->namelen + padding_factor; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 456 | sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 457 | if (sizeof_void_space == 0) |
| 458 | return 0; |
| 459 | |
| 460 | padding_factor = 0; |
| 461 | if (strlen(filename) % 4 != 0) |
| 462 | padding_factor = 4 - (strlen(filename) % 4); |
| 463 | |
| 464 | new_entry_byte_reqd = strlen(filename) + |
| 465 | sizeof(struct ext2_dirent) + padding_factor; |
| 466 | if (sizeof_void_space >= new_entry_byte_reqd) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 467 | dir->direntlen = cpu_to_le16(dentry_length); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 468 | return sizeof_void_space; |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Stefan Brüns | c102068 | 2016-09-06 04:36:42 +0200 | [diff] [blame] | 474 | int ext4fs_update_parent_dentry(char *filename, int file_type) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 475 | { |
| 476 | unsigned int *zero_buffer = NULL; |
| 477 | char *root_first_block_buffer = NULL; |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 478 | int blk_idx; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 479 | long int first_block_no_of_root = 0; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 480 | int totalbytes = 0; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 481 | unsigned int new_entry_byte_reqd; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 482 | int sizeof_void_space = 0; |
| 483 | int templength = 0; |
Stefan Brüns | c102068 | 2016-09-06 04:36:42 +0200 | [diff] [blame] | 484 | int inodeno = -1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 485 | int status; |
| 486 | struct ext_filesystem *fs = get_fs(); |
| 487 | /* directory entry */ |
| 488 | struct ext2_dirent *dir; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 489 | char *temp_dir = NULL; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 490 | uint32_t new_blk_no; |
| 491 | uint32_t new_size; |
| 492 | uint32_t new_blockcnt; |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 493 | uint32_t directory_blocks; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 494 | |
| 495 | zero_buffer = zalloc(fs->blksz); |
| 496 | if (!zero_buffer) { |
| 497 | printf("No Memory\n"); |
Stefan Brüns | c102068 | 2016-09-06 04:36:42 +0200 | [diff] [blame] | 498 | return -1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 499 | } |
| 500 | root_first_block_buffer = zalloc(fs->blksz); |
| 501 | if (!root_first_block_buffer) { |
| 502 | free(zero_buffer); |
| 503 | printf("No Memory\n"); |
Stefan Brüns | c102068 | 2016-09-06 04:36:42 +0200 | [diff] [blame] | 504 | return -1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 505 | } |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 506 | new_entry_byte_reqd = ROUND(strlen(filename) + |
| 507 | sizeof(struct ext2_dirent), 4); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 508 | restart: |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 509 | directory_blocks = le32_to_cpu(g_parent_inode->size) >> |
| 510 | LOG2_BLOCK_SIZE(ext4fs_root); |
| 511 | blk_idx = directory_blocks - 1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 512 | |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 513 | restart_read: |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 514 | /* read the block no allocated to a file */ |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 515 | first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx, |
| 516 | NULL); |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 517 | if (first_block_no_of_root <= 0) |
| 518 | goto fail; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 519 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 520 | status = ext4fs_devread((lbaint_t)first_block_no_of_root |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 521 | * fs->sect_perblk, |
| 522 | 0, fs->blksz, root_first_block_buffer); |
| 523 | if (status == 0) |
| 524 | goto fail; |
| 525 | |
| 526 | if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root)) |
| 527 | goto fail; |
| 528 | dir = (struct ext2_dirent *)root_first_block_buffer; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 529 | totalbytes = 0; |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 530 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 531 | while (le16_to_cpu(dir->direntlen) > 0) { |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 532 | unsigned short used_len = ROUND(dir->namelen + |
| 533 | sizeof(struct ext2_dirent), 4); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 534 | |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 535 | /* last entry of block */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 536 | if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 537 | |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 538 | /* check if new entry fits */ |
| 539 | if ((used_len + new_entry_byte_reqd) <= |
| 540 | le16_to_cpu(dir->direntlen)) { |
| 541 | dir->direntlen = cpu_to_le16(used_len); |
| 542 | break; |
| 543 | } else { |
| 544 | if (blk_idx > 0) { |
| 545 | printf("Block full, trying previous\n"); |
| 546 | blk_idx--; |
| 547 | goto restart_read; |
| 548 | } |
| 549 | printf("All blocks full: Allocate new\n"); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 550 | |
Stefan Brüns | 2bef689 | 2016-09-06 04:36:43 +0200 | [diff] [blame] | 551 | if (le32_to_cpu(g_parent_inode->flags) & |
| 552 | EXT4_EXTENTS_FL) { |
| 553 | printf("Directory uses extents\n"); |
| 554 | goto fail; |
| 555 | } |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 556 | if (directory_blocks >= INDIRECT_BLOCKS) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 557 | printf("Directory exceeds limit\n"); |
| 558 | goto fail; |
| 559 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 560 | new_blk_no = ext4fs_get_new_blk_no(); |
| 561 | if (new_blk_no == -1) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 562 | printf("no block left to assign\n"); |
| 563 | goto fail; |
| 564 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 565 | put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz); |
Stefan Brüns | f577281 | 2016-09-06 04:36:44 +0200 | [diff] [blame] | 566 | g_parent_inode->b.blocks. |
| 567 | dir_blocks[directory_blocks] = |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 568 | cpu_to_le32(new_blk_no); |
| 569 | |
| 570 | new_size = le32_to_cpu(g_parent_inode->size); |
| 571 | new_size += fs->blksz; |
| 572 | g_parent_inode->size = cpu_to_le32(new_size); |
| 573 | |
| 574 | new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt); |
Marek Szyprowski | 07a39ab | 2019-06-21 15:32:51 +0200 | [diff] [blame] | 575 | new_blockcnt += fs->blksz >> LOG2_SECTOR_SIZE; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 576 | g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt); |
| 577 | |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 578 | if (ext4fs_put_metadata |
| 579 | (root_first_block_buffer, |
| 580 | first_block_no_of_root)) |
| 581 | goto fail; |
| 582 | goto restart; |
| 583 | } |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 584 | } |
| 585 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 586 | templength = le16_to_cpu(dir->direntlen); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 587 | totalbytes = totalbytes + templength; |
| 588 | sizeof_void_space = check_void_in_dentry(dir, filename); |
| 589 | if (sizeof_void_space) |
| 590 | break; |
| 591 | |
| 592 | dir = (struct ext2_dirent *)((char *)dir + templength); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* make a pointer ready for creating next directory entry */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 596 | templength = le16_to_cpu(dir->direntlen); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 597 | totalbytes = totalbytes + templength; |
| 598 | dir = (struct ext2_dirent *)((char *)dir + templength); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 599 | |
| 600 | /* get the next available inode number */ |
| 601 | inodeno = ext4fs_get_new_inode_no(); |
| 602 | if (inodeno == -1) { |
| 603 | printf("no inode left to assign\n"); |
| 604 | goto fail; |
| 605 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 606 | dir->inode = cpu_to_le32(inodeno); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 607 | if (sizeof_void_space) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 608 | dir->direntlen = cpu_to_le16(sizeof_void_space); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 609 | else |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 610 | dir->direntlen = cpu_to_le16(fs->blksz - totalbytes); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 611 | |
| 612 | dir->namelen = strlen(filename); |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 613 | dir->filetype = file_type; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 614 | temp_dir = (char *)dir; |
| 615 | temp_dir = temp_dir + sizeof(struct ext2_dirent); |
| 616 | memcpy(temp_dir, filename, strlen(filename)); |
| 617 | |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 618 | /* update or write the 1st block of root inode */ |
| 619 | if (ext4fs_put_metadata(root_first_block_buffer, |
| 620 | first_block_no_of_root)) |
| 621 | goto fail; |
| 622 | |
| 623 | fail: |
| 624 | free(zero_buffer); |
| 625 | free(root_first_block_buffer); |
Stefan Brüns | c102068 | 2016-09-06 04:36:42 +0200 | [diff] [blame] | 626 | |
| 627 | return inodeno; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static int search_dir(struct ext2_inode *parent_inode, char *dirname) |
| 631 | { |
| 632 | int status; |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 633 | int inodeno = 0; |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 634 | int offset; |
| 635 | int blk_idx; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 636 | long int blknr; |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 637 | char *block_buffer = NULL; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 638 | struct ext2_dirent *dir = NULL; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 639 | struct ext_filesystem *fs = get_fs(); |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 640 | uint32_t directory_blocks; |
| 641 | char *direntname; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 642 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 643 | directory_blocks = le32_to_cpu(parent_inode->size) >> |
| 644 | LOG2_BLOCK_SIZE(ext4fs_root); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 645 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 646 | block_buffer = zalloc(fs->blksz); |
| 647 | if (!block_buffer) |
| 648 | goto fail; |
| 649 | |
| 650 | /* get the block no allocated to a file */ |
| 651 | for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) { |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 652 | blknr = read_allocated_block(parent_inode, blk_idx, NULL); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 653 | if (blknr <= 0) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 654 | goto fail; |
| 655 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 656 | /* read the directory block */ |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 657 | status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 658 | 0, fs->blksz, (char *)block_buffer); |
| 659 | if (status == 0) |
| 660 | goto fail; |
| 661 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 662 | offset = 0; |
| 663 | do { |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 664 | if (offset & 3) { |
| 665 | printf("Badly aligned ext2_dirent\n"); |
| 666 | break; |
| 667 | } |
| 668 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 669 | dir = (struct ext2_dirent *)(block_buffer + offset); |
| 670 | direntname = (char*)(dir) + sizeof(struct ext2_dirent); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 671 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 672 | int direntlen = le16_to_cpu(dir->direntlen); |
| 673 | if (direntlen < sizeof(struct ext2_dirent)) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 674 | break; |
| 675 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 676 | if (dir->inode && (strlen(dirname) == dir->namelen) && |
| 677 | (strncmp(dirname, direntname, dir->namelen) == 0)) { |
| 678 | inodeno = le32_to_cpu(dir->inode); |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | offset += direntlen; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 683 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 684 | } while (offset < fs->blksz); |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 685 | |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 686 | if (inodeno > 0) { |
| 687 | free(block_buffer); |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 688 | return inodeno; |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 689 | } |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | fail: |
| 693 | free(block_buffer); |
| 694 | |
| 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | static int find_dir_depth(char *dirname) |
| 699 | { |
| 700 | char *token = strtok(dirname, "/"); |
| 701 | int count = 0; |
| 702 | while (token != NULL) { |
| 703 | token = strtok(NULL, "/"); |
| 704 | count++; |
| 705 | } |
| 706 | return count + 1 + 1; |
| 707 | /* |
| 708 | * for example for string /home/temp |
| 709 | * depth=home(1)+temp(1)+1 extra for NULL; |
| 710 | * so count is 4; |
| 711 | */ |
| 712 | } |
| 713 | |
| 714 | static int parse_path(char **arr, char *dirname) |
| 715 | { |
| 716 | char *token = strtok(dirname, "/"); |
| 717 | int i = 0; |
| 718 | |
| 719 | /* add root */ |
| 720 | arr[i] = zalloc(strlen("/") + 1); |
| 721 | if (!arr[i]) |
| 722 | return -ENOMEM; |
Stephen Warren | 5dd30f7 | 2015-09-04 22:03:44 -0600 | [diff] [blame] | 723 | memcpy(arr[i++], "/", strlen("/")); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 724 | |
| 725 | /* add each path entry after root */ |
| 726 | while (token != NULL) { |
| 727 | arr[i] = zalloc(strlen(token) + 1); |
| 728 | if (!arr[i]) |
| 729 | return -ENOMEM; |
| 730 | memcpy(arr[i++], token, strlen(token)); |
| 731 | token = strtok(NULL, "/"); |
| 732 | } |
| 733 | arr[i] = NULL; |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | int ext4fs_iget(int inode_no, struct ext2_inode *inode) |
| 739 | { |
| 740 | if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0) |
| 741 | return -1; |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Function: ext4fs_get_parent_inode_num |
| 748 | * Return Value: inode Number of the parent directory of file/Directory to be |
| 749 | * created |
| 750 | * dirname : Input parmater, input path name of the file/directory to be created |
| 751 | * dname : Output parameter, to be filled with the name of the directory |
| 752 | * extracted from dirname |
| 753 | */ |
| 754 | int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags) |
| 755 | { |
| 756 | int i; |
| 757 | int depth = 0; |
| 758 | int matched_inode_no; |
| 759 | int result_inode_no = -1; |
| 760 | char **ptr = NULL; |
| 761 | char *depth_dirname = NULL; |
| 762 | char *parse_dirname = NULL; |
| 763 | struct ext2_inode *parent_inode = NULL; |
| 764 | struct ext2_inode *first_inode = NULL; |
| 765 | struct ext2_inode temp_inode; |
| 766 | |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 767 | /* TODO: input validation make equivalent to linux */ |
| 768 | depth_dirname = zalloc(strlen(dirname) + 1); |
| 769 | if (!depth_dirname) |
| 770 | return -ENOMEM; |
| 771 | |
| 772 | memcpy(depth_dirname, dirname, strlen(dirname)); |
| 773 | depth = find_dir_depth(depth_dirname); |
| 774 | parse_dirname = zalloc(strlen(dirname) + 1); |
| 775 | if (!parse_dirname) |
| 776 | goto fail; |
| 777 | memcpy(parse_dirname, dirname, strlen(dirname)); |
| 778 | |
| 779 | /* allocate memory for each directory level */ |
| 780 | ptr = zalloc((depth) * sizeof(char *)); |
| 781 | if (!ptr) |
| 782 | goto fail; |
| 783 | if (parse_path(ptr, parse_dirname)) |
| 784 | goto fail; |
| 785 | parent_inode = zalloc(sizeof(struct ext2_inode)); |
| 786 | if (!parent_inode) |
| 787 | goto fail; |
| 788 | first_inode = zalloc(sizeof(struct ext2_inode)); |
| 789 | if (!first_inode) |
| 790 | goto fail; |
| 791 | memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode)); |
| 792 | memcpy(first_inode, parent_inode, sizeof(struct ext2_inode)); |
| 793 | if (flags & F_FILE) |
| 794 | result_inode_no = EXT2_ROOT_INO; |
| 795 | for (i = 1; i < depth; i++) { |
| 796 | matched_inode_no = search_dir(parent_inode, ptr[i]); |
| 797 | if (matched_inode_no == -1) { |
| 798 | if (ptr[i + 1] == NULL && i == 1) { |
| 799 | result_inode_no = EXT2_ROOT_INO; |
| 800 | goto end; |
| 801 | } else { |
| 802 | if (ptr[i + 1] == NULL) |
| 803 | break; |
| 804 | printf("Invalid path\n"); |
| 805 | result_inode_no = -1; |
| 806 | goto fail; |
| 807 | } |
| 808 | } else { |
| 809 | if (ptr[i + 1] != NULL) { |
| 810 | memset(parent_inode, '\0', |
| 811 | sizeof(struct ext2_inode)); |
| 812 | if (ext4fs_iget(matched_inode_no, |
| 813 | parent_inode)) { |
| 814 | result_inode_no = -1; |
| 815 | goto fail; |
| 816 | } |
| 817 | result_inode_no = matched_inode_no; |
| 818 | } else { |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | end: |
| 825 | if (i == 1) |
| 826 | matched_inode_no = search_dir(first_inode, ptr[i]); |
| 827 | else |
| 828 | matched_inode_no = search_dir(parent_inode, ptr[i]); |
| 829 | |
| 830 | if (matched_inode_no != -1) { |
| 831 | ext4fs_iget(matched_inode_no, &temp_inode); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 832 | if (le16_to_cpu(temp_inode.mode) & S_IFDIR) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 833 | printf("It is a Directory\n"); |
| 834 | result_inode_no = -1; |
| 835 | goto fail; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | if (strlen(ptr[i]) > 256) { |
| 840 | result_inode_no = -1; |
| 841 | goto fail; |
| 842 | } |
| 843 | memcpy(dname, ptr[i], strlen(ptr[i])); |
| 844 | |
| 845 | fail: |
| 846 | free(depth_dirname); |
Mikhail Ilin | 0528150 | 2022-11-22 11:00:55 +0300 | [diff] [blame] | 847 | if (parse_dirname) |
| 848 | free(parse_dirname); |
| 849 | if (ptr) { |
| 850 | for (i = 0; i < depth; i++) { |
| 851 | if (!ptr[i]) |
| 852 | break; |
| 853 | free(ptr[i]); |
| 854 | } |
| 855 | free(ptr); |
Stephen Warren | 5dd30f7 | 2015-09-04 22:03:44 -0600 | [diff] [blame] | 856 | } |
Mikhail Ilin | 0528150 | 2022-11-22 11:00:55 +0300 | [diff] [blame] | 857 | if (parent_inode) |
| 858 | free(parent_inode); |
| 859 | if (first_inode) |
| 860 | free(first_inode); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 861 | |
| 862 | return result_inode_no; |
| 863 | } |
| 864 | |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 865 | static int unlink_filename(char *filename, unsigned int blknr) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 866 | { |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 867 | int status; |
| 868 | int inodeno = 0; |
Stefan Brüns | 59283ff | 2016-10-09 20:15:26 +0200 | [diff] [blame] | 869 | int offset; |
| 870 | char *block_buffer = NULL; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 871 | struct ext2_dirent *dir = NULL; |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 872 | struct ext2_dirent *previous_dir; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 873 | struct ext_filesystem *fs = get_fs(); |
Stephen Warren | 991201e | 2015-09-04 22:03:45 -0600 | [diff] [blame] | 874 | int ret = -1; |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 875 | char *direntname; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 876 | |
Stefan Brüns | 59283ff | 2016-10-09 20:15:26 +0200 | [diff] [blame] | 877 | block_buffer = zalloc(fs->blksz); |
| 878 | if (!block_buffer) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 879 | return -ENOMEM; |
Stefan Brüns | 59283ff | 2016-10-09 20:15:26 +0200 | [diff] [blame] | 880 | |
| 881 | /* read the directory block */ |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 882 | status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, |
Stefan Brüns | 59283ff | 2016-10-09 20:15:26 +0200 | [diff] [blame] | 883 | fs->blksz, block_buffer); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 884 | if (status == 0) |
| 885 | goto fail; |
| 886 | |
Stefan Brüns | 59283ff | 2016-10-09 20:15:26 +0200 | [diff] [blame] | 887 | offset = 0; |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 888 | do { |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 889 | if (offset & 3) { |
| 890 | printf("Badly aligned ext2_dirent\n"); |
| 891 | break; |
| 892 | } |
| 893 | |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 894 | previous_dir = dir; |
| 895 | dir = (struct ext2_dirent *)(block_buffer + offset); |
| 896 | direntname = (char *)(dir) + sizeof(struct ext2_dirent); |
| 897 | |
| 898 | int direntlen = le16_to_cpu(dir->direntlen); |
| 899 | if (direntlen < sizeof(struct ext2_dirent)) |
| 900 | break; |
| 901 | |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 902 | if (dir->inode && (strlen(filename) == dir->namelen) && |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 903 | (strncmp(direntname, filename, dir->namelen) == 0)) { |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 904 | inodeno = le32_to_cpu(dir->inode); |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 905 | break; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 906 | } |
| 907 | |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 908 | offset += direntlen; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 909 | |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 910 | } while (offset < fs->blksz); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 911 | |
Stefan Brüns | 4c491fe | 2016-10-09 20:15:27 +0200 | [diff] [blame] | 912 | if (inodeno > 0) { |
Stefan Brüns | 7fbe42e | 2016-10-09 20:15:28 +0200 | [diff] [blame] | 913 | printf("file found, deleting\n"); |
| 914 | if (ext4fs_log_journal(block_buffer, blknr)) |
| 915 | goto fail; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 916 | |
Stefan Brüns | 7fbe42e | 2016-10-09 20:15:28 +0200 | [diff] [blame] | 917 | if (previous_dir) { |
| 918 | /* merge dir entry with predecessor */ |
| 919 | uint16_t new_len; |
| 920 | new_len = le16_to_cpu(previous_dir->direntlen); |
| 921 | new_len += le16_to_cpu(dir->direntlen); |
| 922 | previous_dir->direntlen = cpu_to_le16(new_len); |
| 923 | } else { |
| 924 | /* invalidate dir entry */ |
| 925 | dir->inode = 0; |
| 926 | } |
Stefan Brüns | 59283ff | 2016-10-09 20:15:26 +0200 | [diff] [blame] | 927 | if (ext4fs_put_metadata(block_buffer, blknr)) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 928 | goto fail; |
Stephen Warren | 991201e | 2015-09-04 22:03:45 -0600 | [diff] [blame] | 929 | ret = inodeno; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 930 | } |
| 931 | fail: |
Stefan Brüns | 59283ff | 2016-10-09 20:15:26 +0200 | [diff] [blame] | 932 | free(block_buffer); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 933 | |
Stephen Warren | 991201e | 2015-09-04 22:03:45 -0600 | [diff] [blame] | 934 | return ret; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 935 | } |
| 936 | |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 937 | int ext4fs_filename_unlink(char *filename) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 938 | { |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 939 | int blk_idx; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 940 | long int blknr = -1; |
| 941 | int inodeno = -1; |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 942 | uint32_t directory_blocks; |
| 943 | |
| 944 | directory_blocks = le32_to_cpu(g_parent_inode->size) >> |
| 945 | LOG2_BLOCK_SIZE(ext4fs_root); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 946 | |
| 947 | /* read the block no allocated to a file */ |
Stefan Brüns | 9139715 | 2016-09-06 04:36:46 +0200 | [diff] [blame] | 948 | for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) { |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 949 | blknr = read_allocated_block(g_parent_inode, blk_idx, NULL); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 950 | if (blknr <= 0) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 951 | break; |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 952 | inodeno = unlink_filename(filename, blknr); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 953 | if (inodeno != -1) |
| 954 | return inodeno; |
| 955 | } |
| 956 | |
| 957 | return -1; |
| 958 | } |
| 959 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 960 | uint32_t ext4fs_get_new_blk_no(void) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 961 | { |
| 962 | short i; |
| 963 | short status; |
| 964 | int remainder; |
| 965 | unsigned int bg_idx; |
| 966 | static int prev_bg_bitmap_index = -1; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 967 | unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 968 | struct ext_filesystem *fs = get_fs(); |
| 969 | char *journal_buffer = zalloc(fs->blksz); |
| 970 | char *zero_buffer = zalloc(fs->blksz); |
| 971 | if (!journal_buffer || !zero_buffer) |
| 972 | goto fail; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 973 | |
| 974 | if (fs->first_pass_bbmap == 0) { |
| 975 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 976 | struct ext2_block_group *bgd = NULL; |
| 977 | bgd = ext4fs_get_group_descriptor(fs, i); |
| 978 | if (ext4fs_bg_get_free_blocks(bgd, fs)) { |
| 979 | uint16_t bg_flags = ext4fs_bg_get_flags(bgd); |
| 980 | uint64_t b_bitmap_blk = |
| 981 | ext4fs_bg_get_block_id(bgd, fs); |
| 982 | if (bg_flags & EXT4_BG_BLOCK_UNINIT) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 983 | memcpy(fs->blk_bmaps[i], zero_buffer, |
| 984 | fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 985 | put_ext4(b_bitmap_blk * fs->blksz, |
| 986 | fs->blk_bmaps[i], fs->blksz); |
| 987 | bg_flags &= ~EXT4_BG_BLOCK_UNINIT; |
| 988 | ext4fs_bg_set_flags(bgd, bg_flags); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 989 | } |
| 990 | fs->curr_blkno = |
| 991 | _get_new_blk_no(fs->blk_bmaps[i]); |
| 992 | if (fs->curr_blkno == -1) |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 993 | /* block bitmap is completely filled */ |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 994 | continue; |
| 995 | fs->curr_blkno = fs->curr_blkno + |
| 996 | (i * fs->blksz * 8); |
| 997 | fs->first_pass_bbmap++; |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 998 | ext4fs_bg_free_blocks_dec(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 999 | ext4fs_sb_free_blocks_dec(fs->sb); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1000 | status = ext4fs_devread(b_bitmap_blk * |
| 1001 | fs->sect_perblk, |
| 1002 | 0, fs->blksz, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1003 | journal_buffer); |
| 1004 | if (status == 0) |
| 1005 | goto fail; |
| 1006 | if (ext4fs_log_journal(journal_buffer, |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1007 | b_bitmap_blk)) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1008 | goto fail; |
| 1009 | goto success; |
| 1010 | } else { |
| 1011 | debug("no space left on block group %d\n", i); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | goto fail; |
| 1016 | } else { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1017 | fs->curr_blkno++; |
Stefan Brüns | 693f791 | 2016-09-06 04:36:49 +0200 | [diff] [blame] | 1018 | restart: |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1019 | /* get the blockbitmap index respective to blockno */ |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 1020 | bg_idx = fs->curr_blkno / blk_per_grp; |
| 1021 | if (fs->blksz == 1024) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1022 | remainder = fs->curr_blkno % blk_per_grp; |
| 1023 | if (!remainder) |
| 1024 | bg_idx--; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * To skip completely filled block group bitmaps |
| 1029 | * Optimize the block allocation |
| 1030 | */ |
| 1031 | if (bg_idx >= fs->no_blkgrp) |
| 1032 | goto fail; |
| 1033 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1034 | struct ext2_block_group *bgd = NULL; |
| 1035 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
| 1036 | if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1037 | debug("block group %u is full. Skipping\n", bg_idx); |
Stefan Brüns | 693f791 | 2016-09-06 04:36:49 +0200 | [diff] [blame] | 1038 | fs->curr_blkno = (bg_idx + 1) * blk_per_grp; |
| 1039 | if (fs->blksz == 1024) |
| 1040 | fs->curr_blkno += 1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1041 | goto restart; |
| 1042 | } |
| 1043 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1044 | uint16_t bg_flags = ext4fs_bg_get_flags(bgd); |
| 1045 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
| 1046 | if (bg_flags & EXT4_BG_BLOCK_UNINIT) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1047 | memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1048 | put_ext4(b_bitmap_blk * fs->blksz, |
| 1049 | zero_buffer, fs->blksz); |
| 1050 | bg_flags &= ~EXT4_BG_BLOCK_UNINIT; |
| 1051 | ext4fs_bg_set_flags(bgd, bg_flags); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx], |
| 1055 | bg_idx) != 0) { |
| 1056 | debug("going for restart for the block no %ld %u\n", |
| 1057 | fs->curr_blkno, bg_idx); |
Stefan Brüns | 693f791 | 2016-09-06 04:36:49 +0200 | [diff] [blame] | 1058 | fs->curr_blkno++; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1059 | goto restart; |
| 1060 | } |
| 1061 | |
| 1062 | /* journal backup */ |
| 1063 | if (prev_bg_bitmap_index != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1064 | status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1065 | 0, fs->blksz, journal_buffer); |
| 1066 | if (status == 0) |
| 1067 | goto fail; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1068 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1069 | goto fail; |
| 1070 | |
| 1071 | prev_bg_bitmap_index = bg_idx; |
| 1072 | } |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 1073 | ext4fs_bg_free_blocks_dec(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1074 | ext4fs_sb_free_blocks_dec(fs->sb); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1075 | goto success; |
| 1076 | } |
| 1077 | success: |
| 1078 | free(journal_buffer); |
| 1079 | free(zero_buffer); |
| 1080 | |
| 1081 | return fs->curr_blkno; |
| 1082 | fail: |
| 1083 | free(journal_buffer); |
| 1084 | free(zero_buffer); |
| 1085 | |
| 1086 | return -1; |
| 1087 | } |
| 1088 | |
| 1089 | int ext4fs_get_new_inode_no(void) |
| 1090 | { |
| 1091 | short i; |
| 1092 | short status; |
| 1093 | unsigned int ibmap_idx; |
| 1094 | static int prev_inode_bitmap_index = -1; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1095 | unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1096 | struct ext_filesystem *fs = get_fs(); |
| 1097 | char *journal_buffer = zalloc(fs->blksz); |
| 1098 | char *zero_buffer = zalloc(fs->blksz); |
| 1099 | if (!journal_buffer || !zero_buffer) |
| 1100 | goto fail; |
Stefan Brüns | b7cdac0 | 2016-09-06 04:36:47 +0200 | [diff] [blame] | 1101 | int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) & |
| 1102 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1103 | |
| 1104 | if (fs->first_pass_ibmap == 0) { |
| 1105 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1106 | uint32_t free_inodes; |
| 1107 | struct ext2_block_group *bgd = NULL; |
| 1108 | bgd = ext4fs_get_group_descriptor(fs, i); |
| 1109 | free_inodes = ext4fs_bg_get_free_inodes(bgd, fs); |
| 1110 | if (free_inodes) { |
| 1111 | uint16_t bg_flags = ext4fs_bg_get_flags(bgd); |
| 1112 | uint64_t i_bitmap_blk = |
| 1113 | ext4fs_bg_get_inode_id(bgd, fs); |
Stefan Brüns | b7cdac0 | 2016-09-06 04:36:47 +0200 | [diff] [blame] | 1114 | if (has_gdt_chksum) |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1115 | bgd->bg_itable_unused = free_inodes; |
| 1116 | if (bg_flags & EXT4_BG_INODE_UNINIT) { |
| 1117 | put_ext4(i_bitmap_blk * fs->blksz, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1118 | zero_buffer, fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1119 | bg_flags &= ~EXT4_BG_INODE_UNINIT; |
| 1120 | ext4fs_bg_set_flags(bgd, bg_flags); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1121 | memcpy(fs->inode_bmaps[i], |
| 1122 | zero_buffer, fs->blksz); |
| 1123 | } |
| 1124 | fs->curr_inode_no = |
| 1125 | _get_new_inode_no(fs->inode_bmaps[i]); |
| 1126 | if (fs->curr_inode_no == -1) |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1127 | /* inode bitmap is completely filled */ |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1128 | continue; |
| 1129 | fs->curr_inode_no = fs->curr_inode_no + |
| 1130 | (i * inodes_per_grp); |
| 1131 | fs->first_pass_ibmap++; |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 1132 | ext4fs_bg_free_inodes_dec(bgd, fs); |
Stefan Brüns | b7cdac0 | 2016-09-06 04:36:47 +0200 | [diff] [blame] | 1133 | if (has_gdt_chksum) |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 1134 | ext4fs_bg_itable_unused_dec(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1135 | ext4fs_sb_free_inodes_dec(fs->sb); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1136 | status = ext4fs_devread(i_bitmap_blk * |
| 1137 | fs->sect_perblk, |
| 1138 | 0, fs->blksz, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1139 | journal_buffer); |
| 1140 | if (status == 0) |
| 1141 | goto fail; |
| 1142 | if (ext4fs_log_journal(journal_buffer, |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1143 | i_bitmap_blk)) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1144 | goto fail; |
| 1145 | goto success; |
| 1146 | } else |
| 1147 | debug("no inode left on block group %d\n", i); |
| 1148 | } |
| 1149 | goto fail; |
| 1150 | } else { |
| 1151 | restart: |
| 1152 | fs->curr_inode_no++; |
| 1153 | /* get the blockbitmap index respective to blockno */ |
| 1154 | ibmap_idx = fs->curr_inode_no / inodes_per_grp; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1155 | struct ext2_block_group *bgd = |
| 1156 | ext4fs_get_group_descriptor(fs, ibmap_idx); |
| 1157 | uint16_t bg_flags = ext4fs_bg_get_flags(bgd); |
| 1158 | uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs); |
| 1159 | |
| 1160 | if (bg_flags & EXT4_BG_INODE_UNINIT) { |
| 1161 | put_ext4(i_bitmap_blk * fs->blksz, |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1162 | zero_buffer, fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1163 | bg_flags &= ~EXT4_BG_INODE_UNINIT; |
| 1164 | ext4fs_bg_set_flags(bgd, bg_flags); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1165 | memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer, |
| 1166 | fs->blksz); |
| 1167 | } |
| 1168 | |
| 1169 | if (ext4fs_set_inode_bmap(fs->curr_inode_no, |
| 1170 | fs->inode_bmaps[ibmap_idx], |
| 1171 | ibmap_idx) != 0) { |
| 1172 | debug("going for restart for the block no %d %u\n", |
| 1173 | fs->curr_inode_no, ibmap_idx); |
| 1174 | goto restart; |
| 1175 | } |
| 1176 | |
| 1177 | /* journal backup */ |
| 1178 | if (prev_inode_bitmap_index != ibmap_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1179 | status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1180 | 0, fs->blksz, journal_buffer); |
| 1181 | if (status == 0) |
| 1182 | goto fail; |
| 1183 | if (ext4fs_log_journal(journal_buffer, |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1184 | le32_to_cpu(bgd->inode_id))) |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1185 | goto fail; |
| 1186 | prev_inode_bitmap_index = ibmap_idx; |
| 1187 | } |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 1188 | ext4fs_bg_free_inodes_dec(bgd, fs); |
Stefan Brüns | b7cdac0 | 2016-09-06 04:36:47 +0200 | [diff] [blame] | 1189 | if (has_gdt_chksum) |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1190 | bgd->bg_itable_unused = bgd->free_inodes; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1191 | ext4fs_sb_free_inodes_dec(fs->sb); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1192 | goto success; |
| 1193 | } |
| 1194 | |
| 1195 | success: |
| 1196 | free(journal_buffer); |
| 1197 | free(zero_buffer); |
| 1198 | |
| 1199 | return fs->curr_inode_no; |
| 1200 | fail: |
| 1201 | free(journal_buffer); |
| 1202 | free(zero_buffer); |
| 1203 | |
| 1204 | return -1; |
| 1205 | |
| 1206 | } |
| 1207 | |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1208 | static void alloc_single_indirect_block(struct ext2_inode *file_inode, |
| 1209 | unsigned int *total_remaining_blocks, |
| 1210 | unsigned int *no_blks_reqd) |
| 1211 | { |
| 1212 | short i; |
| 1213 | short status; |
| 1214 | long int actual_block_no; |
| 1215 | long int si_blockno; |
| 1216 | /* si :single indirect */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1217 | __le32 *si_buffer = NULL; |
| 1218 | __le32 *si_start_addr = NULL; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1219 | struct ext_filesystem *fs = get_fs(); |
| 1220 | |
| 1221 | if (*total_remaining_blocks != 0) { |
| 1222 | si_buffer = zalloc(fs->blksz); |
| 1223 | if (!si_buffer) { |
| 1224 | printf("No Memory\n"); |
| 1225 | return; |
| 1226 | } |
| 1227 | si_start_addr = si_buffer; |
| 1228 | si_blockno = ext4fs_get_new_blk_no(); |
| 1229 | if (si_blockno == -1) { |
| 1230 | printf("no block left to assign\n"); |
| 1231 | goto fail; |
| 1232 | } |
| 1233 | (*no_blks_reqd)++; |
| 1234 | debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks); |
| 1235 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1236 | status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk, |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1237 | 0, fs->blksz, (char *)si_buffer); |
| 1238 | memset(si_buffer, '\0', fs->blksz); |
| 1239 | if (status == 0) |
| 1240 | goto fail; |
| 1241 | |
| 1242 | for (i = 0; i < (fs->blksz / sizeof(int)); i++) { |
| 1243 | actual_block_no = ext4fs_get_new_blk_no(); |
| 1244 | if (actual_block_no == -1) { |
| 1245 | printf("no block left to assign\n"); |
| 1246 | goto fail; |
| 1247 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1248 | *si_buffer = cpu_to_le32(actual_block_no); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1249 | debug("SIAB %u: %u\n", *si_buffer, |
| 1250 | *total_remaining_blocks); |
| 1251 | |
| 1252 | si_buffer++; |
| 1253 | (*total_remaining_blocks)--; |
| 1254 | if (*total_remaining_blocks == 0) |
| 1255 | break; |
| 1256 | } |
| 1257 | |
| 1258 | /* write the block to disk */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 1259 | put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)), |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1260 | si_start_addr, fs->blksz); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1261 | file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1262 | } |
| 1263 | fail: |
| 1264 | free(si_start_addr); |
| 1265 | } |
| 1266 | |
| 1267 | static void alloc_double_indirect_block(struct ext2_inode *file_inode, |
| 1268 | unsigned int *total_remaining_blocks, |
| 1269 | unsigned int *no_blks_reqd) |
| 1270 | { |
| 1271 | short i; |
| 1272 | short j; |
| 1273 | short status; |
| 1274 | long int actual_block_no; |
| 1275 | /* di:double indirect */ |
| 1276 | long int di_blockno_parent; |
| 1277 | long int di_blockno_child; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1278 | __le32 *di_parent_buffer = NULL; |
| 1279 | __le32 *di_child_buff = NULL; |
| 1280 | __le32 *di_block_start_addr = NULL; |
| 1281 | __le32 *di_child_buff_start = NULL; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1282 | struct ext_filesystem *fs = get_fs(); |
| 1283 | |
| 1284 | if (*total_remaining_blocks != 0) { |
| 1285 | /* double indirect parent block connecting to inode */ |
| 1286 | di_blockno_parent = ext4fs_get_new_blk_no(); |
| 1287 | if (di_blockno_parent == -1) { |
| 1288 | printf("no block left to assign\n"); |
| 1289 | goto fail; |
| 1290 | } |
| 1291 | di_parent_buffer = zalloc(fs->blksz); |
| 1292 | if (!di_parent_buffer) |
| 1293 | goto fail; |
| 1294 | |
| 1295 | di_block_start_addr = di_parent_buffer; |
| 1296 | (*no_blks_reqd)++; |
| 1297 | debug("DIPB %ld: %u\n", di_blockno_parent, |
| 1298 | *total_remaining_blocks); |
| 1299 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1300 | status = ext4fs_devread((lbaint_t)di_blockno_parent * |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1301 | fs->sect_perblk, 0, |
| 1302 | fs->blksz, (char *)di_parent_buffer); |
Łukasz Majewski | b55a793 | 2012-12-05 08:06:39 +0000 | [diff] [blame] | 1303 | |
| 1304 | if (!status) { |
| 1305 | printf("%s: Device read error!\n", __func__); |
| 1306 | goto fail; |
| 1307 | } |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1308 | memset(di_parent_buffer, '\0', fs->blksz); |
| 1309 | |
| 1310 | /* |
| 1311 | * start:for each double indirect parent |
| 1312 | * block create one more block |
| 1313 | */ |
| 1314 | for (i = 0; i < (fs->blksz / sizeof(int)); i++) { |
| 1315 | di_blockno_child = ext4fs_get_new_blk_no(); |
| 1316 | if (di_blockno_child == -1) { |
| 1317 | printf("no block left to assign\n"); |
| 1318 | goto fail; |
| 1319 | } |
| 1320 | di_child_buff = zalloc(fs->blksz); |
| 1321 | if (!di_child_buff) |
| 1322 | goto fail; |
| 1323 | |
| 1324 | di_child_buff_start = di_child_buff; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1325 | *di_parent_buffer = cpu_to_le32(di_blockno_child); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1326 | di_parent_buffer++; |
| 1327 | (*no_blks_reqd)++; |
| 1328 | debug("DICB %ld: %u\n", di_blockno_child, |
| 1329 | *total_remaining_blocks); |
| 1330 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1331 | status = ext4fs_devread((lbaint_t)di_blockno_child * |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1332 | fs->sect_perblk, 0, |
| 1333 | fs->blksz, |
| 1334 | (char *)di_child_buff); |
Łukasz Majewski | b55a793 | 2012-12-05 08:06:39 +0000 | [diff] [blame] | 1335 | |
| 1336 | if (!status) { |
| 1337 | printf("%s: Device read error!\n", __func__); |
| 1338 | goto fail; |
| 1339 | } |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1340 | memset(di_child_buff, '\0', fs->blksz); |
| 1341 | /* filling of actual datablocks for each child */ |
| 1342 | for (j = 0; j < (fs->blksz / sizeof(int)); j++) { |
| 1343 | actual_block_no = ext4fs_get_new_blk_no(); |
| 1344 | if (actual_block_no == -1) { |
| 1345 | printf("no block left to assign\n"); |
| 1346 | goto fail; |
| 1347 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1348 | *di_child_buff = cpu_to_le32(actual_block_no); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1349 | debug("DIAB %ld: %u\n", actual_block_no, |
| 1350 | *total_remaining_blocks); |
| 1351 | |
| 1352 | di_child_buff++; |
| 1353 | (*total_remaining_blocks)--; |
| 1354 | if (*total_remaining_blocks == 0) |
| 1355 | break; |
| 1356 | } |
| 1357 | /* write the block table */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 1358 | put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)), |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1359 | di_child_buff_start, fs->blksz); |
| 1360 | free(di_child_buff_start); |
| 1361 | di_child_buff_start = NULL; |
| 1362 | |
| 1363 | if (*total_remaining_blocks == 0) |
| 1364 | break; |
| 1365 | } |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 1366 | put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)), |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1367 | di_block_start_addr, fs->blksz); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1368 | file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1369 | } |
| 1370 | fail: |
| 1371 | free(di_block_start_addr); |
| 1372 | } |
| 1373 | |
| 1374 | static void alloc_triple_indirect_block(struct ext2_inode *file_inode, |
| 1375 | unsigned int *total_remaining_blocks, |
| 1376 | unsigned int *no_blks_reqd) |
| 1377 | { |
| 1378 | short i; |
| 1379 | short j; |
| 1380 | short k; |
| 1381 | long int actual_block_no; |
| 1382 | /* ti: Triple Indirect */ |
| 1383 | long int ti_gp_blockno; |
| 1384 | long int ti_parent_blockno; |
| 1385 | long int ti_child_blockno; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1386 | __le32 *ti_gp_buff = NULL; |
| 1387 | __le32 *ti_parent_buff = NULL; |
| 1388 | __le32 *ti_child_buff = NULL; |
| 1389 | __le32 *ti_gp_buff_start_addr = NULL; |
| 1390 | __le32 *ti_pbuff_start_addr = NULL; |
| 1391 | __le32 *ti_cbuff_start_addr = NULL; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1392 | struct ext_filesystem *fs = get_fs(); |
| 1393 | if (*total_remaining_blocks != 0) { |
| 1394 | /* triple indirect grand parent block connecting to inode */ |
| 1395 | ti_gp_blockno = ext4fs_get_new_blk_no(); |
| 1396 | if (ti_gp_blockno == -1) { |
| 1397 | printf("no block left to assign\n"); |
Tom Rini | 9166c3c | 2015-12-10 16:42:21 -0500 | [diff] [blame] | 1398 | return; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1399 | } |
| 1400 | ti_gp_buff = zalloc(fs->blksz); |
| 1401 | if (!ti_gp_buff) |
Tom Rini | 9166c3c | 2015-12-10 16:42:21 -0500 | [diff] [blame] | 1402 | return; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1403 | |
| 1404 | ti_gp_buff_start_addr = ti_gp_buff; |
| 1405 | (*no_blks_reqd)++; |
| 1406 | debug("TIGPB %ld: %u\n", ti_gp_blockno, |
| 1407 | *total_remaining_blocks); |
| 1408 | |
| 1409 | /* for each 4 byte grand parent entry create one more block */ |
| 1410 | for (i = 0; i < (fs->blksz / sizeof(int)); i++) { |
| 1411 | ti_parent_blockno = ext4fs_get_new_blk_no(); |
| 1412 | if (ti_parent_blockno == -1) { |
| 1413 | printf("no block left to assign\n"); |
| 1414 | goto fail; |
| 1415 | } |
| 1416 | ti_parent_buff = zalloc(fs->blksz); |
| 1417 | if (!ti_parent_buff) |
| 1418 | goto fail; |
| 1419 | |
| 1420 | ti_pbuff_start_addr = ti_parent_buff; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1421 | *ti_gp_buff = cpu_to_le32(ti_parent_blockno); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1422 | ti_gp_buff++; |
| 1423 | (*no_blks_reqd)++; |
| 1424 | debug("TIPB %ld: %u\n", ti_parent_blockno, |
| 1425 | *total_remaining_blocks); |
| 1426 | |
| 1427 | /* for each 4 byte entry parent create one more block */ |
| 1428 | for (j = 0; j < (fs->blksz / sizeof(int)); j++) { |
| 1429 | ti_child_blockno = ext4fs_get_new_blk_no(); |
| 1430 | if (ti_child_blockno == -1) { |
| 1431 | printf("no block left assign\n"); |
Tom Rini | 9166c3c | 2015-12-10 16:42:21 -0500 | [diff] [blame] | 1432 | goto fail1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1433 | } |
| 1434 | ti_child_buff = zalloc(fs->blksz); |
| 1435 | if (!ti_child_buff) |
Tom Rini | 9166c3c | 2015-12-10 16:42:21 -0500 | [diff] [blame] | 1436 | goto fail1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1437 | |
| 1438 | ti_cbuff_start_addr = ti_child_buff; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1439 | *ti_parent_buff = cpu_to_le32(ti_child_blockno); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1440 | ti_parent_buff++; |
| 1441 | (*no_blks_reqd)++; |
| 1442 | debug("TICB %ld: %u\n", ti_parent_blockno, |
| 1443 | *total_remaining_blocks); |
| 1444 | |
| 1445 | /* fill actual datablocks for each child */ |
| 1446 | for (k = 0; k < (fs->blksz / sizeof(int)); |
| 1447 | k++) { |
| 1448 | actual_block_no = |
| 1449 | ext4fs_get_new_blk_no(); |
| 1450 | if (actual_block_no == -1) { |
| 1451 | printf("no block left\n"); |
Tom Rini | 9166c3c | 2015-12-10 16:42:21 -0500 | [diff] [blame] | 1452 | free(ti_cbuff_start_addr); |
| 1453 | goto fail1; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1454 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1455 | *ti_child_buff = cpu_to_le32(actual_block_no); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1456 | debug("TIAB %ld: %u\n", actual_block_no, |
| 1457 | *total_remaining_blocks); |
| 1458 | |
| 1459 | ti_child_buff++; |
| 1460 | (*total_remaining_blocks)--; |
| 1461 | if (*total_remaining_blocks == 0) |
| 1462 | break; |
| 1463 | } |
| 1464 | /* write the child block */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 1465 | put_ext4(((uint64_t) ((uint64_t)ti_child_blockno * |
| 1466 | (uint64_t)fs->blksz)), |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1467 | ti_cbuff_start_addr, fs->blksz); |
| 1468 | free(ti_cbuff_start_addr); |
| 1469 | |
| 1470 | if (*total_remaining_blocks == 0) |
| 1471 | break; |
| 1472 | } |
| 1473 | /* write the parent block */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 1474 | put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)), |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1475 | ti_pbuff_start_addr, fs->blksz); |
| 1476 | free(ti_pbuff_start_addr); |
| 1477 | |
| 1478 | if (*total_remaining_blocks == 0) |
| 1479 | break; |
| 1480 | } |
| 1481 | /* write the grand parent block */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 1482 | put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)), |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1483 | ti_gp_buff_start_addr, fs->blksz); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1484 | file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno); |
Tom Rini | 9166c3c | 2015-12-10 16:42:21 -0500 | [diff] [blame] | 1485 | free(ti_gp_buff_start_addr); |
| 1486 | return; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1487 | } |
Tom Rini | 9166c3c | 2015-12-10 16:42:21 -0500 | [diff] [blame] | 1488 | fail1: |
| 1489 | free(ti_pbuff_start_addr); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1490 | fail: |
| 1491 | free(ti_gp_buff_start_addr); |
| 1492 | } |
| 1493 | |
| 1494 | void ext4fs_allocate_blocks(struct ext2_inode *file_inode, |
| 1495 | unsigned int total_remaining_blocks, |
| 1496 | unsigned int *total_no_of_block) |
| 1497 | { |
| 1498 | short i; |
| 1499 | long int direct_blockno; |
| 1500 | unsigned int no_blks_reqd = 0; |
| 1501 | |
| 1502 | /* allocation of direct blocks */ |
Stephen Warren | e62f1ba | 2014-06-11 12:46:16 -0600 | [diff] [blame] | 1503 | for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) { |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1504 | direct_blockno = ext4fs_get_new_blk_no(); |
| 1505 | if (direct_blockno == -1) { |
| 1506 | printf("no block left to assign\n"); |
| 1507 | return; |
| 1508 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1509 | file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno); |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1510 | debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks); |
| 1511 | |
| 1512 | total_remaining_blocks--; |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | alloc_single_indirect_block(file_inode, &total_remaining_blocks, |
| 1516 | &no_blks_reqd); |
| 1517 | alloc_double_indirect_block(file_inode, &total_remaining_blocks, |
| 1518 | &no_blks_reqd); |
| 1519 | alloc_triple_indirect_block(file_inode, &total_remaining_blocks, |
| 1520 | &no_blks_reqd); |
| 1521 | *total_no_of_block += no_blks_reqd; |
| 1522 | } |
| 1523 | |
| 1524 | #endif |
| 1525 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1526 | static struct ext4_extent_header *ext4fs_get_extent_block |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1527 | (struct ext2_data *data, struct ext_block_cache *cache, |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1528 | struct ext4_extent_header *ext_block, |
| 1529 | uint32_t fileblock, int log2_blksz) |
| 1530 | { |
| 1531 | struct ext4_extent_idx *index; |
| 1532 | unsigned long long block; |
Ionut Nicu | 48b2b59 | 2014-01-13 11:59:24 +0100 | [diff] [blame] | 1533 | int blksz = EXT2_BLOCK_SIZE(data); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1534 | int i; |
| 1535 | |
| 1536 | while (1) { |
| 1537 | index = (struct ext4_extent_idx *)(ext_block + 1); |
| 1538 | |
Rommel Custodio | 5a4f74d | 2013-07-21 10:53:25 +0200 | [diff] [blame] | 1539 | if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1540 | return NULL; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1541 | |
| 1542 | if (ext_block->eh_depth == 0) |
| 1543 | return ext_block; |
| 1544 | i = -1; |
| 1545 | do { |
| 1546 | i++; |
Rommel Custodio | 5a4f74d | 2013-07-21 10:53:25 +0200 | [diff] [blame] | 1547 | if (i >= le16_to_cpu(ext_block->eh_entries)) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1548 | break; |
Ionut Nicu | 01f606c | 2014-01-13 12:00:08 +0100 | [diff] [blame] | 1549 | } while (fileblock >= le32_to_cpu(index[i].ei_block)); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1550 | |
Gero Schumacher | 785dfea | 2019-02-26 15:45:22 +0000 | [diff] [blame] | 1551 | /* |
| 1552 | * If first logical block number is higher than requested fileblock, |
| 1553 | * it is a sparse file. This is handled on upper layer. |
| 1554 | */ |
| 1555 | if (i > 0) |
| 1556 | i--; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1557 | |
Rommel Custodio | 5a4f74d | 2013-07-21 10:53:25 +0200 | [diff] [blame] | 1558 | block = le16_to_cpu(index[i].ei_leaf_hi); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1559 | block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1560 | block <<= log2_blksz; |
| 1561 | if (!ext_cache_read(cache, (lbaint_t)block, blksz)) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1562 | return NULL; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1563 | ext_block = (struct ext4_extent_header *)cache->buf; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | static int ext4fs_blockgroup |
| 1568 | (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) |
| 1569 | { |
| 1570 | long int blkno; |
| 1571 | unsigned int blkoff, desc_per_blk; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 1572 | int log2blksz = get_fs()->dev_desc->log2blksz; |
Stefan Brüns | 8f48fd5 | 2016-09-17 02:10:09 +0200 | [diff] [blame] | 1573 | int desc_size = get_fs()->gdsize; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1574 | |
Paul Emge | 16b19bb | 2019-07-08 16:37:06 -0700 | [diff] [blame] | 1575 | if (desc_size == 0) |
| 1576 | return 0; |
Stefan Brüns | 8f48fd5 | 2016-09-17 02:10:09 +0200 | [diff] [blame] | 1577 | desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1578 | |
Paul Emge | 16b19bb | 2019-07-08 16:37:06 -0700 | [diff] [blame] | 1579 | if (desc_per_blk == 0) |
| 1580 | return 0; |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1581 | blkno = le32_to_cpu(data->sblock.first_data_block) + 1 + |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1582 | group / desc_per_blk; |
Stefan Brüns | 8f48fd5 | 2016-09-17 02:10:09 +0200 | [diff] [blame] | 1583 | blkoff = (group % desc_per_blk) * desc_size; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1584 | |
| 1585 | debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n", |
| 1586 | group, blkno, blkoff); |
| 1587 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1588 | return ext4fs_devread((lbaint_t)blkno << |
| 1589 | (LOG2_BLOCK_SIZE(data) - log2blksz), |
Stefan Brüns | 8f48fd5 | 2016-09-17 02:10:09 +0200 | [diff] [blame] | 1590 | blkoff, desc_size, (char *)blkgrp); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode) |
| 1594 | { |
Benjamin Lim | 003ab30 | 2019-03-29 07:29:45 -0400 | [diff] [blame] | 1595 | struct ext2_block_group *blkgrp; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1596 | struct ext2_sblock *sblock = &data->sblock; |
| 1597 | struct ext_filesystem *fs = get_fs(); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 1598 | int log2blksz = get_fs()->dev_desc->log2blksz; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1599 | int inodes_per_block, status; |
| 1600 | long int blkno; |
| 1601 | unsigned int blkoff; |
| 1602 | |
Benjamin Lim | 003ab30 | 2019-03-29 07:29:45 -0400 | [diff] [blame] | 1603 | /* Allocate blkgrp based on gdsize (for 64-bit support). */ |
| 1604 | blkgrp = zalloc(get_fs()->gdsize); |
| 1605 | if (!blkgrp) |
| 1606 | return 0; |
| 1607 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1608 | /* It is easier to calculate if the first inode is 0. */ |
| 1609 | ino--; |
Paul Emge | 16b19bb | 2019-07-08 16:37:06 -0700 | [diff] [blame] | 1610 | if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) { |
| 1611 | free(blkgrp); |
| 1612 | return 0; |
| 1613 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1614 | status = ext4fs_blockgroup(data, ino / le32_to_cpu |
Benjamin Lim | 003ab30 | 2019-03-29 07:29:45 -0400 | [diff] [blame] | 1615 | (sblock->inodes_per_group), blkgrp); |
| 1616 | if (status == 0) { |
| 1617 | free(blkgrp); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1618 | return 0; |
Benjamin Lim | 003ab30 | 2019-03-29 07:29:45 -0400 | [diff] [blame] | 1619 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1620 | |
| 1621 | inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz; |
Paul Emge | 16b19bb | 2019-07-08 16:37:06 -0700 | [diff] [blame] | 1622 | if ( inodes_per_block == 0 ) { |
| 1623 | free(blkgrp); |
| 1624 | return 0; |
| 1625 | } |
Benjamin Lim | 003ab30 | 2019-03-29 07:29:45 -0400 | [diff] [blame] | 1626 | blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) + |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1627 | (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1628 | blkoff = (ino % inodes_per_block) * fs->inodesz; |
Benjamin Lim | 003ab30 | 2019-03-29 07:29:45 -0400 | [diff] [blame] | 1629 | |
| 1630 | /* Free blkgrp as it is no longer required. */ |
| 1631 | free(blkgrp); |
| 1632 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1633 | /* Read the inode. */ |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1634 | status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) - |
| 1635 | log2blksz), blkoff, |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1636 | sizeof(struct ext2_inode), (char *)inode); |
| 1637 | if (status == 0) |
| 1638 | return 0; |
| 1639 | |
| 1640 | return 1; |
| 1641 | } |
| 1642 | |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1643 | long int read_allocated_block(struct ext2_inode *inode, int fileblock, |
| 1644 | struct ext_block_cache *cache) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1645 | { |
| 1646 | long int blknr; |
| 1647 | int blksz; |
| 1648 | int log2_blksz; |
| 1649 | int status; |
| 1650 | long int rblock; |
| 1651 | long int perblock_parent; |
| 1652 | long int perblock_child; |
| 1653 | unsigned long long start; |
| 1654 | /* get the blocksize of the filesystem */ |
| 1655 | blksz = EXT2_BLOCK_SIZE(ext4fs_root); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 1656 | log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root) |
| 1657 | - get_fs()->dev_desc->log2blksz; |
| 1658 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1659 | if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) { |
Stefan Brüns | 551fb6c | 2016-11-05 22:17:14 +0100 | [diff] [blame] | 1660 | long int startblock, endblock; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1661 | struct ext_block_cache *c, cd; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1662 | struct ext4_extent_header *ext_block; |
| 1663 | struct ext4_extent *extent; |
Stefan Brüns | 551fb6c | 2016-11-05 22:17:14 +0100 | [diff] [blame] | 1664 | int i; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1665 | |
| 1666 | if (cache) { |
| 1667 | c = cache; |
| 1668 | } else { |
| 1669 | c = &cd; |
| 1670 | ext_cache_init(c); |
| 1671 | } |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 1672 | ext_block = |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1673 | ext4fs_get_extent_block(ext4fs_root, c, |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 1674 | (struct ext4_extent_header *) |
| 1675 | inode->b.blocks.dir_blocks, |
| 1676 | fileblock, log2_blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1677 | if (!ext_block) { |
| 1678 | printf("invalid extent block\n"); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1679 | if (!cache) |
| 1680 | ext_cache_fini(c); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1681 | return -EINVAL; |
| 1682 | } |
| 1683 | |
| 1684 | extent = (struct ext4_extent *)(ext_block + 1); |
| 1685 | |
Stefan Brüns | 551fb6c | 2016-11-05 22:17:14 +0100 | [diff] [blame] | 1686 | for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) { |
| 1687 | startblock = le32_to_cpu(extent[i].ee_block); |
| 1688 | endblock = startblock + le16_to_cpu(extent[i].ee_len); |
| 1689 | |
| 1690 | if (startblock > fileblock) { |
| 1691 | /* Sparse file */ |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1692 | if (!cache) |
| 1693 | ext_cache_fini(c); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1694 | return 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1695 | |
Stefan Brüns | 551fb6c | 2016-11-05 22:17:14 +0100 | [diff] [blame] | 1696 | } else if (fileblock < endblock) { |
| 1697 | start = le16_to_cpu(extent[i].ee_start_hi); |
| 1698 | start = (start << 32) + |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1699 | le32_to_cpu(extent[i].ee_start_lo); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1700 | if (!cache) |
| 1701 | ext_cache_fini(c); |
Stefan Brüns | 551fb6c | 2016-11-05 22:17:14 +0100 | [diff] [blame] | 1702 | return (fileblock - startblock) + start; |
| 1703 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1704 | } |
| 1705 | |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 1706 | if (!cache) |
| 1707 | ext_cache_fini(c); |
Stefan Brüns | 551fb6c | 2016-11-05 22:17:14 +0100 | [diff] [blame] | 1708 | return 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | /* Direct blocks. */ |
| 1712 | if (fileblock < INDIRECT_BLOCKS) |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1713 | blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1714 | |
| 1715 | /* Indirect. */ |
| 1716 | else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) { |
| 1717 | if (ext4fs_indir1_block == NULL) { |
| 1718 | ext4fs_indir1_block = zalloc(blksz); |
| 1719 | if (ext4fs_indir1_block == NULL) { |
| 1720 | printf("** SI ext2fs read block (indir 1)" |
| 1721 | "malloc failed. **\n"); |
| 1722 | return -1; |
| 1723 | } |
| 1724 | ext4fs_indir1_size = blksz; |
| 1725 | ext4fs_indir1_blkno = -1; |
| 1726 | } |
| 1727 | if (blksz != ext4fs_indir1_size) { |
| 1728 | free(ext4fs_indir1_block); |
| 1729 | ext4fs_indir1_block = NULL; |
| 1730 | ext4fs_indir1_size = 0; |
| 1731 | ext4fs_indir1_blkno = -1; |
| 1732 | ext4fs_indir1_block = zalloc(blksz); |
| 1733 | if (ext4fs_indir1_block == NULL) { |
| 1734 | printf("** SI ext2fs read block (indir 1):" |
| 1735 | "malloc failed. **\n"); |
| 1736 | return -1; |
| 1737 | } |
| 1738 | ext4fs_indir1_size = blksz; |
| 1739 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1740 | if ((le32_to_cpu(inode->b.blocks.indir_block) << |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1741 | log2_blksz) != ext4fs_indir1_blkno) { |
| 1742 | status = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1743 | ext4fs_devread((lbaint_t)le32_to_cpu |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1744 | (inode->b.blocks. |
| 1745 | indir_block) << log2_blksz, 0, |
| 1746 | blksz, (char *)ext4fs_indir1_block); |
| 1747 | if (status == 0) { |
| 1748 | printf("** SI ext2fs read block (indir 1)" |
| 1749 | "failed. **\n"); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 1750 | return -1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1751 | } |
| 1752 | ext4fs_indir1_blkno = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1753 | le32_to_cpu(inode->b.blocks. |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1754 | indir_block) << log2_blksz; |
| 1755 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1756 | blknr = le32_to_cpu(ext4fs_indir1_block |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1757 | [fileblock - INDIRECT_BLOCKS]); |
| 1758 | } |
| 1759 | /* Double indirect. */ |
| 1760 | else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 * |
| 1761 | (blksz / 4 + 1)))) { |
| 1762 | |
| 1763 | long int perblock = blksz / 4; |
| 1764 | long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4); |
| 1765 | |
| 1766 | if (ext4fs_indir1_block == NULL) { |
| 1767 | ext4fs_indir1_block = zalloc(blksz); |
| 1768 | if (ext4fs_indir1_block == NULL) { |
| 1769 | printf("** DI ext2fs read block (indir 2 1)" |
| 1770 | "malloc failed. **\n"); |
| 1771 | return -1; |
| 1772 | } |
| 1773 | ext4fs_indir1_size = blksz; |
| 1774 | ext4fs_indir1_blkno = -1; |
| 1775 | } |
| 1776 | if (blksz != ext4fs_indir1_size) { |
| 1777 | free(ext4fs_indir1_block); |
| 1778 | ext4fs_indir1_block = NULL; |
| 1779 | ext4fs_indir1_size = 0; |
| 1780 | ext4fs_indir1_blkno = -1; |
| 1781 | ext4fs_indir1_block = zalloc(blksz); |
| 1782 | if (ext4fs_indir1_block == NULL) { |
| 1783 | printf("** DI ext2fs read block (indir 2 1)" |
| 1784 | "malloc failed. **\n"); |
| 1785 | return -1; |
| 1786 | } |
| 1787 | ext4fs_indir1_size = blksz; |
| 1788 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1789 | if ((le32_to_cpu(inode->b.blocks.double_indir_block) << |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1790 | log2_blksz) != ext4fs_indir1_blkno) { |
| 1791 | status = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1792 | ext4fs_devread((lbaint_t)le32_to_cpu |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1793 | (inode->b.blocks. |
| 1794 | double_indir_block) << log2_blksz, |
| 1795 | 0, blksz, |
| 1796 | (char *)ext4fs_indir1_block); |
| 1797 | if (status == 0) { |
| 1798 | printf("** DI ext2fs read block (indir 2 1)" |
| 1799 | "failed. **\n"); |
| 1800 | return -1; |
| 1801 | } |
| 1802 | ext4fs_indir1_blkno = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1803 | le32_to_cpu(inode->b.blocks.double_indir_block) << |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1804 | log2_blksz; |
| 1805 | } |
| 1806 | |
| 1807 | if (ext4fs_indir2_block == NULL) { |
| 1808 | ext4fs_indir2_block = zalloc(blksz); |
| 1809 | if (ext4fs_indir2_block == NULL) { |
| 1810 | printf("** DI ext2fs read block (indir 2 2)" |
| 1811 | "malloc failed. **\n"); |
| 1812 | return -1; |
| 1813 | } |
| 1814 | ext4fs_indir2_size = blksz; |
| 1815 | ext4fs_indir2_blkno = -1; |
| 1816 | } |
| 1817 | if (blksz != ext4fs_indir2_size) { |
| 1818 | free(ext4fs_indir2_block); |
| 1819 | ext4fs_indir2_block = NULL; |
| 1820 | ext4fs_indir2_size = 0; |
| 1821 | ext4fs_indir2_blkno = -1; |
| 1822 | ext4fs_indir2_block = zalloc(blksz); |
| 1823 | if (ext4fs_indir2_block == NULL) { |
| 1824 | printf("** DI ext2fs read block (indir 2 2)" |
| 1825 | "malloc failed. **\n"); |
| 1826 | return -1; |
| 1827 | } |
| 1828 | ext4fs_indir2_size = blksz; |
| 1829 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1830 | if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) << |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1831 | log2_blksz) != ext4fs_indir2_blkno) { |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1832 | status = ext4fs_devread((lbaint_t)le32_to_cpu |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1833 | (ext4fs_indir1_block |
| 1834 | [rblock / |
| 1835 | perblock]) << log2_blksz, 0, |
| 1836 | blksz, |
| 1837 | (char *)ext4fs_indir2_block); |
| 1838 | if (status == 0) { |
| 1839 | printf("** DI ext2fs read block (indir 2 2)" |
| 1840 | "failed. **\n"); |
| 1841 | return -1; |
| 1842 | } |
| 1843 | ext4fs_indir2_blkno = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1844 | le32_to_cpu(ext4fs_indir1_block[rblock |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1845 | / |
| 1846 | perblock]) << |
| 1847 | log2_blksz; |
| 1848 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1849 | blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1850 | } |
| 1851 | /* Tripple indirect. */ |
| 1852 | else { |
| 1853 | rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 + |
| 1854 | (blksz / 4 * blksz / 4)); |
| 1855 | perblock_child = blksz / 4; |
| 1856 | perblock_parent = ((blksz / 4) * (blksz / 4)); |
| 1857 | |
| 1858 | if (ext4fs_indir1_block == NULL) { |
| 1859 | ext4fs_indir1_block = zalloc(blksz); |
| 1860 | if (ext4fs_indir1_block == NULL) { |
| 1861 | printf("** TI ext2fs read block (indir 2 1)" |
| 1862 | "malloc failed. **\n"); |
| 1863 | return -1; |
| 1864 | } |
| 1865 | ext4fs_indir1_size = blksz; |
| 1866 | ext4fs_indir1_blkno = -1; |
| 1867 | } |
| 1868 | if (blksz != ext4fs_indir1_size) { |
| 1869 | free(ext4fs_indir1_block); |
| 1870 | ext4fs_indir1_block = NULL; |
| 1871 | ext4fs_indir1_size = 0; |
| 1872 | ext4fs_indir1_blkno = -1; |
| 1873 | ext4fs_indir1_block = zalloc(blksz); |
| 1874 | if (ext4fs_indir1_block == NULL) { |
| 1875 | printf("** TI ext2fs read block (indir 2 1)" |
| 1876 | "malloc failed. **\n"); |
| 1877 | return -1; |
| 1878 | } |
| 1879 | ext4fs_indir1_size = blksz; |
| 1880 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1881 | if ((le32_to_cpu(inode->b.blocks.triple_indir_block) << |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1882 | log2_blksz) != ext4fs_indir1_blkno) { |
| 1883 | status = ext4fs_devread |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1884 | ((lbaint_t) |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1885 | le32_to_cpu(inode->b.blocks.triple_indir_block) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1886 | << log2_blksz, 0, blksz, |
| 1887 | (char *)ext4fs_indir1_block); |
| 1888 | if (status == 0) { |
| 1889 | printf("** TI ext2fs read block (indir 2 1)" |
| 1890 | "failed. **\n"); |
| 1891 | return -1; |
| 1892 | } |
| 1893 | ext4fs_indir1_blkno = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1894 | le32_to_cpu(inode->b.blocks.triple_indir_block) << |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1895 | log2_blksz; |
| 1896 | } |
| 1897 | |
| 1898 | if (ext4fs_indir2_block == NULL) { |
| 1899 | ext4fs_indir2_block = zalloc(blksz); |
| 1900 | if (ext4fs_indir2_block == NULL) { |
| 1901 | printf("** TI ext2fs read block (indir 2 2)" |
| 1902 | "malloc failed. **\n"); |
| 1903 | return -1; |
| 1904 | } |
| 1905 | ext4fs_indir2_size = blksz; |
| 1906 | ext4fs_indir2_blkno = -1; |
| 1907 | } |
| 1908 | if (blksz != ext4fs_indir2_size) { |
| 1909 | free(ext4fs_indir2_block); |
| 1910 | ext4fs_indir2_block = NULL; |
| 1911 | ext4fs_indir2_size = 0; |
| 1912 | ext4fs_indir2_blkno = -1; |
| 1913 | ext4fs_indir2_block = zalloc(blksz); |
| 1914 | if (ext4fs_indir2_block == NULL) { |
| 1915 | printf("** TI ext2fs read block (indir 2 2)" |
| 1916 | "malloc failed. **\n"); |
| 1917 | return -1; |
| 1918 | } |
| 1919 | ext4fs_indir2_size = blksz; |
| 1920 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1921 | if ((le32_to_cpu(ext4fs_indir1_block[rblock / |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1922 | perblock_parent]) << |
| 1923 | log2_blksz) |
| 1924 | != ext4fs_indir2_blkno) { |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1925 | status = ext4fs_devread((lbaint_t)le32_to_cpu |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1926 | (ext4fs_indir1_block |
| 1927 | [rblock / |
| 1928 | perblock_parent]) << |
| 1929 | log2_blksz, 0, blksz, |
| 1930 | (char *)ext4fs_indir2_block); |
| 1931 | if (status == 0) { |
| 1932 | printf("** TI ext2fs read block (indir 2 2)" |
| 1933 | "failed. **\n"); |
| 1934 | return -1; |
| 1935 | } |
| 1936 | ext4fs_indir2_blkno = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1937 | le32_to_cpu(ext4fs_indir1_block[rblock / |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1938 | perblock_parent]) |
| 1939 | << log2_blksz; |
| 1940 | } |
| 1941 | |
| 1942 | if (ext4fs_indir3_block == NULL) { |
| 1943 | ext4fs_indir3_block = zalloc(blksz); |
| 1944 | if (ext4fs_indir3_block == NULL) { |
| 1945 | printf("** TI ext2fs read block (indir 2 2)" |
| 1946 | "malloc failed. **\n"); |
| 1947 | return -1; |
| 1948 | } |
| 1949 | ext4fs_indir3_size = blksz; |
| 1950 | ext4fs_indir3_blkno = -1; |
| 1951 | } |
| 1952 | if (blksz != ext4fs_indir3_size) { |
| 1953 | free(ext4fs_indir3_block); |
| 1954 | ext4fs_indir3_block = NULL; |
| 1955 | ext4fs_indir3_size = 0; |
| 1956 | ext4fs_indir3_blkno = -1; |
| 1957 | ext4fs_indir3_block = zalloc(blksz); |
| 1958 | if (ext4fs_indir3_block == NULL) { |
| 1959 | printf("** TI ext2fs read block (indir 2 2)" |
| 1960 | "malloc failed. **\n"); |
| 1961 | return -1; |
| 1962 | } |
| 1963 | ext4fs_indir3_size = blksz; |
| 1964 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1965 | if ((le32_to_cpu(ext4fs_indir2_block[rblock |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1966 | / |
| 1967 | perblock_child]) << |
| 1968 | log2_blksz) != ext4fs_indir3_blkno) { |
| 1969 | status = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1970 | ext4fs_devread((lbaint_t)le32_to_cpu |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1971 | (ext4fs_indir2_block |
| 1972 | [(rblock / perblock_child) |
| 1973 | % (blksz / 4)]) << log2_blksz, 0, |
| 1974 | blksz, (char *)ext4fs_indir3_block); |
| 1975 | if (status == 0) { |
| 1976 | printf("** TI ext2fs read block (indir 2 2)" |
| 1977 | "failed. **\n"); |
| 1978 | return -1; |
| 1979 | } |
| 1980 | ext4fs_indir3_blkno = |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1981 | le32_to_cpu(ext4fs_indir2_block[(rblock / |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1982 | perblock_child) % |
| 1983 | (blksz / |
| 1984 | 4)]) << |
| 1985 | log2_blksz; |
| 1986 | } |
| 1987 | |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1988 | blknr = le32_to_cpu(ext4fs_indir3_block |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1989 | [rblock % perblock_child]); |
| 1990 | } |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 1991 | debug("read_allocated_block %ld\n", blknr); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1992 | |
| 1993 | return blknr; |
| 1994 | } |
| 1995 | |
Łukasz Majewski | 900db5d | 2014-05-06 09:36:05 +0200 | [diff] [blame] | 1996 | /** |
| 1997 | * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's |
| 1998 | * global pointers |
| 1999 | * |
| 2000 | * This function assures that for a file with the same name but different size |
| 2001 | * the sequential store on the ext4 filesystem will be correct. |
| 2002 | * |
| 2003 | * In this function the global data, responsible for internal representation |
| 2004 | * of the ext4 data are initialized to the reset state. Without this, during |
| 2005 | * replacement of the smaller file with the bigger truncation of new file was |
| 2006 | * performed. |
| 2007 | */ |
| 2008 | void ext4fs_reinit_global(void) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2009 | { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2010 | if (ext4fs_indir1_block != NULL) { |
| 2011 | free(ext4fs_indir1_block); |
| 2012 | ext4fs_indir1_block = NULL; |
| 2013 | ext4fs_indir1_size = 0; |
| 2014 | ext4fs_indir1_blkno = -1; |
| 2015 | } |
| 2016 | if (ext4fs_indir2_block != NULL) { |
| 2017 | free(ext4fs_indir2_block); |
| 2018 | ext4fs_indir2_block = NULL; |
| 2019 | ext4fs_indir2_size = 0; |
| 2020 | ext4fs_indir2_blkno = -1; |
| 2021 | } |
| 2022 | if (ext4fs_indir3_block != NULL) { |
| 2023 | free(ext4fs_indir3_block); |
| 2024 | ext4fs_indir3_block = NULL; |
| 2025 | ext4fs_indir3_size = 0; |
| 2026 | ext4fs_indir3_blkno = -1; |
| 2027 | } |
Łukasz Majewski | 900db5d | 2014-05-06 09:36:05 +0200 | [diff] [blame] | 2028 | } |
| 2029 | void ext4fs_close(void) |
| 2030 | { |
| 2031 | if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) { |
| 2032 | ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen); |
| 2033 | ext4fs_file = NULL; |
| 2034 | } |
| 2035 | if (ext4fs_root != NULL) { |
| 2036 | free(ext4fs_root); |
| 2037 | ext4fs_root = NULL; |
| 2038 | } |
| 2039 | |
| 2040 | ext4fs_reinit_global(); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name, |
| 2044 | struct ext2fs_node **fnode, int *ftype) |
| 2045 | { |
| 2046 | unsigned int fpos = 0; |
| 2047 | int status; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2048 | loff_t actread; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2049 | struct ext2fs_node *diro = (struct ext2fs_node *) dir; |
| 2050 | |
| 2051 | #ifdef DEBUG |
| 2052 | if (name != NULL) |
| 2053 | printf("Iterate dir %s\n", name); |
| 2054 | #endif /* of DEBUG */ |
| 2055 | if (!diro->inode_read) { |
| 2056 | status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); |
| 2057 | if (status == 0) |
| 2058 | return 0; |
| 2059 | } |
| 2060 | /* Search the file. */ |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2061 | while (fpos < le32_to_cpu(diro->inode.size)) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2062 | struct ext2_dirent dirent; |
| 2063 | |
| 2064 | status = ext4fs_read_file(diro, fpos, |
| 2065 | sizeof(struct ext2_dirent), |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2066 | (char *)&dirent, &actread); |
| 2067 | if (status < 0) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2068 | return 0; |
| 2069 | |
Thomas Fitzsimmons | c6676e0 | 2015-11-18 12:42:53 -0500 | [diff] [blame] | 2070 | if (dirent.direntlen == 0) { |
| 2071 | printf("Failed to iterate over directory %s\n", name); |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2075 | if (dirent.namelen != 0) { |
| 2076 | char filename[dirent.namelen + 1]; |
| 2077 | struct ext2fs_node *fdiro; |
| 2078 | int type = FILETYPE_UNKNOWN; |
| 2079 | |
| 2080 | status = ext4fs_read_file(diro, |
| 2081 | fpos + |
| 2082 | sizeof(struct ext2_dirent), |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2083 | dirent.namelen, filename, |
| 2084 | &actread); |
| 2085 | if (status < 0) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2086 | return 0; |
| 2087 | |
| 2088 | fdiro = zalloc(sizeof(struct ext2fs_node)); |
| 2089 | if (!fdiro) |
| 2090 | return 0; |
| 2091 | |
| 2092 | fdiro->data = diro->data; |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2093 | fdiro->ino = le32_to_cpu(dirent.inode); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2094 | |
| 2095 | filename[dirent.namelen] = '\0'; |
| 2096 | |
| 2097 | if (dirent.filetype != FILETYPE_UNKNOWN) { |
| 2098 | fdiro->inode_read = 0; |
| 2099 | |
| 2100 | if (dirent.filetype == FILETYPE_DIRECTORY) |
| 2101 | type = FILETYPE_DIRECTORY; |
| 2102 | else if (dirent.filetype == FILETYPE_SYMLINK) |
| 2103 | type = FILETYPE_SYMLINK; |
| 2104 | else if (dirent.filetype == FILETYPE_REG) |
| 2105 | type = FILETYPE_REG; |
| 2106 | } else { |
| 2107 | status = ext4fs_read_inode(diro->data, |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2108 | le32_to_cpu |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2109 | (dirent.inode), |
| 2110 | &fdiro->inode); |
| 2111 | if (status == 0) { |
| 2112 | free(fdiro); |
| 2113 | return 0; |
| 2114 | } |
| 2115 | fdiro->inode_read = 1; |
| 2116 | |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2117 | if ((le16_to_cpu(fdiro->inode.mode) & |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2118 | FILETYPE_INO_MASK) == |
| 2119 | FILETYPE_INO_DIRECTORY) { |
| 2120 | type = FILETYPE_DIRECTORY; |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2121 | } else if ((le16_to_cpu(fdiro->inode.mode) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2122 | & FILETYPE_INO_MASK) == |
| 2123 | FILETYPE_INO_SYMLINK) { |
| 2124 | type = FILETYPE_SYMLINK; |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2125 | } else if ((le16_to_cpu(fdiro->inode.mode) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2126 | & FILETYPE_INO_MASK) == |
| 2127 | FILETYPE_INO_REG) { |
| 2128 | type = FILETYPE_REG; |
| 2129 | } |
| 2130 | } |
| 2131 | #ifdef DEBUG |
| 2132 | printf("iterate >%s<\n", filename); |
| 2133 | #endif /* of DEBUG */ |
| 2134 | if ((name != NULL) && (fnode != NULL) |
| 2135 | && (ftype != NULL)) { |
| 2136 | if (strcmp(filename, name) == 0) { |
| 2137 | *ftype = type; |
| 2138 | *fnode = fdiro; |
| 2139 | return 1; |
| 2140 | } |
| 2141 | } else { |
| 2142 | if (fdiro->inode_read == 0) { |
| 2143 | status = ext4fs_read_inode(diro->data, |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2144 | le32_to_cpu( |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2145 | dirent.inode), |
| 2146 | &fdiro->inode); |
| 2147 | if (status == 0) { |
| 2148 | free(fdiro); |
| 2149 | return 0; |
| 2150 | } |
| 2151 | fdiro->inode_read = 1; |
| 2152 | } |
| 2153 | switch (type) { |
| 2154 | case FILETYPE_DIRECTORY: |
| 2155 | printf("<DIR> "); |
| 2156 | break; |
| 2157 | case FILETYPE_SYMLINK: |
| 2158 | printf("<SYM> "); |
| 2159 | break; |
| 2160 | case FILETYPE_REG: |
| 2161 | printf(" "); |
| 2162 | break; |
| 2163 | default: |
| 2164 | printf("< ? > "); |
| 2165 | break; |
| 2166 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2167 | printf("%10u %s\n", |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2168 | le32_to_cpu(fdiro->inode.size), |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2169 | filename); |
| 2170 | } |
| 2171 | free(fdiro); |
| 2172 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2173 | fpos += le16_to_cpu(dirent.direntlen); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2174 | } |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
| 2178 | static char *ext4fs_read_symlink(struct ext2fs_node *node) |
| 2179 | { |
| 2180 | char *symlink; |
| 2181 | struct ext2fs_node *diro = node; |
| 2182 | int status; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2183 | loff_t actread; |
Richard Weinberger | 735814f | 2024-08-09 11:54:28 +0200 | [diff] [blame] | 2184 | size_t alloc_size; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2185 | |
| 2186 | if (!diro->inode_read) { |
| 2187 | status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode); |
| 2188 | if (status == 0) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 2189 | return NULL; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2190 | } |
Richard Weinberger | 735814f | 2024-08-09 11:54:28 +0200 | [diff] [blame] | 2191 | |
| 2192 | if (__builtin_add_overflow(le32_to_cpu(diro->inode.size), 1, &alloc_size)) |
| 2193 | return NULL; |
| 2194 | |
| 2195 | symlink = zalloc(alloc_size); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2196 | if (!symlink) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 2197 | return NULL; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2198 | |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2199 | if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2200 | strncpy(symlink, diro->inode.b.symlink, |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2201 | le32_to_cpu(diro->inode.size)); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2202 | } else { |
| 2203 | status = ext4fs_read_file(diro, 0, |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2204 | le32_to_cpu(diro->inode.size), |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2205 | symlink, &actread); |
Gary Bisson | 086e011 | 2015-09-07 11:20:07 +0200 | [diff] [blame] | 2206 | if ((status < 0) || (actread == 0)) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2207 | free(symlink); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 2208 | return NULL; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2209 | } |
| 2210 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2211 | symlink[le32_to_cpu(diro->inode.size)] = '\0'; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2212 | return symlink; |
| 2213 | } |
| 2214 | |
Heinrich Schuchardt | e2ac638 | 2024-02-20 12:54:23 +0100 | [diff] [blame] | 2215 | int ext4fs_find_file1(const char *currpath, struct ext2fs_node *currroot, |
| 2216 | struct ext2fs_node **currfound, int *foundtype) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2217 | { |
| 2218 | char fpath[strlen(currpath) + 1]; |
| 2219 | char *name = fpath; |
| 2220 | char *next; |
| 2221 | int status; |
| 2222 | int type = FILETYPE_DIRECTORY; |
| 2223 | struct ext2fs_node *currnode = currroot; |
| 2224 | struct ext2fs_node *oldnode = currroot; |
| 2225 | |
| 2226 | strncpy(fpath, currpath, strlen(currpath) + 1); |
| 2227 | |
| 2228 | /* Remove all leading slashes. */ |
| 2229 | while (*name == '/') |
| 2230 | name++; |
| 2231 | |
| 2232 | if (!*name) { |
| 2233 | *currfound = currnode; |
| 2234 | return 1; |
| 2235 | } |
| 2236 | |
| 2237 | for (;;) { |
| 2238 | int found; |
| 2239 | |
| 2240 | /* Extract the actual part from the pathname. */ |
| 2241 | next = strchr(name, '/'); |
| 2242 | if (next) { |
| 2243 | /* Remove all leading slashes. */ |
| 2244 | while (*next == '/') |
| 2245 | *(next++) = '\0'; |
| 2246 | } |
| 2247 | |
| 2248 | if (type != FILETYPE_DIRECTORY) { |
| 2249 | ext4fs_free_node(currnode, currroot); |
| 2250 | return 0; |
| 2251 | } |
| 2252 | |
| 2253 | oldnode = currnode; |
| 2254 | |
| 2255 | /* Iterate over the directory. */ |
| 2256 | found = ext4fs_iterate_dir(currnode, name, &currnode, &type); |
| 2257 | if (found == 0) |
| 2258 | return 0; |
| 2259 | |
| 2260 | if (found == -1) |
| 2261 | break; |
| 2262 | |
| 2263 | /* Read in the symlink and follow it. */ |
| 2264 | if (type == FILETYPE_SYMLINK) { |
| 2265 | char *symlink; |
| 2266 | |
| 2267 | /* Test if the symlink does not loop. */ |
| 2268 | if (++symlinknest == 8) { |
| 2269 | ext4fs_free_node(currnode, currroot); |
| 2270 | ext4fs_free_node(oldnode, currroot); |
| 2271 | return 0; |
| 2272 | } |
| 2273 | |
| 2274 | symlink = ext4fs_read_symlink(currnode); |
| 2275 | ext4fs_free_node(currnode, currroot); |
| 2276 | |
| 2277 | if (!symlink) { |
| 2278 | ext4fs_free_node(oldnode, currroot); |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | debug("Got symlink >%s<\n", symlink); |
| 2283 | |
| 2284 | if (symlink[0] == '/') { |
| 2285 | ext4fs_free_node(oldnode, currroot); |
| 2286 | oldnode = &ext4fs_root->diropen; |
| 2287 | } |
| 2288 | |
| 2289 | /* Lookup the node the symlink points to. */ |
| 2290 | status = ext4fs_find_file1(symlink, oldnode, |
| 2291 | &currnode, &type); |
| 2292 | |
| 2293 | free(symlink); |
| 2294 | |
| 2295 | if (status == 0) { |
| 2296 | ext4fs_free_node(oldnode, currroot); |
| 2297 | return 0; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | ext4fs_free_node(oldnode, currroot); |
| 2302 | |
| 2303 | /* Found the node! */ |
| 2304 | if (!next || *next == '\0') { |
| 2305 | *currfound = currnode; |
| 2306 | *foundtype = type; |
| 2307 | return 1; |
| 2308 | } |
| 2309 | name = next; |
| 2310 | } |
| 2311 | return -1; |
| 2312 | } |
| 2313 | |
| 2314 | int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode, |
| 2315 | struct ext2fs_node **foundnode, int expecttype) |
| 2316 | { |
| 2317 | int status; |
| 2318 | int foundtype = FILETYPE_DIRECTORY; |
| 2319 | |
| 2320 | symlinknest = 0; |
| 2321 | if (!path) |
| 2322 | return 0; |
| 2323 | |
| 2324 | status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype); |
| 2325 | if (status == 0) |
| 2326 | return 0; |
| 2327 | |
| 2328 | /* Check if the node that was found was of the expected type. */ |
| 2329 | if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) |
| 2330 | return 0; |
| 2331 | else if ((expecttype == FILETYPE_DIRECTORY) |
| 2332 | && (foundtype != expecttype)) |
| 2333 | return 0; |
| 2334 | |
| 2335 | return 1; |
| 2336 | } |
| 2337 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2338 | int ext4fs_open(const char *filename, loff_t *len) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2339 | { |
| 2340 | struct ext2fs_node *fdiro = NULL; |
| 2341 | int status; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2342 | |
| 2343 | if (ext4fs_root == NULL) |
| 2344 | return -1; |
| 2345 | |
| 2346 | ext4fs_file = NULL; |
| 2347 | status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro, |
| 2348 | FILETYPE_REG); |
| 2349 | if (status == 0) |
| 2350 | goto fail; |
| 2351 | |
| 2352 | if (!fdiro->inode_read) { |
| 2353 | status = ext4fs_read_inode(fdiro->data, fdiro->ino, |
| 2354 | &fdiro->inode); |
| 2355 | if (status == 0) |
| 2356 | goto fail; |
| 2357 | } |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2358 | *len = le32_to_cpu(fdiro->inode.size); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2359 | ext4fs_file = fdiro; |
| 2360 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 2361 | return 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2362 | fail: |
| 2363 | ext4fs_free_node(fdiro, &ext4fs_root->diropen); |
| 2364 | |
| 2365 | return -1; |
| 2366 | } |
| 2367 | |
Sean Anderson | ab125f5 | 2023-11-08 12:51:09 -0500 | [diff] [blame] | 2368 | int ext4fs_mount(void) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2369 | { |
| 2370 | struct ext2_data *data; |
| 2371 | int status; |
| 2372 | struct ext_filesystem *fs = get_fs(); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 2373 | data = zalloc(SUPERBLOCK_SIZE); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2374 | if (!data) |
| 2375 | return 0; |
| 2376 | |
| 2377 | /* Read the superblock. */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 2378 | status = ext4_read_superblock((char *)&data->sblock); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2379 | |
| 2380 | if (status == 0) |
| 2381 | goto fail; |
| 2382 | |
| 2383 | /* Make sure this is an ext2 filesystem. */ |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2384 | if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC) |
Marek Behún | fe614ef | 2018-03-08 00:26:08 +0100 | [diff] [blame] | 2385 | goto fail_noerr; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2386 | |
Stefan Brüns | b2c28a2 | 2016-09-17 02:10:07 +0200 | [diff] [blame] | 2387 | if (le32_to_cpu(data->sblock.revision_level) == 0) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2388 | fs->inodesz = 128; |
Stefan Brüns | 44632b7 | 2016-12-27 02:35:08 +0100 | [diff] [blame] | 2389 | fs->gdsize = 32; |
Stefan Brüns | b2c28a2 | 2016-09-17 02:10:07 +0200 | [diff] [blame] | 2390 | } else { |
Richard Weinberger | 9493a9a | 2024-07-29 23:37:31 +0200 | [diff] [blame] | 2391 | int missing = __le32_to_cpu(data->sblock.feature_incompat) & |
| 2392 | ~(EXT4_FEATURE_INCOMPAT_SUPP | |
| 2393 | EXT4_FEATURE_INCOMPAT_SUPP_LAZY_RO); |
| 2394 | |
| 2395 | if (missing) { |
| 2396 | /* |
| 2397 | * This code used to be relaxed about feature flags. |
| 2398 | * We don't stop the mount to avoid breaking existing setups. |
| 2399 | * But, incompatible features can cause serious read errors. |
| 2400 | */ |
| 2401 | log_err("fs uses incompatible features: %08x, ignoring\n", |
| 2402 | missing); |
| 2403 | } |
| 2404 | |
Stefan Brüns | b2c28a2 | 2016-09-17 02:10:07 +0200 | [diff] [blame] | 2405 | debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n", |
| 2406 | __le32_to_cpu(data->sblock.feature_compatibility), |
| 2407 | __le32_to_cpu(data->sblock.feature_incompat), |
| 2408 | __le32_to_cpu(data->sblock.feature_ro_compat)); |
| 2409 | |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 2410 | fs->inodesz = le16_to_cpu(data->sblock.inode_size); |
Stefan Brüns | b2c28a2 | 2016-09-17 02:10:07 +0200 | [diff] [blame] | 2411 | fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) & |
| 2412 | EXT4_FEATURE_INCOMPAT_64BIT ? |
| 2413 | le16_to_cpu(data->sblock.descriptor_size) : 32; |
| 2414 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2415 | |
Stefan Brüns | b2c28a2 | 2016-09-17 02:10:07 +0200 | [diff] [blame] | 2416 | debug("EXT2 rev %d, inode_size %d, descriptor size %d\n", |
| 2417 | le32_to_cpu(data->sblock.revision_level), |
| 2418 | fs->inodesz, fs->gdsize); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2419 | |
| 2420 | data->diropen.data = data; |
| 2421 | data->diropen.ino = 2; |
| 2422 | data->diropen.inode_read = 1; |
| 2423 | data->inode = &data->diropen.inode; |
| 2424 | |
| 2425 | status = ext4fs_read_inode(data, 2, data->inode); |
| 2426 | if (status == 0) |
| 2427 | goto fail; |
| 2428 | |
| 2429 | ext4fs_root = data; |
| 2430 | |
| 2431 | return 1; |
| 2432 | fail: |
Simon Glass | 3862fab | 2022-10-11 09:47:11 -0600 | [diff] [blame] | 2433 | log_debug("Failed to mount ext2 filesystem...\n"); |
Marek Behún | fe614ef | 2018-03-08 00:26:08 +0100 | [diff] [blame] | 2434 | fail_noerr: |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2435 | free(data); |
| 2436 | ext4fs_root = NULL; |
| 2437 | |
| 2438 | return 0; |
| 2439 | } |