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