예제 #1
0
void myformat(int size) {
  // Do not touch or move this function
  dcreate_connect();
  
  // Calculate number of data blocks, FAT entries.
  int num_dblocks = get_num_dblocks(size);

  // Format volume control block.
  vcb volblock = make_volblock(num_dblocks);
  char vcbtemp[BLOCKSIZE];
  memset(vcbtemp,0,BLOCKSIZE);
  memcpy(vcbtemp,&volblock,sizeof(vcb));
  dwrite(0,vcbtemp);
  
  // Format directory entries.
  dirent dent = make_directent();
  char dirtemp[BLOCKSIZE]; //FIXME: Eventually will have to be smaller.
  memset(dirtemp,0,BLOCKSIZE);
  memcpy(dirtemp,&dent, sizeof(dirent));
  
  for(int i = 1; i < 101; i++){
    dwrite(i,dirtemp);
  }




/*
  dirent dent;

  char dirblock[BLOCKSIZE];
  memset(dirblock,0,BLOCKSIZE);
  memcpy(dirblock,&dirent,sizeof(dirblock));
  for(int i = 1; i <= 100; i++){
   dwrite(i,dirblock);
  }  */

  /* 3600: FILL IN CODE HERE.  YOU SHOULD INITIALIZE ANY ON-DISK
           STRUCTURES TO THEIR INITIAL VALUE, AS YOU ARE FORMATTING
           A BLANK DISK.  YOUR DISK SHOULD BE size BLOCKS IN SIZE. */

  /* 3600: AN EXAMPLE OF READING/WRITING TO THE DISK IS BELOW - YOU'LL
           WANT TO REPLACE THE CODE BELOW WITH SOMETHING MEANINGFUL. */


/*
  // first, create a zero-ed out array of memory  
  char *tmp = (char *) malloc(BLOCKSIZE);
  memset(tmp, 0, BLOCKSIZE);

  // now, write that to every block
  for (int i=0; i<size; i++) 
    if (dwrite(i, tmp) < 0) 
      perror("Error while writing to disk");
*/
  // voila! we now have a disk containing all zeros

  // Do not touch or move this function
  dunconnect();
}
예제 #2
0
파일: muicfs.c 프로젝트: agentzh2m/SeaIceFS
/*
 * Given an absolute path to a file (for example /a/b/myFile), xmp_create
 * will create a new file named myFile in the /a/b directory.
 * Ignore the mode parameter as we are not implementing
 * the permissions. Also ignore the rdev parameter.
 * The steps your implementation will do to make a new
 * file will be the following: (similar to the xmp_mkdir except the foruth step)
 *
 * 1. Resolve the directory path
 *
 * 2. Allocate and initialize a new directory entry
 *
 * 3. Update the directory entry for 'path' by adding an entry for
 *    the newly created file.
 *
 *
 * You should return 0 on success or -1 on error (e.g., the path doesn't
 * exist, or file name already exists).
 */
static int xmp_create(const char *path, mode_t mode, dev_t rdev)
{
	int* tuple = traverse_dir_w(path);
	printf("tup 0: %d, tup 1: %d \n", tuple[1], tuple[0]);
	int path_data = tuple[1];
	int path_inode = tuple[0];
	//free(tuple);
	printf("dir_amt is: %d \n", DIR_AMT);
	Directory *dir_buf = (Directory*)malloc(sizeof(Directory)*DIR_AMT);
	Directory *init_dbuf = dir_buf;
	DEBUG_ME;
	char* last_path;
	int free_inum = assign_bitmap(IMAP_OFFSET);
	DEBUG_ME;
	printf("Assigning inum %d \n", free_inum);
	for(char* path_pt = strtok(path,"/"); path_pt!=NULL; path_pt = strtok(NULL,"/")){
		last_path = path_pt;
	}
	printf("Start read at blck %d \n", path_data );
	if(dread(global_fd, path_data + DATA_OFFSET, init_dbuf) < 0){
		RFAIL;
		return -1;
	}
	//assign file to free dir
	for(int i = 0; i < DIR_AMT; i++){
		printf("finding free dir right now at %s \n", dir_buf->f_name);
		if(strlen(dir_buf->f_name) < 1){
			printf("Assigning %s at dir \n", last_path);
			strcpy(dir_buf->f_name, last_path);
			dir_buf->inode_num = free_inum;
			break;
		}
		dir_buf++;
	}
	if(dwrite(global_fd, path_data + DATA_OFFSET, init_dbuf) < 0){
		RFAIL;
		return -1;
	}
	//free(init_dbuf);
	//configure inode according to the new inum
	Inode *ibuf = (Inode*)malloc(sizeof(Inode)*INODE_AMT);
	Inode *init_ibuf = ibuf;
	if(dread(global_fd, free_inum/INODE_AMT + INODE_OFFSET, init_ibuf) < 0){
		RFAIL;
		return -1;
	}
	ibuf+= free_inum%INODE_AMT;
	ibuf->f_size = 0;
	ibuf->f_type = 0;
	ibuf->total_blck = 0;
	if(dwrite(global_fd, free_inum/INODE_AMT + INODE_OFFSET, init_ibuf) < 0){
		RFAIL;
		return -1;
	}
	//free(init_ibuf);
	return 0;

}
예제 #3
0
파일: mkfs.c 프로젝트: JamesLinus/FUZIX
void mkfs(uint16_t fsize, uint16_t isize)
{
	uint16_t j;

	/* Zero out the blocks */

	for (j = 0; j < fsize; ++j)
		dwrite(j, zero512);

	/* Initialize the super-block */

	fs_super.s_mounted = swizzle16(SMOUNTED);	/* Magic number */
	fs_super.s_isize = swizzle16(isize);
	fs_super.s_fsize = swizzle16(fsize);
	fs_super.s_nfree = swizzle16(1);
	fs_super.s_free[0] = 0;
	fs_super.s_tfree = 0;
	fs_super.s_ninode = 0;
	fs_super.s_tinode = swizzle16(8 * (isize - 2) - 2);

	/* Free each block, building the free list */
	for (j = fsize - 1; j >= isize + 1; --j) {
		int n;
		if (swizzle16(fs_super.s_nfree) == 50) {
			dwrite(j, (char *) &fs_super.s_nfree);
			fs_super.s_nfree = 0;
		}

		fs_super.s_tfree =
		    swizzle16(swizzle16(fs_super.s_tfree) + 1);
		n = swizzle16(fs_super.s_nfree);
		fs_super.s_free[n++] = swizzle16(j);
		fs_super.s_nfree = swizzle16(n);
	}

	/* The inodes are already zeroed out */
	/* create the root dir */
	inode[ROOTINODE].i_mode = swizzle16(F_DIR | (0777 & MODE_MASK));
	inode[ROOTINODE].i_nlink = swizzle16(3);
	inode[ROOTINODE].i_size = swizzle32(64);
	inode[ROOTINODE].i_addr[0] = swizzle16(isize);

	/* Reserve reserved inode */
	inode[0].i_nlink = swizzle16(1);
	inode[0].i_mode = ~0;

	dwrite(2, (char *) inode);

	dirbuf[0].d_ino = swizzle16(dirbuf[0].d_ino);
	dirbuf[1].d_ino = swizzle16(dirbuf[1].d_ino);
	dwrite(isize, (char *) dirbuf);

	/* Write out super block */
	dwrite(1, (char *) &fs_super);
}
예제 #4
0
void myformat(int size) {
  // Do not touch or move this function
  dcreate_connect();

  //setting up vcb
  vcb myvcb;
  initvcb(&myvcb, size);
  
  dirent myde;
  initde(&myde);

  fatent myfat = { 0, 0, 0 }; //like 20% bruh
  char tmp[BLOCKSIZE];
  int block = 0; //current block to write to
  
  //write vcb to file
  memset(tmp, 0, BLOCKSIZE);
  memcpy(tmp, &myvcb, sizeof(vcb));
  dwrite(block,tmp);
  block++;

  //write directory entries to file
  for(int i=0; i<25; i++){
    memset(tmp, 0, BLOCKSIZE);
    memcpy(tmp, &myde, sizeof(dirent));
    memcpy(tmp + 128, &myde, sizeof(dirent));
    memcpy(tmp + 256, &myde, sizeof(dirent));
    memcpy(tmp + 384, &myde, sizeof(dirent));
    if(dwrite(block,tmp)< 0)
      perror("Error while writing to disk");
    block++;
  }

  //write fatents to file
  for (int i=0; i< myvcb.fat_length; i++){ 
    memset(tmp, 0, BLOCKSIZE);
    for(int x=0; x<128; x++){
      memcpy(tmp + (4 * x), &myfat, sizeof(fatent));
    }
    if (dwrite(block, tmp) < 0) 
      perror("Error while writing to disk");
    block++;
  }
  
  //0 out diskspace where there will be data(not really needed..)
  for (int i=0; i<size; i++){ 
    if (dwrite(block, tmp) < 0) 
      perror("Error while writing to disk");
    block++;
  }

  // Do not touch or move this function
  dunconnect();
}
예제 #5
0
파일: mkfs.c 프로젝트: Abioy/FUZIX
void mkfs(uint16_t fsize, uint16_t isize)
{
    uint16_t j;

    /* Zero out the blocks */

    for (j=0; j < fsize; ++j)
        dwrite(j, zero512);

    /* Initialize the super-block */

    fs_super.s_mounted = SMOUNTED; /* Magic number */
    fs_super.s_isize =  isize;
    fs_super.s_fsize =  fsize;
    fs_super.s_nfree =  1;
    fs_super.s_free[0] =  0;
    fs_super.s_tfree =  0;
    fs_super.s_ninode = 0;
    fs_super.s_tinode =  8 * (isize-2) - 2;

    /* Free each block, building the free list */
    for (j= fsize-1; j >= isize+1; --j)
    {
        if (fs_super.s_nfree == 50)
        {
            dwrite(j, (char *)&fs_super.s_nfree);
            fs_super.s_nfree = 0;
        }

        ++fs_super.s_tfree;
        fs_super.s_free[(fs_super.s_nfree)++] = j;
    }

    /* The inodes are already zeroed out */
    /* create the root dir */
    inode[ROOTINODE].i_mode = F_DIR | (0777 & MODE_MASK);
    inode[ROOTINODE].i_nlink = 3;
    inode[ROOTINODE].i_size = 64;
    inode[ROOTINODE].i_addr[0] = isize;

    /* Reserve reserved inode */
    inode[0].i_nlink = 1;
    inode[0].i_mode = ~0;

    dwrite(2, (char *)inode);

    dwrite(isize,(char *)dirbuf);

    /* Write out super block */
    dwrite(1,(char *)&fs_super);
}
예제 #6
0
// Attempts to append a new FAT entry to a given FAT entry's next.
// Mutates fe to reflect new fat entry.
int allocate_fat(fatent* fe){
  vcb vb = getvcb();
  fatent free_fatent[128];
  // Read in fat entries from disk
  char block[BLOCKSIZE];

  // Look for a free fat entry
  for(int count_fat_blocks = 0; count_fat_blocks < (vb.fat_length/128); count_fat_blocks++){
    
    memset(block,0,BLOCKSIZE);
    dread(vb.fat_start+count_fat_blocks, block);
    
    for(int i = 0; i < 128; i++){
      memcpy(&free_fatent[i], &block[i*4], sizeof(fatent));
    }
    for(int j = 0; j < 128; j++){
      if(free_fatent[j].used == 0){
	// We've found an unused fat entry, change eof and append
	fe->eof = 0;
	fe->next = j + (count_fat_blocks * 128);

	free_fatent[j].eof = 1;
	free_fatent[j].next = 0;
	free_fatent[j].used = 1;
	
	memcpy(&block[j*4], &free_fatent[j], sizeof(fatent));
	dwrite(vb.fat_start + count_fat_blocks, block);
	return 0;
      }
    }
  }
  return -1;
}
예제 #7
0
/*
 * Flush all cached modifications to the disk and free the allocated space
 * Only called when unmounting the file system
 */
