ext4_common.c 59.1 KB
Newer Older
Uma Shankar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * (C) Copyright 2011 - 2012 Samsung Electronics
 * EXT4 filesystem implementation in Uboot by
 * Uma Shankar <uma.shankar@samsung.com>
 * Manjunatha C Achar <a.manjunatha@samsung.com>
 *
 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
 *
 * (C) Copyright 2004
 * esd gmbh <www.esd-electronics.com>
 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
 *
 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
 * GRUB  --  GRand Unified Bootloader
 * Copyright (C) 2003, 2004  Free Software Foundation, Inc.
 *
Uma Shankar committed
17 18
 * ext4write : Based on generic ext4 protocol.
 *
19
 * SPDX-License-Identifier:	GPL-2.0+
Uma Shankar committed
20 21 22 23 24
 */

#include <common.h>
#include <ext_common.h>
#include <ext4fs.h>
25
#include <inttypes.h>
Uma Shankar committed
26
#include <malloc.h>
27
#include <memalign.h>
Uma Shankar committed
28 29 30 31 32 33 34 35
#include <stddef.h>
#include <linux/stat.h>
#include <linux/time.h>
#include <asm/byteorder.h>
#include "ext4_common.h"

struct ext2_data *ext4fs_root;
struct ext2fs_node *ext4fs_file;
36
__le32 *ext4fs_indir1_block;
Uma Shankar committed
37 38
int ext4fs_indir1_size;
int ext4fs_indir1_blkno = -1;
39
__le32 *ext4fs_indir2_block;
Uma Shankar committed
40 41 42
int ext4fs_indir2_size;
int ext4fs_indir2_blkno = -1;

43
__le32 *ext4fs_indir3_block;
Uma Shankar committed
44 45 46 47 48
int ext4fs_indir3_size;
int ext4fs_indir3_blkno = -1;
struct ext2_inode *g_parent_inode;
static int symlinknest;

49
#if defined(CONFIG_EXT4_WRITE)
50 51 52 53 54 55
struct ext2_block_group *ext4fs_get_group_descriptor
	(const struct ext_filesystem *fs, uint32_t bg_idx)
{
	return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
}

56 57 58 59 60 61 62
static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
{
	sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
}

static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
{
63 64 65 66 67 68
	uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
	free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
	free_blocks--;

	sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
	sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
69 70
}

71 72
static inline void ext4fs_bg_free_inodes_dec
	(struct ext2_block_group *bg, const struct ext_filesystem *fs)
73
{
74 75 76 77 78 79 80 81
	uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
	if (fs->gdsize == 64)
		free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
	free_inodes--;

	bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
	if (fs->gdsize == 64)
		bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
82 83
}

84 85
static inline void ext4fs_bg_free_blocks_dec
	(struct ext2_block_group *bg, const struct ext_filesystem *fs)
86
{
87 88 89 90 91 92 93 94
	uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
	if (fs->gdsize == 64)
		free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
	free_blocks--;

	bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
	if (fs->gdsize == 64)
		bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
95 96
}

97 98
static inline void ext4fs_bg_itable_unused_dec
	(struct ext2_block_group *bg, const struct ext_filesystem *fs)
99
{
100 101 102 103 104 105 106 107
	uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
	if (fs->gdsize == 64)
		free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
	free_inodes--;

	bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
	if (fs->gdsize == 64)
		bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
{
	uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
	free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
	return free_blocks;
}

void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
{
	sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
	sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
}

uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
				   const struct ext_filesystem *fs)
{
	uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
	if (fs->gdsize == 64)
		free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
	return free_blocks;
}

static inline
uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
				   const struct ext_filesystem *fs)
{
	uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
	if (fs->gdsize == 64)
		free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
	return free_inodes;
}

static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
{
	return le16_to_cpu(bg->bg_flags);
}

static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
				       uint16_t flags)
{
	bg->bg_flags = cpu_to_le16(flags);
}

/* Block number of the block bitmap */
uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
				const struct ext_filesystem *fs)
{
	uint64_t block_nr = le32_to_cpu(bg->block_id);
	if (fs->gdsize == 64)
		block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
	return block_nr;
}

/* Block number of the inode bitmap */
uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
				const struct ext_filesystem *fs)
{
	uint64_t block_nr = le32_to_cpu(bg->inode_id);
	if (fs->gdsize == 64)
		block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
	return block_nr;
}
#endif

/* Block number of the inode table */
uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
				      const struct ext_filesystem *fs)
{
	uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
	if (fs->gdsize == 64)
		block_nr +=
			(uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
	return block_nr;
}

#if defined(CONFIG_EXT4_WRITE)
Uma Shankar committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
{
	uint32_t res = size / n;
	if (res * n != size)
		res++;

	return res;
}

void put_ext4(uint64_t off, void *buf, uint32_t size)
{
	uint64_t startblock;
	uint64_t remainder;
	unsigned char *temp_ptr = NULL;
	struct ext_filesystem *fs = get_fs();
201 202
	int log2blksz = fs->dev_desc->log2blksz;
	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankar committed
203

204
	startblock = off >> log2blksz;
Uma Shankar committed
205
	startblock += part_offset;
206
	remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankar committed
207 208 209 210

	if (fs->dev_desc == NULL)
		return;

211
	if ((startblock + (size >> log2blksz)) >
Uma Shankar committed
212
	    (part_offset + fs->total_sect)) {
213
		printf("part_offset is " LBAFU "\n", part_offset);
214
		printf("total_sector is %" PRIu64 "\n", fs->total_sect);
Uma Shankar committed
215 216 217 218 219
		printf("error: overflow occurs\n");
		return;
	}

	if (remainder) {
220 221 222 223
		blk_dread(fs->dev_desc, startblock, 1, sec_buf);
		temp_ptr = sec_buf;
		memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
		blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankar committed
224
	} else {
225
		if (size >> log2blksz != 0) {
226 227
			blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
				   (unsigned long *)buf);
Uma Shankar committed
228
		} else {
229
			blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankar committed
230 231
			temp_ptr = sec_buf;
			memcpy(temp_ptr, buf, size);
232 233
			blk_dwrite(fs->dev_desc, startblock, 1,
				   (unsigned long *)sec_buf);
Uma Shankar committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		}
	}
}

