Exemplo n.º 1
0
void do_mkdir()
{
    struct inode cur_dir,new_dir;
    char dirname[256];
    struct dir_ent ent;
    scanf("%s",dirname);
    if(strcmp(dirname,".")==0 || strcmp(dirname,"..")==0)
    {
        printf("Error directory name.\n");        
        return ;
    }
    get_inode(pwd,&cur_dir);  
        /*分配一个inode给新的目录*/
    new_dir.i_num=alloc_inode();
    new_dir.i_start_sect=alloc_data();        /*分配一个数据扇区*/
    new_dir.i_nr_sects=1;
    
    new_dir.i_pnum=pwd;
    new_dir.i_mode=2;
    new_dir.i_size=0;

        //给新目录增加默认目录项
    ent.i_num=pwd;    strcpy(ent.name,"..");
    add_entry(&new_dir,&ent);
    ent.i_num=new_dir.i_num;  strcpy(ent.name,".");
    add_entry(&new_dir,&ent);  
 
    ent.i_num=new_dir.i_num;    strcpy(ent.name,dirname);     
    add_entry(&cur_dir,&ent);
    //写入inode数组中
    write_inode(pwd,&cur_dir);
    write_inode(new_dir.i_num,&new_dir); 
}
Exemplo n.º 2
0
static int __logfs_create(struct inode *dir, struct dentry *dentry,
		struct inode *inode, const char *dest, long destlen)
{
	struct logfs_super *super = logfs_super(dir->i_sb);
	struct logfs_inode *li = logfs_inode(inode);
	struct logfs_transaction *ta;
	int ret;

	ta = kzalloc(sizeof(*ta), GFP_KERNEL);
	if (!ta) {
		inode->i_nlink--;
		iput(inode);
		return -ENOMEM;
	}

	ta->state = CREATE_1;
	ta->ino = inode->i_ino;
	mutex_lock(&super->s_dirop_mutex);
	logfs_add_transaction(inode, ta);

	if (dest) {
		/* symlink */
		ret = logfs_inode_write(inode, dest, destlen, 0, WF_LOCK, NULL);
		if (!ret)
			ret = write_inode(inode);
	} else {
		/* creat/mkdir/mknod */
		ret = write_inode(inode);
	}
	if (ret) {
		abort_transaction(inode, ta);
		li->li_flags |= LOGFS_IF_STILLBORN;
		/* FIXME: truncate symlink */
		inode->i_nlink--;
		iput(inode);
		goto out;
	}

	ta->state = CREATE_2;
	logfs_add_transaction(dir, ta);
	ret = logfs_write_dir(dir, dentry, inode);
	/* sync directory */
	if (!ret)
		ret = write_inode(dir);

