applied patch 14-sparse-fix--remove-unused.diff
[yaffs2.git] / yaffs_fs.c
index c2213f0..ca78391 100644 (file)
@@ -15,7 +15,8 @@
  * the VFS.
  *
  * Special notes: 
  * the VFS.
  *
  * Special notes: 
- * >> sb->u.generic_sbp points to the yaffs_Device associated with this superblock
+ * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with this superblock
+ * >> 2.6: sb->s_fs_info  points to the yaffs_Device associated with this superblock
  * >> inode->u.generic_ip points to the associated yaffs_Object.
  *
  *
  * >> inode->u.generic_ip points to the associated yaffs_Object.
  *
  *
  */
 
 
  */
 
 
-const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.3 2005-04-24 08:05:16 charles Exp $";
+const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.18 2005-07-31 04:09:26 marty Exp $";
 extern const char *yaffs_guts_c_version;
 
 
 extern const char *yaffs_guts_c_version;
 
 
-
 #include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -43,6 +43,7 @@ extern const char *yaffs_guts_c_version;
 #include <linux/list.h>
 #include <linux/fs.h>
 #include <linux/proc_fs.h>
 #include <linux/list.h>
 #include <linux/fs.h>
 #include <linux/proc_fs.h>
+#include <linux/smp_lock.h>
 #include <linux/pagemap.h>
 #include <linux/mtd/mtd.h>
 #include <linux/interrupt.h>
 #include <linux/pagemap.h>
 #include <linux/mtd/mtd.h>
 #include <linux/interrupt.h>
@@ -55,12 +56,13 @@ extern const char *yaffs_guts_c_version;
 #include <asm/statfs.h>
 #define UnlockPage(p) unlock_page(p)
 #define Page_Uptodate(page)    test_bit(PG_uptodate, &(page)->flags)
 #include <asm/statfs.h>
 #define UnlockPage(p) unlock_page(p)
 #define Page_Uptodate(page)    test_bit(PG_uptodate, &(page)->flags)
-//#define kdevname(x) cdevname(to_kdev_t(x))
-#define kdevname(x) "(unavailable)"    // temporary fix
+#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)       // FIXME: use sb->s_id instead ?
 
 #else
 
 #include <linux/locks.h>
 
 #else
 
 #include <linux/locks.h>
+#define        BDEVNAME_SIZE           0
+#define        yaffs_devname(sb, buf)  kdevname(sb->s_dev)
 
 #endif
 
 
 #endif
 
@@ -152,87 +154,75 @@ static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
 #endif
 static void yaffs_read_inode (struct inode *inode);
 
 #endif
 static void yaffs_read_inode (struct inode *inode);
 
-//#if defined(CONFIG_KERNEL_2_5)
-#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
-static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data);
-#else
-static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
-#endif
-
 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 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);
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
+#else
 static int yaffs_writepage(struct page *page);
 static int yaffs_writepage(struct page *page);
+#endif
 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
 
 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
 
-static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen);
+static int yaffs_readlink(struct dentry *dentry, char __user *buffer, int buflen);
 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
 
 
 
 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
 
 
 
-
 static struct address_space_operations yaffs_file_address_operations = {
 static struct address_space_operations yaffs_file_address_operations = {
-       readpage:               yaffs_readpage,
-       writepage:              yaffs_writepage,
-       prepare_write:  yaffs_prepare_write,
-       commit_write:   yaffs_commit_write
+       .readpage       = yaffs_readpage,
+       .writepage      = yaffs_writepage,
+       .prepare_write  = yaffs_prepare_write,
+       .commit_write   = yaffs_commit_write,
 };
 
 };
 
-
 static struct file_operations yaffs_file_operations = {
 static struct file_operations yaffs_file_operations = {
-
-       read:           generic_file_read,
-       write:          generic_file_write,
-
-       mmap:           generic_file_mmap,
-       flush:          yaffs_file_flush,
-       fsync:          yaffs_sync_object,
+       .read           = generic_file_read,
+       .write          = generic_file_write,
+       .mmap           = generic_file_mmap,
+       .flush          = yaffs_file_flush,
+       .fsync          = yaffs_sync_object,
 };
 
 };
 