static int _get_new_inode_no(unsigned char *buffer)
{
	struct ext_filesystem *fs = get_fs();
	unsigned char input;
	int operand, status;
	int count = 1;
	int j = 0;

	/* get the blocksize of the filesystem */
	unsigned char *ptr = buffer;
	while (*ptr == 255) {
		ptr++;
		count += 8;
251
		if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankar committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
			return -1;
	}

	for (j = 0; j < fs->blksz; j++) {
		input = *ptr;
		int i = 0;
		while (i <= 7) {
			operand = 1 << i;
			status = input & operand;
			if (status) {
				i++;
				count++;
			} else {
				*ptr |= operand;
				return count;
			}
		}
		ptr = ptr + 1;
	}

	return -1;
}

static int _get_new_blk_no(unsigned char *buffer)
{
277
	int operand;
Uma Shankar committed
278
	int count = 0;
279
	int i;
Uma Shankar committed
280 281 282 283 284 285 286 287 288 289
	unsigned char *ptr = buffer;
	struct ext_filesystem *fs = get_fs();

	while (*ptr == 255) {
		ptr++;
		count += 8;
		if (count == (fs->blksz * 8))
			return -1;
	}

290 291 292 293 294 295 296 297 298 299
	if (fs->blksz == 1024)
		count += 1;

	for (i = 0; i <= 7; i++) {
		operand = 1 << i;
		if (*ptr & operand) {
			count++;
		} else {
			*ptr |= operand;
			return count;
Uma Shankar committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
		}
	}

	return -1;
}

int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
{
	int i, remainder, status;
	unsigned char *ptr = buffer;
	unsigned char operand;
	i = blockno / 8;
	remainder = blockno % 8;
	int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);

	i = i - (index * blocksize);
	if (blocksize != 1024) {
		ptr = ptr + i;
		operand = 1 << remainder;
		status = *ptr & operand;
		if (status)
			return -1;

		*ptr = *ptr | operand;
		return 0;
	} else {
		if (remainder == 0) {
			ptr = ptr + i - 1;
			operand = (1 << 7);
		} else {
			ptr = ptr + i;
			operand = (1 << (remainder - 1));
		}
		status = *ptr & operand;
		if (status)
			return -1;

		*ptr = *ptr | operand;
		return 0;
	}
}

void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
{
	int i, remainder, status;
	unsigned char *ptr = buffer;
	unsigned char operand;
	i = blockno / 8;
	remainder = blockno % 8;
	int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);

	i = i - (index * blocksize);
	if (blocksize != 1024) {
		ptr = ptr + i;
		operand = (1 << remainder);
		status = *ptr & operand;
		if (status)
			*ptr = *ptr & ~(operand);
	} else {
		if (remainder == 0) {
			ptr = ptr + i - 1;
			operand = (1 << 7);
		} else {
			ptr = ptr + i;
			operand = (1 << (remainder - 1));
		}
		status = *ptr & operand;
		if (status)
			*ptr = *ptr & ~(operand);
	}
}

int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
{
	int i, remainder, status;
	unsigned char *ptr = buffer;
	unsigned char operand;

378
	inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankar committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	i = inode_no / 8;
	remainder = inode_no % 8;
	if (remainder == 0) {
		ptr = ptr + i - 1;
		operand = (1 << 7);
	} else {
		ptr = ptr + i;
		operand = (1 << (remainder - 1));
	}
	status = *ptr & operand;
	if (status)
		return -1;

	*ptr = *ptr | operand;

	return 0;
}

void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
{
	int i, remainder, status;
	unsigned char *ptr = buffer;
	unsigned char operand;

403
	inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankar committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417
	i = inode_no / 8;
	remainder = inode_no % 8;
	if (remainder == 0) {
		ptr = ptr + i - 1;
		operand = (1 << 7);
	} else {
		ptr = ptr + i;
		operand = (1 << (remainder - 1));
	}
	status = *ptr & operand;
	if (status)
		*ptr = *ptr & ~(operand);
}

418
uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankar committed
419 420 421
{
	struct ext2_block_group *desc;
	struct ext_filesystem *fs = get_fs();
422 423
	uint16_t crc = 0;
	__le32 le32_i = cpu_to_le32(i);
Uma Shankar committed
424

425
	desc = ext4fs_get_group_descriptor(fs, i);
426
	if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankar committed
427 428 429 430
		int offset = offsetof(struct ext2_block_group, bg_checksum);

		crc = ext2fs_crc16(~0, fs->sb->unique_id,
				   sizeof(fs->sb->unique_id));
431
		crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
Uma Shankar committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		crc = ext2fs_crc16(crc, desc, offset);
		offset += sizeof(desc->bg_checksum);	/* skip checksum */
		assert(offset == sizeof(*desc));
	}

	return crc;
}

static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
{
	int dentry_length;
	int sizeof_void_space;
	int new_entry_byte_reqd;
	short padding_factor = 0;

	if (dir->namelen % 4 != 0)
		padding_factor = 4 - (dir->namelen % 4);

	dentry_length = sizeof(struct ext2_dirent) +
			dir->namelen + padding_factor;
452
	sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankar committed
453 454 455 456 457 458 459 460 461 462
	if (sizeof_void_space == 0)
		return 0;

	padding_factor = 0;
	if (strlen(filename) % 4 != 0)
		padding_factor = 4 - (strlen(filename) % 4);

	new_entry_byte_reqd = strlen(filename) +
	    sizeof(struct ext2_dirent) + padding_factor;
	if (sizeof_void_space >= new_entry_byte_reqd) {
463
		dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankar committed
464 465 466 467 468 469
		return sizeof_void_space;
	}

	return 0;
}

