Esempio n. 1
0
int
testfs_init_super_block(const char *file, struct super_block **sbp)
{
	struct super_block *sb = malloc(sizeof(struct super_block));
	char block[BLOCK_SIZE];
	int ret, sock;

	if (!sb) {
		return -ENOMEM;
	}

	if ((sock = open(file, O_RDWR)) < 0) {
		return errno;
	} else if ((sb->dev = fdopen(sock, "r+")) == NULL) {
		return errno;
	}

	read_blocks(sb, block, 0, 1);
	memcpy(&sb->sb, block, sizeof(struct dsuper_block));

	ret = bitmap_create(BLOCK_SIZE * INODE_FREEMAP_SIZE * BITS_PER_WORD,
			    &sb->inode_freemap);
	if (ret < 0)
		return ret;
	read_blocks(sb, bitmap_getdata(sb->inode_freemap),
		    sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE);

	ret = bitmap_create(BLOCK_SIZE * BLOCK_FREEMAP_SIZE * BITS_PER_WORD,
			    &sb->block_freemap);
	if (ret < 0)
		return ret;
	read_blocks(sb, bitmap_getdata(sb->block_freemap),
		    sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE);
	inode_hash_init();
	*sbp = sb;

	return 0;
}
Esempio n. 2
0
struct super_block *
testfs_make_super_block(char *file) {
	struct super_block *sb = calloc(1, sizeof(struct super_block));

	if (!sb) {
		EXIT("malloc");
	}
	if ((sb->dev = fopen(file, "w")) == NULL) {
		EXIT(file);
	}
	sb->sb.inode_freemap_start = SUPER_BLOCK_SIZE;
	sb->sb.block_freemap_start = sb->sb.inode_freemap_start +
	INODE_FREEMAP_SIZE;
	sb->sb.csum_table_start = sb->sb.block_freemap_start +
	BLOCK_FREEMAP_SIZE;
	sb->sb.inode_blocks_start = sb->sb.csum_table_start +
	CSUM_TABLE_SIZE;
	sb->sb.data_blocks_start = sb->sb.inode_blocks_start + NR_INODE_BLOCKS;
	sb->sb.modification_time = 0;
	testfs_write_super_block(sb);
	inode_hash_init();
	return sb;
}
Esempio n. 3
0
struct super_block *
testfs_make_super_block(const char *dev, u64 max_fs_blocks)
{
	struct super_block *sb = calloc(1, sizeof(struct super_block));

	if (!sb) {
		EXIT("malloc");
	}
	if ((sb->dev = fopen(dev, "w")) == NULL) {
		EXIT(dev);
	}
	sb->sb.inode_freemap_start = SUPER_BLOCK_SIZE;
	sb->sb.block_freemap_start = sb->sb.inode_freemap_start +
		INODE_FREEMAP_SIZE;
	sb->sb.inode_blocks_start = sb->sb.block_freemap_start +
		BLOCK_FREEMAP_SIZE;
	sb->sb.data_blocks_start = sb->sb.inode_blocks_start + NR_INODE_BLOCKS;
	sb->sb.used_inode_count = 0;
	sb->sb.used_block_count = 0;
	sb->sb.max_fs_blocks = max_fs_blocks;
	testfs_write_super_block(sb);
	inode_hash_init();
	return sb;
}
Esempio n. 4
0
/* returns negative value on error 
 file is name of the disk that was given to testfs.
 this function initializes all the in memory data structures maintained by the sb
 block.
 */
int testfs_init_super_block(const char *file, int corrupt,
		struct super_block **sbp) {
	struct super_block *sb = malloc(sizeof(struct super_block));
	char block[BLOCK_SIZE];
	int ret, sock;

	if (!sb) {
		return -ENOMEM;
	}

	if ((sock = open(file, O_RDWR
#ifndef DISABLE_OSYNC
			| O_SYNC
#endif
			)) < 0) {
		return errno;
	} else if ((sb->dev = fdopen(sock, "r+")) == NULL) {
		return errno;
	}

	// sb->dev type = FILE
	// read from sb into block.
	read_blocks(sb, block, 0, 1);
	// copy only 24 bytes from block corresponding to dsuper_block
	_memcpy(&sb->sb, block, sizeof(struct dsuper_block));

	// 64 * 1 * 8
	// bitmap create will return a inode_bitmap structure.
	// and point sb->inode_freemap to that structure.
	// currently the inode bitmap is all 0.
	// at the end of this function, bitmap is created in memory 
	ret = bitmap_create(BLOCK_SIZE * INODE_FREEMAP_SIZE * BITS_PER_WORD,
			&sb->inode_freemap);
	if (ret < 0)
		return ret;
	// bitmap_getdata returns v -> the byte array containing bit info
	// read_blocks reads sb->v into sb at offset freemap_start till 
	// INODE_FREEMAP_SIZE
	// sb is only sent to read_blocks since we need the sb device handle.
	// data from sb->dev is used to populate arg 2  sb->inode_freemap
	read_blocks(sb, bitmap_getdata(sb->inode_freemap),
			sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE);

	ret = bitmap_create(BLOCK_SIZE * BLOCK_FREEMAP_SIZE * BITS_PER_WORD,
			&sb->block_freemap);
	if (ret < 0)
		return ret;
	read_blocks(sb, bitmap_getdata(sb->block_freemap),
			sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE);
	sb->csum_table = malloc(CSUM_TABLE_SIZE * BLOCK_SIZE);
	if (!sb->csum_table)
		return -ENOMEM;
	read_blocks(sb, (char *) sb->csum_table, sb->sb.csum_table_start,
	CSUM_TABLE_SIZE);
	sb->tx_in_progress = TX_NONE;
	/*
	 inode_hash_init() initializes inode_hash_table of size 256 bytes
	 each entry of the inode table contains a first pointer. each
	 node of the first pointer has a prev pointer and a next pointer.
	 */
	inode_hash_init();
	*sbp = sb;

	return 0;
}