void flush_cache(vcb *vol_ctrl_blk) {
  for (int i=0; i<vol_ctrl_blk->volume_size; i++) {
    if (disk_cache[i].modified) // if block has been modified, write it back to disk
      if (dwrite(i, disk_cache[i].data) < 0)
        perror("Error: [flush]: Error writing to disk\n");
  }
  free(disk_cache);
}
예제 #8
0
파일: muicfs.c 프로젝트: agentzh2m/SeaIceFS
/* Find free bitmap and return the free inode num or data blck num */
static int assign_bitmap(int offset){
	char *my_bbuf = (char*)malloc(sizeof(char) * BLOCKSIZE);
	char *init_bbuf = my_bbuf;
	int free_obj = -1;
	//search for the free block
	if (offset == IMAP_OFFSET){
		if(dread(global_fd, IMAP_OFFSET, my_bbuf) < 0){
			printf("Read data blck fail at %d \n", IMAP_OFFSET);
			free(init_bbuf);
			return -1;
		}
		for(int i = 0; i < MAXFILE; i++){
			if(!*my_bbuf){
				*my_bbuf = 1;
				if(dwrite(global_fd, IMAP_OFFSET, init_bbuf) < 0){
					WFAIL;
					return -1;
				}
				free(init_bbuf);
				return i;
			}
			my_bbuf++;
		}
	}else {
		for(int i = 0; i < total_blck; i++){
			if(dread(global_fd, DMAP_OFFSET + i, init_bbuf) < 0){
				printf("Read data blck fail at %d \n", i);
				return -1;
			}
			for(int j = 0; j < BLOCKSIZE; j++){
				if(!*my_bbuf){
					*my_bbuf = 1;
					if(dwrite(global_fd, DMAP_OFFSET+i, init_bbuf) < 0){
						WFAIL;
						return -1;
					}
					free(init_bbuf);
					return (i * BLOCKSIZE) + j;
				}
				my_bbuf++;
			}
		}
	}
	return -1;
}
예제 #9
0
파일: fsck.c 프로젝트: NoSuchProcess/FUZIX
void pass2(void)
{
    blkno_t j;
    blkno_t oldtfree;
    int s;
    int yes();

    printf("Rebuild free list? ");
    if (!yes())
        return;

    oldtfree = swizzle16(superblock.s_tfree);

    /* Initialize the superblock-block */

    superblock.s_ninode = 0;
    superblock.s_nfree = swizzle16(1);
    superblock.s_free[0] = 0;
    superblock.s_tfree = 0;

    /* Free each block, building the free list */

    for (j = swizzle16(superblock.s_fsize) - 1; j >= swizzle16(superblock.s_isize); --j) {
        if (bitmap[j] == 0) {
            if (swizzle16(superblock.s_nfree) == 50) {
                dwrite(j, (char *) &superblock.s_nfree);
                superblock.s_nfree = 0;
            }
            superblock.s_tfree = swizzle16(swizzle16(superblock.s_tfree)+1);
            s = swizzle16(superblock.s_nfree);
            superblock.s_free[s++] = swizzle16(j);
            superblock.s_nfree = swizzle16(s);
        }
    }

    dwrite((blkno_t) 1, (char *) &superblock);

    if (oldtfree != swizzle16(superblock.s_tfree))
        printf("During free list regeneration s_tfree was changed to %d from %d.\n",
                swizzle16(superblock.s_tfree), oldtfree);

}
예제 #10
0
static void writer(void)
{
	PRUintn	i = 0;
	size_t	nbytes;
	
	do {
		(void) PR_WaitSem(fullBufs);
		nbytes = buf[i].nbytes;
		if (nbytes > 0) {
			nbytes = dwrite(1, buf[i].data, nbytes);
			PR_PostSem(emptyBufs);
			i = (i + 1) % 2;
		}
	} while (nbytes > 0);
}
예제 #11
0
파일: vm_markov.cpp 프로젝트: pasis/fvm2
bool vmMarkov::executeCmd(QString cmd) {

	if ( ! checkSyntax(cmd) )
		return false;

	if ( cmd == "" )
		return true;

	int	index = cmd.indexOf("->");
	if ( index < 0 )
		return false;

	QString	s1 = deleteSymb( cmd.mid( 0, index ), 'E' );
	QString	s2 = deleteSymb( cmd.mid( index + 2, cmd.size() - index - 2 ), 'E' );
	QString	lastMemory = memory;
	bool	last = false;

	if ( s2[0] == '.' ) {
		last = true;
		s2.remove(0, 1);
	}

	index = memory.indexOf(s1);
	if ( index < 0 )
		return false;
	memory.remove(index, s1.size());
	memory.insert(index, s2);

	if ( last )
		stop();
	#ifndef WITHOUT_INFINITE_LOOP_CHECK
	else if ( memory == lastMemory ) {
		stop(); //! @todo в покроковий режим
		dwrite(QString().fromUtf8("Помічено зациклення, виконання припинено"));
	}
	#endif

	return true;

}
예제 #12
0
파일: muicfs.c 프로젝트: agentzh2m/SeaIceFS
/**
 * The function xmp_write will attempt to write 'size' bytes from
 * memory address 'buf' into a file specified by an absolute 'path'
 *
 * For time being ignore the fuse_file_info parameter. If you would
 * like to use please see the open function in fuse.h file for more
 * detials.
 *
 * On error (e.g., the path does not exist) xmp_write will return -1,
 * otherwise xmp_write should return the number of bytes written.
 *
 * Note size is not necessarily an integral number of blocks.
 * Similar to xmp_read, the actual implementation of xmp_write will
 * depend on how you decide to allocate file space.
 *
 */