470
int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankar committed
471 472 473
{
	unsigned int *zero_buffer = NULL;
	char *root_first_block_buffer = NULL;
474
	int blk_idx;
Uma Shankar committed
475 476 477 478 479
	long int first_block_no_of_root = 0;
	int totalbytes = 0;
	unsigned int new_entry_byte_reqd;
	int sizeof_void_space = 0;
	int templength = 0;
480
	int inodeno = -1;
Uma Shankar committed
481 482 483 484 485
	int status;
	struct ext_filesystem *fs = get_fs();
	/* directory entry */
	struct ext2_dirent *dir;
	char *temp_dir = NULL;
486 487 488
	uint32_t new_blk_no;
	uint32_t new_size;
	uint32_t new_blockcnt;
489
	uint32_t directory_blocks;
Uma Shankar committed
490 491 492 493

	zero_buffer = zalloc(fs->blksz);
	if (!zero_buffer) {
		printf("No Memory\n");
494
		return -1;
Uma Shankar committed
495 496 497 498 499
	}
	root_first_block_buffer = zalloc(fs->blksz);
	if (!root_first_block_buffer) {
		free(zero_buffer);
		printf("No Memory\n");
500
		return -1;
Uma Shankar committed
501
	}
502 503
	new_entry_byte_reqd = ROUND(strlen(filename) +
				    sizeof(struct ext2_dirent), 4);
Uma Shankar committed
504
restart:
505 506 507
	directory_blocks = le32_to_cpu(g_parent_inode->size) >>
		LOG2_BLOCK_SIZE(ext4fs_root);
	blk_idx = directory_blocks - 1;
Uma Shankar committed
508

509
restart_read:
Uma Shankar committed
510
	/* read the block no allocated to a file */
511 512 513
	first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
	if (first_block_no_of_root <= 0)
		goto fail;
Uma Shankar committed
514

515
	status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankar committed
516 517 518 519 520 521 522 523 524
				* fs->sect_perblk,
				0, fs->blksz, root_first_block_buffer);
	if (status == 0)
		goto fail;

	if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
		goto fail;
	dir = (struct ext2_dirent *)root_first_block_buffer;
	totalbytes = 0;
525

526
	while (le16_to_cpu(dir->direntlen) > 0) {
527 528 529 530
		unsigned short used_len = ROUND(dir->namelen +
		    sizeof(struct ext2_dirent), 4);

		/* last entry of block */
531
		if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
532 533 534 535 536 537 538 539 540 541 542 543 544

			/* check if new entry fits */
			if ((used_len + new_entry_byte_reqd) <=
			    le16_to_cpu(dir->direntlen)) {
				dir->direntlen = cpu_to_le16(used_len);
				break;
			} else {
				if (blk_idx > 0) {
					printf("Block full, trying previous\n");
					blk_idx--;
					goto restart_read;
				}
				printf("All blocks full: Allocate new\n");
Uma Shankar committed
545

546 547 548 549 550
				if (le32_to_cpu(g_parent_inode->flags) &
						EXT4_EXTENTS_FL) {
					printf("Directory uses extents\n");
					goto fail;
				}
551
				if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankar committed
552 553 554
					printf("Directory exceeds limit\n");
					goto fail;
				}
555 556
				new_blk_no = ext4fs_get_new_blk_no();
				if (new_blk_no == -1) {
Uma Shankar committed
557 558 559
					printf("no block left to assign\n");
					goto fail;
				}
560
				put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
561 562
				g_parent_inode->b.blocks.
					dir_blocks[directory_blocks] =
563 564 565 566 567 568 569 570 571 572
					cpu_to_le32(new_blk_no);

				new_size = le32_to_cpu(g_parent_inode->size);
				new_size += fs->blksz;
				g_parent_inode->size = cpu_to_le32(new_size);

				new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
				new_blockcnt += fs->sect_perblk;
				g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);

Uma Shankar committed
573 574 575 576 577 578 579 580
				if (ext4fs_put_metadata
				    (root_first_block_buffer,
				     first_block_no_of_root))
					goto fail;
				goto restart;
			}
		}

581
		templength = le16_to_cpu(dir->direntlen);
Uma Shankar committed
582 583 584 585 586 587 588 589 590
		totalbytes = totalbytes + templength;
		sizeof_void_space = check_void_in_dentry(dir, filename);
		if (sizeof_void_space)
			break;

		dir = (struct ext2_dirent *)((char *)dir + templength);
	}

	/* make a pointer ready for creating next directory entry */
591
	templength = le16_to_cpu(dir->direntlen);
Uma Shankar committed
592 593 594 595 596 597 598 599 600
	totalbytes = totalbytes + templength;
	dir = (struct ext2_dirent *)((char *)dir + templength);

	/* get the next available inode number */
	inodeno = ext4fs_get_new_inode_no();
	if (inodeno == -1) {
		printf("no inode left to assign\n");
		goto fail;
	}
601
	dir->inode = cpu_to_le32(inodeno);
Uma Shankar committed
602
	if (sizeof_void_space)
603
		dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankar committed
604
	else
605
		dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankar committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

	dir->namelen = strlen(filename);
	dir->filetype = FILETYPE_REG;	/* regular file */
	temp_dir = (char *)dir;
	temp_dir = temp_dir + sizeof(struct ext2_dirent);
	memcpy(temp_dir, filename, strlen(filename));

	/* update or write  the 1st block of root inode */
	if (ext4fs_put_metadata(root_first_block_buffer,
				first_block_no_of_root))
		goto fail;

fail:
	free(zero_buffer);
	free(root_first_block_buffer);
621 622

	return inodeno;
Uma Shankar committed
623 624 625 626 627
}

static int search_dir(struct ext2_inode *parent_inode, char *dirname)
{
	int status;
628
	int inodeno = 0;
629 630
	int offset;
	int blk_idx;
Uma Shankar committed
631
	long int blknr;
632
	char *block_buffer = NULL;
Uma Shankar committed
633 634
	struct ext2_dirent *dir = NULL;
	struct ext_filesystem *fs = get_fs();
635 636
	uint32_t directory_blocks;
	char *direntname;
Uma Shankar committed
637

638 639 640 641 642 643
	directory_blocks = le32_to_cpu(parent_inode->size) >>
		LOG2_BLOCK_SIZE(ext4fs_root);

	block_buffer = zalloc(fs->blksz);
	if (!block_buffer)
		goto fail;
Uma Shankar committed
644

645 646 647
	/* get the block no allocated to a file */
	for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
		blknr = read_allocated_block(parent_inode, blk_idx);
648
		if (blknr <= 0)
Uma Shankar committed
649 650
			goto fail;

651
		/* read the directory block */
652
		status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankar committed
653 654 655 656
					0, fs->blksz, (char *)block_buffer);
		if (status == 0)
			goto fail;

657 658 659 660
		offset = 0;
		do {
			dir = (struct ext2_dirent *)(block_buffer + offset);
			direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankar committed
661

662 663
			int direntlen = le16_to_cpu(dir->direntlen);
			if (direntlen < sizeof(struct ext2_dirent))
Uma Shankar committed
664 665
				break;

666 667 668 669 670 671 672
			if (dir->inode && (strlen(dirname) == dir->namelen) &&
			    (strncmp(dirname, direntname, dir->namelen) == 0)) {
				inodeno = le32_to_cpu(dir->inode);
				break;
			}

			offset += direntlen;
Uma Shankar committed
673

674
		} while (offset < fs->blksz);
