Exemple #1
0
/* Main */
void 
main(){

	/* Init Disco Virtual */
	vDisk = (virtualDisk*)malloc(sizeof(virtualDisk));
	vDisk->disk = (void *)malloc(DISK_SIZE);

	/* Init Global Variables */	
	masterBootRecord * mbr = (masterBootRecord *)malloc(512);
	superblock = (masterBlock*)malloc(512);		
	bitmap = (BM*)calloc(BITMAP_SIZE,1);	
	inodemap = (IM*)calloc(INODEMAP_SIZE,1);
	
	
	
	/* Create & Init File System */
	mbr = (masterBootRecord *)read_disk(vDisk->disk,0,mbr,BLOCK_SIZE,0);

	if ( mbr->existFS == 0 ){
		init_filesystem("Chinux", mbr);
	}else{
		load_filesystem();
	}

	printf("mbr:%d\n",mbr->existFS);
	superblock = read_disk(vDisk->disk,1,superblock,BLOCK_SIZE,0);
	printf("name:%s\nblock:%d\nfreeBlocks:%d\nusedBlocks:%d\n",superblock->name, superblock->blockSize, superblock->freeBlocks, superblock->usedBlocks);
	printf("InodeSize:%d\n",sizeof(iNode));
	printf("Directory:%d\n",sizeof(directoryEntry));//16 Directorios o archivos en bloques directos..

	iNode * nodo = fs_creat_inode(DIRECTORY,777,512,superblock->root);
	fs_insert_inode(nodo);
	iNode * nodo3 = fs_creat_inode(DIRECTORY,737,512,superblock->root);
	fs_insert_inode(nodo3);

	//nodo->iNode_number = 20;
	printf("\n\nTABLA DATOS\n");
	printf("inode-number:%d\n",nodo->iNode_number);		
	printf("mode:%d\n",nodo->mode);
	printf("size:%d\n",nodo->size);
	printf("iden:%d\n",nodo->identifier);

	
	iNode * nodo2 = fs_get_inode(nodo->iNode_number);
	printf("inode-number:%d\n",nodo->iNode_number);		
	printf("mode:%d\n",nodo2->mode);
	printf("size:%d\n",nodo2->size);
	printf("iden:%d\n",nodo2->identifier);

	insert_directory("Hola",superblock->root);
	//ls("asd");
	makeDir("comostas");
	print_directories(superblock->root);
	makeDir("Hola/Comocomo");
	cd("Hola");
	print_directories(current);

	
}
Exemple #2
0
void free_block(uint32_t nr_block)
{
	uint32_t tmp = nr_block / 32;
	uint32_t tmp_bitmap;
	assert(nr_block < BITMAP_SZ/sizeof(uint32_t));
	read_disk(&tmp_bitmap, BITMAP_ST + tmp * sizeof(uint32_t), sizeof(uint32_t));
	uint32_t mask = ~(1 << (31 - (nr_block % 32)));
	tmp_bitmap &= mask;
	read_disk(&tmp_bitmap, BITMAP_ST + tmp * sizeof(uint32_t), sizeof(uint32_t));
}
Exemple #3
0
int Ext2Partition::mount()
{
    EXT2_SUPER_BLOCK sblock;
    int gSizes, gSizeb;		/* Size of total group desc in sectors */
    char *tmpbuf;

    read_disk(handle, &sblock, relative_sect + 2, 2, sect_size);	/* read superBlock of root */
    if(sblock.s_magic != EXT2_SUPER_MAGIC)
    {
        LOG("Bad Super Block. The drive %s is not ext2 formatted.\n", linux_name.c_str());
        return -1;
    }

    if(sblock.s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
    {
        LOG("File system compression is used which is not supported.\n");
    }
    blocksize = EXT2_BLOCK_SIZE(&sblock);
    inodes_per_group = EXT2_INODES_PER_GROUP(&sblock);
    inode_size = EXT2_INODE_SIZE(&sblock);

    LOG("Block size %d, inp %d, inodesize %d\n", blocksize, inodes_per_group, inode_size);
    totalGroups = (sblock.s_blocks_count)/EXT2_BLOCKS_PER_GROUP(&sblock);
    gSizeb = (sizeof(EXT2_GROUP_DESC) * totalGroups);
    gSizes = (gSizeb / sect_size)+1;

    desc = (EXT2_GROUP_DESC *) calloc(totalGroups, sizeof(EXT2_GROUP_DESC));
    if(desc == NULL)
    {
        LOG("Not enough Memory: mount: desc: Exiting\n");
        exit(1);
    }

    if((tmpbuf = (char *) malloc(gSizes * sect_size)) == NULL)
    {
        LOG("Not enough Memory: mount: tmpbuf: Exiting\n");
        exit(1);
    }

    /* Read all Group descriptors and store in buffer */
    /* I really dont know the official start location of Group Descriptor array */
    if((blocksize/sect_size) <= 2)
        read_disk(handle, tmpbuf, relative_sect + ((blocksize/sect_size) + 2), gSizes, sect_size);
    else
        read_disk(handle, tmpbuf, relative_sect + (blocksize/sect_size), gSizes, sect_size);

    memcpy(desc, tmpbuf, gSizeb);

    free(tmpbuf);

    return 0;
}
Exemple #4
0
int Ext2Partition::ext2_readblock(lloff_t blocknum, void *buffer)
{
    char *newbuffer;
    int nsects = blocksize/sect_size;
    int ret;
    lloff_t sectno;

    newbuffer = buffercache.take(blocknum);
    if(!newbuffer)
    {
        newbuffer = new char[blocksize];
        if(!newbuffer)
            return -1;

        if(lvol)
        {
            sectno = lvol->lvm_mapper((lloff_t)nsects * blocknum);
        }
        else
        {
            sectno = (lloff_t)((lloff_t)nsects * blocknum) + relative_sect;
        }
        ret = read_disk(handle, newbuffer, sectno, nsects, sect_size);
        if(ret < 0)
        {
            delete [] newbuffer;
            return ret;
        }
    }

    memcpy(buffer, newbuffer, blocksize);
    buffercache.insert(blocknum, newbuffer, 1);
    return 0;
}
Exemple #5
0
uint32_t apply_block()
{
	int i, j;
	static int cur_st = 0;
	while(cur_st < BITMAP_SZ / sizeof(uint32_t))
	{
		for(i = 0; i < sizeof(bitmap)/sizeof(uint32_t); i++)
		{
			if(bitmap[i] != 0xffffffff)
			{
				for(j = 31; j >= 0; j--)
				{
					if((1 & (bitmap[i] >> j)) == 0)
					{
						uint32_t mask = 1 << j;
						bitmap[i] |= mask;
						return cur_st * 32 + i * 32 + (31 - j);
					}
				}
			}
		}
		write_disk(bitmap, BITMAP_ST + cur_st * sizeof(uint32_t), sizeof(bitmap));
		cur_st += sizeof(bitmap)/sizeof(uint32_t);
		read_disk(bitmap, BITMAP_ST + cur_st * sizeof(uint32_t), sizeof(bitmap));
	}
	assert(0);
	return 0xffffffff;
}
Exemple #6
0
int recursive_remove(iNode * current) {

	int ret;

	if (current->gid < currentUsr.group)
		return 1;

	if (is_base_case(current)) //CASOBASE QUE ES QUE EL DIRECTORIO ESTE VACIO O SEA UN ARCHIVO)
		return 0;

	int init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*) calloc(sizeof(directoryEntry), 96);
	read_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);
	int i;
	for (i = 2; i < 96; i++) {
		if (dr[i].type != 0) {
			ret = recursive_remove(fs_get_inode(dr[i].inode));
			if (!ret) {
				dr[i].type = 0;
				dr[i].inode = 0;
				dr[i].lenght = 0;
			}

		}
	}
	write_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);

	return ret;
}
Exemple #7
0
/*** Load the user program to memory ***/
bool load_disk_to_memory(uint32_t LBA, uint32_t n_sectors, uint8_t *mem) {
	uint8_t status;
	uint16_t read_count = 0;

	// read up to 256 sectors at a time
	for (;n_sectors>0;) {
		read_count = (n_sectors>=256?256:n_sectors);

		status = read_disk(LBA,(read_count==256?0:read_count),mem);
		
		if (status == DISK_ERROR_LBA_OUTSIDE_RANGE
			|| status == DISK_ERROR_SECTORCOUNT_TOO_BIG) 
			return FALSE;

		else if (status == DISK_ERROR || status == DISK_ERROR_ERR
				|| status == DISK_ERROR_DF) 
			return FALSE;

		n_sectors -= read_count;
		LBA += read_count;
		mem += 512*read_count;
	}

	return TRUE;
}
Exemple #8
0
/* Scans The partitions */
int scan_partitions(struct disk_info *dsk)
{
    static unsigned char sector[SECTOR_SIZE];
    struct MBRpartition *part;
    struct partition_info *current = NULL;
    int sector_size;
    int ret, i;

    ret = read_disk(dsk->fd, sector, 0, 1, dsk->sector_size);
    if(ret < 0)
        return ret;

    if(ret < sector_size)
    {
        LOG_INFO("Error Reading the MBR on %s \n", path);
        return -1;
    }

    if(!valid_part_table_flag(sector))
    {
        LOG_INFO("Partition Table Error on %s\n", dsk->device_file);
        LOG_INFO("Invalid End of sector marker");
        return -INVALID_TABLE;
    }

    /* First Scan primary Partitions */
    for(i = 0; i < 4; i++)
    {
        part = pt_offset(sector, i);
        if((part->sys_ind != 0x00) || (get_nr_sects(part) != 0x00))
        {
            LOG_INFO("index %d ID %X size %Ld \n", i, part->sys_ind, get_nr_sects(part));

            if(!dsk->partition) {
                current = malloc(sizeof(struct partition_info));
                memset(current, 0, sizeof(struct partition_info));
                dsk->partition = current;
            } else {
                current->next = malloc(sizeof(struct partition_info));
                memset(current->next, 0, sizeof(struct partition_info));
                current = current->next;
            }
            current->fstype = part->sys_ind;
            current->base = get_start_sect(part);
            current->size = get_nr_sects(part) * dsk->sector_size;
            if(is_extended(part->sys_ind)) {
                current->flag = PARTITION_TYPE_EXTENDED;
                scan_ebr(dsk, current);
            } else {
                current->flag = PARTITION_TYPE_PRIMARY;
            }
        }
    }

    return 0;
}
Exemple #9
0
void cp(char * filename, char * path)
{
	int i, name_length, type, ret, j;
	filename[str_len(filename) - 1] = 0;
	iNode * path_inode = current;
	path_inode = parser_path(path, path_inode);
	iNode * filename_inode = current;
	filename_inode = parser_path(filename, filename_inode);

	if(filename_inode->gid < currentUsr.group)
	{
		printf("\nCan not copy '%s'. Permission denied.", filename);
		return ;
	}

	if(filename_inode == NULL)
	{
		printf("\nCan not copy '%s'. File doesn't exist.", filename);
		return ;
	}

	if(path_inode == NULL)
	{
		name_length = str_len(path);
		for(i = 0; i < name_length; i++)
			if(path[i] == '/')
			{
				printf("\nCan not copy '%s' to '%s'. Directory doesn't exist.", filename, path);
				return;
			}
		rename_file(filename_inode->iNode_number, path);
		return ;
	}

	int init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(64 * 96, 1);
	read_disk(0, init_block, dr, (BLOCK_SIZE * 12), 0);
	for(i = 1; i < 96; i++){
		if( strcmp(filename, dr[i].name) == 1){
			type = dr[i].type;
			break;
		}
	}

	if(type == FILE)
	{
		cp_file(filename, filename_inode, path_inode);
	}
	else if(type == DIRECTORY)
	{
		recursive_cp(filename, filename_inode, path_inode);
	}

	return ;
}
Exemple #10
0
// Format: diskdump [start LBA] [sector count]
// Displays content of <sector count> number of sectors
// starting from <start LBA>
void command_diskdump(char *args) {
	uint8_t a_sector[512];
	uint8_t status;
	uint32_t LBA;
	uint32_t n_sectors;
	
	int i;
	
	// get start LBA
	if (*args==0 || *args==' ') {
		puts("Usage: diskdump [start LBA] [sector count]\n");
		return;
	}
	if (is_pos_number(args)==FALSE) {
		puts("diskdump: Invalid start LBA.\n");
		return;
	}
	LBA = atoi(args);

	// get sector count
	while (*args!=0 && *args!=' ') args++;	// goto end of first argument
	args++;					// second argument from next position
	if (*args==0 || *args==' ') {
		puts("Usage: diskdump [start LBA] [sector count]\n");
		return;
	}
	if (!is_pos_number(args)) {
		puts("diskdump: Invalid sector count.\n");
		return;
	}
	n_sectors = atoi(args);

	// read one sector at a time and display
	for (; n_sectors>0; n_sectors--,LBA++) {
		status = read_disk(LBA,1,a_sector);
		
		if (status == DISK_ERROR_LBA_OUTSIDE_RANGE
			|| status == DISK_ERROR_SECTORCOUNT_TOO_BIG) {
			puts("diskdump: LBA out of range.\n");
			return;
		}
		else if (status == DISK_ERROR || status == DISK_ERROR_ERR
				|| status == DISK_ERROR_DF) {
			puts("diskdump: Disk read error.\n");
			return;
		}
		// display the 512 bytes of the sector in hex notation
		for (i=0; i<512; i++) {
			sys_printf("%x%x ",a_sector[i]>>4,a_sector[i]&0x0F);
			if ((i+1)%16==0) putc('\n'); // new line every 16 bytes
		}
		puts("\n");
	}
}
Exemple #11
0
/* Reads The Extended Partitions */
int scan_ebr(struct disk_info *dsk, struct partition_info *ext)
{
    static unsigned char sector[SECTOR_SIZE];
    struct MBRpartition *part, *part1;
    struct partition_info *current = NULL, *pt;
    int logical = 4, ret;
    lloff_t  ebrBase, nextPart, ebr2=0;

    ebrBase = ext->base;
    nextPart = ext->base;
    while (1) {
        ret = read_disk(dsk->fd, sector, nextPart, 1, dsk->sector_size);
        if (ret < 0)
            return ret;

        if (ret < dsk->sector_size) {
            LOG_INFO("Error Reading the EBR \n");
            return -1;
        }
        part = pt_offset(sector, 0);
        LOG_INFO("index %d ID %X size %Ld \n", logical, part->sys_ind, get_nr_sects(part));

        if (is_extended(part->sys_ind)) {
            // special case. ebr has extended partition with offset to another ebr.
            ebr2 += get_start_sect(part);
            nextPart = (ebr2 + ebrBase);
            continue;
        }
        part1 = pt_offset(sector, 1);
        ebr2 = get_start_sect(part1);
        nextPart = (ebr2 + ebrBase);

        pt = malloc(sizeof(struct partition_info));
        if (!pt) {
            LOG_INFO("Allocation Error\n");
        }
        memset(pt, 0, sizeof(struct partition_info));
        if (!current) {
            current = pt;
            ext->ext = pt;
        } else {
            current->next = pt;
            current = current->next;
        }
        pt->fstype = part->sys_ind;
        pt->base = get_start_sect(part);
        pt->size = get_nr_sects(part) * dsk->sector_size;
        logical++;
        if (part1->sys_ind == 0)
            break;
    }
    return logical;
}
Exemple #12
0
unsigned int get_next_clu(unsigned int clu)
{
	int *point, i, value;
	point = (int *) kmalloc(512);
	i = ((clu / 128) + PBR1.FAT_start);/*一个FAT扇区512字节,有128个簇表项*/
	read_disk(i, (unsigned short int*) point, 1);
	debug(point, 512);
	io_hlt();
	value = point[(clu % 128)];
	kfree(point, 512);
	return value;
}
Exemple #13
0
int fs_read(INODE *pinode, uint32_t off, uint32_t size, void *buf)
{
	buf = (uint8_t *)buf;
	int i, j, tsize, ed;
	uint32_t stblockno, edblockno;
	uint32_t block_addr[64];
	uint32_t stno = off / BLOCKSZ, edno = (off + size - 1) / BLOCKSZ;
	if(off >= pinode->filesz || size == 0)
		return 0;
	size = min(pinode->filesz, off + size) - off;
	ed = off + size;

	/* read the first block(given that some read operation on bytes less than 4096) */
	tsize = min(BLOCKSZ - off % BLOCKSZ, size);
	get_disk_blockno(pinode, stno, 1, &stblockno);
	read_disk(buf, FILE_ST + stblockno * BLOCKSZ + off % BLOCKSZ, tsize);
	buf += tsize;
	off += tsize;
	size -= tsize;
	for(i = stno + 1; i < edno; i += 64)
	{
		int t = get_disk_blockno(pinode, i, min(64, edno - i), block_addr);
		for(j = 0; j < t; j++)
		{
			read_disk(buf, FILE_ST + block_addr[j] * BLOCKSZ, BLOCKSZ);
			buf += BLOCKSZ;
			off += BLOCKSZ;
			size -= BLOCKSZ;
		}
	}
	/* read the last block */
	tsize = ed - off;
	get_disk_blockno(pinode, edno, 1, &edblockno);
	read_disk(buf, FILE_ST + edblockno * BLOCKSZ + off % BLOCKSZ, tsize);
	buf += tsize;
	off += tsize;
	size -= tsize;

	return size;
}
Exemple #14
0
void recursive_cp(char * filename, iNode * origin, iNode * destination)
{
	int i;
	iNode * path;

	if(origin->gid < currentUsr.group)
		return ;

	cp_dir(filename, destination);

	//get new path
	int init_block = destination->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(64 * 96, 1);
	read_disk(0, init_block, dr, (BLOCK_SIZE * 12), 0);
	for(i = 2; i < 96; i++){
		if( strcmp(filename, dr[i].name)){
			path = fs_get_inode(dr[i].inode);
			break;
		}
	}

	//search for files and folders
	init_block = origin->data.direct_blocks[0];
	dr = (directoryEntry*)calloc(sizeof(directoryEntry), 96);
	read_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);
	for ( i = 2; i < 96; i++)
	{
		if (dr[i].type == FILE)
		{
			cp_file(dr[i].name, origin, path);
		}
		else if(dr[i].type == DIRECTORY)
		{
			recursive_cp(dr[i].name, fs_get_inode(dr[i].inode), path);
		}
	}
	write_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);

	return ;
}
Exemple #15
0
/**********************************************
Starting point of the whole OS
*************************************************/
int
kmain()
{
	int i, h;
	char * buffer = calloc(512 , 1);
	_Cli();
	k_clear_screen();
	printf("screen clear\n");
	//cache_initarray();
	printf("array init\n");
	//cache_sortarray();
	printf("array sort\n");

	initializeSemaphoreTable();
	printf("semaphore init\n");
	initializeIDT();
	unmaskPICS();
	initializePaging();
	_StartCR3();
	SetupScheduler();
	//printf("after SetupScheduler\n");

	for(h = 0; h < 200; h++){
		write_disk(0,h,buffer,BLOCK_SIZE,0);
	}

	fd_table = (filedescriptor *)calloc(100,1);
	masterBootRecord * mbr = (masterBootRecord *)malloc(512);
	superblock = (masterBlock*)malloc(512);		
	bitmap = (BM*)calloc(BITMAP_SIZE,1);	
	inodemap = (IM*)calloc(INODEMAP_SIZE,1);
	
	read_disk(0,0,mbr,BLOCK_SIZE,0);

	if ( mbr->existFS == 0 ){
		init_filesystem("Chinux", mbr);
	}else{
		load_filesystem();
	}
	
	ready = NULL;
	for(i = 0; i < 4; i++)
		startTerminal(i);

	//free(mbr);
	logPID = CreateProcessAt("Login", (int(*)(int, char**))logUser, 0, 0, (char**)0, PAGE_SIZE, 4, 1);
	_Sti();

	while(TRUE)
	;
	return 1;
}
Exemple #16
0
iNode * search_directory(char * name, iNode * actual_node) {

	int init_block = actual_node->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*) calloc(64 * 96, 1);
	read_disk(0, init_block, dr, (BLOCK_SIZE * 12), 0);
	int i;
	for (i = 1; i < 96; i++) {
		if (strcmp(name, dr[i].name) == 1) {
			return fs_get_inode(dr[i].inode);
		}
	}
	return NULL;
}
Exemple #17
0
iNode * search_directory(char * name, iNode * actual_node){
	int init_block = actual_node->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(sizeof(directoryEntry),96);
	read_disk(vDisk->disk,init_block,dr,BLOCK_SIZE*12,0);
	
	int i;
	for(i=0;i<96;i++){
		if( strcmp(name,dr[i].name) == 0){
			return fs_get_inode(dr[i].inode);
		}
	}
	return NULL;
}
Exemple #18
0
void inti_FAT32(void)
{
	char *point;
	point = (char *) kmalloc(512);
	read_disk(LBA_start, (unsigned short int*) point, 1);
	/*拷贝直接得到的数据*/
	kmemcpy((point + 0x03), &PBR1.OEM, 8);
	kmemcpy((point + 0x0d), &PBR1.cluster_size, 1);
	kmemcpy((point + 0x0e), &PBR1.reserve, 2);
	kmemcpy((point + 0x10), &PBR1.FAT_num, 1);
	kmemcpy((point + 0x11), &PBR1.root_max_num, 2);
	kmemcpy((point + 0x24), &PBR1.FAT_size, 4);
	kmemcpy((point + 0x2c), &PBR1.root_start, 4);
	/*算出间接提供的数据*/
	PBR1.FAT_start = LBA_start + PBR1.reserve;
	/*加载FAT*/
	PBR1.FAT_addr = (int *) kmalloc(PBR1.FAT_size * 512);
	printk("reading the FAT.\n");
	read_disk(PBR1.FAT_start, (unsigned short int*) PBR1.FAT_addr, PBR1.FAT_size);
	printk("OEM:%s,Cluster size:%X,reserve:%X,root max number:%X,root start:%X\n", &PBR1.OEM, PBR1.cluster_size, PBR1.reserve, PBR1.root_max_num, PBR1.root_start);
	printk("FAT32 start in:0x%X\n",PBR1.FAT_start);
	
}
Exemple #19
0
int read_inode(iNode * inode, char * buf, int n) {

	int file_size = inode->size;
	int newrequeried_blocks = inode->data.direct_blocks[1]
			+ (int) (n / BLOCK_SIZE) + 1;
	int i, lastblock;
	int init_block = inode->data.direct_blocks[0];
	int quantity = inode->data.direct_blocks[1];

	char * receive_buffer = (char *) malloc(quantity * 512);

	if (n < (quantity * BLOCK_SIZE)) {
		read_disk(0, init_block, receive_buffer, quantity * 512, 0);
		memcpy(buf, receive_buffer, n);
	} else {
		read_disk(0, init_block, receive_buffer, quantity * 512, 0);
		memcpy(buf, receive_buffer, n);

	}

	return n;

}
Exemple #20
0
void print_directories(iNode * current){
	
	int init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(sizeof(directoryEntry),96);
	read_disk(vDisk->disk,init_block,dr,BLOCK_SIZE*12,0);
	
	int i;
	for(i=0;i<96;i++){
		if( dr[i].type != 0){
		printf("%s ", dr[i].name);
		}
	}
	putchar(10);
	return;
}
Exemple #21
0
void rename_file(int iNode_number, char * new_name)
{
	int i, init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(sizeof(directoryEntry), 96);
	read_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);
	for (i = 0; i < 96; i++){
		if ( dr[i].inode == iNode_number){
			memcpy(dr[i].name, new_name, str_len(new_name));
			break;
		}
	}
	write_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);

	return;
}
Exemple #22
0
int is_base_case(iNode * current) {

	if (current->identifier != DIRECTORY) {
		return 1;
	}
	int init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*) calloc(sizeof(directoryEntry), 96);
	read_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);
	int i;
	for (i = 2; i < 96; i++) {
		if (dr[i].type != 0) {
			return 0;
		}
	}
	return 1;
}
Exemple #23
0
INODE *open_inode(uint32_t inodeno)
{
	int i;
	assert(inodeno < NR_INODE);
	for(i = 0; i < sizeof(inode) / sizeof(inode[0]); i++)
	{
		if(!inode_dirty[i])
		{
			inode_dirty[i] = 1;
			read_disk(&inode[i], INODE_ST + inodeno * sizeof(INODE), sizeof(INODE));
			return &inode[i];
		}
	}
	assert(0);
	return NULL;
}
Exemple #24
0
iNode * fs_get_inode(int number){

	int sector = number/4;
	int offset = number%4;
	iNode * ret = (iNode*)malloc(sizeof(iNode));
	void * recieve = malloc(512);
	
	if ( get_bit(number, INODEMAP) != 0 ){
		recieve = read_disk(vDisk->disk,INODETABLESECTOR + sector,recieve,BLOCK_SIZE,0);
		memcpy(ret,recieve + (128*offset),128);
	}else{
		return NULL;
	}

	return ret;
}
Exemple #25
0
int fs_insert_inode(iNode * node) {

	int number = node->iNode_number;
	int sector = number / 4;
	int offset = number % 4;
	void * receive = (void *) malloc(BLOCK_SIZE);

	if (get_bit(number, INODEMAP) == 0) {
		set_bit(number, INODEMAP);
	}

	read_disk(0, INODETABLESECTOR + sector, receive, BLOCK_SIZE, 0);
	memcpy(receive + (128 * offset), node, 128); //+(128*offset)
	write_disk(0, INODETABLESECTOR + sector, receive, BLOCK_SIZE, 0);

	return number;
}
Exemple #26
0
void print_directories(iNode * current) {

	int init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*) calloc(sizeof(directoryEntry), 96);
	read_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);

	int i;
	for (i = 0; i < 96; i++) {
		if (dr[i].type != 0
				&& (dr[i].name[0] != '.' || !dr[i].name[1]
						|| (dr[i].name[1] == '.' && !dr[i].name[2]))) {
			printf("%s ", dr[i].name);
		}
	}
	printf("\n");
	return;
}
Exemple #27
0
void insert_directory_entry(iNode * newDirectory, iNode * current, char * name){
	int init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(sizeof(directoryEntry),96);
	read_disk(vDisk->disk,init_block,dr,BLOCK_SIZE*12,0);
	int i;
	for ( i = 0; i < 96; i++){
		if ( dr[i].type == 0 ){
			dr[i].type = DIRECTORY;
			dr[i].inode = newDirectory->iNode_number;
			dr[i].lenght = 0;
			memcpy(dr[i].name,name,strlen(name));
			break;
		}
	}
	write_disk(vDisk->disk,init_block,dr,BLOCK_SIZE*12,0);

	return;
	
}
Exemple #28
0
 uint32_t TestPeripheral::send_command(uint32_t command)
 {
     switch (command)
     {
         default:
         case UNKNOWN:
             std::cout << "Unknown test peripheral command: " << command << "\n";
             return -1;
         case MEMORY_REQUEST:
             return 32;
         case STATUS:
             uint_data(0, 1);
             return 0;
         case READ:
             return read_disk();
         case WRITE:
             return write_disk();
     }
     return 0;
 }