-
 static struct inode_operations yaffs_file_inode_operations = {
 static struct inode_operations yaffs_file_inode_operations = {
-       setattr:        yaffs_setattr,
+       .setattr        = yaffs_setattr,
 };
 
 };
 
-
-struct inode_operations yaffs_symlink_inode_operations =
-{      
-       readlink:       yaffs_readlink,
-       follow_link:    yaffs_follow_link,
-       setattr:        yaffs_setattr
+static struct inode_operations yaffs_symlink_inode_operations = {      
+       .readlink       = yaffs_readlink,
+       .follow_link    = yaffs_follow_link,
+       .setattr        = yaffs_setattr,
 };
 
 static struct inode_operations yaffs_dir_inode_operations = {
 };
 
 static struct inode_operations yaffs_dir_inode_operations = {
-       create:         yaffs_create,
-       lookup:         yaffs_lookup,
-       link:           yaffs_link,
-       unlink:         yaffs_unlink,   
-       symlink:        yaffs_symlink,
-       mkdir:          yaffs_mkdir,
-       rmdir:          yaffs_unlink,
-       mknod:          yaffs_mknod,
-       rename:         yaffs_rename,
-       setattr:        yaffs_setattr,
+       .create         = yaffs_create,
+       .lookup         = yaffs_lookup,
+       .link           = yaffs_link,
+       .unlink         = yaffs_unlink, 
+       .symlink        = yaffs_symlink,
+       .mkdir          = yaffs_mkdir,
+       .rmdir          = yaffs_unlink,
+       .mknod          = yaffs_mknod,
+       .rename         = yaffs_rename,
+       .setattr        = yaffs_setattr,
 };
 
 static struct file_operations yaffs_dir_operations = {
 };
 
 static struct file_operations yaffs_dir_operations = {
-       read:           generic_read_dir,
-       readdir:        yaffs_readdir,
-       fsync:          yaffs_sync_object,
+       .read           = generic_read_dir,
+       .readdir        = yaffs_readdir,
+       .fsync          = yaffs_sync_object,
 };
 
 };
 
-
 static struct super_operations yaffs_super_ops = {
 static struct super_operations yaffs_super_ops = {
-       statfs:                 yaffs_statfs,
-       read_inode:             yaffs_read_inode,
-       put_inode:              yaffs_put_inode,
-       put_super:              yaffs_put_super,
-//     remount_fs:
-       delete_inode:           yaffs_delete_inode,
-       clear_inode:            yaffs_clear_inode,
+       .statfs         = yaffs_statfs,
+       .read_inode     = yaffs_read_inode,
+       .put_inode      = yaffs_put_inode,
+       .put_super      = yaffs_put_super,
+       .delete_inode   = yaffs_delete_inode,
+       .clear_inode    = yaffs_clear_inode,
 };
 
 
 };
 
 
@@ -251,7 +241,7 @@ static void yaffs_GrossUnlock(yaffs_Device *dev)
 
 }
 
 
 }
 
