yaffs Change file Linux vfs truncation to new mechanism
[yaffs2.git] / yaffs_fs.c
index 8e39ce251d5513557cb93130c760a11fa2e5040c..7c51041184350166bea88fbaa3628badd95103f8 100644 (file)
 #define YAFFS_COMPILE_EXPORTFS
 #endif
 
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
+#define YAFFS_USE_SETATTR_COPY
+#define YAFFS_USE_TRUNCATE_SETSIZE
+#endif
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
+#define YAFFS_HAS_EVICT_INODE
+#endif
+
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
 #define YAFFS_NEW_FOLLOW_LINK 1
 #else
@@ -151,6 +159,7 @@ unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
 unsigned int yaffs_auto_checkpoint = 1;
 unsigned int yaffs_gc_control = 1;
+unsigned int yaffs_bg_enable = 1;
 
 /* Module Parameters */
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
@@ -158,6 +167,7 @@ module_param(yaffs_traceMask, uint, 0644);
 module_param(yaffs_wr_attempts, uint, 0644);
 module_param(yaffs_auto_checkpoint, uint, 0644);
 module_param(yaffs_gc_control, uint, 0644);
+module_param(yaffs_bg_enable, uint, 0644);
 #else
 MODULE_PARM(yaffs_traceMask, "i");
 MODULE_PARM(yaffs_wr_attempts, "i");
@@ -267,8 +277,12 @@ static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
 static void yaffs_put_inode(struct inode *inode);
 #endif
 
+#ifdef YAFFS_HAS_EVICT_INODE
+static void yaffs_evict_inode(struct inode *);
+#else
 static void yaffs_delete_inode(struct inode *);
 static void yaffs_clear_inode(struct inode *);
+#endif
 
 static int yaffs_readpage(struct file *file, struct page *page);
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
@@ -315,6 +329,9 @@ static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
 
 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
 