	if (ret) {
		logfs_del_transaction(dir, ta);
		ta->state = CREATE_2;
		logfs_add_transaction(inode, ta);
		logfs_remove_inode(inode);
		iput(inode);
		goto out;
	}
	d_instantiate(dentry, inode);
out:
	mutex_unlock(&super->s_dirop_mutex);
	return ret;
}
Exemplo n.º 3
0
int main(int argc, char **argv) {
  int devfd;

  // get the device from argv
  if(argc != 2) {
    fprintf(stderr, "need to pass in a device\n");
    exit(1);
  }
  // open device
  devfd = open_device(argv[1], O_WRONLY);

  // find if there is enough space -- seek blocks * block size
  verify_device_space(devfd);

  // write the superblock
  write_superblock(devfd);

  // setup the inodes for /
  write_inode(devfd, 3, 1);

  // setup the inode for lost+found
  write_inode(devfd, 2, 2);

  // skip into file section
  // write directories for / ., ..
  struct rdfs_dirent *p = calloc(3, sizeof(struct rdfs_dirent));
  struct rdfs_dirent *dirent = p;
  p->d_inode = 1;
  strcpy(p->d_name, ".");
  p++;
  p->d_inode = 1;
  strcpy(p->d_name, "..");
  p++;
  p->d_inode = 2;
  strcpy(p->d_name, "lost+found");
  write_directory(devfd, dirent, 3, 0);
  free(dirent);

  // write directories for lost+found ., ..
  p = calloc(2, sizeof(struct rdfs_dirent));
  dirent = p;
  p->d_inode = 2;
  strcpy(p->d_name, ".");
  p++;
  p->d_inode = 1;
  strcpy(p->d_name, "..");
  write_directory(devfd, dirent, 2, 1);

  free(dirent);

  return 0;
}
Exemplo n.º 4
0
/* 将外部文件写入到镜像中 */
void do_write()
{
    struct stat st;
    struct inode cur_dir,new_file;
    struct dir_ent ent;
    char buf[SECTOR_SIZE+10];
    int i;
    FILE *fin;
    char filename[16];
    scanf("%s",filename);
    if(stat(filename,&st)<0) {
        printf("No such file.\n");
        return ;
    }
    fin=fopen(filename,"rb");
    get_inode(pwd,&cur_dir);
    new_file.i_num=alloc_inode();
    new_file.i_pnum=pwd;
    new_file.i_size=st.st_size; 
    new_file.i_mode=1; //标示为文件
    ent.i_num=new_file.i_num;   strcpy(ent.name,filename);    
    add_entry(&cur_dir,&ent);
    
    u32 last_sector=0,cur_sector;
    u32 need=st.st_size/(SECTOR_SIZE-sizeof(u32))+1;
    new_file.i_nr_sects=need;
        //根据文件大小,逐个分配扇区来写入数据,并采用单链表方式连接各个扇区
    for(i=0;i<need;i++)
    {
        cur_sector=alloc_data();
        if(last_sector==0)  {
            new_file.i_start_sect=cur_sector;
        }else{ //上一个扇区到这个扇区的指针
            fseek(fimg,last_sector*SECTOR_SIZE+SECTOR_SIZE-sizeof(u32),SEEK_SET);
            fwrite(&cur_sector,sizeof(u32),1,fimg);
        }
        int nread=fread(buf,sizeof(char),SECTOR_SIZE-sizeof(u32),fin);
        if(nread<0)  {
            printf("Read file %s error.\n",filename );
            return ;
        }
        fseek(fimg,cur_sector*SECTOR_SIZE,SEEK_SET);
        fwrite(buf,sizeof(char),nread,fimg);
        last_sector=cur_sector;
    }
    fseek(fimg,last_sector*SECTOR_SIZE+SECTOR_SIZE-sizeof(u32),SEEK_SET);
    u32 tmp=0;
    fwrite(&tmp,sizeof(u32),1,fimg);
    write_inode(new_file.i_num,&new_file);
    write_inode(cur_dir.i_num,&cur_dir);
}
Exemplo n.º 5
0
Arquivo: util.c Projeto: er13/e2tools
long
delete_file(ext2_filsys fs, ext2_ino_t inode)
{
  struct ext2_inode inode_buf;
  long retval;

  if ((retval = read_inode(fs, inode, &inode_buf)))
    {
      fprintf(stderr, "%s\n", error_message(retval));
      return(retval);
    }

  inode_buf.i_dtime = time(NULL);
  if ((retval = write_inode(fs, inode, &inode_buf)))
    {
      fprintf(stderr, "%s\n", error_message(retval));
      return(retval);
    }

  if ((retval = ext2fs_block_iterate(fs, inode, 0, NULL,
                                     release_blocks_proc, NULL)))
    {
      fprintf(stderr, "%s\n", error_message(retval));
      return(retval);
    }

  ext2fs_inode_alloc_stats(fs, inode, -1);

  return(0);
}
Exemplo n.º 6
0
/*
 * Cross-directory rename, target does not exist.  Just a little nasty.
 * Create a new dentry in the target dir, then remove the old dentry,
 * all the while taking care to remember our operation in the journal.
 */
static int logfs_rename_cross(struct inode *old_dir, struct dentry *old_dentry,
			      struct inode *new_dir, struct dentry *new_dentry)
{
	struct logfs_super *super = logfs_super(old_dir->i_sb);
	struct logfs_disk_dentry dd;
	struct logfs_transaction *ta;
	loff_t pos;
	int err;

	/* 1. locate source dd */
	err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
	if (err)
		return err;

	ta = kzalloc(sizeof(*ta), GFP_KERNEL);
	if (!ta)
		return -ENOMEM;

	ta->state = CROSS_RENAME_1;
	ta->dir = old_dir->i_ino;
	ta->pos = pos;