-static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen)
+static int yaffs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
 {
        unsigned char *alias;
        int ret;
 {
        unsigned char *alias;
        int ret;
@@ -329,7 +319,7 @@ static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
        {
                T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
                
        {
                T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
                
-               inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
+               inode = yaffs_get_inode(dir->i_sb, obj->yst_mode,0,obj);
                
                if(inode)
                {
                
                if(inode)
                {
@@ -462,8 +452,12 @@ static int yaffs_readpage_nolock(struct file *f, struct page * pg)
        dev = obj->myDev;
        
        
        dev = obj->myDev;
        
        
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+        BUG_ON(!PageLocked(pg));
+#else
        if (!PageLocked(pg))
                 PAGE_BUG(pg);
        if (!PageLocked(pg))
                 PAGE_BUG(pg);
+#endif
 
        pg_buf = kmap(pg);
        /* FIXME: Can kmap fail? */
 
        pg_buf = kmap(pg);
        /* FIXME: Can kmap fail? */
@@ -507,7 +501,11 @@ static int yaffs_readpage(struct file *f, struct page * pg)
 // writepage inspired by/stolen from smbfs
 //
 
 // writepage inspired by/stolen from smbfs
 //
 
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
+#else
 static int yaffs_writepage(struct page *page)
 static int yaffs_writepage(struct page *page)
+#endif
 {
        struct address_space *mapping = page->mapping;
        struct inode *inode;
 {
        struct address_space *mapping = page->mapping;
        struct inode *inode;
@@ -548,7 +546,7 @@ static int yaffs_writepage(struct page *page)
        yaffs_GrossLock(obj->myDev);
 
 
        yaffs_GrossLock(obj->myDev);
 
 
-       nWritten = yaffs_WriteDataToFile(obj,buffer,page->index << PAGE_CACHE_SHIFT,nBytes);
+       nWritten = yaffs_WriteDataToFile(obj,buffer,page->index << PAGE_CACHE_SHIFT,nBytes,0);
 
        yaffs_GrossUnlock(obj->myDev);
        
 
        yaffs_GrossUnlock(obj->myDev);
        
@@ -612,25 +610,25 @@ static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
        if (inode && obj) 
        {
                inode->i_ino = obj->objectId;
        if (inode && obj) 
        {
                inode->i_ino = obj->objectId;
-               inode->i_mode = obj->st_mode;
-               inode->i_uid = obj->st_uid;
-               inode->i_gid = obj->st_gid;
+               inode->i_mode = obj->yst_mode;
+               inode->i_uid = obj->yst_uid;
+               inode->i_gid = obj->yst_gid;
                inode->i_blksize = inode->i_sb->s_blocksize;
 //#if defined(CONFIG_KERNEL_2_5)
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 
                inode->i_blksize = inode->i_sb->s_blocksize;
 //#if defined(CONFIG_KERNEL_2_5)
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 
-               inode->i_rdev = old_decode_dev(obj->st_rdev);
-               inode->i_atime.tv_sec = (time_t)(obj->st_atime);
+               inode->i_rdev = old_decode_dev(obj->yst_rdev);
+               inode->i_atime.tv_sec = (time_t)(obj->yst_atime);
                inode->i_atime.tv_nsec = 0;
                inode->i_atime.tv_nsec = 0;
-               inode->i_mtime.tv_sec = (time_t)obj->st_mtime;
+               inode->i_mtime.tv_sec = (time_t)obj->yst_mtime;
                inode->i_mtime.tv_nsec =0;
                inode->i_mtime.tv_nsec =0;
-               inode->i_ctime.tv_sec = (time_t)obj->st_ctime;
+               inode->i_ctime.tv_sec = (time_t)obj->yst_ctime;
                inode->i_ctime.tv_nsec = 0;
 #else
                inode->i_ctime.tv_nsec = 0;
 #else
-               inode->i_rdev = obj->st_rdev;
-               inode->i_atime = obj->st_atime;
-               inode->i_mtime = obj->st_mtime;
-               inode->i_ctime = obj->st_ctime;
+               inode->i_rdev = obj->yst_rdev;
+               inode->i_atime = obj->yst_atime;
+               inode->i_mtime = obj->yst_mtime;
+               inode->i_ctime = obj->yst_ctime;
 #endif
                inode->i_size = yaffs_GetObjectFileLength(obj);
                inode->i_blocks = (inode->i_size + 511) >> 9;
 #endif
                inode->i_size = yaffs_GetObjectFileLength(obj);
                inode->i_blocks = (inode->i_size + 511) >> 9;
@@ -640,11 +638,15 @@ static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
                T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
                                inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
                
                T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
                                inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
                
-               switch (obj->st_mode & S_IFMT) 
+               switch (obj->yst_mode & S_IFMT) 
                {
                        default: // fifo, device or socket
                {
                        default: // fifo, device or socket
-                               init_special_inode(inode, obj->st_mode,(dev_t)(obj->st_rdev));
-                               break;
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+                               init_special_inode(inode, obj->yst_mode,old_decode_dev(obj->yst_rdev));
+#else
+                                 init_special_inode(inode, obj->yst_mode,(dev_t)(obj->yst_rdev));
+#endif                         
+                        break;
                        case S_IFREG:   // file         
                                inode->i_op = &yaffs_file_inode_operations;
                                inode->i_fop = &yaffs_file_operations;
                        case S_IFREG:   // file         
                                inode->i_op = &yaffs_file_inode_operations;
                                inode->i_fop = &yaffs_file_operations;
@@ -783,7 +785,7 @@ static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_
                T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
        }
 
                T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
        }
 
-       nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
+       nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n,0);
 
        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
        if(nWritten > 0)
 
        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
        if(nWritten > 0)
@@ -902,6 +904,8 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int r
        yaffs_Object *parent = yaffs_InodeToObject(dir);
        
        int error = -ENOSPC;
        yaffs_Object *parent = yaffs_InodeToObject(dir);
        
        int error = -ENOSPC;
+       uid_t uid = current->fsuid;
+       gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
 
        if(parent)
        {
 
        if(parent)
        {
@@ -926,15 +930,19 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int r
                default:
                        // Special (socket, fifo, device...)
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n"));
                default:
                        // Special (socket, fifo, device...)
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n"));
-                       obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev);
-                       break;
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+                        obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,uid, gid,old_encode_dev(rdev));
+#else
+                        obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,uid, gid,rdev);
+#endif                 
+                break;
                case S_IFREG:   // file         
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
                case S_IFREG:   // file         
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
-                       obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
+                       obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,uid, gid);
                        break;
                case S_IFDIR:   // directory
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n"));
                        break;
                case S_IFDIR:   // directory
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n"));
-                       obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
+                       obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,uid, gid);
                        break;
                case S_IFLNK:   // symlink
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
                        break;
                case S_IFLNK:   // symlink
                        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
@@ -1070,13 +1078,15 @@ static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char *
 {
        yaffs_Object *obj;
        yaffs_Device *dev;
 {
        yaffs_Object *obj;
        yaffs_Device *dev;
+       uid_t uid = current->fsuid;
+       gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
        
        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n"));
        
        dev = yaffs_InodeToObject(dir)->myDev;
        yaffs_GrossLock(dev);
        obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
        
        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n"));
        
        dev = yaffs_InodeToObject(dir)->myDev;
        yaffs_GrossLock(dev);
        obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, 
-                                                        S_IFLNK | S_IRWXUGO, current->uid, current->gid,
+                                                        S_IFLNK | S_IRWXUGO, uid, gid,
                                                         symname);
        yaffs_GrossUnlock(dev);
 
                                                         symname);
        yaffs_GrossUnlock(dev);
 
@@ -1085,7 +1095,7 @@ static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char *
 
                struct inode* inode;
        
 
                struct inode* inode;
        
-               inode = yaffs_get_inode(dir->i_sb, obj->st_mode, 0, obj);
+               inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
                d_instantiate(dentry, inode);
                T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n"));
                return 0;
                d_instantiate(dentry, inode);
                T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n"));
                return 0;
@@ -1194,7 +1204,8 @@ static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
                        error = -EPERM;
                }
                yaffs_GrossUnlock(dev);
                        error = -EPERM;
                }
                yaffs_GrossUnlock(dev);
-               inode_setattr(inode,attr);
+               if (!error)
+                       error = inode_setattr(inode,attr);
        }
        return error;
 }
        }
        return error;
 }