675

676 677
		if (inodeno > 0) {
			free(block_buffer);
678
			return inodeno;
679
		}
Uma Shankar committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	}

fail:
	free(block_buffer);

	return -1;
}

static int find_dir_depth(char *dirname)
{
	char *token = strtok(dirname, "/");
	int count = 0;
	while (token != NULL) {
		token = strtok(NULL, "/");
		count++;
	}
	return count + 1 + 1;
	/*
	 * for example  for string /home/temp
	 * depth=home(1)+temp(1)+1 extra for NULL;
	 * so count is 4;
	 */
}

static int parse_path(char **arr, char *dirname)
{
	char *token = strtok(dirname, "/");
	int i = 0;

	/* add root */
	arr[i] = zalloc(strlen("/") + 1);
	if (!arr[i])
		return -ENOMEM;
713
	memcpy(arr[i++], "/", strlen("/"));
Uma Shankar committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

	/* add each path entry after root */
	while (token != NULL) {
		arr[i] = zalloc(strlen(token) + 1);
		if (!arr[i])
			return -ENOMEM;
		memcpy(arr[i++], token, strlen(token));
		token = strtok(NULL, "/");
	}
	arr[i] = NULL;

	return 0;
}

int ext4fs_iget(int inode_no, struct ext2_inode *inode)
{
	if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
		return -1;

	return 0;
}

/*
 * Function: ext4fs_get_parent_inode_num
 * Return Value: inode Number of the parent directory of  file/Directory to be
 * created
 * dirname : Input parmater, input path name of the file/directory to be created
 * dname : Output parameter, to be filled with the name of the directory
 * extracted from dirname
 */
int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
{
	int i;
	int depth = 0;
	int matched_inode_no;
	int result_inode_no = -1;
	char **ptr = NULL;
	char *depth_dirname = NULL;
	char *parse_dirname = NULL;
	struct ext2_inode *parent_inode = NULL;
	struct ext2_inode *first_inode = NULL;
	struct ext2_inode temp_inode;

	if (*dirname != '/') {
		printf("Please supply Absolute path\n");
		return -1;
	}

	/* TODO: input validation make equivalent to linux */
	depth_dirname = zalloc(strlen(dirname) + 1);
	if (!depth_dirname)
		return -ENOMEM;

	memcpy(depth_dirname, dirname, strlen(dirname));
	depth = find_dir_depth(depth_dirname);
	parse_dirname = zalloc(strlen(dirname) + 1);
	if (!parse_dirname)
		goto fail;
	memcpy(parse_dirname, dirname, strlen(dirname));

	/* allocate memory for each directory level */
	ptr = zalloc((depth) * sizeof(char *));
	if (!ptr)
		goto fail;
	if (parse_path(ptr, parse_dirname))
		goto fail;
	parent_inode = zalloc(sizeof(struct ext2_inode));
	if (!parent_inode)
		goto fail;
	first_inode = zalloc(sizeof(struct ext2_inode));
	if (!first_inode)
		goto fail;
	memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
	memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
	if (flags & F_FILE)
		result_inode_no = EXT2_ROOT_INO;
	for (i = 1; i < depth; i++) {
		matched_inode_no = search_dir(parent_inode, ptr[i]);
		if (matched_inode_no == -1) {
			if (ptr[i + 1] == NULL && i == 1) {
				result_inode_no = EXT2_ROOT_INO;
				goto end;
			} else {
				if (ptr[i + 1] == NULL)
					break;
				printf("Invalid path\n");
				result_inode_no = -1;
				goto fail;
			}
		} else {
			if (ptr[i + 1] != NULL) {
				memset(parent_inode, '\0',
				       sizeof(struct ext2_inode));
				if (ext4fs_iget(matched_inode_no,
						parent_inode)) {
					result_inode_no = -1;
					goto fail;
				}
				result_inode_no = matched_inode_no;
			} else {
				break;
			}
		}
	}

end:
	if (i == 1)
		matched_inode_no = search_dir(first_inode, ptr[i]);
	else
		matched_inode_no = search_dir(parent_inode, ptr[i]);

	if (matched_inode_no != -1) {
		ext4fs_iget(matched_inode_no, &temp_inode);
827
		if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankar committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
			printf("It is a Directory\n");
			result_inode_no = -1;
			goto fail;
		}
	}

	if (strlen(ptr[i]) > 256) {
		result_inode_no = -1;
		goto fail;
	}
	memcpy(dname, ptr[i], strlen(ptr[i]));

fail:
	free(depth_dirname);
	free(parse_dirname);
843 844 845 846 847
	for (i = 0; i < depth; i++) {
		if (!ptr[i])
			break;
		free(ptr[i]);
	}
Uma Shankar committed
848 849 850 851 852 853 854
	free(ptr);
	free(parent_inode);
	free(first_inode);

	return result_inode_no;
}

855
static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankar committed
856
{
857 858
	int status;
	int inodeno = 0;
859 860
	int offset;
	char *block_buffer = NULL;
Uma Shankar committed
861
	struct ext2_dirent *dir = NULL;
862
	struct ext2_dirent *previous_dir;
Uma Shankar committed
863
	struct ext_filesystem *fs = get_fs();
864
	int ret = -1;
865
	char *direntname;
Uma Shankar committed
866

867 868
	block_buffer = zalloc(fs->blksz);
	if (!block_buffer)
Uma Shankar committed
869
		return -ENOMEM;
870 871

	/* read the directory block */
872
	status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
873
				fs->blksz, block_buffer);
Uma Shankar committed
874 875 876
	if (status == 0)
		goto fail;

877
	offset = 0;
878 879 880 881 882 883 884 885 886
	do {
		previous_dir = dir;
		dir = (struct ext2_dirent *)(block_buffer + offset);
		direntname = (char *)(dir) + sizeof(struct ext2_dirent);

		int direntlen = le16_to_cpu(dir->direntlen);
		if (direntlen < sizeof(struct ext2_dirent))
			break;

887
		if (dir->inode && (strlen(filename) == dir->namelen) &&
888
		    (strncmp(direntname, filename, dir->namelen) == 0)) {
889 890
			inodeno = le32_to_cpu(dir->inode);
			break;
Uma Shankar committed
891 892
		}

893
		offset += direntlen;
Uma Shankar committed
894

895
	} while (offset < fs->blksz);
Uma Shankar committed
896