	/* 2. write target dd */
	mutex_lock(&super->s_dirop_mutex);
	logfs_add_transaction(new_dir, ta);
	err = logfs_write_dir(new_dir, new_dentry, old_dentry->d_inode);
	if (!err)
		err = write_inode(new_dir);

	if (err) {
		super->s_rename_dir = 0;
		super->s_rename_pos = 0;
		abort_transaction(new_dir, ta);
		goto out;
	}

	/* 3. remove source dd */
	ta->state = CROSS_RENAME_2;
	logfs_add_transaction(old_dir, ta);
	err = logfs_delete_dd(old_dir, pos);
	if (!err)
		err = write_inode(old_dir);
	LOGFS_BUG_ON(err, old_dir->i_sb);
out:
	mutex_unlock(&super->s_dirop_mutex);
	return err;
}
Exemplo n.º 7
0
static int logfs_remove_inode(struct inode *inode)
{
	int ret;

	inode->i_nlink--;
	ret = write_inode(inode);
	LOGFS_BUG_ON(ret, inode->i_sb);
	return ret;
}
Exemplo n.º 8
0
Arquivo: dir.c Projeto: bmiro/EmoFS
/** Crea l'enllaç de una entrada de directori del src_path a l'inode espeificic
 * per la altre entrada de directori link_path. Actualitza la quatitat
 * d'enllaços d'entrades en el directori de l'inode. Té control de
 * concurrència.
 * @src_path: ruta del fitxer a enllaçar
 * @link_path: ruta de l'enllaç
 * @return: 0 si correctament.
 *	    -1 en cas d'error.
 */