@@ -1206,20 +1217,35 @@ static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
 #endif
 {
 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
 #endif
 {
+
+       
        yaffs_Device *dev = yaffs_SuperToDevice(sb);
        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n"));
 
        yaffs_GrossLock(dev);
        
        yaffs_Device *dev = yaffs_SuperToDevice(sb);
        T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n"));
 
        yaffs_GrossLock(dev);
        
+       
        buf->f_type = YAFFS_MAGIC;
        buf->f_bsize = sb->s_blocksize;
        buf->f_namelen = 255;
        buf->f_type = YAFFS_MAGIC;
        buf->f_bsize = sb->s_blocksize;
        buf->f_namelen = 255;
-       buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/
-                                               (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
+       if(sb->s_blocksize > dev->nBytesPerChunk)
+       {
+               
+               buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * dev->nChunksPerBlock/
+                                               (sb->s_blocksize/dev->nBytesPerChunk);
+               buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
+                                               (sb->s_blocksize/dev->nBytesPerChunk);
+       }
+       else
+       {
+               
+               buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * dev->nChunksPerBlock *
+                                               (dev->nBytesPerChunk/sb->s_blocksize);
+               buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev) *
+                                               (dev->nBytesPerChunk/sb->s_blocksize);
+       }
        buf->f_files = 0;
        buf->f_ffree = 0;
        buf->f_files = 0;
        buf->f_ffree = 0;
-       buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
-                                               (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
        buf->f_bavail =  buf->f_bfree;
        
        yaffs_GrossUnlock(dev);
        buf->f_bavail =  buf->f_bfree;
        
        yaffs_GrossUnlock(dev);
@@ -1242,14 +1268,7 @@ static void yaffs_read_inode (struct inode *inode)
 
 }
 
 
 }
 
-
-
-// Todo
-// Currently we only can report a single partition. Need to use lists here
-static yaffs_Device *yaffs_dev;
-static yaffs_Device *yaffsram_dev;
-
-
+static LIST_HEAD(yaffs_dev_list);
 
 static void yaffs_put_super(struct super_block *sb)
 {
 
 static void yaffs_put_super(struct super_block *sb)
 {
@@ -1263,9 +1282,9 @@ static void yaffs_put_super(struct super_block *sb)
        yaffs_Deinitialise(dev);
        yaffs_GrossUnlock(dev);
 
        yaffs_Deinitialise(dev);
        yaffs_GrossUnlock(dev);
 
-       if(dev == yaffs_dev) yaffs_dev = NULL;
-       if(dev == yaffsram_dev) yaffsram_dev = NULL;
-               
+       /* we assume this is protected by lock_kernel() in mount/umount */
+       list_del(&dev->devList);
+
        kfree(dev);
 }
 
        kfree(dev);
 }
 
@@ -1293,7 +1312,8 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
        int nBlocks;
        struct inode * inode = NULL;
        struct dentry * root;
        int nBlocks;
        struct inode * inode = NULL;
        struct dentry * root;
-       yaffs_Device *dev;
+       yaffs_Device *dev = 0;
+       char devname_buf[BDEVNAME_SIZE+1];
        int err;
        
        sb->s_magic = YAFFS_MAGIC;
        int err;
        
        sb->s_magic = YAFFS_MAGIC;
@@ -1303,10 +1323,10 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
                printk(KERN_INFO"yaffs: sb is NULL\n");
        else if(!sb->s_dev)
                printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
                printk(KERN_INFO"yaffs: sb is NULL\n");
        else if(!sb->s_dev)
                printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
-       else if(! kdevname(sb->s_dev))
-               printk(KERN_INFO"yaffs: kdevname is NULL\n");
+       else if(!yaffs_devname(sb, devname_buf))
+               printk(KERN_INFO"yaffs: devname is NULL\n");
        else
        else
-               printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
+               printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, yaffs_devname(sb, devname_buf));
 
        
 
 
        
 
@@ -1332,7 +1352,13 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
 #ifdef CONFIG_YAFFS_RAM_ENABLED
                // Set the yaffs_Device up for ram emulation
 
 #ifdef CONFIG_YAFFS_RAM_ENABLED
                // Set the yaffs_Device up for ram emulation
 