897
	if (inodeno > 0) {
898 899 900
		printf("file found, deleting\n");
		if (ext4fs_log_journal(block_buffer, blknr))
			goto fail;
Uma Shankar committed
901

902 903 904 905 906 907 908 909 910 911
		if (previous_dir) {
			/* merge dir entry with predecessor */
			uint16_t new_len;
			new_len = le16_to_cpu(previous_dir->direntlen);
			new_len += le16_to_cpu(dir->direntlen);
			previous_dir->direntlen = cpu_to_le16(new_len);
		} else {
			/* invalidate dir entry */
			dir->inode = 0;
		}
912
		if (ext4fs_put_metadata(block_buffer, blknr))
Uma Shankar committed
913
			goto fail;
914
		ret = inodeno;
Uma Shankar committed
915 916
	}
fail:
917
	free(block_buffer);
Uma Shankar committed
918

919
	return ret;
Uma Shankar committed
920 921
}

922
int ext4fs_filename_unlink(char *filename)
Uma Shankar committed
923
{
924
	int blk_idx;
Uma Shankar committed
925 926
	long int blknr = -1;
	int inodeno = -1;
927 928 929 930
	uint32_t directory_blocks;

	directory_blocks = le32_to_cpu(g_parent_inode->size) >>
		LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankar committed
931 932

	/* read the block no allocated to a file */
933 934
	for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
		blknr = read_allocated_block(g_parent_inode, blk_idx);
935
		if (blknr <= 0)
Uma Shankar committed
936
			break;
937
		inodeno = unlink_filename(filename, blknr);
Uma Shankar committed
938 939 940 941 942 943 944
		if (inodeno != -1)
			return inodeno;
	}

	return -1;
}

945
uint32_t ext4fs_get_new_blk_no(void)
Uma Shankar committed
946 947 948 949 950 951
{
	short i;
	short status;
	int remainder;
	unsigned int bg_idx;
	static int prev_bg_bitmap_index = -1;
952
	unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankar committed
953 954 955 956 957 958 959 960
	struct ext_filesystem *fs = get_fs();
	char *journal_buffer = zalloc(fs->blksz);
	char *zero_buffer = zalloc(fs->blksz);
	if (!journal_buffer || !zero_buffer)
		goto fail;

	if (fs->first_pass_bbmap == 0) {
		for (i = 0; i < fs->no_blkgrp; i++) {
961 962 963 964 965 966 967
			struct ext2_block_group *bgd = NULL;
			bgd = ext4fs_get_group_descriptor(fs, i);
			if (ext4fs_bg_get_free_blocks(bgd, fs)) {
				uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
				uint64_t b_bitmap_blk =
					ext4fs_bg_get_block_id(bgd, fs);
				if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankar committed
968 969
					memcpy(fs->blk_bmaps[i], zero_buffer,
					       fs->blksz);
970 971 972 973
					put_ext4(b_bitmap_blk * fs->blksz,
						 fs->blk_bmaps[i], fs->blksz);
					bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
					ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankar committed
974 975 976 977
				}
				fs->curr_blkno =
				    _get_new_blk_no(fs->blk_bmaps[i]);
				if (fs->curr_blkno == -1)
978
					/* block bitmap is completely filled */
Uma Shankar committed
979 980 981 982
					continue;
				fs->curr_blkno = fs->curr_blkno +
						(i * fs->blksz * 8);
				fs->first_pass_bbmap++;
983
				ext4fs_bg_free_blocks_dec(bgd, fs);
984
				ext4fs_sb_free_blocks_dec(fs->sb);
985 986 987
				status = ext4fs_devread(b_bitmap_blk *
							fs->sect_perblk,
							0, fs->blksz,
Uma Shankar committed
988 989 990 991
							journal_buffer);
				if (status == 0)
					goto fail;
				if (ext4fs_log_journal(journal_buffer,
992
						       b_bitmap_blk))
Uma Shankar committed
993 994 995 996 997 998 999 1000 1001 1002
					goto fail;
				goto success;
			} else {
				debug("no space left on block group %d\n", i);
			}
		}

		goto fail;
	} else {
		fs->curr_blkno++;
1003
restart:
Uma Shankar committed
1004
		/* get the blockbitmap index respective to blockno */
1005 1006
		bg_idx = fs->curr_blkno / blk_per_grp;
		if (fs->blksz == 1024) {
Uma Shankar committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
			remainder = fs->curr_blkno % blk_per_grp;
			if (!remainder)
				bg_idx--;
		}

		/*
		 * To skip completely filled block group bitmaps
		 * Optimize the block allocation
		 */
		if (bg_idx >= fs->no_blkgrp)
			goto fail;

1019 1020 1021
		struct ext2_block_group *bgd = NULL;
		bgd = ext4fs_get_group_descriptor(fs, bg_idx);
		if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankar committed
1022
			debug("block group %u is full. Skipping\n", bg_idx);
1023 1024 1025
			fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
			if (fs->blksz == 1024)
				fs->curr_blkno += 1;
Uma Shankar committed
1026 1027 1028
			goto restart;
		}

1029 1030 1031
		uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
		uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
		if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankar committed
1032
			memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
1033 1034 1035 1036
			put_ext4(b_bitmap_blk * fs->blksz,
				 zero_buffer, fs->blksz);
			bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
			ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankar committed
1037 1038 1039 1040 1041 1042
		}

		if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
				   bg_idx) != 0) {
			debug("going for restart for the block no %ld %u\n",
			      fs->curr_blkno, bg_idx);
1043
			fs->curr_blkno++;
Uma Shankar committed
1044 1045 1046 1047 1048
			goto restart;
		}

		/* journal backup */
		if (prev_bg_bitmap_index != bg_idx) {
1049
			status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankar committed
1050 1051 1052
						0, fs->blksz, journal_buffer);
			if (status == 0)
				goto fail;
1053
			if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankar committed
1054 1055 1056 1057
				goto fail;

			prev_bg_bitmap_index = bg_idx;
		}
1058
		ext4fs_bg_free_blocks_dec(bgd, fs);
1059
		ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankar committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
		goto success;
	}
success:
	free(journal_buffer);
	free(zero_buffer);

	return fs->curr_blkno;
fail:
	free(journal_buffer);
	free(zero_buffer);

	return -1;
}

