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