int emofs_link(const char *src_path, const char *link_path) {
	int p_dir_src_inode = 0;
	int p_src_inode = 0;
	int p_dir_dst_inode = 0;
	int p_dst_inode = 0;
	int p_entry = 0;

	emofs_inode inode;
	emofs_dir_entry dir_entry;
		
	char link_name[MAX_FILENAME_LEN];
	char partial_link_path[MAX_PATH_LEN];

	if (!mutex){
		emofs_sem_get(&mutex);
	}
	emofs_sem_wait(mutex);
	
	emofs_get_partial_path(link_path, partial_link_path, link_name);
	/* Comprovam i obtenim informació del primer path */
	p_dir_src_inode = 0;
	if (emofs_find_entry(src_path, \
			     &p_dir_src_inode, &p_src_inode, &p_entry) == -1) {
		puts("EmoFS_Link: El fitxer o directori font no existeix");
		emofs_sem_signal(mutex);
		return -1;
	}
	
	/* Comprovam que no estiqui ja creat el fitxer */
	p_dir_dst_inode = 0;
	if (emofs_find_entry(link_path, \
			     &p_dir_dst_inode, &p_dst_inode, &p_entry) == 0) {
		puts("EmoFS_Link: El fitxer ja existeix");
		emofs_sem_signal(mutex);
		return -1;
	}
	
	/* Incrementam el numero d'enllaços de l'inode font */
	read_inode(p_src_inode, &inode);
	inode.link_count++;
	write_inode(p_src_inode, &inode);
	
	/* Cream l'entrada de directori de l'enllaç */
	dir_entry.inode = p_src_inode;
	strcpy(dir_entry.name, link_name);
	
	/* Anam a escriure l'entrada de directori al direcotri desti */
	read_inode(p_dir_dst_inode, &inode); /* Per saber el tamany de fitxer */
	if (write_file(p_dir_dst_inode, &dir_entry, \
		       inode.size, sizeof(emofs_dir_entry)) == -1) {
		puts("EmoFS_Link: Error d'escriptura");
		emofs_sem_signal(mutex);
		return -1;
	}
	emofs_sem_signal(mutex);
	return 0;	
}
Exemplo n.º 9
0
static int msfs_ftruncate(const char * path, off_t off, struct fuse_file_info * fi) {
	printf("msfs_ftruncate(%s)\n", path);
	reset_error();
	inode_t inode = read_inode((addr_t)fi->fh);
	if(msfs_error != 0) return msfs_error;
	if(!check_access(&inode, O_TRUNC)) return -EPERM;
	inode.attributes.st_size = (addr_t) off;
	write_inode(&inode);
	return msfs_error;
}
Exemplo n.º 10
0
int main ( int argc, char * argv [] )
{
	struct gfs_super *super;
	struct conf conf;
	int super_len, i;
	
	prase_cmdLine(&conf, argv, argc);
	init_conf(&conf);	
	super_len = init_super(&conf, &super);
	
	write_inode(&conf, 0, super, super_len, S_IFREG | 0777);
	write_inode(&conf, 1, NULL, 0, S_IFDIR | 0777);
	for( i = 0;i < conf.nblocks; i++)
		write_inode(&conf, i+2, NULL, 0, 0);
		
	printf("Created fs in file %s.\n%ld blocks with bs %d.\nTotal size %ld bytes.\n",
			conf.fname, conf.nblocks, conf.block_size, conf.nblocks*conf.block_size);	
	return 0;
}
Exemplo n.º 11
0
int Server_UnLink(int inum,char *name)
{	int inode_num,i;
	int dir_inum=inum;
	int last_slash=-1;
	struct inode curr_inode;
	char dir_path[MAXPATHNAMELEN];
	
	if(inum<1||inum>max_inode_num||name==NULL)
		return ERROR;

	inode_num=LookUp(name,inum,0);
	if((inode_num==ERROR)||(inode_num==0))
	{	printf("\nERROR: Path Not Found!");
		return ERROR;
	}

	GetInode(inode_num,&curr_inode);
	if(curr_inode.type==INODE_DIRECTORY)
	{	printf("\nERROR: Cannot Unlink a Directory!");
		return ERROR;
	}

	for(i=0;i<strlen(name);i++)
		if(name[i]=='/')
			last_slash=i;
	if(last_slash==-1)
		dir_inum=inum;
	else if (last_slash==0)
		dir_inum=ROOTINODE;
	else
	{
		strncpy(dir_path,name,last_slash);
		dir_path[last_slash]='\0';
		dir_inum=LookUp(dir_path,inum,0);
	}

	if(curr_inode.nlink>1)
	{
		curr_inode.nlink=curr_inode.nlink-1;
		
	}
	/*Else Delete the file inode and all the blocks allocated to it*/
	else 
	{
		truncate_file(&curr_inode);
		curr_inode.type=INODE_FREE;
		Add_free_inode(inode_num);
		
	}
	Delete_from_dir(dir_inum,inode_num);
	write_inode(inode_num,&curr_inode);
	return 0;

}
Exemplo n.º 12
0
Arquivo: inode.c Projeto: lithoxs/elks
void sync_inodes(kdev_t dev)
{
    register struct inode *inode = first_inode;

    do {
	if (dev && inode->i_dev != dev)
	    continue;
	wait_on_inode(inode);
	if (inode->i_dirt)
	    write_inode(inode);
    } while ((inode = inode->i_prev) != first_inode);
}
Exemplo n.º 13
0
Arquivo: inode.c Projeto: Levrifon/ASE
/* alloue et initialise un bloc et range l'inoeud dedans retourne l'inode correspondant*/
unsigned int create_inode(enum file_type_e type) {
	int i,inumber;
	struct inode_s inode;
	inode.type = type;
	inode.ind_size = 0; /* inode size */
	for(i = 0 ; i < NDIRECT; i++) {
		inode.direct[i] = 0;
	}
	inumber = new_bloc();
	if(inumber == 0) { return 0;} /* volume plein */
	write_inode(inumber,&inode); /*on alloue le bloc pour y ranger l'inoeud */
	return inumber;
}
Exemplo n.º 14
0
void
close_ifile(file_desc_t *fd)
{
    struct inode_s inode;
    
    /* if the buffer is dirty, flush the file */
    flush_ifile(fd);
    
    /* update the inode information (size) */
    read_inode(fd->fds_inumber, &inode);
    inode.ind_size = fd->fds_size;
    write_inode(fd->fds_inumber, &inode);
}
Exemplo n.º 15
0
int do_write(int fd, char * buf, int n) {

	//_Cli();
	int inode_number = search_for_fd(fd); //search fd in inode;
	if (inode_number == -1) {
		return -1;
	}
	iNode * inode = fs_get_inode(inode_number);
	return write_inode(inode, buf, n);
	//_Sti();	
	//return 1;

}
Exemplo n.º 16
0
Arquivo: inode.c Projeto: Levrifon/ASE
unsigned int create_inode(enum file_type_e type) {
	int i,inumber;
	struct inode_s inode;
	inode.type = type;
	inode.ind_size = 0;
	inode.taille = 0;
	for(i = 0 ; i < n ; i++) {
		inode.inode_direct[i] = 0;
	}
	inumber = new_bloc();
	if(inumber == 0) { return 0;} /* volume plein */
	write_inode(inumber,&inode);
	return inumber;
}
Exemplo n.º 17
0
void sync_inodes(kdev_t dev)
{
    register struct inode *inode;
    register char *pi;

    inode = first_inode;
    for (pi = 0; ((int) pi) < nr_inodes * 2; pi++, inode = inode->i_next) {
	if (dev && inode->i_dev != dev)
	    continue;
	wait_on_inode(inode);
	if (inode->i_dirt)
	    write_inode(inode);
    }
}
Exemplo n.º 18
0
struct inode *get_empty_inode(void)
{
    static ino_t ino = 0;
    register struct inode *inode, *best;
    int i;

