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