Exemple #29
0
int fs_insert_inode(iNode * node){
	
	int number = node->iNode_number;
	int sector = number/4;
	int offset = number%4;
	iNode * node2 = malloc(sizeof(iNode));
	void * recieve = malloc(BLOCK_SIZE);
	void * recieve2 = malloc(BLOCK_SIZE);
	
	if ( get_bit(number, INODEMAP) == 0 ){
		set_bit(number,INODEMAP);	
	}

	recieve = read_disk(vDisk->disk,INODETABLESECTOR+sector,recieve, BLOCK_SIZE,0);
	memcpy(recieve+(128*offset),node,128);//+(128*offset)
	write_disk(vDisk->disk,INODETABLESECTOR+sector,recieve,BLOCK_SIZE,0);
	
	//recieve2 = read_disk(vDisk->disk,INODETABLESECTOR+sector,recieve2, BLOCK_SIZE,0);
	//memcpy(node2,recieve2,128);

	return number;
}
Exemple #30
0
int write_inode(iNode * inode, char * buf, int n) {

	int file_size = inode->size;
	int posible_requeried = (int) ((file_size + n) / BLOCK_SIZE) + 1;
	int newrequeried_blocks, freeblock;
	if (posible_requeried > inode->data.direct_blocks[1]) {
		newrequeried_blocks = posible_requeried;
		freeblock = search_free_blocks(newrequeried_blocks);
	} else {
		freeblock = inode->data.direct_blocks[0];
		newrequeried_blocks = inode->data.direct_blocks[1];
	}

	int i, lastblock;
	int init_block = inode->data.direct_blocks[0];
	int quantity = inode->data.direct_blocks[1];

	if (freeblock != -1) {
		char * buffer = (char *) malloc(quantity * 512);
		char * insert_buffer = (char *) malloc(newrequeried_blocks * 512);
		read_disk(0, init_block, buffer, quantity * BLOCK_SIZE,0);

		memcpy(insert_buffer, buffer, quantity * 512);
		memcpy((insert_buffer + file_size), buf,n);
		write_disk(0, freeblock, insert_buffer, (newrequeried_blocks * 512), 0);

		inode->data.direct_blocks[0] = freeblock;
		inode->data.direct_blocks[1] = newrequeried_blocks;
		inode->size = file_size + n;

		free_used_blocks(init_block, quantity, BITMAP);

		fs_insert_inode(inode);
	}

	return n;
}