  repeat:
    inode = first_inode;
    best = 0;
    for (inode = inode_block, i = 0; i < nr_inodes; inode++, i++) {
	if (!inode->i_count && !inode->i_lock && !inode->i_dirt) {
	    best = inode;
	    break;
	}
    }
    if (!best) {
	printk("VFS: No free inodes - contact somebody other than Linus\n");
	list_inode_status();
	sleep_on(&inode_wait);
	goto repeat;
    }
/* Here we are doing the same checks again. There cannot be a significant *
 * race condition here - no time has passed */
#if 0
    if (inode->i_lock) {
	wait_on_inode(inode);
	goto repeat;
    }
    if (inode->i_dirt) {
	write_inode(inode);
	goto repeat;
    }
    if (inode->i_count)
	goto repeat;
#endif
    clear_inode(inode);
    inode->i_count = inode->i_nlink = 1;
#ifdef BLOAT_FS
    inode->i_version = ++event;
#endif
    inode->i_sem = 0;
    inode->i_ino = ++ino;
    inode->i_dev = 0;
    nr_free_inodes--;
    if (nr_free_inodes < 0) {
	printk("VFS: get_empty_inode: bad free inode count.\n");
	nr_free_inodes = 0;
    }
    return inode;
}
Exemplo n.º 19
0
static int logfs_unlink(struct inode *dir, struct dentry *dentry)
{
	struct logfs_super *super = logfs_super(dir->i_sb);
	struct inode *inode = dentry->d_inode;
	struct logfs_transaction *ta;
	struct page *page;
	pgoff_t index;
	int ret;

	ta = kzalloc(sizeof(*ta), GFP_KERNEL);
	if (!ta)
		return -ENOMEM;

	ta->state = UNLINK_1;
	ta->ino = inode->i_ino;

	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;

	page = logfs_get_dd_page(dir, dentry);
	if (!page) {
		kfree(ta);
		return -ENOENT;
	}
	if (IS_ERR(page)) {
		kfree(ta);
		return PTR_ERR(page);
	}
	index = page->index;
	page_cache_release(page);

	mutex_lock(&super->s_dirop_mutex);
	logfs_add_transaction(dir, ta);

	ret = logfs_delete(dir, index, NULL);
	if (!ret)
		ret = write_inode(dir);

	if (ret) {
		abort_transaction(dir, ta);
		printk(KERN_ERR"LOGFS: unable to delete inode\n");
		goto out;
	}

	ta->state = UNLINK_2;
	logfs_add_transaction(inode, ta);
	ret = logfs_remove_inode(inode);
out:
	mutex_unlock(&super->s_dirop_mutex);
	return ret;
}
Exemplo n.º 20
0
Arquivo: dir.c Projeto: bmiro/EmoFS
/** Borra l'entrada de directori especificada y en cas de que sigui l'últim
 * enllaç existent borra el propi fitxer/directori. Té control de concurrència.
 * @path: ruta del fitxer/directori
 * @return:  0 si correctament
 *	     -1 en cas d'error.
 */