+               
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+               sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
+#else
                sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
                sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
+#endif
+
                if(!dev)
                {
                        // Deep shit could not allocate device structure
                if(!dev)
                {
                        // Deep shit could not allocate device structure
@@ -1342,9 +1368,10 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
 
                memset(dev,0,sizeof(yaffs_Device));
                dev->genericDevice = NULL; // Not used for RAM emulation.
 
                memset(dev,0,sizeof(yaffs_Device));
                dev->genericDevice = NULL; // Not used for RAM emulation.
+               dev->name = sb->s_type->name;
 
                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->startBlock = 0;  
                dev->endBlock = nBlocks - 1;
                dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
                dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
                dev->endBlock = nBlocks - 1;
                dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
                dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
@@ -1362,7 +1389,7 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
                        dev->nChunksPerBlock = nandemul2k_GetChunksPerBlock();
                        dev->nBytesPerChunk =  nandemul2k_GetBytesPerChunk();;
                        nBlocks = nandemul2k_GetNumberOfBlocks();
                        dev->nChunksPerBlock = nandemul2k_GetChunksPerBlock();
                        dev->nBytesPerChunk =  nandemul2k_GetBytesPerChunk();;
                        nBlocks = nandemul2k_GetNumberOfBlocks();
-                       dev->startBlock = 1;  // Don't use block 0
+                       dev->startBlock = 0;
                        dev->endBlock = nBlocks - 1;
                }
                else
                        dev->endBlock = nBlocks - 1;
                }
                else
