Add yaffs2 autoselection
[yaffs2.git] / yaffs_fs.c
index 574eda6f989522ab413314b6121e36cbe6f2f91b..2a631623f82eb2563d47c57bac8047360e0cd52b 100644 (file)
@@ -31,7 +31,7 @@
  */
 
 const char *yaffs_fs_c_version =
-    "$Id: yaffs_fs.c,v 1.29 2005-08-11 01:07:43 marty Exp $";
+    "$Id: yaffs_fs.c,v 1.37 2005-12-14 01:18:45 charles Exp $";
 extern const char *yaffs_guts_c_version;
 
 #include <linux/config.h>
@@ -72,14 +72,11 @@ extern const char *yaffs_guts_c_version;
 #include "yportenv.h"
 #include "yaffs_guts.h"
 
-unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS;
-/*unsigned yaffs_traceMask = 0xFFFFFFFF; */
+unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS /* | 0xFFFFFFFF */; 
 
-#ifdef CONFIG_YAFFS_YAFFS1
 #include <linux/mtd/mtd.h>
 #include "yaffs_mtdif.h"
 #include "yaffs_mtdif2.h"
-#endif                         /*CONFIG_YAFFS_YAFFS1 */
 
 /*#define T(x) printk x */
 
@@ -155,7 +152,11 @@ static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
 
 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
                          int buflen);
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
+static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
+#else
 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
+#endif
 
 static struct address_space_operations yaffs_file_address_operations = {
        .readpage = yaffs_readpage,
@@ -246,7 +247,11 @@ static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
        return ret;
 }
 
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
+static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
+#else
 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
+#endif
 {
        unsigned char *alias;
        int ret;
@@ -259,11 +264,19 @@ static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
        yaffs_GrossUnlock(dev);
 
        if (!alias)
-               return -ENOMEM;
+        {
+               ret = -ENOMEM;
+               goto out;
+        }
 
        ret = vfs_follow_link(nd, alias);
        kfree(alias);
+out:
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
+       return ERR_PTR (ret);
+#else
        return ret;
+#endif
 }
 
 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
@@ -296,6 +309,9 @@ static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
                                   dentry->d_name.name);
 
        obj = yaffs_GetEquivalentObject(obj);   /* in case it was a hardlink */
+       
+       /* Can't hold gross lock when calling yaffs_get_inode() */
+       yaffs_GrossUnlock(dev);
 
        if (obj) {
                T(YAFFS_TRACE_OS,
@@ -312,8 +328,6 @@ static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
                        /*dget(dentry); // try to solve directory bug */
                        d_add(dentry, inode);
 
-                       yaffs_GrossUnlock(dev);
-
                        /* return dentry; */
                        return NULL;
 #endif
@@ -323,7 +337,6 @@ static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
                T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
 
        }
-       yaffs_GrossUnlock(dev);
 
 /* added NCB for 2.5/6 compatability - forces add even if inode is
  * NULL which creates dentry hash */
@@ -401,6 +414,9 @@ static void yaffs_delete_inode(struct inode *inode)
                yaffs_DeleteFile(obj);
                yaffs_GrossUnlock(dev);
        }
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
+        truncate_inode_pages (&inode->i_data, 0);
+#endif
        clear_inode(inode);
 }
 
@@ -607,9 +623,10 @@ static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
        }
 
        T(YAFFS_TRACE_OS,
-         (KERN_DEBUG "yaffs_commit_write returning %d\n", nWritten));
+         (KERN_DEBUG "yaffs_commit_write returning %d\n",
+          nWritten == nBytes ? 0 : -1));
 
-       return nWritten;
+       return nWritten == nBytes ? 0 : -1;
 
 }
 
@@ -708,6 +725,7 @@ struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
 
        /* NB Side effect: iget calls back to yaffs_read_inode(). */
        /* iget also increments the inode's i_count */
+       /* NB You can't be holding grossLock or deadlock will happen! */
 
        return inode;
 }
@@ -923,6 +941,9 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
                obj = NULL;     /* Do we ever get here? */
                break;
        }
+       
+       /* Can not call yaffs_get_inode() with gross lock held */
+       yaffs_GrossUnlock(dev);
 
        if (obj) {
                inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
@@ -937,8 +958,6 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
                error = -ENOMEM;
        }
 
-       yaffs_GrossUnlock(dev);
-
        return error;
 }
 
@@ -1100,9 +1119,9 @@ static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
 {
        yaffs_Device *dev;
        int retVal = YAFFS_FAIL;
-       int removed = 0;
        yaffs_Object *target;
 
+        T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
        dev = yaffs_InodeToObject(old_dir)->myDev;
 
        yaffs_GrossLock(dev);
@@ -1111,15 +1130,21 @@ static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
        target =
            yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
                                   new_dentry->d_name.name);