static int xmp_write(const char *path, const char *buf, size_t size,
                     off_t offset, struct fuse_file_info *fi)
{
    int* tuple = traverse_dir_r(path);
    int ino = tuple[0];
    free(tuple);
    //retrieve inode
    Inode* ibuf = (Inode*)malloc(sizeof(Inode)*INODE_AMT);
    Inode* init_ibuf = ibuf;
    if(dread(global_fd, ino/INODE_AMT, ibuf) < 0){
    	RFAIL;
    	return -1;
    }
    ibuf+= ino%INODE_AMT;
    //change size into total amt of blck
    int need_blck = (size/BLOCKSIZE) + 1;
    if(need_blck > 16){
    	printf("Our fail system is too stupid to support more space \n");
    	return -1;
    }
    char *dbuf = (char*)malloc(sizeof(char) * (BLOCKSIZE * need_blck));
    char *init_dbuf = dbuf;
    memcpy(init_dbuf, buf, size);
    int free_dnum = -1;
    for(int i = 0; i < need_blck; i++ ){
    	free_dnum = assign_bitmap(DMAP_OFFSET);
    	ibuf->block_pt[i] = free_dnum;
    	if(dwrite(global_fd, DATA_OFFSET + free_dnum, dbuf) < 0){
    		WFAIL;
    		return -1;
    	}
    	dbuf+=BLOCKSIZE;
    }
    free(init_dbuf);
    return 0;

}
예제 #13
0
파일: fsck.c 프로젝트: NoSuchProcess/FUZIX
void pass5(void)
{
    uint16_t n;
    struct dinode ino;
    int yes();

    for (n = ROOTINODE; n < 8 * (swizzle16(superblock.s_isize) - 2); ++n) {
        iread(n, &ino);

        if (ino.i_mode == 0) {
            if (linkmap[n] != -1)
                panic("Inconsistent linkmap");
            continue;
        }

        if (linkmap[n] == -1 && ino.i_mode != 0)
            panic("Inconsistent linkmap");

        if (linkmap[n] > 0 && swizzle16(ino.i_nlink) != linkmap[n]) {
            printf("Inode %d has link count %d should be %d. Fix? ",
                    n, swizzle16(ino.i_nlink), linkmap[n]);
            if (yes()) {
                ino.i_nlink = swizzle16(linkmap[n]);
                iwrite(n, &ino);
            }
        }

        if (linkmap[n] == 0) {
            if ((swizzle16(ino.i_mode) & F_MASK) == F_BDEV ||
                    (swizzle16(ino.i_mode) & F_MASK) == F_CDEV ||
                    (ino.i_size == 0)) {
                printf("Useless inode %d with mode 0%o has become detached. Link count is %d. Zap? ",
                        n, swizzle16(ino.i_mode), swizzle16(ino.i_nlink));
                if (yes()) {
                    ino.i_nlink = 0;
                    ino.i_mode = 0;
                    iwrite(n, &ino);
                    superblock.s_tinode =
                                swizzle16(swizzle16(superblock.s_tinode) + 1);
                    dwrite((blkno_t) 1, (char *) &superblock);
                }
            } else {
#if 0
                printf("Inode %d has become detached. Link count is %d. Fix? ",
                        n, swizzle16(ino.i_nlink));
                if (yes()) {
                    ino.i_nlink = 1;
                    iwrite(n, &ino);
                    mkentry(n);
                }
#else
                printf("Inode %d has become detached. Link count is %d. ",
                        n, swizzle16(ino.i_nlink));
                if (ino.i_nlink == 0)
                    printf("Zap? ");
                else
                    printf("Fix? ");
                if (yes()) {
                    if (ino.i_nlink == 0) {
                        ino.i_nlink = 0;
                        ino.i_mode = 0;
                        iwrite(n, &ino);
                        superblock.s_tinode =
                                swizzle16(swizzle16(superblock.s_tinode) + 1);
                        dwrite((blkno_t) 1, (char *) &superblock);
                    } else {
                        ino.i_nlink = swizzle16(1);
                        iwrite(n, &ino);
                        mkentry(n);
                    }
                }
#endif
            }
        }

    }
}
예제 #14
0
파일: fsck.c 프로젝트: NoSuchProcess/FUZIX
int main(int argc, char **argv)
{
    char *buf;
    char *op;

    if(argc != 2){
        fprintf(stderr, "syntax: fsck [devfile][:offset]\n");
        return 1;
    }
    
    op = strchr(argv[1], ':');
    if (op) {
        *op++ = 0;
        offset = atol(op);
    }

    if(fd_open(argv[1])){
        printf("Cannot open file\n");
        return -1;
    }

    buf = daread(1);
    bcopy(buf, (char *) &superblock, sizeof(struct filesys));

    /* Verify the fsize and isize parameters */
    if (superblock.s_mounted == SMOUNTED_WRONGENDIAN) {
        swizzling = 1;
        printf("Checking file system with reversed byte order.\n");
    }

    if (swizzle16(superblock.s_mounted) != SMOUNTED) {
        printf("Device %d has invalid magic number %d. Fix? ", dev, superblock.s_mounted);
        if (!yes())
            exit(-1);
        superblock.s_mounted = swizzle16(SMOUNTED);
        dwrite((blkno_t) 1, (char *) &superblock);
    }
    printf("Device %d has fsize = %d and isize = %d. Continue? ",
            dev, swizzle16(superblock.s_fsize), swizzle16(superblock.s_isize));
    if (!yes())
        exit(-1);

    bitmap = calloc(swizzle16(superblock.s_fsize), sizeof(char));
    linkmap = (int16_t *) calloc(8 * swizzle16(superblock.s_isize), sizeof(int16_t));

    if (!bitmap || !linkmap) {
        fprintf(stderr, "Not enough memory.\n");
        exit(-1);
    }

    printf("Pass 1: Checking inodes...\n");
    pass1();

    printf("Pass 2: Rebuilding free list...\n");
    pass2();

    printf("Pass 3: Checking block allocation...\n");
    pass3();

    printf("Pass 4: Checking directory entries...\n");
    pass4();

    printf("Pass 5: Checking link counts...\n");
    pass5();

    printf("Done.\n");

    exit(0);
}
예제 #15
0
파일: fsck.c 프로젝트: NoSuchProcess/FUZIX
void pass3(void)
{
    uint16_t n;
    struct dinode ino;
    uint16_t mode;
    blkno_t b;
    blkno_t bno;
    blkno_t newno;
    blkno_t blk_alloc0();
    /*--- was blk_alloc ---*/
    blkno_t getblkno();
    int yes();

    for (b = swizzle16(superblock.s_isize); b < swizzle16(superblock.s_fsize); ++b)
        bitmap[b] = 0;

    for (n = ROOTINODE; n < 8 * (swizzle16(superblock.s_isize) - 2); ++n) {
        iread(n, &ino);

        mode = swizzle16(ino.i_mode) & F_MASK;
        if (mode != F_REG && mode != F_DIR)
            continue;

        /* Check singly indirect blocks */

        for (b = 18; b < 20; ++b) {
            if (ino.i_addr[b] != 0) {
                if (bitmap[swizzle16(ino.i_addr[b])] != 0) {
                    printf("Indirect block %d in inode %u value %u multiply allocated. Fix? ",
                            b, n, swizzle16(ino.i_addr[b]));
                    if (yes()) {
                        newno = blk_alloc0(&superblock);
                        if (newno == 0)
                            printf("Sorry... No more free blocks.\n");
                        else {
                            dwrite(newno, daread(swizzle16(ino.i_addr[b])));
                            ino.i_addr[b] = swizzle16(newno);
                            iwrite(n, &ino);
                        }
                    }
                } else
                    bitmap[swizzle16(ino.i_addr[b])] = 1;
            }
        }

        /* Check the rest */
        for (bno = 0; bno <= swizzle32(ino.i_size)/512; ++bno) {
            b = getblkno(&ino, bno);

            if (b != 0) {
                if (bitmap[b] != 0) {
                    printf("Block %d in inode %u value %u multiply allocated. Fix? ",
                            bno, n, b);
                    if (yes()) {
                        newno = blk_alloc0(&superblock);
                        if (newno == 0)
                            printf("Sorry... No more free blocks.\n");
                        else {
                            dwrite(newno, daread(b));
                            setblkno(&ino, bno, newno);
                            iwrite(n, &ino);
                        }
                    }
                } else
                    bitmap[b] = 1;
            }
        }

    }

}
예제 #16
0
파일: fsck.c 프로젝트: NoSuchProcess/FUZIX
void pass1(void)
{
    uint16_t n;
    struct dinode ino;
    uint16_t mode;
    blkno_t b;
    blkno_t bno;
    uint16_t icount;
    blkno_t *buf;

    blkno_t getblkno();
    int yes();			/* 1.4.98 - HFB */

    icount = 0;

    for (n = ROOTINODE; n < 8 * (swizzle16(superblock.s_isize) - 2); ++n) {
        iread(n, &ino);
        linkmap[n] = -1;
        if (ino.i_mode == 0)
            continue;

        mode = swizzle16(ino.i_mode) & F_MASK;
        /* FIXME: named pipes.. */

        /* Check mode */
        if (mode != F_REG && mode != F_DIR && mode != F_BDEV && mode != F_CDEV) {
            printf("Inode %d with mode 0%o is not of correct type. Zap? ",
                    n, swizzle16(ino.i_mode));
            if (yes()) {
                ino.i_mode = 0;
                ino.i_nlink = 0;
                iwrite(n, &ino);
                continue;
            }
        }
        linkmap[n] = 0;
        ++icount;
        /* Check size */

        if (swizzle32(ino.i_size) < 0) {
            printf("Inode %d offset is negative with value of %ld. Fix? ",
                    n, (long)swizzle32(ino.i_size));
            if (yes()) {
                ino.i_size = 0;
                iwrite(n, &ino);
            }
        }
        /* Check blocks and build free block map */
        if (mode == F_REG || mode == F_DIR) {
            /* Check singly indirect blocks */

            for (b = 18; b < 20; ++b) {
                if (ino.i_addr[b] != 0 && 
                    (swizzle16(ino.i_addr[b]) < swizzle16(superblock.s_isize) ||
                            swizzle16(ino.i_addr[b]) >= swizzle16(superblock.s_fsize))) {
                    printf("Inode %d singly ind. blk %d out of range, val = %u. Zap? ",
                            n, b, swizzle16(ino.i_addr[b]));
                    if (yes()) {
                        ino.i_addr[b] = 0;
                        iwrite(n, &ino);
                    }
                }
                if (ino.i_addr[b] != 0 && swizzle32(ino.i_size) < 18*512) {
                    printf("Inode %d singly ind. blk %d past end of file, val = %u. Zap? ",
                            n, b, swizzle16(ino.i_addr[b]));
                    if (yes()) {
                        ino.i_addr[b] = 0;
                        iwrite(n, &ino);
                    }
                }
                if (ino.i_addr[b] != 0)
                    bitmap[swizzle16(ino.i_addr[b])] = 1;
            }

            /* Check the double indirect blocks */
            if (ino.i_addr[19] != 0) {
                buf = (blkno_t *) daread(swizzle16(ino.i_addr[19]));
                for (b = 0; b < 256; ++b) {
                    if (buf[b] != 0 && (swizzle16(buf[b]) < swizzle16(superblock.s_isize) ||
                                swizzle16(buf[b]) >= swizzle16(superblock.s_fsize))) {
                        printf("Inode %d doubly ind. blk %d is ", n, b);
                        printf("out of range, val = %u. Zap? ", swizzle16(buf[b]));
                        /* 1.4.98 - line split.  HFB */
                        if (yes()) {
                            buf[b] = 0;
                            dwrite(b, (char *) buf);
                        }
                    }
                    if (buf[b] != 0)
                        bitmap[swizzle16(buf[b])] = 1;
                }
            }
            /* Check the rest */
            for (bno = 0; bno <= swizzle32(ino.i_size)/512; ++bno) {
                b = getblkno(&ino, bno);

                if (b != 0 && (b < swizzle16(superblock.s_isize) || b >= swizzle16(superblock.s_fsize))) {
                    printf("Inode %d block %d out of range, val = %u. Zap? ",
                            n, bno, b);
                    if (yes()) {
                        setblkno(&ino, bno, 0);
                        iwrite(n, &ino);
                    }
                }
                if (b != 0)
                    bitmap[b] = 1;
            }
        }
    }
    /* Fix free inode count in superblock block */
    if (swizzle16(superblock.s_tinode) != 8 * (swizzle16(superblock.s_isize) - 2) - ROOTINODE - icount) {
        printf("Free inode count in superblock block is %u, should be %u. Fix? ",
                swizzle16(superblock.s_tinode), 8 * (swizzle16(superblock.s_isize) - 2) - ROOTINODE - icount);

        if (yes()) {
            superblock.s_tinode = 8 * (swizzle16(superblock.s_isize) - 2) - ROOTINODE - icount;
            dwrite((blkno_t) 1, (char *) &superblock);
        }
    }
}
예제 #17
0
void setvcb(vcb vb){
  char temp_vb[BLOCKSIZE];
  memset(temp_vb, 0, BLOCKSIZE);
  memcpy(temp_vb, &vb, sizeof(vcb));
  dwrite(0, temp_vb);
}
예제 #18
0
void myformat(int size) {
  // Do not touch or move this function
  dcreate_connect();

  int num_dblocks = get_num_dblocks(size);

  // Format vcb
  vcb volblock = make_volblock(num_dblocks);
  // Do we need to malloc space(BLOCKSIZE) for vcbtemp?
  char vcbtemp[BLOCKSIZE];
  memset(vcbtemp, 0, BLOCKSIZE);
  memcpy(vcbtemp, &volblock, BLOCKSIZE);
  dwrite(0, vcbtemp);

  // Format dirents
  dirent dent = make_dirent();
  // Do we need to malloc space(BLOCKSIZE) for dirtemp?
  char dirtemp[BLOCKSIZE];
  memset(dirtemp, 0, BLOCKSIZE);
  memcpy(dirtemp, &dent, BLOCKSIZE);
  
  for(int i = volblock.de_start; i < volblock.de_start+volblock.de_length; i++){
    dwrite(i, dirtemp);
  }

  //  char fat_block_temp[BLOCKSIZE];
  // memset(fat_block_temp,0,BLOCKSIZE);

  fatent fe = make_fatent();

  fatent fat_block[128];

  int remaining = volblock.fat_length;
  int block = volblock.fat_start;

  while(remaining > 0){
    for(int i = 0; i<128; i++){
      fat_block[i] = fe;
      remaining--;
    }
    char fat_block_temp[BLOCKSIZE];
    memset(fat_block_temp,0,BLOCKSIZE);
    memcpy(fat_block_temp,&fat_block,sizeof(fat_block));
    dwrite(block,fat_block_temp);
    block++;
  }

  char empty_block[BLOCKSIZE];
  memset(empty_block,0,BLOCKSIZE);
  for(int i = 0; i < num_dblocks; i++){
     dwrite((volblock.db_start + i), empty_block);
  }


/*
  char fat_entry_temp[4];
  //  memset(fat_entry_temp,0,4);
  //  memcpy(fat_entry_temp,&fe,4);
  

  for(int i = volblock.fat_start; i<volblock.fat_start+volblock.fat_length;i++){
    fprintf(stderr,"writing fat shit");
    for(int j = 0; j < 128; j++){
       // Set FAT defaults
       // Format single FAT block with 128 default FAT entries
       memcpy(&fat_block_temp[4*j],fat_entry_temp,4);
       fprintf(stderr,"memcpy(&fat_block_temp..."); 
    }
    dwrite(i, fat_block_temp);
  }*/

  /* 3600: FILL IN CODE HERE.  YOU SHOULD INITIALIZE ANY ON-DISK
           STRUCTURES TO THEIR INITIAL VALUE, AS YOU ARE FORMATTING
           A BLANK DISK.  YOUR DISK SHOULD BE size BLOCKS IN SIZE. */

  /* 3600: AN EXAMPLE OF READING/WRITING TO THE DISK IS BELOW - YOU'LL
           WANT TO REPLACE THE CODE BELOW WITH SOMETHING MEANINGFUL. */

  /*
  // first, create a zero-ed out array of memory  
  char *tmp = (char *) malloc(BLOCKSIZE);
  memset(tmp, 0, BLOCKSIZE);

  // now, write that to every block
  for (int i=0; i<size; i++) 
    if (dwrite(i, tmp) < 0) 
      perror("Error while writing to disk");

  // voila! we now have a disk containing all zeros
  */

  // Do not touch or move this function
  dunconnect();
}
예제 #19
0
파일: muicfs.c 프로젝트: agentzh2m/SeaIceFS
/**
 * Given an absolute path to a directory (which may or may not end in
 * '/'), xmp_mkdir will create a new directory named dirname in that
 * directory. Ignore the mode parameter as we are not implementing
 * the permissions. The steps your implementation will do to make a new
 * directory will be the following:
 *
 * 1. Resolve the directory path
 *
 * 2. Allocate and initialize a new directory block
 *
 * 3. Update the directory block for 'path' by adding an entry for
 *    the newly created directory.
 *
 * 4. You also need to create appropriate entries for "." and ".." in the
 *    New directory
 *
 * You should return 0 on success or -1 on error (e.g., the path doesn't
 * exist, or dirname already does).
 *
 */
