Hi friends,

I am trying to learn filesystems and writing a small module. Till now
I am able to do simple mount,cd commands. However in order to support
creating new files, I have implemented the below function. However I
am stuck at few points. It would be of great help if someone can
clarify.

1) When is d_instantiate needed. Is it required always ?? . As far as
i understand it attaches the inode to the dentry object and may not be
needed always.
2) Once I have got the new inode * by a call to new_inode, how do I
fill my file system specific data.. ie... i_private member is
initialised as NULL.
     Should I allocate memory for my inode and set i_private to it ??........


int nngfs_create(struct inode *dir, struct dentry *dentry, int
mode,struct nameidata *nd){
       int inum=0,ret=0;
       struct buffer_head *bh;
       struct inode *newnip;
       struct nngfs_inode *nino;
       struct super_block *sb = dir->i_sb;
       struct nngfs_info *nsb = (struct nngfs_info *)(sb->s_fs_info);
       struct nngfs_superblock *n_sb = nsb->ngfs_sb;

       //See if the entry already exists in the dir
       ret = nngfs_find_entry(dir,dentry->d_name.name);
       if(ret!=-1){
          return ERR_PTR(-EEXIST);
       }

       //See the bitmap and get the inode number
       inum = Get_free_inode_number(n_sb);
       if(inum == -1){
        printk(KERN_INFO "NGFS (%s) :Got free inode number =
%d\n",__FUNCTION__,inum);
        return ERR_PTR(-ENOSPC);
       }

       //Allocate the new inode
       newnip = new_inode(sb);
       if(!newnip){
        printk(KERN_INFO "NGFS (%s) :Could not allocate free
inode\n",__FUNCTION__);
        return ERR_PTR(-ENOSPC);
       }       newnip->i_ino = inum;

       //Add the name information to dir entry
       if(nngfs_dir_add(dir,inum,dentry->d_name.name)){
        printk(KERN_INFO "NGFS (%s) :Could not allocate free
inode\n",__FUNCTION__);
        return ERR_PTR(-EMFILE);
       }

       bh = (struct buffer_head *)(nsb->sbh);
       n_sb->inode_bitmap=setbit(n_sb->inode_bitmap,inum);
       newnip->i_ino = inum;

       dir->i_nlink++;

       newnip->i_mode =  NNGFS_FILE;
       newnip->i_atime = newnip->i_mtime = newnip->i_ctime = CURRENT_TIME;
       newnip->i_uid = dir->i_uid;
       newnip->i_gid = dir->i_gid;


      /* Stuck below this point. Not sure what to do */


       d_instantiate(dir,newnip);
       nino = (struct nngfs_inode *)newnip->i_private;
#ifdef DEBUG
      printk(KERN_CRIT "NGFS (%s)(%d) : Going to reference .... [%p]
",__FUNCTION__,__LINE__,nino);
#endif
       nino->filetype =  NNGFS_FILE;
#ifdef DEBUG
      printk(KERN_CRIT "NGFS (%s)(%d) : Seems referencing is ok ....
",__FUNCTION__,__LINE__);
#endif
       nino->atime = nino->mtime = nino->ctime = CURRENT_TIME;
       nino->uid = dir->i_uid;
       nino->gid = dir->i_gid;

       sb->s_dirt=1;
       mark_buffer_dirty(bh); //Write the new bitmap to disk
       mark_inode_dirty(newnip);
       mark_inode_dirty(dir);
       return 0;
}


Note that i am writing this only for learning, so not much concerned
about other details like atomicity and race conditions etc. at this
point. Also I am not using any caches for my inode allocation.
Appreciate any help/suggestions.


-- 
Thanks & Regards,
********************************************
Manish Katiyar  ( http://mkatiyar.googlepages.com )
3rd Floor, Fair Winds Block
EGL Software Park
Off Intermediate Ring Road
Bangalore 560071, India
***********************************************

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to [EMAIL PROTECTED]
Please read the FAQ at http://kernelnewbies.org/FAQ

Reply via email to