*** empty log message ***
[yaffs/.git] / yaffs_fs.c
index d494e83b7f016874bed3af57a1349a865f4baadd..29c84089cb0a155aeffaca204f514bf258b2aee3 100644 (file)
@@ -21,7 +21,7 @@
  *
  * Acknowledgements:
  * * Luc van OostenRyck for numerous patches.
- * * Nick Bane for patches marked NCB.
+ * * Nick Bane for numerous patches.
  * * Some code bodily lifted from JFFS2.
  */
 
@@ -90,6 +90,9 @@ static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
 static void yaffs_read_inode (struct inode *inode);
 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
 static void yaffs_put_inode (struct inode *inode);
+static void yaffs_delete_inode(struct inode *);
+static void yaffs_clear_inode(struct inode *);
+
 static int yaffs_readpage(struct file *file, struct page * page);
 
 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
@@ -101,7 +104,6 @@ static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
 
 
 
-
 static struct address_space_operations yaffs_file_address_operations = {
        readpage:               yaffs_readpage,
        prepare_write:  yaffs_prepare_write,
@@ -160,9 +162,9 @@ static struct super_operations yaffs_super_ops = {
        read_inode:             yaffs_read_inode,
        put_inode:              yaffs_put_inode,
        put_super:              yaffs_put_super,
-//     read_inode:
 //     remount_fs:
-//     clear_inode:
+       delete_inode:           yaffs_delete_inode,
+       clear_inode:            yaffs_clear_inode,
 };
 
 
@@ -257,13 +259,14 @@ static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
                
                if(inode)
                {
-                       T((KERN_DEBUG"yaffs_loookup looks good\n"));
-                       // try to fix ln -s prob dget(dentry); // try to solve directory bug
+                       T((KERN_DEBUG"yaffs_loookup dentry \n"));
+                       //dget(dentry); // try to solve directory bug
                        d_add(dentry,inode);
                        
                        yaffs_GrossUnlock(dev);
 
-                       return dentry;
+                       // return dentry;
+                       return NULL;
                }
 
        }
@@ -275,18 +278,57 @@ static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
        yaffs_GrossUnlock(dev);
        
        return NULL;
+       //      return (ERR_PTR(-EIO));
        
 }
 
 // For now put inode is just for debugging
+// Put inode is called when the inode **structure** is put.
 static void yaffs_put_inode(struct inode *inode)
 {
        T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
        
-       // yaffs_FlushFile(yaffs_InodeToObject(inode));
+}
+
+// clear is called to tell the fs to release any per-inode data it holds
+static void yaffs_clear_inode(struct inode *inode)
+{
+       yaffs_Object *obj = yaffs_InodeToObject(inode);
+       
+       T(("yaffs_clear_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
+               obj ? "object exists" : "null object"));        
+
+       if(obj)
+       {
+               obj->myInode = NULL;
+               inode->u.generic_ip = NULL;     
+       }
+       
        
 }
 
+// delete is called when the link count is zero and the inode
+// is put (ie. nobody wants to know about it anymore, time to
+// delete the file).
+// NB Must call clear_inode()
+static void yaffs_delete_inode(struct inode *inode)
+{
+       yaffs_Object *obj = yaffs_InodeToObject(inode);
+       yaffs_Device *dev;
+
+       T(("yaffs_delete_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
+               obj ? "object exists" : "null object"));
+       
+       if(obj)
+       {
+               dev = obj->myDev;
+               yaffs_GrossLock(dev);
+               yaffs_DeleteFile(obj);
+               yaffs_GrossUnlock(dev);
+       }
+       clear_inode(inode);
+}
+
 
 static int yaffs_file_flush(struct file* file)
 {
@@ -455,6 +497,7 @@ static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
                
                
                inode->u.generic_ip = obj;
+               obj->myInode = inode;
                
        }
        else
@@ -473,7 +516,8 @@ struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Ob
        inode = iget(sb,obj->objectId);
 
        // NB Side effect: iget calls back to yaffs_read_inode().
-
+       // iget also increments the inode's i_count
+       
        return inode;
 }
 