static int xmp_mkdir(const char *path, mode_t mode)
{
	printf("Start creating directory \n");
	int *tup = traverse_dir_w(path);
	int dir_inum = tup[0];
	int dir_dnum = tup[1];
	//pull the corresponding dir data blck
	Directory *dir_buf = (Directory*)malloc(sizeof(Directory) * DIR_AMT);
	Directory *init_dbuf = dir_buf;
	char* last_path;
	for(char* path_pt = strtok(path,"/"); path_pt!=NULL; path_pt = strtok(NULL,"/")){
			last_path = path_pt;
	}
	if(dread(global_fd, dir_dnum + DATA_OFFSET, init_dbuf) < 0){
		RFAIL;
		return -1;
	}

	//finding free inode and dblck for dir
	int free_inum = assign_bitmap(IMAP_OFFSET);
	int free_dnum = assign_bitmap(DMAP_OFFSET);
	printf("Assigning inum : %d, Assigning dnum: %d\n", free_inum, free_dnum);
	for(int i = 0; i < DIR_AMT; i++){
		//finding free blck
		if(strlen(dir_buf->f_name) < 1){
			strcpy(dir_buf->f_name, last_path);
			dir_buf->inode_num = free_inum;
			break;
		}
		dir_buf++;
	}
	//writing into previous dir block before I forgot to do so lol
	if(dwrite(global_fd, dir_dnum + DATA_OFFSET, init_dbuf) < 0){
		RFAIL;
		return -1;
	}
	free(init_dbuf);
	//make a data block a into a Directory and insert inum of previous and current
	//into . and ..
	Directory* new_dbuf = (Directory*)malloc(sizeof(Directory) * DIR_AMT);
	Directory* init_new_dbuf = new_dbuf;

	for(int i = 0; i < DIR_AMT; i++){
		if(i == 0){
			strcpy(new_dbuf->f_name, ".");
			new_dbuf->inode_num = free_inum;
		}else if(i == 1){
			strcpy(new_dbuf->f_name, "..");
			new_dbuf->inode_num = dir_inum;
		}else {
			strcpy(new_dbuf->f_name, "");
			new_dbuf->inode_num = -1;
		}
		new_dbuf++;
	}

	if(dwrite(global_fd, DATA_OFFSET + free_dnum, init_new_dbuf) < 0 ){
		RFAIL;
		return -1;
	}
	free(init_new_dbuf);
	//configure inode according to dir
	Inode *ibuf = (Inode*)malloc(sizeof(Inode) * INODE_AMT);
	Inode *init_ibuf = ibuf;

	if(dread(global_fd, INODE_OFFSET + free_inum/INODE_AMT, init_ibuf) < 0){
		RFAIL;
		return -1;
	}
	ibuf+=free_inum%(INODE_AMT);
	ibuf->f_size = 512;
	ibuf->total_blck = 1;
	ibuf->block_pt[0] = free_dnum;
	ibuf->f_type = 1;

	if(dwrite(global_fd, INODE_OFFSET + free_inum/INODE_AMT, init_ibuf) < 0){
		WFAIL;
		return -1;
	}

	return 0;
}
예제 #20
0
/*
 * 3. read bitmapAREA(onMemory) and write bmpFile with adding spacing
 *    BMP-file: noCompression(BI_RGB), 8bitColor, Windows-Win32 type
 */