int ext4fs_get_new_inode_no(void)
{
	short i;
	short status;
	unsigned int ibmap_idx;
	static int prev_inode_bitmap_index = -1;
1080
	unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankar committed
1081 1082 1083 1084 1085
	struct ext_filesystem *fs = get_fs();
	char *journal_buffer = zalloc(fs->blksz);
	char *zero_buffer = zalloc(fs->blksz);
	if (!journal_buffer || !zero_buffer)
		goto fail;
1086 1087
	int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
		EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankar committed
1088 1089 1090

	if (fs->first_pass_ibmap == 0) {
		for (i = 0; i < fs->no_blkgrp; i++) {
1091 1092 1093 1094 1095 1096 1097 1098
			uint32_t free_inodes;
			struct ext2_block_group *bgd = NULL;
			bgd = ext4fs_get_group_descriptor(fs, i);
			free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
			if (free_inodes) {
				uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
				uint64_t i_bitmap_blk =
					ext4fs_bg_get_inode_id(bgd, fs);
1099
				if (has_gdt_chksum)
1100 1101 1102
					bgd->bg_itable_unused = free_inodes;
				if (bg_flags & EXT4_BG_INODE_UNINIT) {
					put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankar committed
1103
						 zero_buffer, fs->blksz);
1104 1105
					bg_flags &= ~EXT4_BG_INODE_UNINIT;
					ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankar committed
1106 1107 1108 1109 1110 1111
					memcpy(fs->inode_bmaps[i],
					       zero_buffer, fs->blksz);
				}
				fs->curr_inode_no =
				    _get_new_inode_no(fs->inode_bmaps[i]);
				if (fs->curr_inode_no == -1)
1112
					/* inode bitmap is completely filled */
Uma Shankar committed
1113 1114 1115 1116
					continue;
				fs->curr_inode_no = fs->curr_inode_no +
							(i * inodes_per_grp);
				fs->first_pass_ibmap++;
1117
				ext4fs_bg_free_inodes_dec(bgd, fs);
1118
				if (has_gdt_chksum)
1119
					ext4fs_bg_itable_unused_dec(bgd, fs);
1120
				ext4fs_sb_free_inodes_dec(fs->sb);
1121 1122 1123
				status = ext4fs_devread(i_bitmap_blk *
							fs->sect_perblk,
							0, fs->blksz,
Uma Shankar committed
1124 1125 1126 1127
							journal_buffer);
				if (status == 0)
					goto fail;
				if (ext4fs_log_journal(journal_buffer,
1128
						       i_bitmap_blk))
Uma Shankar committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
					goto fail;
				goto success;
			} else
				debug("no inode left on block group %d\n", i);
		}
		goto fail;
	} else {
restart:
		fs->curr_inode_no++;
		/* get the blockbitmap index respective to blockno */
		ibmap_idx = fs->curr_inode_no / inodes_per_grp;
1140 1141 1142 1143 1144 1145 1146
		struct ext2_block_group *bgd =
			ext4fs_get_group_descriptor(fs, ibmap_idx);
		uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
		uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);

		if (bg_flags & EXT4_BG_INODE_UNINIT) {
			put_ext4(i_bitmap_blk * fs->blksz,
1147
				 zero_buffer, fs->blksz);
1148 1149
			bg_flags &= ~EXT4_BG_INODE_UNINIT;
			ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankar committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
			memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
				fs->blksz);
		}

		if (ext4fs_set_inode_bmap(fs->curr_inode_no,
					  fs->inode_bmaps[ibmap_idx],
					  ibmap_idx) != 0) {
			debug("going for restart for the block no %d %u\n",
			      fs->curr_inode_no, ibmap_idx);
			goto restart;
		}

		/* journal backup */
		if (prev_inode_bitmap_index != ibmap_idx) {
1164
			status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankar committed
1165 1166 1167 1168
						0, fs->blksz, journal_buffer);
			if (status == 0)
				goto fail;
			if (ext4fs_log_journal(journal_buffer,
1169
						le32_to_cpu(bgd->inode_id)))
Uma Shankar committed
1170 1171 1172
				goto fail;
			prev_inode_bitmap_index = ibmap_idx;
		}
1173
		ext4fs_bg_free_inodes_dec(bgd, fs);
1174
		if (has_gdt_chksum)
1175
			bgd->bg_itable_unused = bgd->free_inodes;
1176
		ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankar committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
		goto success;
	}

success:
	free(journal_buffer);
	free(zero_buffer);

	return fs->curr_inode_no;
fail:
	free(journal_buffer);
	free(zero_buffer);

	return -1;

}


static void alloc_single_indirect_block(struct ext2_inode *file_inode,
					unsigned int *total_remaining_blocks,
					unsigned int *no_blks_reqd)
{
	short i;
	short status;
	long int actual_block_no;
	long int si_blockno;
	/* si :single indirect */
1203 1204
	__le32 *si_buffer = NULL;
	__le32 *si_start_addr = NULL;
Uma Shankar committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	struct ext_filesystem *fs = get_fs();

	if (*total_remaining_blocks != 0) {
		si_buffer = zalloc(fs->blksz);
		if (!si_buffer) {
			printf("No Memory\n");
			return;
		}
		si_start_addr = si_buffer;
		si_blockno = ext4fs_get_new_blk_no();
		if (si_blockno == -1) {
			printf("no block left to assign\n");
			goto fail;
		}
		(*no_blks_reqd)++;
		debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);

1222
		status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankar committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
					0, fs->blksz, (char *)si_buffer);
		memset(si_buffer, '\0', fs->blksz);
		if (status == 0)
			goto fail;

		for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
			actual_block_no = ext4fs_get_new_blk_no();
			if (actual_block_no == -1) {
				printf("no block left to assign\n");
				goto fail;
			}
1234
			*si_buffer = cpu_to_le32(actual_block_no);
Uma Shankar committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
			debug("SIAB %u: %u\n", *si_buffer,
				*total_remaining_blocks);

			si_buffer++;
			(*total_remaining_blocks)--;
			if (*total_remaining_blocks == 0)
				break;
		}

		/* write the block to disk */
1245
		put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankar committed
1246
			 si_start_addr, fs->blksz);
1247
		file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankar committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
	}
fail:
	free(si_start_addr);
}