int emofs_unlink(const char *path) {
	int p_inode = 0;
	int p_dir_inode = 0;
	int p_entry = 0;

	emofs_inode inode;
	emofs_dir_entry *last_dir_entry;

	if(!mutex){
		emofs_sem_get(&mutex);
	}
	emofs_sem_wait(mutex);
	if (emofs_find_entry(path, &p_dir_inode, &p_inode, &p_entry) == -1) {
		puts("EmoFS_UnLink: El fitxer o directori font no existeix");
		emofs_sem_signal(mutex);
		return -1;
	}

	read_inode(p_dir_inode, &inode);
	/* Borram entrada de directori */
	if (inode.size > sizeof(emofs_dir_entry)) {
		/* Hi ha més entrades posam l'última al lloc de la que volem 
		 * borrar. */
		read_file(p_dir_inode, (void *)&last_dir_entry, \
			  inode.size-sizeof(emofs_dir_entry), \
			  sizeof(emofs_dir_entry));
		write_file(p_dir_inode, last_dir_entry, \
			   p_entry*sizeof(emofs_dir_entry), \
			   sizeof(emofs_dir_entry));
	}

	free(last_dir_entry);

	/* Truncam per l'ultima entrada, si sols hi ha la que volem borrar be
	 * sino llevam l'ultima que ara estara copiada al lloc de la que voliem
	 * borrar. */
	truncate_file(p_dir_inode, inode.size-sizeof(emofs_dir_entry));
	read_inode(p_inode, &inode);
	if (inode.link_count == 0) {
		/* Hem de alliberar blocs de dades i inode */
		truncate_file(p_inode, 0);
		free_inode(p_inode);
	} else {
		inode.link_count--;
		write_inode(p_inode, &inode);
	}
	emofs_sem_signal(mutex);
	return 0;
}
Exemplo n.º 21
0
int main(int argc, char *argv[])
{
	int fd;
	ssize_t ret;

	char welcomefile_body[] = "Love is God. God is Love. Anbe Murugan.\n";
	struct simplefs_inode welcome = {
		.mode = S_IFREG,
		.inode_no = WELCOMEFILE_INODE_NUMBER,
		.data_block_number = WELCOMEFILE_DATABLOCK_NUMBER,
		.file_size = sizeof(welcomefile_body),
	};
	struct simplefs_dir_record record = {
		.filename = "vanakkam",
		.inode_no = WELCOMEFILE_INODE_NUMBER,
	};

	if (argc != 2) {
		printf("Usage: mkfs-simplefs <device>\n");
		return -1;
	}

	fd = open(argv[1], O_RDWR);
	if (fd == -1) {
		perror("Error opening the device");
		return -1;
	}

	ret = 1;
	do {
		if (write_superblock(fd))
			break;
		if (write_inode_store(fd))
			break;

		if (write_inode(fd, &welcome))
			break;
		if (write_dirent(fd, &record))
			break;
		if (write_block(fd, welcomefile_body, welcome.file_size))
			break;

		ret = 0;
	} while (0);

	close(fd);
	return ret;
}
Exemplo n.º 22
0
unsigned int create_inode(enum file_type_e type)
{
	struct inode_s inode;
	int i;
	/*memset(&inode, 0, sizeof(struct inode_s)); */	
	inode.type = type;
	inode.size = 0;
	inode.blocs_double_indirect = 0;
	inode.blocs_indirect = 0;
	for (i=0; i<NB_DIRECTS; i++){
		inode.blocs_directs[i] = 0;
	}
	unsigned int inumber = new_bloc();
	write_inode(inumber, &inode);
	return inumber;
}
Exemplo n.º 23
0
void iput(register struct inode *inode)
{
    if (inode) {
	wait_on_inode(inode);
	if (!inode->i_count) {
	    printk("VFS: iput: trying to free free inode\n");
	    printk("VFS: device %s, inode %lu, mode=0%07o\n",
		   kdevname(inode->i_rdev), inode->i_ino, inode->i_mode);
	    return;
	}
#ifdef NOT_YET
	if (inode->i_pipe)
	    wake_up_interruptible(&PIPE_WAIT(*inode));
#endif
      repeat:
	if (inode->i_count > 1) {
	    inode->i_count--;
	    return;
	}

	wake_up(&inode_wait);
#ifdef NOT_YET
	if (inode->i_pipe) {
	    /* Free up any memory allocated to the pipe */
	}
#endif

	if (inode->i_sb) {
	    struct super_operations *sop = inode->i_sb->s_op;
	    if (sop && sop->put_inode) {
		sop->put_inode(inode);
		if (!inode->i_nlink)
		    return;
	    }
	}

	if (inode->i_dirt) {
	    write_inode(inode);	/* we can sleep - so do again */
	    wait_on_inode(inode);
	    goto repeat;
	}
	inode->i_count--;
	nr_free_inodes++;
    }

    return;
}
Exemplo n.º 24
0
unsigned int create_inode(enum file_type_e type) {
	struct inode_s inode;
	unsigned int inumber;
	memset(&inode, 0, sizeof(struct inode_s));
	inode.inode_type = type;

	inumber = new_bloc();

	if(inumber == BLOC_NULL) {
		fprintf(stderr, "Impossible to create new inode. no space remain\n");
		return BLOC_NULL;
	}

	write_inode(inumber, &inode);

	return inumber;
}
Exemplo n.º 25
0
void
push_free_inode(inode* free_inode)
{
    void* superblock = malloc(SD_SECTORSIZE); //gets superblock
    safe_read(0, superblock);
    int free_inode_num = *((int*)superblock);
    free(superblock);

    // advance free inode list
    int free_list_buf = free_inode->next_inode_num;
    write_to_offset(0, 0, (void*)&free_list_buf, sizeof(int));

    free_inode->next_inode_num = free_inode_num;

    write_inode(free_inode_num, (void*)free_inode);
    free(free_inode);
}
Exemplo n.º 26
0
Arquivo: inode.c Projeto: lithoxs/elks
static struct inode *get_empty_inode(void)
{
    static ino_t ino = 0;
    register struct inode *inode;