@@ -1373,19 +1400,16 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
                        dev->initialiseNAND = nandemul_InitialiseNAND;
                        dev->isYaffs2 = 0;
                }
                        dev->initialiseNAND = nandemul_InitialiseNAND;
                        dev->isYaffs2 = 0;
                }
-               
-               yaffsram_dev = dev;
-               
 #endif
 
        }
        else
        {       
 #endif
 
        }
        else
        {       
-#ifdef CONFIG_YAFFS_MTD_ENABLED
+#if defined(CONFIG_YAFFS_MTD_ENABLED) || defined(CONFIG_YAFFS2_MTD_ENABLED)
                struct mtd_info *mtd;
                
                T(YAFFS_TRACE_ALWAYS,("yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
                struct mtd_info *mtd;
                
                T(YAFFS_TRACE_ALWAYS,("yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
-                MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev)));
+                MAJOR(sb->s_dev),MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
                        
                // Check it's an mtd device.....
                if(MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
                        
                // Check it's an mtd device.....
                if(MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
@@ -1408,13 +1432,13 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
                        return NULL;
                }
 
                        return NULL;
                }
 
-               T(YAFFS_TRACE_OS,(" erase %x\n",mtd->erase));
-               T(YAFFS_TRACE_OS,(" read %x\n",mtd->read));
-               T(YAFFS_TRACE_OS,(" write %x\n",mtd->write));
-               T(YAFFS_TRACE_OS,(" readoob %x\n",mtd->read_oob));
-               T(YAFFS_TRACE_OS,(" writeoob %x\n",mtd->write_oob));
-               T(YAFFS_TRACE_OS,(" block_isbad %x\n",mtd->block_isbad));
-               T(YAFFS_TRACE_OS,(" block_markbad %x\n",mtd->block_markbad));
+               T(YAFFS_TRACE_OS,(" erase %p\n",mtd->erase));
+               T(YAFFS_TRACE_OS,(" read %p\n",mtd->read));
+               T(YAFFS_TRACE_OS,(" write %p\n",mtd->write));
+               T(YAFFS_TRACE_OS,(" readoob %p\n",mtd->read_oob));
+               T(YAFFS_TRACE_OS,(" writeoob %p\n",mtd->write_oob));
+               T(YAFFS_TRACE_OS,(" block_isbad %p\n",mtd->block_isbad));
+               T(YAFFS_TRACE_OS,(" block_markbad %p\n",mtd->block_markbad));
                T(YAFFS_TRACE_OS,(" oobblock %d\n",mtd->oobblock));
                T(YAFFS_TRACE_OS,(" oobsize %d\n",mtd->oobsize));
                T(YAFFS_TRACE_OS,(" erasesize %d\n",mtd->erasesize));
                T(YAFFS_TRACE_OS,(" oobblock %d\n",mtd->oobblock));
                T(YAFFS_TRACE_OS,(" oobsize %d\n",mtd->oobsize));
                T(YAFFS_TRACE_OS,(" erasesize %d\n",mtd->erasesize));
@@ -1490,11 +1514,12 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
 
                memset(dev,0,sizeof(yaffs_Device));
                dev->genericDevice = mtd; 
 
                memset(dev,0,sizeof(yaffs_Device));
                dev->genericDevice = mtd; 
+               dev->name = mtd->name;
 
                // Set up the memory size parameters....
                
                nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
 
                // Set up the memory size parameters....
                
                nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
-               dev->startBlock = 1;  // Don't use block 0
+               dev->startBlock = 0;
                dev->endBlock = nBlocks - 1;
                dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
                dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
                dev->endBlock = nBlocks - 1;
                dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
                dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
@@ -1514,7 +1539,7 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
                        dev->nBytesPerChunk = mtd->oobblock;
                        dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
                        nBlocks = mtd->size / mtd->erasesize;
                        dev->nBytesPerChunk = mtd->oobblock;
                        dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
                        nBlocks = mtd->size / mtd->erasesize;
-                       dev->startBlock = 1;  // Don't use block 0
+                       dev->startBlock = 0;
                        dev->endBlock = nBlocks - 1;
                }
                else
                        dev->endBlock = nBlocks - 1;
                }
                else
