Exemplo n.º 1
0
static int
ospfs_link(struct dentry *src_dentry, struct inode *dir, struct dentry *dst_dentry) {
	/* EXERCISE: Your code here. */
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	ospfs_direntry_t *od;
	ospfs_inode_t *oi;
	uint32_t entry_ino = 0;
	if (dst_dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;
	if (find_direntry(dir_oi, dst_dentry->d_name.name, dst_dentry->d_name.len))
		return -EEXIST;

	od = create_blank_direntry(dir_oi);

	if (IS_ERR(od))
		return -ENOSPC;
	for (entry_ino = 2; entry_ino < ospfs_super->os_ninodes; entry_ino++) {
		oi = ospfs_inode(entry_ino);
		if (oi->oi_nlink == 0)
			break;
	}
	if (entry_ino == ospfs_super->os_ninodes)
		return -EIO;
	od->od_ino = src_dentry->d_inode->i_ino;
	strcpy(od->od_name, dst_dentry->d_name.name);
	oi = ospfs_inode(od->od_ino);
	oi->oi_nlink++;

	return 0;
}
Exemplo n.º 2
0
static ospfs_direntry_t *
create_blank_direntry(ospfs_inode_t *dir_oi)
{
	uint32_t blockno;
	uint32_t off = 1;
	int ret;
	uint32_t entry_off;

	// Outline:
	// 1. Check the existing directory data for an empty entry.  Return one
	//    if you find it.
	// 2. If there's no empty entries, add a block to the directory.
	//    Use ERR_PTR if this fails; otherwise, clear out all the directory
	//    entries and return one of them.

	/* EXERCISE: Your code here. */
	// Search through the directory block
	for (entry_off = 0; entry_off < dir_oi->oi_size;
	     entry_off += OSPFS_DIRENTRY_SIZE) {
		// Find the OSPFS inode for the entry
		ospfs_direntry_t *od = ospfs_inode_data(dir_oi, entry_off);

		// Set 'entry_inode' if we find the file we are looking for
		if (od->od_ino == 0) {
			return od;
		}
	}
	if ((ret = change_size(dir_oi, dir_oi->oi_size + OSPFS_DIRENTRY_SIZE))) {
		return NULL;
	}
	return create_blank_direntry(dir_oi);
}
Exemplo n.º 3
0
static int
ospfs_link(struct dentry *src_dentry, struct inode *dir, struct dentry *dst_dentry) {
	ospfs_direntry_t* link;

	// check if name too long
	if (dst_dentry->d_name.len > OSPFS_MAXNAMELEN)
        return -ENAMETOOLONG;
	// check if directory entry already exists
	if (find_direntry(ospfs_inode(dir->i_ino),
		dst_dentry->d_name.name, dst_dentry->d_name.len))
		return -EEXIST;

	// create new hardlinked file
	link = create_blank_direntry(ospfs_inode(dir->i_ino));
	if (IS_ERR(link))
		return PTR_ERR(link);

	// copy file information
	link->od_ino = src_dentry->d_inode->i_ino;
	memcpy(link->od_name, dst_dentry->d_name.name, dst_dentry->d_name.len);
	link->od_name[dst_dentry->d_name.len] = '\0';

	// increment source file link count
	ospfs_inode(src_dentry->d_inode->i_ino)->oi_nlink++;

	return 0;
}
Exemplo n.º 4
0
static int
ospfs_link(struct dentry *src_dentry, struct inode *dir, struct dentry *dst_dentry) {
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	ospfs_inode_t *src_oi = ospfs_inode(src_dentry->d_inode->i_ino);
	ospfs_direntry_t *entry;


	if (dst_dentry->d_name.len > OSPFS_MAXSYMLINKLEN) {
		return -ENAMETOOLONG;
	}
	
	if (find_direntry(dir_oi, dst_dentry->d_name.name, dst_dentry->d_name.len) != NULL) {
		return -EEXIST;
	}
	
	if (dir_oi == NULL || dir_oi->oi_nlink + 1 == 0) {
		return -EIO;
	}
	
	entry = create_blank_direntry(dir_oi);
	if (IS_ERR(entry)) {
		return PTR_ERR(entry);
	} else if(entry == NULL) {
		return -EIO;
	}
	
	entry->od_ino = src_dentry->d_inode->i_ino;
	memcpy(entry->od_name, dst_dentry->d_name.name, dst_dentry->d_name.len);
	entry->od_name[dst_dentry->d_name.len] = 0;
	
	src_oi->oi_nlink++;
	dir_oi->oi_nlink++;
	return 0;
}
Exemplo n.º 5
0
static int
ospfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;

	ospfs_symlink_inode_t *symlink_inode;

	if (find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;

	if (strlen(symname) > OSPFS_MAXSYMLINKLEN || dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;

	// create a blank direntry, and return error if bad things happen
	ospfs_direntry_t* od = create_blank_direntry(dir_oi);
	if (IS_ERR(od)) {
		return PTR_ERR(od);
	}

	// find empty inode by linear searching the inodes
	for (entry_ino = 0; entry_ino < ospfs_super->os_ninodes; entry_ino++) {
		if (!ospfs_inode(entry_ino)->oi_nlink)
			break;
	}

	// entry_ino is now either a ptr to an empty inode we can use or ninodes (no space)

	if (entry_ino == ospfs_super->os_ninodes)
		return -ENOSPC;

	// write directory entry (inode number and the name)
	od->od_ino = entry_ino;
	memcpy(od->od_name, dentry->d_name.name, dentry->d_name.len);
	od->od_name[dentry->d_name.len] = '\0';

	symlink_inode = ospfs_inode(entry_ino);

	// set values in inode
	symlink_inode->oi_nlink = 1;
	symlink_inode->oi_size = strlen(symname);
	symlink_inode->oi_ftype = OSPFS_FTYPE_SYMLINK;
	memcpy(symlink_inode->oi_symlink, symname, strlen(symname));
	symlink_inode->oi_symlink[strlen(symname)] = '\0';
	

	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 6
0
static int
ospfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;

	// if the entry is found, it already exists
	if (find_direntry(dir, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;

	// if entry name is too long
	if (dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;
	
	// create a blank direntry, and return error if bad things happen
	ospfs_direntry_t* od = create_blank_direntry(dir_oi);
	if (IS_ERR(od)) {
		return PTR_ERR(od);
	}

	// find empty inode by linear searching the inodes
	for (entry_ino = 0; entry_ino < ospfs_super->os_ninodes; entry_ino++) {
		if (!ospfs_inode(entry_ino)->oi_nlink)
			break;
	}

	// entry_ino is now either a ptr to an empty inode we can use or ninodes (no space)

	if (entry_ino == ospfs_super->os_ninodes)
		return -ENOSPC;

	od->od_ino = entry_ino;
	memcpy(od->od_name, dentry->d_name.name, dentry->d_name.len);
	od->od_name[dentry->d_name.len] = '\0';

	ospfs_inode_t* inode = ospfs_inode(entry_ino);

	// set values in inode
	inode->oi_mode = mode;
	inode->oi_nlink = 1;
	inode->oi_size = 0;
	inode->oi_ftype = OSPFS_FTYPE_REG;
	memset(inode->oi_direct, 0, sizeof(inode->oi_direct[0]) * OSPFS_NDIRECT);
	inode->oi_indirect = 0;
	inode->oi_indirect2 = 0;

	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 7
0
static int
ospfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;
	ospfs_direntry_t *entry = NULL;
	ospfs_symlink_inode_t *symlink = NULL;
	/* EXERCISE: Your code here. */
	
	if(dentry->d_name.len > OSPFS_MAXNAMELEN || strlen(symname) > OSPFS_MAXNAMELEN) {
		return -ENAMETOOLONG;
	}
	if(find_direntry(dir_oi,dentry->d_name.name,dentry->d_name.len)) {
		return -EEXIST; 
	}
	
	entry = create_blank_direntry(dir_oi);
	if (IS_ERR(entry)) {
		return PTR_ERR(entry);
	}
	
	while(entry_ino < ospfs_super->os_ninodes) {
		symlink = ospfs_inode(entry_ino);
		if(symlink != NULL && symlink->oi_nlink == 0) {
			break;
		}
		entry_ino++;
	}
	
	if (entry_ino == ospfs_super->os_ninodes) {
		return -ENOSPC;
	}
	
	symlink->oi_size = strlen(symname);
	symlink->oi_ftype = OSPFS_FTYPE_SYMLINK;
	symlink->oi_nlink = 1;
	strncpy(symlink->oi_symlink,symname,symlink->oi_size);
	symlink->oi_symlink[symlink->oi_size] = 0;
	dir_oi->oi_nlink++;
	
	entry->od_ino = entry_ino;
	strncpy(entry->od_name,dentry->d_name.name, dentry->d_name.len);
	entry->od_name[dentry->d_name.len] = 0; 
	
	
	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 8
0
static int
ospfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
{
	//eprintk("simlink");
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;
	//EXERCISE: Your code here. 
	if (dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;
	if (find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;
	//create a new symlink inode
	ospfs_inode_t *free_inode = ospfs_block(ospfs_super->os_firstinob);
	uint32_t count = 1;
	while(count<ospfs_super->os_ninodes)
	{
		if (free_inode[count].oi_nlink == 0)
			break;
		count++;
	}
	//no free inode
	if (count == ospfs_super->os_ninodes)
	{
		count = 0;
		return -ENOSPC;
	}
	else 
		entry_ino = count;
	//create new inode and entry;
	ospfs_symlink_inode_t* symlink_oi = ospfs_inode(entry_ino);
	ospfs_direntry_t * sym_dentry = create_blank_direntry(dir_oi);
	if (IS_ERR(sym_dentry))
		return PTR_ERR(sym_dentry);
	//init symlink inode:
	if (strlen(symname)>OSPFS_MAXSYMLINKLEN)
		return -ENAMETOOLONG;
	symlink_oi->oi_size = strlen(symname);
	symlink_oi->oi_ftype = OSPFS_FTYPE_SYMLINK;
	symlink_oi->oi_nlink = 1;
	memcpy (symlink_oi->oi_symlink, symname, strlen(symname)); 
	symlink_oi->oi_symlink[strlen(symname)] = '\0';
	memcpy (sym_dentry->od_name, dentry->d_name.name, dentry->d_name.len);
	sym_dentry->od_name[dentry->d_name.len] = '\0';
	sym_dentry->od_ino = entry_ino;

	 //Execute this code after your function has successfully created the
	   //file.  Set entry_ino to the created file's inode number before
	   //getting here. 
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 9
0
static int
ospfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;
	ospfs_direntry_t *entry = NULL;
	ospfs_inode_t *temp = NULL;
	/* EXERCISE: Your code here. */
	
	if (dentry->d_name.len > OSPFS_MAXNAMELEN) {
		return -ENAMETOOLONG;
	}
	
	if (find_direntry(ospfs_inode(dir->i_ino), dentry->d_name.name, dentry->d_name.len) != NULL) {
		return -EEXIST;
	}
	
	entry = create_blank_direntry(dir_oi);
	if (IS_ERR(entry)) {
		return PTR_ERR(entry);
	}
	
	while(entry_ino < ospfs_super->os_ninodes) {
		temp = ospfs_inode(entry_ino);
		if(temp != NULL && temp->oi_nlink == 0) {
			break;
		}
		entry_ino++;
	}
	
	if (entry_ino == ospfs_super->os_ninodes) {
		return -ENOSPC;
	}
	
	temp->oi_size = 0;
	temp->oi_ftype = OSPFS_FTYPE_REG;
	temp->oi_nlink = 1;
	temp->oi_mode = mode;
	temp->oi_direct[0] = 0;
	dir_oi->oi_nlink++;
	
	entry->od_ino = entry_ino;
	memcpy(entry->od_name, dentry->d_name.name, dentry->d_name.len);
	entry->od_name[dentry->d_name.len] = 0;

	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 10
0
static int
ospfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	ospfs_direntry_t *dir_entry;
	ospfs_inode_t *inode;
	uint32_t entry_ino = 0;

	// check if name too long
	if (dentry->d_name.len > OSPFS_MAXNAMELEN)
        return -ENAMETOOLONG;
	// check if directory entry already exists
	if (find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;

	// create new directory entry
	dir_entry = create_blank_direntry(dir_oi);
	if (IS_ERR(dir_entry))
		return PTR_ERR(dir_entry);

	// find empty inode
	for (entry_ino = 0; entry_ino < ospfs_super->os_ninodes; entry_ino++) {
		inode = ospfs_inode(entry_ino);
		if (inode->oi_nlink == 0)
			break; // empty inode
	}
	if (entry_ino == ospfs_super->os_ninodes)
		return -ENOSPC;

	// initialize directory entry
	dir_entry->od_ino = entry_ino;
	memcpy(dir_entry->od_name, dentry->d_name.name, dentry->d_name.len);
	dir_entry->od_name[dentry->d_name.len] = '\0';
	// initialize file
	inode->oi_size = 0;
	inode->oi_ftype = OSPFS_FTYPE_REG;
	inode->oi_mode = mode;
	inode->oi_nlink = 1;

	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 11
0
static int
ospfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;

	/* EXERCISE: Your code here. */
	ospfs_direntry_t *od;
	ospfs_symlink_inode_t *oi = NULL;
	if (dentry->d_name.len > OSPFS_MAXNAMELEN || strlen(symname) > OSPFS_MAXSYMLINKLEN)
		return -ENAMETOOLONG;
	if (find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;
	od = create_blank_direntry(dir_oi);
	if (IS_ERR(od))
		return -ENOSPC;
	for (entry_ino = 2; entry_ino < ospfs_super->os_ninodes; entry_ino++) {
		oi = (ospfs_symlink_inode_t *) ospfs_inode(entry_ino);
		if (oi->oi_nlink == 0)
			break;
	}
	if (entry_ino == ospfs_super->os_ninodes)
		return -EIO;
	od->od_ino = entry_ino;
	strcpy(od->od_name, dentry->d_name.name);
	oi->oi_size = strlen(symname);
	oi->oi_ftype = OSPFS_FTYPE_SYMLINK;
	oi->oi_nlink = 1;
	strcpy(oi->oi_symlink, symname);

	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 12
0
static int
ospfs_link(struct dentry *src_dentry, struct inode *dir, struct dentry *dst_dentry) {
	/* EXERCISE: Your code here. */
	//

	ospfs_inode_t *dir_oi =  ospfs_inode(dir->i_ino);
	ospfs_inode_t *dummy_i;

	//if name is too long
	if(dst_dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;

	//if the file already exists
	if(find_direntry(dir_oi, dst_dentry->d_name.name, dst_dentry->d_name.len))
		return -EEXIST;

	// get a directory entry
	ospfs_direntry_t* dir_entry = create_blank_direntry(dir_oi);
	if(IS_ERR(dir_entry))
		return PTR_ERR(dir_entry);
	
	// increment link count
	dummy_i = ospfs_inode(src_dentry->d_inode->i_ino);
	if(!dummy_i)
		return -EIO;
	dummy_i->oi_nlink++;

	// set fields of directory entry
	dir_entry->od_ino = src_dentry->d_inode->i_ino;
	memcpy(dir_entry->od_name, dst_dentry->d_name.name, dst_dentry->d_name.len);
	//add null ending character
	dir_entry->od_name[dst_dentry->d_name.len] = 0;


	//eprintk("Complete\n");

	return 0;

	//return -EINVAL;
}
Exemplo n.º 13
0
int
ospfs_add_entry_lostfound(uint32_t ino, char *d_name)
{
	ospfs_inode_t *dir_oi = ospfs_inode(6);
	ospfs_direntry_t *file_od;
	ospfs_inode_t *file_oi = NULL;
	uint32_t entry_ino = 0;

	if (strlen(d_name) >= OSPFS_MAXNAMELEN) {
		return -ENAMETOOLONG;
	} else if (find_direntry(dir_oi, d_name, strlen(d_name))) {
		return -EEXIST;
	}

	file_od = create_blank_direntry(dir_oi);
	if (file_od == NULL) {
		return -ENOSPC;
	}

	file_od->od_ino = ino;
	memcpy(file_od->od_name, d_name, strlen(d_name));
}
Exemplo n.º 14
0
static int
ospfs_link(struct dentry *src_dentry, struct inode *dir, struct dentry *dst_dentry) {
	// EXERCISE: Your code here. 
	ospfs_inode_t* dir_oi = ospfs_inode (dir->i_ino);
	if (dst_dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;
	if (find_direntry(dir_oi, dst_dentry->d_name.name, dst_dentry->d_name.len))
		return -EEXIST;
	//create new dir entry;
	ospfs_direntry_t *new_dentry = create_blank_direntry (dir_oi);
	if (IS_ERR(new_dentry))
		return PTR_ERR (new_dentry);

	//the link of the inode++
	ospfs_inode_t * target_ino = ospfs_inode(src_dentry->d_inode->i_ino);
	if (!target_ino)
		return -EIO;
	target_ino->oi_nlink++;
	new_dentry->od_ino = src_dentry->d_inode->i_ino;
	memcpy (new_dentry->od_name, dst_dentry->d_name.name, dst_dentry->d_name.len);
	new_dentry->od_name[dst_dentry->d_name.len]='\0';
	return 0;
}
Exemplo n.º 15
0
static int
ospfs_link(struct dentry *src_dentry, struct inode *dir, struct dentry *dst_dentry) {

	// if the name is too long, return too long
	if (dst_dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;
	// if the entry is found, it already exists
	if (find_direntry(ospfs_inode(dir->i_ino), dst_dentry->d_name.name, dst_dentry->d_name.len))
		return -EEXIST;

	ospfs_direntry_t* hard_link = create_blank_direntry(ospfs_inode(dir->i_ino));

	if (IS_ERR(hard_link))
		return PTR_ERR(hard_link);
	
	hard_link->od_ino = src_dentry->d_inode->i_ino;
	memcpy(hard_link->od_name, dst_dentry->d_name.name, dst_dentry->d_name.len);
	hard_link->od_name[dst_dentry->d_name.len] = '\0';

	ospfs_inode(src_dentry->d_inode->i_ino)->oi_nlink++;
	
	return 0;	
}
Exemplo n.º 16
0
static int
ospfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;
	/* EXERCISE: Your code here. */
	//return -EINVAL; // Replace this line
	if (dir_oi == NULL)
		return -EIO;
	if (dentry->d_name.len > OSPFS_MAXNAMELEN)
	{
		return -ENAMETOOLONG;
	}
	// find_direntry(dir_oi, name, namelen)
	if (find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;
	//find a free inode
	// ospfs_block(blockno)
//	Use this function to load a block's contents from "disk".
	ospfs_inode_t* free_inode = ospfs_block(ospfs_super->os_firstinob);
	uint32_t count = 1;
	while(count<ospfs_super->os_ninodes)
	{
		if (free_inode[count].oi_nlink == 0)
			break;
		count++;
	}
	//no free inode
	if (count == ospfs_super->os_ninodes)
	{
		count = 0;
		return -ENOSPC;
	}
	else 
		entry_ino = count;
	
	//initialize inode:
	ospfs_inode_t* new_ino = NULL;
	new_ino = ospfs_inode (entry_ino);
	new_ino -> oi_size = 0;
	new_ino -> oi_ftype = OSPFS_FTYPE_REG;
	new_ino -> oi_nlink = 1;
	new_ino -> oi_mode = mode;
	//initialize entry:
	ospfs_direntry_t* new_dentry = create_blank_direntry(dir_oi);
	if (IS_ERR(new_dentry))
		return PTR_ERR (new_dentry);
	new_dentry->od_ino = entry_ino;
	
	memcpy (new_dentry->od_name, dentry->d_name.name, dentry->d_name.len);
	new_dentry->od_name[dentry->d_name.len]='\0';





	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 17
0
static int
ospfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;

	/* EXERCISE: Your code here. */
	//Error checking..
	if(!dir_oi)
		return -EIO;
	if(dentry->d_name.len > OSPFS_MAXNAMELEN || strlen(symname) > OSPFS_MAXSYMLINKLEN)
		return -ENAMETOOLONG; 
	if(find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;

	//Find free inode in inode block
	int i;
	for(i = ospfs_super->os_firstinob; i < ospfs_super->os_ninodes; i++){
		//Break if inode is free
		ospfs_inode_t *oi = ospfs_inode(i);
		if(oi->oi_nlink == 0)
			break;		
	}
	
	//Could not find any free inodes.
	if(i >= ospfs_super->os_ninodes)
		return -ENOSPC;

	//set up new empty node and names	
	ospfs_symlink_inode_t *symlink = (ospfs_symlink_inode_t*)ospfs_inode(i);
	symlink->oi_size = strlen(symname);
	symlink->oi_ftype = OSPFS_FTYPE_SYMLINK;
	memcpy(symlink->oi_symlink, symname, strlen(symname));

	//Find an empty direntry to store new file
	ospfs_direntry_t *od = create_blank_direntry(dir_oi);
	if(IS_ERR(od))
		return PTR_ERR(od);
	od->od_ino = i;
	memcpy(od->od_name, dentry->d_name.name, dentry->d_name.len);
	//add null character
	od->od_name[dentry->d_name.len] = 0;
	//ospfs_symlink_inode_t *symlink = ospfs_inode(entry_ino); //create sym link

	//for conditional symbolic links
	char* q_ptr = strpbrk(symname, "?");
	char* c_ptr = strpbrk(symname, ":");
	if(q_ptr && c_ptr)
	{
		memcpy(symlink->oi_symlink + strlen(symname) + 1, q_ptr, c_ptr-q_ptr - 1);

	}
	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 18
0
static int
ospfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;

	/* EXERCISE: Your code here. */
	//Check if file name is too long
	if(dentry->d_name.len > OSPFS_MAXNAMELEN)
		return -ENAMETOOLONG;

	//Check if file already exists in directory
	if(find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
		return -EEXIST;
	
	//Find an empty direntry to store new file
	ospfs_direntry_t *od = create_blank_direntry(dir_oi);
	if(IS_ERR(od))
		return PTR_ERR(od);
	
	//Find free inode in inode block
	int i;
	for(i = ospfs_super->os_firstinob; i < ospfs_super->os_ninodes; i++){
		//Break if inode is free
		ospfs_inode_t *oi = ospfs_inode(i);
		if(oi->oi_nlink == 0)
			break;		
	}
	
	//Could not find any free inodes.
	if(i >= ospfs_super->os_ninodes)
		return -ENOSPC;

	//Store new file in empty direntry
	od->od_ino = i;
	
	int j;
	for(j = 0; j < dentry->d_name.len; j++){
		od->od_name[j] = dentry->d_name.name[j];
	}
	od->od_name[j] = '\0';

	//Set inode
	ospfs_inode_t *oi = ospfs_inode(i);
	oi->oi_size = 0;
	oi->oi_ftype = OSPFS_FTYPE_REG;
	oi->oi_nlink = 1;
	oi->oi_mode = mode;
	oi->oi_indirect = 0;
	oi->oi_indirect2 = 0;
	int k;
	for(k = 0; k < OSPFS_NDIRECT; k++){
		oi->oi_direct[k] = 0;	
	}

	//eprintk("create:%d,  %s\n", i, dentry->d_name.name);

	entry_ino = od->od_ino;
	/* Execute this code after your function has successfully created the
	   file.  Set entry_ino to the created file's inode number before
	   getting here. */
	{
		struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
		if (!i)
			return -ENOMEM;
		d_instantiate(dentry, i);
		return 0;
	}
}
Exemplo n.º 19
0
static int
ospfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
	ospfs_inode_t *dir_oi = ospfs_inode(dir->i_ino);
	uint32_t entry_ino = 0;
	/* EXERCISE: Your code here. */ 
	//return -EINVAL; // Replace this line

	//edge cases:
	//ENAMETOOLONG: return if dentry length exceeds max length
	//EEXIST: return if file name already exists
	//ENOSPC: if disk is full
	//EIO: if I/O error

	if (dentry->d_name.len > OSPFS_MAXNAMELEN)
    	return -ENAMETOOLONG;
	if (find_direntry(dir_oi, dentry->d_name.name, dentry->d_name.len))
    	return -EEXIST;

	int upperlimit = ospfs_super->os_ninodes;
	int found_iNode = 0;
	uint32_t inode_counter = 2;
	ospfs_direntry_t *od = create_blank_direntry(dir_oi);
	//find an empty inode to stick our file into
	while (inode_counter < upperlimit)
	{
    	if (ospfs_inode(inode_counter)->oi_nlink == 0) {
        	//if we are in  here, we found an empty inode
        	found_iNode = 1;
        	entry_ino = inode_counter;
    	}
        	inode_counter++;
	}
   	 
	//if we are here and found_iNode is 0, then we didn't find a free inode
	if (found_iNode == 0)
    	return -ENOSPC;
    
	//if we are here, then all is good and we can make our direntry
	od->od_ino = entry_ino;
	strncpy(od->od_name, dentry->d_name.name, dentry->d_name.len);
    
	ospfs_inode_t *file_oi = ospfs_inode(entry_ino);
	file_oi->oi_size = 0;
	file_oi->oi_ftype = OSPFS_FTYPE_REG;
	file_oi->oi_nlink = 1;
	file_oi->oi_mode = mode;
	file_oi->oi_direct[0] = 0;
    
	dir_oi->oi_nlink++;
    
	/* Execute this code after your function has successfully created the
   	file.  Set entry_ino to the created file's inode number before
   	getting here. */
	{
    	struct inode *i = ospfs_mk_linux_inode(dir->i_sb, entry_ino);
    	if (!i)
        	return -ENOMEM;
    	d_instantiate(dentry, i);
    	return 0;
	}
}