    do {
        inode = first_inode->i_next;
        do {
            if (!inode->i_count && !inode->i_dirt && !inode->i_lock) {
                goto found_empty_inode;
            }
        } while ((inode = inode->i_next) != first_inode->i_next);
        printk("VFS: No free inodes\n");
        list_inode_status();
        sleep_on(&inode_wait);
    } while (1);
  found_empty_inode:
/* Here we are doing the same checks again. There cannot be a significant *
 * race condition here - no time has passed */
#if 0
    if (inode->i_lock) {
	wait_on_inode(inode);
	goto repeat;
    }
    if (inode->i_dirt) {
	write_inode(inode);
	goto repeat;
    }
    if (inode->i_count)
	goto repeat;
#endif
    clear_inode(inode);
    inode->i_count = inode->i_nlink = 1;
    inode->i_uid = current->euid;
#ifdef BLOAT_FS
    inode->i_version = ++event;
#endif
    inode->i_ino = ++ino;
    nr_free_inodes--;
    if (nr_free_inodes < 0) {
	printk("VFS: get_empty_inode: bad free inode count.\n");
	nr_free_inodes = 0;
    }
    return inode;
}
Exemplo n.º 27
0
Arquivo: file.c Projeto: bmiro/EmoFS
/** Trunca un fitxer a partir del byte n.
 * Si n_bytes = 0 alliberam tots els blocs.
 * @inode: el nombre d'inode.
 * @n_byte: el byte final del fitxer.
 * @return: 0 si exit.
 */
int truncate_file(int inode, int n_byte) {
	int how_many, block, f_offset, l_size;
	int n_block, n_bytes, n_inode;
	int i;
	emofs_inode tmp_inode;
	int blocks_to_truncate;

	read_inode(inode, &tmp_inode);

	if (tmp_inode.size <= n_byte) {
		/* Si fitxer es mes petit o del tamany a truncar
		 * no s'ha de fer res. */
		return 0;
	}

	blocks_to_truncate = (tmp_inode.size-n_byte)/BLOCK_SIZE;
	for(i = INDIRECT_POINTER_COUNT-1; i >= 0; i--) {
		if(tmp_inode.indirect_pointer[i] != NULL_POINTER) {
			truncate_assist(tmp_inode.indirect_pointer[i], i,  \
					&tmp_inode, &blocks_to_truncate);
			if (blocks_to_truncate > 0) {
				free_block(tmp_inode.indirect_pointer[i]);
				tmp_inode.indirect_pointer[i] = NULL_POINTER;
				tmp_inode.block_count--;
				blocks_to_truncate--;
			}
		}
	}
	for(i = DIRECT_POINTER_COUNT-1; i >= 0; i--) {
		/* El blocks_to_truncate > 0 no esta a la condicio
		 * del bucle per donar suport als fitxers esparsos. */
		if (blocks_to_truncate > 0 && \
		    tmp_inode.direct_pointer[i] != NULL_POINTER) {
			free_block(tmp_inode.direct_pointer[i]);
			tmp_inode.direct_pointer[i] = NULL_POINTER;
			tmp_inode.block_count--;
			blocks_to_truncate--;
		}
	}
	
	tmp_inode.size = n_byte;
	write_inode(inode, &tmp_inode);
	return 0;
}
Exemplo n.º 28
0
static int logfs_replace_inode(struct inode *dir, struct dentry *dentry,
		struct logfs_disk_dentry *dd, struct inode *inode)
{
	loff_t pos;
	int err;