void writeBmpFile(unsigned char *bitmapP, int spacing, int colchar, FILE *bmpP){
        long bmpw; /* bmp-image width (pixel) */
        long bmph; /* bmp-image height (pixel) */
        int bmppad; /* number of padding pixels */
        unsigned long bmpTotalSize; /* bmp filesize (byte) */
        /*  bmp-lines needs to be long alined and padded with 0 */
        uint32_t uint32;
        uint16_t uint16;
        int32_t sint32;
        unsigned char uchar;
        int i,x,y,g,tmp;
        int rowchar; /* number of row glyphs */
        int bx, by;

        /* bmp-image width */
        bmpw = (font.w+spacing)*colchar + spacing;

        /* bmp-image height */
        rowchar = (chars/colchar) + (chars%colchar!=0);
        bmph = (font.h+spacing)*rowchar + spacing;

        v_printf("  BMP width = %d pixels\n", (int)bmpw);
        v_printf("  BMP height = %d pixels\n", (int)bmph);
        d_printf("  number of glyphs column=%d ", colchar);
        d_printf("row=%d\n", rowchar);

        bmppad = ((bmpw + 3) / 4 * 4) - bmpw;
        bmpTotalSize = (bmpw + bmppad) * bmph
                + sizeof(int32_t)*11 + sizeof(int16_t)*5 + sizeof(char)*4*256;
        v_printf("  BMP filesize = %d bytes\n", (int)bmpTotalSize);


        /*
         * BITMAPFILEHEADER struct
         */
        uint16 = 0x4d42; /* 4d = 'M', 42 = 'B' */
        dwrite(&uint16, sizeof(uint16), bmpP);
        uint32 = bmpTotalSize;
        dwrite(&uint32, sizeof(uint32), bmpP);
        uint16 = 0x00;
        dwrite(&uint16, sizeof(uint16), bmpP); /* reserved as 0 */
        dwrite(&uint16, sizeof(uint16), bmpP); /* reserved as 0 */

        /* bfOffBits: offset to image-data array */
        uint32 = sizeof(int32_t)*11 + sizeof(int16_t)*5 + sizeof(char)*4*256;
        dwrite(&uint32, sizeof(uint32), bmpP);


        /*
         * BITMAPINFOHEADER struct
         */
        uint32 = 40; /* when Windows-BMP, this is 40 */
        dwrite(&uint32, sizeof(uint32), bmpP);
        sint32 = bmpw; /* biWidth */
        dwrite(&sint32, sizeof(sint32), bmpP);
        sint32 = bmph; /* biHeight: down-top */
        dwrite(&sint32, sizeof(sint32), bmpP);
        uint16 = 1; /* biPlanes: must be 1 */
        dwrite(&uint16, sizeof(uint16), bmpP);
        uint16 = 8; /* biBitCount: 8bitColor */
        dwrite(&uint16, sizeof(uint16), bmpP);
        uint32 = 0; /* biCompression: noCompression(BI_RGB) */
        dwrite(&uint32, sizeof(uint32), bmpP);
        uint32 = 0; /* biSizeImage: when noComprsn, 0 is ok */
        dwrite(&uint32, sizeof(uint32), bmpP);
        sint32 = 0; /* biXPelsPerMeter: resolution x; 0 ok */
        dwrite(&sint32, sizeof(sint32), bmpP);
        sint32 = 0; /* biYPelsPerMeter: res y; 0 is ok */
        dwrite(&sint32, sizeof(sint32), bmpP);
        uint32 = 0; /* biClrUsed: optimized color palette; not used */
        dwrite(&uint32, sizeof(uint32), bmpP);
        uint32 = 0; /* biClrImportant: 0 is ok */
        dwrite(&uint32, sizeof(uint32), bmpP);

        /*
         * RGBQUAD[256]: color palette
         */
        /*   palette[0]: background of glyphs */
        uchar = 0xff;
        dwrite(&uchar, sizeof(uchar), bmpP); /* rgbBlue: B */
        dwrite(&uchar, sizeof(uchar), bmpP); /* rgbGreen: G */
        dwrite(&uchar, sizeof(uchar), bmpP); /* rgbRed: R */
        uchar = 0;
        dwrite(&uchar, sizeof(uchar), bmpP); /* rgbReserved: must be 0 */

        /*   palette[1]: foreground of glyphs */
        uchar = 0;
        for(i=0; i<4; i++)
                dwrite(&uchar, sizeof(uchar), bmpP); /* palette[1]: #000000 */

        /*   palette[2]: spacing */
        uchar = COLOR_SPACING_BLUE;
        dwrite(&uchar, sizeof(uchar), bmpP); /* B */
        uchar = COLOR_SPACING_GREEN;
        dwrite(&uchar, sizeof(uchar), bmpP); /* G */
        uchar = COLOR_SPACING_RED;
        dwrite(&uchar, sizeof(uchar), bmpP); /* R */
        uchar = 0;
        dwrite(&uchar, sizeof(uchar), bmpP); /* must be 0 */

        /*   palette[3]: out of dwidth over baseline */
        uchar = COLOR_OVERBL_BLUE;
        dwrite(&uchar, sizeof(uchar), bmpP); /* B */
        uchar = COLOR_OVERBL_GREEN;
        dwrite(&uchar, sizeof(uchar), bmpP); /* G */
        uchar = COLOR_OVERBL_RED;
        dwrite(&uchar, sizeof(uchar), bmpP); /* R */
        uchar = 0;
        dwrite(&uchar, sizeof(uchar), bmpP); /* must be 0 */

        /*   palette[4]: out of dwidth; under baseline */
        uchar = COLOR_UNDERBL_BLUE;
        dwrite(&uchar, sizeof(uchar), bmpP); /* B */
        uchar = COLOR_UNDERBL_GREEN;
        dwrite(&uchar, sizeof(uchar), bmpP); /* G */
        uchar = COLOR_UNDERBL_RED;
        dwrite(&uchar, sizeof(uchar), bmpP); /* R */
        uchar = 0;
        dwrite(&uchar, sizeof(uchar), bmpP); /* must be 0 */

        /*   palette[5] to palette[255]: not used */
        for(i=5; i<256; i++){
                uchar = 0x00; /* palette[5to255]: #000000 */
                dwrite(&uchar, sizeof(uchar), bmpP);
                dwrite(&uchar, sizeof(uchar), bmpP);
                dwrite(&uchar, sizeof(uchar), bmpP);
                dwrite(&uchar, sizeof(uchar), bmpP);
        }

        /*
         * IMAGE DATA array
         */
        for(y=bmph-1; y>=0; y--){
                for(x=0; x<bmpw+bmppad; x++){
                        if(x>=bmpw){
                                /* padding: long(4bytes) aligned */
                                uchar = 0; /* must pad with 0 */
                                dwrite(&uchar, sizeof(uchar), bmpP);
                        }else{
                                if( (y%(font.h+spacing)<spacing) || (x%(font.w+spacing)<spacing) ){
                                        /* spacing */
                                        uchar = 2; /* fill palette[2] */
                                        dwrite(&uchar, sizeof(uchar), bmpP);
                                }else{
                                        /* read bitmapAREA & write bmpFile */
                                        g = (x/(font.w+spacing)) + (y/(font.h+spacing)*colchar);
                                        bx = x - (spacing*(g%colchar)) - spacing;
                                        by = y - (spacing*(g/colchar)) - spacing;
                                        tmp = g*(font.h*font.w) + (by%font.h)*font.w + (bx%font.w);
                                        if(tmp >= chars*font.h*font.w){
                                                /* spacing over the last glyph */
                                                uchar = 2; /* fill palette[2] */
                                        }else
                                                uchar = *( bitmapP + tmp);
                                        dwrite(&uchar, sizeof(uchar), bmpP);
                                }
                        }
                }
        }
        return;
}
예제 #21
0
/*
 * The function vfs_write will attempt to write 'size' bytes from 
 * memory address 'buf' into a file specified by an absolute 'path'.
 * It should do so starting at the specified offset 'offset'.  If
 * offset is beyond the current size of the file, you should pad the
 * file with 0s until you reach the appropriate length.
 *
 * You should return the number of bytes written.
 *
 * HINT: Ignore 'fi'
 */
