*** empty log message ***
[yaffs/.git] / yaffs_fs.c
index d494e83b7f016874bed3af57a1349a865f4baadd..9be8428cad8cff648e75494dea064f36ff75a0e1 100644 (file)
  *
  * Acknowledgements:
  * * Luc van OostenRyck for numerous patches.
- * * Nick Bane for patches marked NCB.
+ * * Nick Bane for numerous patches.
+ * * Andras Toth for mknod rdev issue.
  * * Some code bodily lifted from JFFS2.
  */
 
 
+const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.17 2002-10-02 02:11:25 charles Exp $";
+extern const char *yaffs_guts_c_version;
+
 #include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -90,6 +94,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 +108,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 +166,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 +263,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,16 +282,55 @@ 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);
 }
 
 
@@ -319,7 +365,7 @@ static int yaffs_readpage_nolock(struct file *f, struct page * pg)
        yaffs_Device *dev;
        
        T((KERN_DEBUG"yaffs_readpage at %08x, size %08x\n", 
-                     pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE));
+                     (unsigned)(pg->index << PAGE_CACHE_SHIFT), (unsigned)PAGE_CACHE_SIZE));
 
        obj  = yaffs_DentryToObject(f->f_dentry);
 
@@ -389,7 +435,7 @@ static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
        int nWritten;
        
        unsigned spos = pos;
-       unsigned saddr = addr;
+       unsigned saddr = (unsigned)addr;
 
        T((KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
        
@@ -455,6 +501,7 @@ static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
                
                
                inode->u.generic_ip = obj;
+               obj->myInode = inode;
                
        }
        else
@@ -473,7 +520,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;
 }
 
@@ -569,7 +617,7 @@ static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_
                        inode->i_size = ipos;
                        inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
                        
-                       T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,inode->i_blocks));
+                       T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks)));
                }
                
        }
@@ -660,7 +708,7 @@ static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
 /*
  * File creation. Allocate an inode, and we're done..
  */
-static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
+static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
 {
        struct inode *inode;
        
@@ -683,7 +731,7 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int d
        }
        
        T(("yaffs_mknod: making oject for %s, mode %x dev %x\n",
-                                       dentry->d_name.name, mode,dev));
+                                       dentry->d_name.name, mode,rdev));
 
        dev = parent->myDev;
        
@@ -694,7 +742,7 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int d
                default:
                        // Special (socket, fifo, device...)
                        T((KERN_DEBUG"yaffs_mknod: making special\n"));
-                       obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,dev);
+                       obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev);
                        break;
                case S_IFREG:   // file         
                        T((KERN_DEBUG"yaffs_mknod: making file\n"));
@@ -712,7 +760,7 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int d
        
        if(obj)
        {
-               inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
+               inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
                d_instantiate(dentry, inode);
                T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
                error = 0;
@@ -753,19 +801,26 @@ 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;
        
        
-       T((KERN_DEBUG"yaffs_unlink %d:%s\n",dir->i_ino,dentry->d_name.name));
+       T((KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name));
        
        dev = yaffs_InodeToObject(dir)->myDev;
        
        yaffs_GrossLock(dev);
+       
+       
        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 +849,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 +921,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)
 {
@@ -863,6 +933,10 @@ static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struc
 
        yaffs_GrossLock(dev);
        
+       // Unlink the target if it exists
+       yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
+
+       
        retVal =  yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
                                         yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
        yaffs_GrossUnlock(dev);
@@ -916,7 +990,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;
@@ -945,6 +1019,12 @@ static void yaffs_read_inode (struct inode *inode)
 }
 
 
+
+static yaffs_Device *yaffs_dev;
+static yaffs_Device *yaffsram_dev;
+
+
+
 static void yaffs_put_super(struct super_block *sb)
 {
        yaffs_Device *dev = yaffs_SuperToDevice(sb);
@@ -956,7 +1036,10 @@ static void yaffs_put_super(struct super_block *sb)
        }
        yaffs_Deinitialise(dev);
        yaffs_GrossUnlock(dev);
-       
+
+       if(dev == yaffs_dev) yaffs_dev = NULL;
+       if(dev == yaffsram_dev) yaffsram_dev = NULL;
+               
        kfree(dev);
 }
 
@@ -978,8 +1061,10 @@ static void  yaffs_MTDPutSuper(struct super_block *sb)
 
 #endif
 
+
 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;
@@ -1005,7 +1090,7 @@ static struct super_block *yaffs_internal_read_super(int useRam, struct super_bl
        sb->s_blocksize = PAGE_CACHE_SIZE;
        sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
 #endif
-       T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",sb->s_blocksize));
+       T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",(int)(sb->s_blocksize)));
 
 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
        T(("yaffs: Write verification disabled. All guarantees null and void\n");
@@ -1030,14 +1115,16 @@ 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;
                dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
                dev->initialiseNAND = nandemul_InitialiseNAND;
+
+               yaffsram_dev = dev;
                
 #endif
 
@@ -1064,13 +1151,13 @@ static struct super_block *yaffs_internal_read_super(int useRam, struct super_bl
                        return NULL;
                }
 
-               printk(KERN_DEBUG" erase %x\n",mtd->erase);
-               printk(KERN_DEBUG" read %x\n",mtd->read);
-               printk(KERN_DEBUG" write %x\n",mtd->write);
-               printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
-               printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
-               printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
-               printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
+               //printk(KERN_DEBUG" erase %x\n",mtd->erase);
+               //printk(KERN_DEBUG" read %x\n",mtd->read);
+               //printk(KERN_DEBUG" write %x\n",mtd->write);
+               //printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
+               //printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
+               //printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
+               //printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
 
 
                if(!mtd->erase ||
@@ -1108,18 +1195,20 @@ 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;
                dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
                dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
                dev->initialiseNAND = nandmtd_InitialiseNAND;
-               
+                               
                dev->putSuperFunc = yaffs_MTDPutSuper;
+
+               yaffs_dev = dev;
+               
 #endif
        }
 
@@ -1177,7 +1266,39 @@ 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 char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
+{
+       buf +=sprintf(buf,"\nDevice %s\n",name);
+       buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
+       buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
+       buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
+       buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
+       buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
+       buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
+       buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
+       buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
+       buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
+       buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
+       buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
+       buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
+       buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
+       buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
+       buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
+       buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
+       buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
+       buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed);
+       buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed);
+       buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed);
+       buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed);
+       buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
+       buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
+       buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
+       buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
+       
+       return buf;     
+}
 
 static int  yaffs_proc_read(
         char *page,
@@ -1189,24 +1310,44 @@ static int  yaffs_proc_read(
        )
 {
 
-       static char my_buffer[1000];
+       char my_buffer[3000];
+       char *buf;
+       buf = my_buffer;
 
        if (offset > 0) return 0;
 
        /* Fill the buffer and get its length */
-       sprintf( my_buffer, 
-               "YAFFS built:"__DATE__ " "__TIME__"\n"
-               
-       );
+       buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n%s\n%s\n", yaffs_fs_c_version,yaffs_guts_c_version);
+       
+       if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs");
+       if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram");
+       
 
        strcpy(page,my_buffer);
        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;
        
+       yaffs_dev = yaffsram_dev = NULL;
+       
        printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
        printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
@@ -1227,6 +1368,20 @@ static int __init init_yaffs_fs(void)
     }
 
 #ifdef CONFIG_YAFFS_RAM_ENABLED
+#if 0
+    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;
+    }
+#endif
 
     error = register_filesystem(&yaffs_ram_fs_type);
     if(error)