@@ -1532,12 +1557,12 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
 #ifdef CONFIG_YAFFS_USE_NANDECC
                dev->useNANDECC = 1;
 #endif
 #ifdef CONFIG_YAFFS_USE_NANDECC
                dev->useNANDECC = 1;
 #endif
-
-               yaffs_dev = dev;
-               
 #endif
        }
 
 #endif
        }
 
+       /* we assume this is protected by lock_kernel() in mount/umount */
+       list_add_tail(&dev->devList, &yaffs_dev_list);
+
        init_MUTEX(&dev->grossLock);
        
        
        init_MUTEX(&dev->grossLock);
        
        
@@ -1578,29 +1603,15 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,int useRam
 }
 
 
 }
 
 
-static int yaffs_internal_read_super_ram(struct super_block * sb, void * data, int silent)
-{
-        return yaffs_internal_read_super(1,1,sb,data,silent) ? 0 : -1;
-}
+
+#ifdef CONFIG_YAFFS_MTD_ENABLED
+
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
 {
         return yaffs_internal_read_super(1,0,sb,data,silent) ? 0 : -1;
 }
 
 static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
 {
         return yaffs_internal_read_super(1,0,sb,data,silent) ? 0 : -1;
 }
 
-static int yaffs2_internal_read_super_ram(struct super_block * sb, void * data, int silent)
-{
-        return yaffs_internal_read_super(2,1,sb,data,silent) ? 0 : -1;
-}
-static int yaffs2_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
-{
-        return yaffs_internal_read_super(2,0,sb,data,silent) ? 0 : -1;
-}
-
-
-
-#ifdef CONFIG_YAFFS_MTD_ENABLED
-//#if defined(CONFIG_KERNEL_2_5)
-#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
@@ -1631,6 +1642,11 @@ static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV)
 #ifdef CONFIG_YAFFS2_MTD_ENABLED
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 #ifdef CONFIG_YAFFS2_MTD_ENABLED
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+static int yaffs2_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
+{
+        return yaffs_internal_read_super(2,0,sb,data,silent) ? 0 : -1;
+}
+
 static struct super_block *yaffs2_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
 static struct super_block *yaffs2_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
@@ -1662,10 +1678,15 @@ static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super, FS_REQUIRES_D
 #ifdef CONFIG_YAFFS_RAM_ENABLED
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 #ifdef CONFIG_YAFFS_RAM_ENABLED
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+static int yaffs_internal_read_super_ram(struct super_block * sb, void * data, int silent)
+{
+        return yaffs_internal_read_super(1,1,sb,data,silent) ? 0 : -1;
+}
+
 static struct super_block *yaffs_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
 static struct super_block *yaffs_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
-    return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_ram);
+    return get_sb_nodev(fs, flags, data, yaffs_internal_read_super_ram);
 }
 
 
 }
 
 
@@ -1673,9 +1694,8 @@ static struct file_system_type yaffs_ram_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "yaffsram",
        .get_sb         = yaffs_ram_read_super,
        .owner          = THIS_MODULE,
        .name           = "yaffsram",
        .get_sb         = yaffs_ram_read_super,
-       .kill_sb        = kill_block_super,
-//     .kill_sb        = kill_litter_super,
-       .fs_flags       = FS_SINGLE,
+       .kill_sb        = kill_litter_super,
+       .fs_flags       = 0 ,
 };
 #else
 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
 };
 #else
 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
@@ -1691,10 +1711,15 @@ static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SI
 #ifdef CONFIG_YAFFS2_RAM_ENABLED
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
 #ifdef CONFIG_YAFFS2_RAM_ENABLED
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+static int yaffs2_internal_read_super_ram(struct super_block * sb, void * data, int silent)
+{
+        return yaffs_internal_read_super(2,1,sb,data,silent) ? 0 : -1;
+}
+
 static struct super_block *yaffs2_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
 static struct super_block *yaffs2_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
 {
 
-    return get_sb_bdev(fs, flags, dev_name, data, yaffs2_internal_read_super_ram);
+    return get_sb_nodev(fs, flags, data, yaffs2_internal_read_super_ram);
 }
 
 
 }
 
 