+static int yaffs_vfs_setattr(struct inode *, struct iattr *);
+
+
 static struct address_space_operations yaffs_file_address_operations = {
        .readpage = yaffs_readpage,
        .writepage = yaffs_writepage,
@@ -327,6 +344,7 @@ static struct address_space_operations yaffs_file_address_operations = {
 #endif
 };
 
+
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
 static const struct file_operations yaffs_file_operations = {
        .read = do_sync_read,
@@ -440,12 +458,39 @@ static const struct super_operations yaffs_super_ops = {
        .put_inode = yaffs_put_inode,
 #endif
        .put_super = yaffs_put_super,
+#ifdef YAFFS_HAS_EVICT_INODE
+       .evict_inode = yaffs_evict_inode,
+#else
        .delete_inode = yaffs_delete_inode,
        .clear_inode = yaffs_clear_inode,
+#endif
        .sync_fs = yaffs_sync_fs,
        .write_super = yaffs_write_super,
 };
 
+
+static  int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
+{
+#ifdef  YAFFS_USE_SETATTR_COPY
+       setattr_copy(inode,attr);
+       return 0;
+#else
+       return inode_setattr(inode, attr);
+#endif
+
+}
+
+static  int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
+{
+#ifdef  YAFFS_USE_TRUNCATE_SETSIZE
+       truncate_setsize(inode,newsize);
+       return 0;
+#else
+       return simple_setsize(inode, newsize);
+#endif
+
+}
+
 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
 {
        return yaffs_gc_control;
@@ -788,36 +833,85 @@ static void yaffs_put_inode(struct inode *inode)
 }
 #endif
 
-/* clear is called to tell the fs to release any per-inode data it holds */
-static void yaffs_clear_inode(struct inode *inode)
+
+static void yaffs_UnstitchObject(struct inode *inode, yaffs_Object *obj)
+{
+       /* Clear the association between the inode and
+        * the yaffs_Object.
+        */
+       obj->myInode = NULL;
+       yaffs_InodeToObjectLV(inode) = NULL;
+
+       /* If the object freeing was deferred, then the real
+        * free happens now.
+        * This should fix the inode inconsistency problem.
+        */
+       yaffs_HandleDeferedFree(obj);
+}
+
+#ifdef YAFFS_HAS_EVICT_INODE
+/* yaffs_evict_inode combines into one operation what was previously done in
+ * yaffs_clear_inode() and yaffs_delete_inode()
+ *
+ */
+static void yaffs_evict_inode( struct inode *inode)
 {
        yaffs_Object *obj;
        yaffs_Device *dev;
+       int deleteme = 0;
 
        obj = yaffs_InodeToObject(inode);
 
        T(YAFFS_TRACE_OS,
-               (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
+               (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
                atomic_read(&inode->i_count),
                obj ? "object exists" : "null object"));
 
+       if (!inode->i_nlink && !is_bad_inode(inode))
+               deleteme = 1;
+       truncate_inode_pages(&inode->i_data,0);
+       end_writeback(inode);
+
+       if(deleteme && obj){
+               dev = obj->myDev;
+               yaffs_GrossLock(dev);
+               yaffs_DeleteObject(obj);
+               yaffs_GrossUnlock(dev);
+       }
        if (obj) {
                dev = obj->myDev;
                yaffs_GrossLock(dev);
+               yaffs_UnstitchObject(inode,obj);
+               yaffs_GrossUnlock(dev);
+       }
 
-               /* Clear the association between the inode and
-                * the yaffs_Object.
-                */
-               obj->myInode = NULL;
-               yaffs_InodeToObjectLV(inode) = NULL;
 
-               /* If the object freeing was deferred, then the real
-                * free happens now.
-                * This should fix the inode inconsistency problem.
-                */
+}
+#else
 
-               yaffs_HandleDeferedFree(obj);
+/* clear is called to tell the fs to release any per-inode data it holds.
+ * The object might still exist on disk and is just being thrown out of the cache
+ * or else the object has actually been deleted and we're being called via
+ * the chain
+ *   yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
+ */
 
+static void yaffs_clear_inode(struct inode *inode)
+{
+       yaffs_Object *obj;
+       yaffs_Device *dev;
+
+       obj = yaffs_InodeToObject(inode);
+
+       T(YAFFS_TRACE_OS,
+               (TSTR("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) {
+               dev = obj->myDev;
+               yaffs_GrossLock(dev);
+               yaffs_UnstitchObject(inode,obj);
                yaffs_GrossUnlock(dev);
        }
 
@@ -849,6 +943,8 @@ static void yaffs_delete_inode(struct inode *inode)
 #endif
        clear_inode(inode);
 }
+#endif
+
 
 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
 static int yaffs_file_flush(struct file *file, fl_owner_t id)
@@ -1881,10 +1977,12 @@ static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
        if (error == 0) {
                int result;
                if (!error){
-                       error = inode_setattr(inode, attr);
+                       error = yaffs_vfs_setattr(inode, attr);
                        T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
-                       if (attr->ia_valid & ATTR_SIZE)
-                               truncate_inode_pages(&inode->i_data,attr->ia_size);
+                       if (attr->ia_valid & ATTR_SIZE){
+                               yaffs_vfs_setsize(inode,attr->ia_size);
+                               inode->i_blocks = (inode->i_size + 511) >> 9;
+                       }
                }
                dev = yaffs_InodeToObject(inode)->myDev;
                if (attr->ia_valid & ATTR_SIZE){
@@ -1950,9 +2048,8 @@ ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
        yaffs_Object *obj = yaffs_InodeToObject(inode);
 
        T(YAFFS_TRACE_OS,
-               (TSTR("yaffs_getxattr of object %d\n"),
-               obj->objectId));
-
+               (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
+               name, obj->objectId));
 
        if (error == 0) {
                dev = obj->myDev;
@@ -2232,12 +2329,12 @@ static int yaffs_BackgroundThread(void *data)
 
                now = jiffies;
 
-               if(time_after(now, next_dir_update)){
+               if(time_after(now, next_dir_update) && yaffs_bg_enable){
                        yaffs_UpdateDirtyDirectories(dev);
                        next_dir_update = now + HZ;
                }
 
-               if(time_after(now,next_gc)){
+               if(time_after(now,next_gc) && yaffs_bg_enable){
                        if(!dev->isCheckpointed){
                                urgency = yaffs_bg_gc_urgency(dev);
                                gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
@@ -3106,6 +3203,7 @@ static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
        buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
        buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
        buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
+       buf += sprintf(buf, "nGCBlocks.......... %u\n", dev->nGCBlocks);
        buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
        buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
        buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);