static int vfs_write(const char *path, const char *buf, size_t size,
                     off_t offset, struct fuse_file_info *fi)
{
  // First, we ensure thepath is valid.
  if(validate_path(path) != 0)
    return -1;
  
  vcb vb = getvcb();
  path++;               // Get rid of leading slash in path
  int byteswritten = 0; // Amount of bytes we've written to disk from buffer
  int num_pad = 0;      // Amount of 0s we need to pad between EOF and offset
  int write_offset = 0; // variable to hold an offset for vfs_write.

  dirent de;
  int found_dirent = 0;
  int dirent_index = -1;
  
  for(int i = vb.de_start; i < vb.de_start + vb.de_length, found_dirent == 0; i++){
    de = getdirent(i);
    if(de.valid == 1)
      if(strcmp(path,de.name)==0){
        found_dirent = 1;
        dirent_index = i;
      }
  }

  if(found_dirent){	// Begin writing to disk.
    if(offset > de.size){ // Check for padding.
      num_pad = (offset - de.size); // Number of 0's to add.
    }
    if((size + offset) > de.size){
      de.size = (size + offset);    // Set the new size of the file.
    }
    
    // Update access modify times of file
    struct timespec newtime;
    clock_gettime(CLOCK_REALTIME, &newtime);
    
    de.access_time = newtime;
    de.modify_time = newtime;
    
    /* Next, since we have our dirent to write to, we must:
       - Check to see if the dirent has a FAT entry allocated.
       - Attempt to allocate one if necessary.
       x If the dirent has at least one FAT entry, begin writing:
       x Begin traversing offset, decrementing it as necessary until offset == 0.
       x Attempt to create new FAT entries if necessary.
       x Begin padding the file with zeros, if necessary, decrementing num_pad until it == 0.
       x Attempt to create new FAT entries if necessary.
       x Begin appending buf to file, decrementing size while doing so.
       x Attempt to create new FAT entries if necessary.
       x If, in any of the above cases, creating a new FAT entry fails, return -ENOSPC.
    */    
   
    

 
    // Check if found dirent has allocated FAT. If not, attempt to allocate one.
    if((int) de.first_block == -1){
      char block[BLOCKSIZE];
      int found_free = 0;
      
      for(int i = vb.fat_start;(i < vb.fat_start + ((int) (vb.fat_length/128))) && found_free == 0; i++){ // For each fat block...
	int block_index = (i-vb.fat_start)*128;
	
	memset(block,0,BLOCKSIZE); // Reset block.
	dread(i, block); // Read FAT Block into block.
	
	for(int j = 0; j < 128 && found_free == 0; j++){
	  fatent fe = getfe( block_index + j);
	  if(fe.used == 0){
	    de.first_block = block_index + j;
	    fe.used = 1;
	    fe.eof = 1;
	    fe.next = 0;
	    found_free = 1;
	  }
	}
      }
      if(found_free != 1){
        return -ENOSPC;
      }
    }
    
    
    
    fatent fe = getfe(de.first_block);
    
    char block[BLOCKSIZE];
    memset(block,0,BLOCKSIZE);
      
    // Expand file and pad 0's, if necessary.
    if(num_pad > 0){
      
      int eof_fat_idx = get_eof_fe(&fe) + vb.db_start;// find index of eof 
      dread(eof_fat_idx,block);
	
      int eof_data_idx;
	
      for(int i = 0; block[i] != EOF; i++)
        eof_data_idx++;

      eof_data_idx++; // Increment counter so block[eof_data_idx] == EOF.
	
	while(num_pad > 0){
          if(eof_data_idx < BLOCKSIZE){
	    memset(&block[eof_data_idx],0,1);
	    eof_data_idx++;
	    num_pad--;
	    if(num_pad == 0){
		dwrite(eof_data_idx,block);
		memset(block,0,BLOCKSIZE);
	    }
          } 
	  else{
	    dwrite(eof_fat_idx,block);
	    memset(block,0,BLOCKSIZE);
	    if(allocate_fat(&fe) != 0){
		return -ENOSPC;
	    }
	    eof_fat_idx = get_eof_fe(&fe) + vb.db_start;
	    eof_data_idx = 0;
	  }
	}
    }    
        
    // Now, we need to start writing size chars from buf into the file, starting at offset
    
    // Start by finding where offset is in the datablock.
    int offset_block = (int)(offset/512);
    int offset_into_block = offset % 512;
    int buffer_offset = 0;
    
    // Read in offset block, write at offset into block
    memset(block,0,BLOCKSIZE);
    dread(offset_block + vb.db_start, block);
    
    // Memcpy the rest of the block, or size bytes, whichever bounds first
    while(offset_into_block < BLOCKSIZE && size > 0){
      memcpy(&block[offset_into_block], buf, 1);
      size--;
      buf++;
      buffer_offset++;
      offset_into_block++;
      byteswritten++;
    }

    // Write block rest of block
    dwrite(offset_block + vb.db_start, block);
    
    // While there remains bytes to be written...
    while(size > 0){
      if(offset_into_block == BLOCKSIZE){
	// Write block
	dwrite(offset_block + vb.db_start, block);
	
	// Allocate new fat/data block
	if(allocate_fat(&fe) != 0){
	  return -ENOSPC;
	}
	
	// reset offset_into_block
	offset_into_block = 0;
	offset_block = get_eof_fe(&fe);
        memset(block,0,BLOCKSIZE);
      }
      
      memcpy(&block[offset_into_block], &buf[buffer_offset], 1);
      
      size--;
      buffer_offset++;
      offset_into_block++;
      byteswritten++;
    }

    // Write the rest of size bytes
    dwrite(offset_block + vb.db_start, block);
    setdirent(dirent_index,de);
    return byteswritten;
  }
  else{
    // No free dirents found
    return -1;
  }
  
}
예제 #22
0
void setdirent(int idx, dirent de){
  char temp_de[BLOCKSIZE];
  memset(temp_de,0,BLOCKSIZE);
  memcpy(temp_de,&de,sizeof(de));
  dwrite(idx,temp_de);
}
예제 #23
0
void myformat(int size) {
    // Do not touch or move this function
    dcreate_connect();

    /* 3600: FILL IN CODE HERE.  YOU SHOULD INITIALIZE ANY ON-DISK
       STRUCTURES TO THEIR INITIAL VALUE, AS YOU ARE FORMATTING
       A BLANK DISK.  YOUR DISK SHOULD BE size BLOCKS IN SIZE. */

    blocknum *blocks = calloc(size-1, BLOCKSIZE);

    for (int i = 1; i < size; i++) {
        blocknum b;
        b.index = i;
        b.valid = 0;
        blocks[i] = b;
    }

    freeb *free_blocks = calloc(size - 3, BLOCKSIZE);
    for (int i = 0; i < size - 3; i++) {
        freeb f;
        blocknum b = blocks[i+4];

        if ((i + 4) == size) {
            b.valid = 1;
            b.index = -1;
        } else {
            b.valid = 0;
        }

        f.next = b;
        free_blocks[i] = f;
        // printf("Free block created with next pointing to %d\n", b.index);
    }

    blocknum invalid_block;
    invalid_block.index = -1;
    invalid_block.valid = 0;

    vcb myvcb;
    myvcb.blocksize = BLOCKSIZE;
    myvcb.magic = 17;
    myvcb.root = blocks[1];
    myvcb.root.valid = 1;
    myvcb.free = blocks[3];
    strcpy(myvcb.name, "Josh and Mich's excellent file system.");

    dnode root;
    root.direct[0] = blocks[2];
    root.direct[0].valid = 1;
    root.size = sizeof(blocknum);
    root.user = getuid();
    root.group = getgid();
    root.mode = 0777;
    root.single_indirect = invalid_block;
    root.double_indirect = invalid_block;
 
    for (int i = 1; i < 54; i++) {
        root.direct[i] = invalid_block;
    }
    
    struct timespec current_time;
    struct timeval ctval;
    if (gettimeofday(&ctval, NULL) != 0) {
        printf("Error getting time.\n");
    }
    current_time.tv_sec = ctval.tv_sec;
    current_time.tv_nsec = ctval.tv_usec * 1000;
    // if ((clock_gettime(CLOCK_REALTIME, &current_time)) != 0) {
    //     printf("ERROR: Cannot get time. \n");
    // }
    root.access_time = current_time;
    root.modify_time = current_time;
    root.create_time = current_time;

    direntry dot;
    dot.type = 'd';
    dot.block = blocks[1];
    dot.block.valid = 1;
    strncpy(dot.name, ".", 55);

    direntry dotdot;
    dotdot.type = 'd';
    dotdot.block = blocks[1];
    dotdot.block.valid = 1;
    strncpy(dotdot.name, "..", 55);

    dirent root_dir;
    root_dir.entries[0] = dot;
    root_dir.entries[1] = dotdot;

    direntry empty;
    empty.type = '\0';
    empty.block = blocks[size];
    strcpy(empty.name, "");

    for (int i = 2; i < 8; i++) {
        root_dir.entries[i] = empty;
    }

    //printf("The size of vcb is: %d\n", (int) sizeof(myvcb));


    // Write the vcb to memory at block 0
    if (bwrite(0, &myvcb) < 0) {
        perror("ERROR: Could not write vcb to disk.\n");
    }

   // printf("The size of root_dir is %d\n", (int) sizeof(root_dir));

    // Write the root dir to memory at block 1
    char* root_tmp = malloc(sizeof(root));
    memcpy(root_tmp, &root, sizeof(root));
    if (dwrite(1, root_tmp) < 0) {
        perror("Error writing root dir to disk.");
    }
    free(root_tmp);

    // Write the root dir entries to memory at block 2 
    char* root_dir_tmp = malloc(sizeof(root_dir));
    memcpy(root_dir_tmp, &root_dir, sizeof(root_dir));
    if (dwrite(2, root_dir_tmp) < 0) {
        perror("Error writing root dir entries to disk.");
    }
    free(root_dir_tmp);

    // Write the rest of the disk as free blocks
    for (int i = 3; i < size; i++) {
        freeb f = free_blocks[i-3];
        char* free_tmp = malloc(sizeof(f));
        memcpy(free_tmp, &f, sizeof(f));
        // printf("Writing free block %d to disk at block %d.\n", f.next.index, i);
        if (dwrite(i, free_tmp) < 0) {
            perror("Error writing free blocks to disk.");
        }
        free(free_tmp);
    }

    /* 3600: AN EXAMPLE OF READING/WRITING TO THE DISK IS BELOW - YOU'LL
       WANT TO REPLACE THE CODE BELOW WITH SOMETHING MEANINGFUL. */
/*
    // first, create a zero-ed out array of memory  
    char *tmp = (char *) malloc(BLOCKSIZE);
    memset(tmp, 0, BLOCKSIZE);

    // now, write that to every block
    for (int i=0; i<size; i++) 
        if (dwrite(i, tmp) < 0) 
            perror("Error while writing to disk");

    // voila! we now have a disk containing all zeros
*/
    // Do not touch or move this function
    dunconnect();
}
예제 #24
0
파일: mkfs.c 프로젝트: erkinalp/FUZIX
int main(int argc, char **argv)
{
	uint16_t fsize, isize, bsize = 512, shift = 0;
	uint16_t j;
	uint32_t s;
	int opt;

	while((opt = getopt(argc, argv, "Xb:")) != -1) {
		switch(opt) {
			case 'X':	
				swizzling = 1;
				break;
			case 'b':
				bsize = atoi(optarg);
				shift = validate(bsize);
				break;
			default:
				usage();
		}
	}
	if (argc - optind != 3)
		usage();

	if (sizeof(inode) != 512) {
		printf("inode is the wrong size -- %d\n",
		       (int) sizeof(inode));
	}

	isize = (uint16_t) atoi(argv[optind + 1]);
	fsize = (uint16_t) atoi(argv[optind + 2]);

	if (fsize < 3 || isize < 2 || isize >= fsize) {
		printf("Bad parameter values\n");
		return -1;
	}

	memset(zero512, 0, 512);

	printf("Making %d byte/block filesystem with %s byte order on device %s with fsize = %u and isize = %u.\n",
	       bsize, swizzling==0 ? "normal" : "reversed", argv[optind], fsize, isize);

	if (fd_open(argv[optind])) {
		printf("Can't open device");
		return -1;
	}

	s = fsize << shift;

	/* Zero out the blocks */
	for (j = 0; j < s; ++j)
		dwrite(j, zero512);

	/* Initialize the super-block */

	fs_super.fs.s_mounted = swizzle16(SMOUNTED);	/* Magic number */
	fs_super.fs.s_isize = swizzle16(isize);
	fs_super.fs.s_fsize = swizzle16(fsize);
	fs_super.fs.s_nfree = swizzle16(1);
	fs_super.fs.s_free[0] = 0;
	fs_super.fs.s_tfree = 0;
	fs_super.fs.s_ninode = 0;
	fs_super.fs.s_tinode = swizzle16(8 * (isize - 2) - 2);
	fs_super.fs.s_shift = shift;

	/* Free each block, building the free list. This is done in
	   terms of the block shift, while isize is in 512 byte blocks.
	   Adjust isize so that it's in block terms and references the
	   block after the last inode */
	  
	isize <<= shift;

	/* Don't free the block isize because it's got the / directory in it */
	for (j = fsize - 1; j > isize; --j) {
		int n;
		if (swizzle16(fs_super.fs.s_nfree) == 50) {
			dwrite(j, (char *) &fs_super.fs.s_nfree);
			fs_super.fs.s_nfree = 0;
		}

		fs_super.fs.s_tfree =
		    swizzle16(swizzle16(fs_super.fs.s_tfree) + 1);
		n = swizzle16(fs_super.fs.s_nfree);
		fs_super.fs.s_free[n++] = swizzle16(j);
		fs_super.fs.s_nfree = swizzle16(n);
	}

	/* The inodes are already zeroed out */
	/* create the root dir */
	inode[ROOTINODE].i_mode = swizzle16(F_DIR | (0777 & MODE_MASK));
	inode[ROOTINODE].i_nlink = swizzle16(3);
	inode[ROOTINODE].i_size = swizzle32(64);
	inode[ROOTINODE].i_addr[0] = swizzle16(isize);

	/* Reserve reserved inode */
	inode[0].i_nlink = swizzle16(1);
	inode[0].i_mode = ~0;

	dwrite(2, (char *) inode);

	dirbuf[0].d_ino = swizzle16(dirbuf[0].d_ino);
	dirbuf[1].d_ino = swizzle16(dirbuf[1].d_ino);
	dwrite(isize, (char *) dirbuf);

	/* Write out super block */
	dwrite(1, (char *) &fs_super);
	return 0;
}
예제 #25
0
파일: muicfs.c 프로젝트: agentzh2m/SeaIceFS
/**
 * This function deletes the last component of the path (e.g., /a/b/c you
 * need to remove the file 'c' from the directory /a/b). The steps you
 * will take will be the following:
 *
 * 1. Locate the directory entry for the file by resolving the path - this
 *    will probably involve reading each directory in the path to get
 *    the location of the first data block (or location of data block with
 *    the file's inode) of the specified file.
 *
 * 2. Update the directory entry for the deleted file
 *
 * 3. Free any data blocks used by the file
 *
 * Again, these steps are very general and the actual logistics of how
 * to locate data blocks of a file, how to free data blocks and how to update
 * directory entries are dependent on your implementation of directories
 * and file allocation.
 *
 * For time being ignore the fuse_file_info parameter. If you would
 * like to use please see the open function in fuse.h file for more
 * detials.
 *
 * On error (e.g., the path does not exist) xmp_delete will return -1,
 * otherwise xmp_delete should return the number of bytes written.
 *
 */