+       
+       
 
        if (target &&
            target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
            !list_empty(&target->variant.directoryVariant.children)) {
+           
+               T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
+
                retVal = YAFFS_FAIL;
        } else {
 
                /* Now does unlinking internally using shadowing mechanism */
-
+               T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
+               
                retVal =
                    yaffs_RenameObject(yaffs_InodeToObject(old_dir),
                                       old_dentry->d_name.name,
@@ -1130,7 +1155,7 @@ static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
        yaffs_GrossUnlock(dev);
 
        if (retVal == YAFFS_OK) {
-               if (removed == YAFFS_OK) {
+               if(target) {
                        new_dentry->d_inode->i_nlink--;
                        mark_inode_dirty(new_dentry->d_inode);
                }
@@ -1213,8 +1238,9 @@ static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
 
 static void yaffs_read_inode(struct inode *inode)
 {
-       /* NB This is called as a side effect of other functions and
-        * thus gross locking should always be in place already.
+       /* NB This is called as a side effect of other functions, but
+        * we had to release the lock to prevent deadlocks, so 
+        * need to lock again.
         */
 
        yaffs_Object *obj;
@@ -1223,10 +1249,13 @@ static void yaffs_read_inode(struct inode *inode)
        T(YAFFS_TRACE_OS,
          (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
 
+       yaffs_GrossLock(dev);
+       
        obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
 
        yaffs_FillInodeFromObject(inode, obj);
 
+       yaffs_GrossUnlock(dev);
 }
 
 static LIST_HEAD(yaffs_dev_list);
@@ -1248,7 +1277,6 @@ static void yaffs_put_super(struct super_block *sb)
        kfree(dev);
 }
 
-#ifdef CONFIG_YAFFS_YAFFS1
 
 static void yaffs_MTDPutSuper(struct super_block *sb)
 {
@@ -1262,7 +1290,6 @@ static void yaffs_MTDPutSuper(struct super_block *sb)
        put_mtd_device(mtd);
 }
 
-#endif
 
 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
                                                     struct super_block *sb,
@@ -1337,6 +1364,15 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,
        T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
        T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
        T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
+       
+#if CONFIG_YAFFS_AUTO_YAFFS2
+
+       if (yaffsVersion == 1 && 
+           mtd->oobblock >= 2048) {
+           T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
+           yaffsVersion = 2;
+       }       
+#endif
 
        if (yaffsVersion == 2) {
                /* Check for version 2 style functions */
@@ -1444,6 +1480,10 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,
        dev->useNANDECC = 1;
 #endif
 
+#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
+       dev->wideTnodesDisabled = 1;
+#endif
+
        /* we assume this is protected by lock_kernel() in mount/umount */
        list_add_tail(&dev->devList, &yaffs_dev_list);
 
@@ -1456,14 +1496,15 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,
        T(YAFFS_TRACE_OS,
          ("yaffs_read_super: guts initialised %s\n",
           (err == YAFFS_OK) ? "OK" : "FAILED"));
+       
+       /* Release lock before yaffs_get_inode() */
+       yaffs_GrossUnlock(dev);
 
        /* Create root inode */
        if (err == YAFFS_OK)
                inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
                                        yaffs_Root(dev));
 
-       yaffs_GrossUnlock(dev);
-
        if (!inode)
                return NULL;
 
@@ -1486,13 +1527,12 @@ static struct super_block *yaffs_internal_read_super(int yaffsVersion,
        return sb;
 }
 
-#ifdef CONFIG_YAFFS_YAFFS1
 
 #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, sb, data, silent) ? 0 : -1;
+       return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
 }
 
 static struct super_block *yaffs_read_super(struct file_system_type *fs,
@@ -1522,7 +1562,6 @@ static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
                      FS_REQUIRES_DEV);
 #endif
 
-#endif                         /* CONFIG_YAFFS_YAFFS1 */
 
 #ifdef CONFIG_YAFFS_YAFFS2
 
@@ -1530,7 +1569,7 @@ static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
                                          int silent)
 {
-       return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -1;
+       return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
 }
 
 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
@@ -1652,12 +1691,12 @@ struct file_system_to_install {
 };
 
 static struct file_system_to_install fs_to_install[] = {
-#ifdef CONFIG_YAFFS_YAFFS1
+//#ifdef CONFIG_YAFFS_YAFFS1
        {&yaffs_fs_type, 0},
-#endif
-#ifdef CONFIG_YAFFS_YAFFS2
+//#endif
+//#ifdef CONFIG_YAFFS_YAFFS2
        {&yaffs2_fs_type, 0},
-#endif
+//#endif
        {NULL, 0}
 };