static void alloc_double_indirect_block(struct ext2_inode *file_inode,
					unsigned int *total_remaining_blocks,
					unsigned int *no_blks_reqd)
{
	short i;
	short j;
	short status;
	long int actual_block_no;
	/* di:double indirect */
	long int di_blockno_parent;
	long int di_blockno_child;
1264 1265 1266 1267
	__le32 *di_parent_buffer = NULL;
	__le32 *di_child_buff = NULL;
	__le32 *di_block_start_addr = NULL;
	__le32 *di_child_buff_start = NULL;
Uma Shankar committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	struct ext_filesystem *fs = get_fs();

	if (*total_remaining_blocks != 0) {
		/* double indirect parent block connecting to inode */
		di_blockno_parent = ext4fs_get_new_blk_no();
		if (di_blockno_parent == -1) {
			printf("no block left to assign\n");
			goto fail;
		}
		di_parent_buffer = zalloc(fs->blksz);
		if (!di_parent_buffer)
			goto fail;

		di_block_start_addr = di_parent_buffer;
		(*no_blks_reqd)++;
		debug("DIPB %ld: %u\n", di_blockno_parent,
		      *total_remaining_blocks);

1286
		status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankar committed
1287 1288
					fs->sect_perblk, 0,
					fs->blksz, (char *)di_parent_buffer);
1289 1290 1291 1292 1293

		if (!status) {
			printf("%s: Device read error!\n", __func__);
			goto fail;
		}
Uma Shankar committed
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
		memset(di_parent_buffer, '\0', fs->blksz);

		/*
		 * start:for each double indirect parent
		 * block create one more block
		 */
		for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
			di_blockno_child = ext4fs_get_new_blk_no();
			if (di_blockno_child == -1) {
				printf("no block left to assign\n");
				goto fail;
			}
			di_child_buff = zalloc(fs->blksz);
			if (!di_child_buff)
				goto fail;

			di_child_buff_start = di_child_buff;
1311
			*di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankar committed
1312 1313 1314 1315 1316
			di_parent_buffer++;
			(*no_blks_reqd)++;
			debug("DICB %ld: %u\n", di_blockno_child,
			      *total_remaining_blocks);

1317
			status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankar committed
1318 1319 1320
						fs->sect_perblk, 0,
						fs->blksz,
						(char *)di_child_buff);
1321 1322 1323 1324 1325

			if (!status) {
				printf("%s: Device read error!\n", __func__);
				goto fail;
			}
Uma Shankar committed
1326 1327 1328 1329 1330 1331 1332 1333
			memset(di_child_buff, '\0', fs->blksz);
			/* filling of actual datablocks for each child */
			for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
				actual_block_no = ext4fs_get_new_blk_no();
				if (actual_block_no == -1) {
					printf("no block left to assign\n");
					goto fail;
				}
1334
				*di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankar committed
1335 1336 1337 1338 1339 1340 1341 1342 1343
				debug("DIAB %ld: %u\n", actual_block_no,
				      *total_remaining_blocks);

				di_child_buff++;
				(*total_remaining_blocks)--;
				if (*total_remaining_blocks == 0)
					break;
			}
			/* write the block  table */
1344
			put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankar committed
1345 1346 1347 1348 1349 1350 1351
				 di_child_buff_start, fs->blksz);
			free(di_child_buff_start);
			di_child_buff_start = NULL;

			if (*total_remaining_blocks == 0)
				break;
		}
1352
		put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankar committed
1353
			 di_block_start_addr, fs->blksz);
1354
		file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankar committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
	}
fail:
	free(di_block_start_addr);
}

static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
					unsigned int *total_remaining_blocks,
					unsigned int *no_blks_reqd)
{
	short i;
	short j;
	short k;
	long int actual_block_no;
	/* ti: Triple Indirect */
	long int ti_gp_blockno;
	long int ti_parent_blockno;
	long int ti_child_blockno;
1372 1373 1374 1375 1376 1377
	__le32 *ti_gp_buff = NULL;
	__le32 *ti_parent_buff = NULL;
	__le32 *ti_child_buff = NULL;
	__le32 *ti_gp_buff_start_addr = NULL;
	__le32 *ti_pbuff_start_addr = NULL;
	__le32 *ti_cbuff_start_addr = NULL;
Uma Shankar committed
1378 1379 1380 1381 1382 1383
	struct ext_filesystem *fs = get_fs();
	if (*total_remaining_blocks != 0) {
		/* triple indirect grand parent block connecting to inode */
		ti_gp_blockno = ext4fs_get_new_blk_no();
		if (ti_gp_blockno == -1) {
			printf("no block left to assign\n");
1384
			return;
Uma Shankar committed
1385 1386 1387
		}
		ti_gp_buff = zalloc(fs->blksz);
		if (!ti_gp_buff)
1388
			return;
Uma Shankar committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406

		ti_gp_buff_start_addr = ti_gp_buff;
		(*no_blks_reqd)++;
		debug("TIGPB %ld: %u\n", ti_gp_blockno,
		      *total_remaining_blocks);

		/* for each 4 byte grand parent entry create one more block */
		for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
			ti_parent_blockno = ext4fs_get_new_blk_no();
			if (ti_parent_blockno == -1) {
				printf("no block left to assign\n");
				goto fail;
			}
			ti_parent_buff = zalloc(fs->blksz);
			if (!ti_parent_buff)
				goto fail;

			ti_pbuff_start_addr = ti_parent_buff;
1407
			*ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankar committed
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
			ti_gp_buff++;
			(*no_blks_reqd)++;
			debug("TIPB %ld: %u\n", ti_parent_blockno,
			      *total_remaining_blocks);

			/* for each 4 byte entry parent create one more block */
			for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
				ti_child_blockno = ext4fs_get_new_blk_no();
				if (ti_child_blockno == -1) {
					printf("no block left assign\n");
1418
					goto fail1;
Uma Shankar committed
1419 1420 1421
				}
				ti_child_buff = zalloc(fs->blksz);
				if (!ti_child_buff)
1422
					goto fail1;
Uma Shankar committed
1423 1424

				ti_cbuff_start_addr = ti_child_buff;
1425
				*ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankar committed
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
				ti_parent_buff++;
				(*no_blks_reqd)++;
				debug("TICB %ld: %u\n", ti_parent_blockno,
				      *total_remaining_blocks);

				/* fill actual datablocks for each child */
				for (k = 0; k < (fs->blksz / sizeof(int));
					k++) {
					actual_block_no =
					    ext4fs_get_new_blk_no();
					if (actual_block_no == -1) {
						printf("no block left\n");
1438 1439
						free(ti_cbuff_start_addr);
						goto fail1;
Uma Shankar committed
1440
					}
1441
					*ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankar committed
1442 1443 1444 1445 1446 1447 1448 1449 1450
					debug("TIAB %ld: %u\n", actual_block_no,
					      *total_remaining_blocks);

					ti_child_buff++;
					(*total_remaining_blocks)--;
					if (*total_remaining_blocks == 0)
						break;
				}
				/* write the child block */
1451 1452
				put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
						      (uint64_t)fs->blksz)),