	err = logfs_get_dd(dir, dentry, dd, &pos);
	if (err)
		return err;
	dd->ino = cpu_to_be64(inode->i_ino);
	dd->type = logfs_type(inode);

	err = write_dir(dir, dd, pos);
	if (err)
		return err;
	log_dir("Replace dentry (%lx, %llx) %s -> %llx\n", dir->i_ino, pos,
			dd->name, be64_to_cpu(dd->ino));
	return write_inode(dir);
}
Exemplo n.º 29
0
int ext2_truncate(inode_t *inode, off_t off) {

        __u32 size = off;
        ext2_fs_instance_t *instance = (ext2_fs_instance_t*)inode->i_instance;
        struct ext2_inode *einode = read_inode(instance, inode->i_ino);
        if (einode) {

                __u32 n_blk;
                if (size > 0) {
                        n_blk   = (size - 1) / (1024 << instance->superblock.s_log_block_size) + 1;
                } else {
                        n_blk = 1;
                }

                __u32 addr = get_data_block (instance,einode, n_blk);;
                if (addr) {
                       
                        while (addr) {
                                if (off <= 0) {
                                        free_block(instance, addr / (1024 << instance->superblock.s_log_block_size));
                                } else {
                                        off -= 1024 << instance->superblock.s_log_block_size;
                                }
                                n_blk++;
                                addr = get_data_block (instance,einode, n_blk);
                        }
                } else {
                       
                        while (off > 0) {
                                set_block_inode_data(instance, einode, n_blk, alloc_block(instance));
                                n_blk++;
                                off -= 1024 << instance->superblock.s_log_block_size;
                        }
                }

                einode->i_size = size;
                write_inode(instance, inode->i_ino, einode);
                ext2inode_2_inode(inode, inode->i_instance, inode->i_ino, einode);
                return 0;
        }
        return -ENOENT;
}
Exemplo n.º 30
0
int ext2_truncate(inode_t *inode, off_t off) {

	uint32_t size = off;
	ext2_fs_instance_t *instance = (ext2_fs_instance_t*)inode->i_instance;
	struct ext2_inode *einode = read_inode(instance, inode->i_ino);
	if (einode) {
// TODO vérifier le bon fonctionnement.
		int n_blk;
		if (size > 0) {
			n_blk	= (size - 1) / (1024 << instance->superblock.s_log_block_size) + 1;
		} else {
			n_blk = 1;
		}

		int addr = addr_inode_data2(instance, einode, n_blk);
		if (addr) {
			// 1er cas : off est plus petit que la taille du fichier.
			while (addr) {
				if (off <= 0) {
					free_block(instance, addr / (1024 << instance->superblock.s_log_block_size));
				} else {
					off -= 1024 << instance->superblock.s_log_block_size;
				}
				n_blk++;
				addr = addr_inode_data2(instance, einode, n_blk);
			}
		} else {
			// 2er cas : off est plus grand.
			while (off > 0) {
				set_block_inode_data(instance, einode, n_blk, alloc_block(instance));
				n_blk++;
				off -= 1024 << instance->superblock.s_log_block_size;
			}
		}

		einode->i_size = size;
		write_inode(instance, inode->i_ino, einode);
		ext2inode_2_inode(inode, inode->i_instance, inode->i_ino, einode);
		return 0;
	}
	return -ENOENT;
}