static int xmp_delete(const char *path)
{
    int* tuple = traverse_dir_r(path);
    int ino = tuple[0];
    free(tuple);
    tuple = traverse_dir_w(path);
    int dir_dnum = tuple[1];
    free(tuple);
    //retrieve inode
    Inode* ibuf = (Inode*)malloc(sizeof(Inode)*INODE_AMT);
    Inode* init_ibuf = ibuf;
    if(dread(global_fd, ino/INODE_AMT, init_ibuf) < 0){
    	RFAIL;
    	return -1;
    }
    ibuf+=ino%INODE_AMT;
    //retrieve dmap need to implement more if dmap use more than 1 blck
    char* dmap_buf = (char*)malloc(sizeof(char) * BLOCKSIZE);
    char* imap_buf = (char*)malloc(sizeof(char) * BLOCKSIZE);
    if(dread(global_fd, DMAP_OFFSET, dmap_buf) < 0){
    	RFAIL;
    	return -1;
    }
    //remove inode and associated dblck
    for(int i = 0; i < ibuf->total_blck; i++){
    	dmap_buf[ibuf->block_pt[i]] = 0;
    }
    if(dwrite(global_fd, DMAP_OFFSET, dmap_buf) < 0){
    	WFAIL;
    	return -1;
    }
    if(dread(global_fd, INODE_OFFSET, imap_buf) < 0){
    	RFAIL;
    	return -1;
    }
    imap_buf[ino] = 0;
    if(dwrite(global_fd, INODE_OFFSET, imap_buf) < 0){
       	RFAIL;
       	return -1;
    }
    //retrieve dir entry
    Directory* dbuf = (Inode*)malloc(sizeof(Directory) * DIR_AMT);
    Directory* init_dbuf = dbuf;
    if(dread(global_fd, DATA_OFFSET + dir_dnum, init_dbuf) < 0){
    	RFAIL;
    	return -1;
    }
    char* last_path;
    for(char* path_pt = strtok(path, "/"); path_pt!=NULL; path_pt = strtok(NULL, "/")){
    	last_path = path_pt;
    }
    //find last path to remove
    for(int i = 0; i < DIR_AMT; i++){
    	if(!strcmp(dbuf->f_name, last_path)){
    		strcpy(dbuf->f_name, "");
    		dbuf->inode_num = -1;
    	}
    }
    if(dwrite(global_fd, DATA_OFFSET + dir_dnum, init_dbuf) < 0){
    	WFAIL;
    	return -1;
    }
    return 0;


}
예제 #26
0
int 
write_struct(int blocknum, void *structp){
	return dwrite(blocknum, structp);
}