@@ -1702,9 +1727,8 @@ static struct file_system_type yaffs2_ram_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "yaffs2ram",
        .get_sb         = yaffs2_ram_read_super,
        .owner          = THIS_MODULE,
        .name           = "yaffs2ram",
        .get_sb         = yaffs2_ram_read_super,
-       .kill_sb        = kill_block_super,
-//     .kill_sb        = kill_litter_super,
-       .fs_flags       = FS_SINGLE,
+       .kill_sb        = kill_litter_super,
+       .fs_flags       = 0 ,
 };
 #else
 static struct super_block *yaffs2_ram_read_super(struct super_block * sb, void * data, int silent)
 };
 #else
 static struct super_block *yaffs2_ram_read_super(struct super_block * sb, void * data, int silent)
@@ -1720,11 +1744,9 @@ static DECLARE_FSTYPE(yaffs2_ram_fs_type, "yaffs2ram", yaffs2_ram_read_super, FS
 
 
 static struct proc_dir_entry *my_proc_entry;
 
 
 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)
+static char * yaffs_dump_dev(char *buf,yaffs_Device *dev)
 {
 {
-       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,"startBlock......... %d\n",dev->startBlock);
        buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
        buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
@@ -1754,7 +1776,6 @@ static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
        buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC);
        buf +=sprintf(buf,"isYaffs2........... %d\n",dev->isYaffs2);
 
        buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC);
        buf +=sprintf(buf,"isYaffs2........... %d\n",dev->isYaffs2);
 
-       
        return buf;     
 }
 
        return buf;     
 }
 
@@ -1767,25 +1788,44 @@ static int  yaffs_proc_read(
        void *data
        )
 {
        void *data
        )
 {
+       struct list_head *item;
+       char *buf = page;
+       int step = offset;
+       int n = 0;
+
+       /* Get proc_file_read() to step 'offset' by one on each sucessive call.
+        * We use 'offset' (*ppos) to indicate where we are in devList.
+        * This also assumes the user has posted a read buffer large
+        * enough to hold the complete output; but that's life in /proc.
+        */
+
+       *(int *)start = 1;
+
+       /* Print header first */
+       if (step == 0) {
+               buf += sprintf(buf, "YAFFS built:" __DATE__ " "__TIME__
+               "\n%s\n%s\n", yaffs_fs_c_version, yaffs_guts_c_version);
+       }
 
 
-       char my_buffer[3000];
-       char *buf;
-       buf = my_buffer;
-
-       if (offset > 0) return 0;
+       /* hold lock_kernel while traversing yaffs_dev_list */
+       lock_kernel();
 
 
-       /* Fill the buffer and get its length */
-       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");
-       
+       /* Locate and print the Nth entry.  Order N-squared but N is small. */
+       list_for_each(item, &yaffs_dev_list) {
+               yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
+               if (n < step) {
+                       n++;
+                       continue;
+               }
+               buf += sprintf(buf,"\nDevice %d \"%s\"\n", n, dev->name);
+               buf = yaffs_dump_dev(buf, dev);
+               break;
+       }
+       unlock_kernel();
 
 
-       strcpy(page,my_buffer);
-       return strlen(my_buffer);
+       return buf-page < count ? buf-page : count;
 }
 
 }
 
-
 static int  yaffs_proc_ram_write(
         char *page,
        char **start,
 static int  yaffs_proc_ram_write(
         char *page,
        char **start,
@@ -1831,8 +1871,6 @@ static int __init init_yaffs_fs(void)
        int error = 0;
        struct file_system_to_install *fsinst;  
        
        int error = 0;
        struct file_system_to_install *fsinst;  
        
-       yaffs_dev = yaffsram_dev = NULL;
-       
    T(YAFFS_TRACE_ALWAYS,("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
 
 
    T(YAFFS_TRACE_ALWAYS,("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
 
 
@@ -1913,3 +1951,4 @@ MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002,2003,2004");
 MODULE_LICENSE("GPL");
 
 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002,2003,2004");
 MODULE_LICENSE("GPL");
 
+