@@ -753,6 +797,8 @@ static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
 {
        int retVal;
+       int nlinks;
+       
        yaffs_Device *dev;
        
        
@@ -761,11 +807,17 @@ static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
        dev = yaffs_InodeToObject(dir)->myDev;
        
        yaffs_GrossLock(dev);
+       
+       nlinks = 
+       
        retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
+       
+       
        yaffs_GrossUnlock(dev);
        
        if( retVal == YAFFS_OK)
        {
+               dentry->d_inode->i_nlink--;
                return 0;
        }
        else
@@ -794,10 +846,23 @@ static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dent
        
        link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
        
+
+       if(link)
+       {
+               old_dentry->d_inode->i_nlink =  yaffs_GetObjectLinkCount(obj);
+               d_instantiate(dentry, old_dentry->d_inode);
+               atomic_inc(&old_dentry->d_inode->i_count);
+               T((KERN_DEBUG"yaffs_link link count %d i_count %d\n",   
+                       old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
+       
+       }
+       
        yaffs_GrossUnlock(dev);
        
+
        if(link)
        {
+       
                return 0;
        }
        
@@ -853,6 +918,8 @@ static int yaffs_sync_object(struct file * file, struct dentry *dentry, int data
 
 /*
  * The VFS layer already does all the dentry stuff for rename.
+ *
+ * NB: POSIX says you can rename an object over an old object of the same name
  */
 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
 {
@@ -916,7 +983,7 @@ static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
        buf->f_type = YAFFS_MAGIC;
        buf->f_bsize = sb->s_blocksize;
        buf->f_namelen = 255;
-       buf->f_blocks = dev->nBlocks * YAFFS_CHUNKS_PER_BLOCK/
+       buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/
                                                (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
        buf->f_files = 0;
        buf->f_ffree = 0;
@@ -980,6 +1047,7 @@ static void  yaffs_MTDPutSuper(struct super_block *sb)
 
 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
 {
+       int nBlocks;
        struct inode * inode;
        struct dentry * root;
        yaffs_Device *dev;
@@ -1030,9 +1098,9 @@ static struct super_block *yaffs_internal_read_super(int useRam, struct super_bl
                memset(dev,0,sizeof(yaffs_Device));
                dev->genericDevice = NULL; // Not used for RAM emulation.
 
-               dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
+               nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
                dev->startBlock = 1;  // Don't use block 0
-               dev->endBlock = dev->nBlocks - 1;
+               dev->endBlock = nBlocks - 1;
 
                dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
                dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
@@ -1108,10 +1176,9 @@ static struct super_block *yaffs_internal_read_super(int useRam, struct super_bl
 
                // Set up the memory size parameters....
                
-// NCB         dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
-               dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
+               nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
                dev->startBlock = 1;  // Don't use block 0
-               dev->endBlock = dev->nBlocks - 1;
+               dev->endBlock = nBlocks - 1;
 
                // ... and the functions.
                dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
@@ -1177,6 +1244,7 @@ static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SI
 
 
 static struct proc_dir_entry *my_proc_entry;
+static struct proc_dir_entry *my_proc_ram_write_entry;
 
 
 static int  yaffs_proc_read(
@@ -1203,6 +1271,21 @@ static int  yaffs_proc_read(
        return strlen(my_buffer);
 }
 
+
+static int  yaffs_proc_ram_write(
+        char *page,
+       char **start,
+       off_t offset,
+       int count,
+       int *eof,
+       void *data
+       )
+{
+
+       printk(KERN_DEBUG "yaffs write size %d\n",count);
+       return count;
+}
+
 static int __init init_yaffs_fs(void)
 {
        int error = 0;
@@ -1228,6 +1311,18 @@ static int __init init_yaffs_fs(void)
 
 #ifdef CONFIG_YAFFS_RAM_ENABLED
 
+    my_proc_ram_write_entry = create_proc_entry("yaffs_ram",
+                                           S_IRUGO | S_IFREG,
+                                          &proc_root);
+    if(!my_proc_ram_write_entry)
+    {
+       return -ENOMEM;
+    }
+    else
+    {
+       my_proc_ram_write_entry->write_proc = yaffs_proc_ram_write;
+    }
     error = register_filesystem(&yaffs_ram_fs_type);
     if(error)
     {