Uma Shankar committed
1453 1454 1455 1456 1457 1458 1459
					 ti_cbuff_start_addr, fs->blksz);
				free(ti_cbuff_start_addr);

				if (*total_remaining_blocks == 0)
					break;
			}
			/* write the parent block */
1460
			put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankar committed
1461 1462 1463 1464 1465 1466 1467
				 ti_pbuff_start_addr, fs->blksz);
			free(ti_pbuff_start_addr);

			if (*total_remaining_blocks == 0)
				break;
		}
		/* write the grand parent block */
1468
		put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankar committed
1469
			 ti_gp_buff_start_addr, fs->blksz);
1470
		file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
1471 1472
		free(ti_gp_buff_start_addr);
		return;
Uma Shankar committed
1473
	}
1474 1475
fail1:
	free(ti_pbuff_start_addr);
Uma Shankar committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
fail:
	free(ti_gp_buff_start_addr);
}

void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
				unsigned int total_remaining_blocks,
				unsigned int *total_no_of_block)
{
	short i;
	long int direct_blockno;
	unsigned int no_blks_reqd = 0;

	/* allocation of direct blocks */
1489
	for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankar committed
1490 1491 1492 1493 1494
		direct_blockno = ext4fs_get_new_blk_no();
		if (direct_blockno == -1) {
			printf("no block left to assign\n");
			return;
		}
1495
		file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankar committed
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
		debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);

		total_remaining_blocks--;
	}

	alloc_single_indirect_block(file_inode, &total_remaining_blocks,
				    &no_blks_reqd);
	alloc_double_indirect_block(file_inode, &total_remaining_blocks,
				    &no_blks_reqd);
	alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
				    &no_blks_reqd);
	*total_no_of_block += no_blks_reqd;
}

#endif

Uma Shankar committed
1512 1513 1514 1515 1516 1517 1518
static struct ext4_extent_header *ext4fs_get_extent_block
	(struct ext2_data *data, char *buf,
		struct ext4_extent_header *ext_block,
		uint32_t fileblock, int log2_blksz)
{
	struct ext4_extent_idx *index;
	unsigned long long block;
1519
	int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankar committed
1520 1521 1522 1523 1524
	int i;

	while (1) {
		index = (struct ext4_extent_idx *)(ext_block + 1);

1525
		if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1526
			return NULL;
Uma Shankar committed
1527 1528 1529 1530 1531 1532

		if (ext_block->eh_depth == 0)
			return ext_block;
		i = -1;
		do {
			i++;
1533
			if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankar committed
1534
				break;
1535
		} while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankar committed
1536 1537

		if (--i < 0)
1538
			return NULL;
Uma Shankar committed
1539

1540
		block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankar committed
1541 1542
		block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);

1543
		if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1544
				   buf))
Uma Shankar committed
1545 1546
			ext_block = (struct ext4_extent_header *)buf;
		else
1547
			return NULL;
Uma Shankar committed
1548 1549 1550 1551 1552 1553 1554 1555
	}
}

static int ext4fs_blockgroup
	(struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
{
	long int blkno;
	unsigned int blkoff, desc_per_blk;
1556
	int log2blksz = get_fs()->dev_desc->log2blksz;
1557
	int desc_size = get_fs()->gdsize;
Uma Shankar committed
1558

1559
	desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankar committed
1560

1561
	blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankar committed
1562
			group / desc_per_blk;
1563
	blkoff = (group % desc_per_blk) * desc_size;
Uma Shankar committed
1564 1565 1566 1567

	debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
	      group, blkno, blkoff);

1568 1569
	return ext4fs_devread((lbaint_t)blkno <<
			      (LOG2_BLOCK_SIZE(data) - log2blksz),
1570
			      blkoff, desc_size, (char *)blkgrp);
Uma Shankar committed
1571 1572 1573 1574 1575 1576 1577
}

int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
{
	struct ext2_block_group blkgrp;
	struct ext2_sblock *sblock = &data->sblock;
	struct ext_filesystem *fs = get_fs();
1578
	int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankar committed
1579 1580 1581 1582 1583 1584
	int inodes_per_block, status;
	long int blkno;
	unsigned int blkoff;

	/* It is easier to calculate if the first inode is 0. */
	ino--;
1585
	status = ext4fs_blockgroup(data, ino / le32_to_cpu
Uma Shankar committed
1586 1587 1588 1589 1590
				   (sblock->inodes_per_group), &blkgrp);
	if (status == 0)
		return 0;

	inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1591
	blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
1592
	    (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankar committed
1593 1594
	blkoff = (ino % inodes_per_block) * fs->inodesz;
	/* Read the inode. */
1595 1596
	status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
				log2blksz), blkoff,
Uma Shankar committed
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
				sizeof(struct ext2_inode), (char *)inode);
	if (status == 0)
		return 0;

	return 1;
}

long int read_allocated_block(struct ext2_inode *inode, int fileblock)
{
	long int blknr;
	int blksz;
	int log2_blksz;
	int status;
	long int rblock;
	long int perblock_parent;
	long int perblock_child;
	unsigned long long start;
	/* get the blocksize of the filesystem */
	blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1616 1617 1618
	log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
		- get_fs()->dev_desc->log2blksz;

Uma Shankar committed
1619
	if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1620
		long int startblock, endblock;
Uma Shankar committed
1621 1622 1623 1624 1625
		char *buf = zalloc(blksz);
		if (!buf)
			return -ENOMEM;
		struct ext4_extent_header *ext_block;
		struct ext4_extent *extent;
1626
		int i;
1627 1628 1629 1630 1631
		ext_block =
			ext4fs_get_extent_block(ext4fs_root, buf,
						(struct ext4_extent_header *)
						inode->b.blocks.dir_blocks,
						fileblock, log2_blksz);
Uma Shankar committed
1632 1633 1634 1635 1636 1637 1638 1639
		if (!ext_block) {
			printf("invalid extent block\n");
			free(buf);
			return -EINVAL;
		}

		extent = (struct ext4_extent *)(ext_block + 1);

1640 1641 1642 1643 1644 1645
		for (i = 0; i < le16_to_cpu<