msi-wmi: depend on backlight and fix corner-cases problems
[safe/jmp/linux-2.6] / fs / ext4 / migrate.c
index 9ee1f7c..8141581 100644 (file)
@@ -13,8 +13,8 @@
  */
 
 #include <linux/module.h>
-#include <linux/ext4_jbd2.h>
-#include <linux/ext4_fs_extents.h>
+#include "ext4_jbd2.h"
+#include "ext4_extents.h"
 
 /*
  * The contiguous blocks details which can be
@@ -43,6 +43,7 @@ static int finish_range(handle_t *handle, struct inode *inode,
 
        if (IS_ERR(path)) {
                retval = PTR_ERR(path);
+               path = NULL;
                goto err_out;
        }
 
@@ -52,19 +53,20 @@ static int finish_range(handle_t *handle, struct inode *inode,
         * credit. But below we try to not accumalate too much
         * of them by restarting the journal.
         */
-       needed = ext4_ext_calc_credits_for_insert(inode, path);
+       needed = ext4_ext_calc_credits_for_single_extent(inode,
+                   lb->last_block - lb->first_block + 1, path);
 
        /*
         * Make sure the credit we accumalated is not really high
         */
-       if (needed && handle->h_buffer_credits >= EXT4_RESERVE_TRANS_BLOCKS) {
+       if (needed && ext4_handle_has_enough_credits(handle,
+                                               EXT4_RESERVE_TRANS_BLOCKS)) {
                retval = ext4_journal_restart(handle, needed);
                if (retval)
                        goto err_out;
-       }
-       if (needed) {
+       } else if (needed) {
                retval = ext4_journal_extend(handle, needed);
-               if (retval != 0) {
+               if (retval) {
                        /*
                         * IF not able to extend the journal restart the journal
                         */
@@ -73,8 +75,12 @@ static int finish_range(handle_t *handle, struct inode *inode,
                                goto err_out;
                }
        }
-       retval = ext4_ext_insert_extent(handle, inode, path, &newext);
+       retval = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
 err_out:
+       if (path) {
+               ext4_ext_drop_refs(path);
+               kfree(path);
+       }
        lb->first_pblock = 0;
        return retval;
 }
@@ -220,6 +226,26 @@ static int update_tind_extent_range(handle_t *handle, struct inode *inode,
 
 }
 
+static int extend_credit_for_blkdel(handle_t *handle, struct inode *inode)
+{
+       int retval = 0, needed;
+
+       if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
+               return 0;
+       /*
+        * We are freeing a blocks. During this we touch
+        * superblock, group descriptor and block bitmap.
+        * So allocate a credit of 3. We may update
+        * quota (user and group).
+        */
+       needed = 3 + EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
+
+       if (ext4_journal_extend(handle, needed) != 0)
+               retval = ext4_journal_restart(handle, needed);
+
+       return retval;
+}
+
 static int free_dind_blocks(handle_t *handle,
                                struct inode *inode, __le32 i_data)
 {
@@ -234,12 +260,19 @@ static int free_dind_blocks(handle_t *handle,
 
        tmp_idata = (__le32 *)bh->b_data;
        for (i = 0; i < max_entries; i++) {
-               if (tmp_idata[i])
-                       ext4_free_blocks(handle, inode,
-                                       le32_to_cpu(tmp_idata[i]), 1, 1);
+               if (tmp_idata[i]) {
+                       extend_credit_for_blkdel(handle, inode);
+                       ext4_free_blocks(handle, inode, 0,
+                                        le32_to_cpu(tmp_idata[i]), 1,
+                                        EXT4_FREE_BLOCKS_METADATA |
+                                        EXT4_FREE_BLOCKS_FORGET);
+               }
        }
        put_bh(bh);
-       ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
+       extend_credit_for_blkdel(handle, inode);
+       ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
+                        EXT4_FREE_BLOCKS_METADATA |
+                        EXT4_FREE_BLOCKS_FORGET);
        return 0;
 }
 
@@ -267,29 +300,36 @@ static int free_tind_blocks(handle_t *handle,
                }
        }
        put_bh(bh);
-       ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
+       extend_credit_for_blkdel(handle, inode);
+       ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
+                        EXT4_FREE_BLOCKS_METADATA |
+                        EXT4_FREE_BLOCKS_FORGET);
        return 0;
 }
 
-static int free_ind_block(handle_t *handle, struct inode *inode)
+static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
 {
        int retval;
-       struct ext4_inode_info *ei = EXT4_I(inode);
 
-       if (ei->i_data[EXT4_IND_BLOCK])
-               ext4_free_blocks(handle, inode,
-                               le32_to_cpu(ei->i_data[EXT4_IND_BLOCK]), 1, 1);
+       /* ei->i_data[EXT4_IND_BLOCK] */
+       if (i_data[0]) {
+               extend_credit_for_blkdel(handle, inode);
+               ext4_free_blocks(handle, inode, 0,
+                               le32_to_cpu(i_data[0]), 1,
+                                EXT4_FREE_BLOCKS_METADATA |
+                                EXT4_FREE_BLOCKS_FORGET);
+       }
 
-       if (ei->i_data[EXT4_DIND_BLOCK]) {
-               retval = free_dind_blocks(handle, inode,
-                                               ei->i_data[EXT4_DIND_BLOCK]);
+       /* ei->i_data[EXT4_DIND_BLOCK] */
+       if (i_data[1]) {
+               retval = free_dind_blocks(handle, inode, i_data[1]);
                if (retval)
                        return retval;
        }
 
-       if (ei->i_data[EXT4_TIND_BLOCK]) {
-               retval = free_tind_blocks(handle, inode,
-                                               ei->i_data[EXT4_TIND_BLOCK]);
+       /* ei->i_data[EXT4_TIND_BLOCK] */
+       if (i_data[2]) {
+               retval = free_tind_blocks(handle, inode, i_data[2]);
                if (retval)
                        return retval;
        }
@@ -297,26 +337,40 @@ static int free_ind_block(handle_t *handle, struct inode *inode)
 }
 
 static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
-                               struct inode *tmp_inode, int retval)
+                                               struct inode *tmp_inode)
 {
+       int retval;
+       __le32  i_data[3];
        struct ext4_inode_info *ei = EXT4_I(inode);
        struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
 
-       retval = free_ind_block(handle, inode);
-       if (retval)
-               goto err_out;
-
        /*
         * One credit accounted for writing the
         * i_data field of the original inode
         */
        retval = ext4_journal_extend(handle, 1);
-       if (retval != 0) {
+       if (retval) {
                retval = ext4_journal_restart(handle, 1);
                if (retval)
                        goto err_out;
        }
 
+       i_data[0] = ei->i_data[EXT4_IND_BLOCK];
+       i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
+       i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
+
+       down_write(&EXT4_I(inode)->i_data_sem);
+       /*
+        * if EXT4_STATE_EXT_MIGRATE is cleared a block allocation
+        * happened after we started the migrate. We need to
+        * fail the migrate
+        */
+       if (!(EXT4_I(inode)->i_state & EXT4_STATE_EXT_MIGRATE)) {
+               retval = -EAGAIN;
+               up_write(&EXT4_I(inode)->i_data_sem);
+               goto err_out;
+       } else
+               EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE;
        /*
         * We have the extent map build with the tmp inode.
         * Now copy the i_data across
@@ -336,8 +390,15 @@ static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
        spin_lock(&inode->i_lock);
        inode->i_blocks += tmp_inode->i_blocks;
        spin_unlock(&inode->i_lock);
+       up_write(&EXT4_I(inode)->i_data_sem);
 
+       /*
+        * We mark the inode dirty after, because we decrement the
+        * i_blocks when freeing the indirect meta-data blocks
+        */
+       retval = free_ind_block(handle, inode, i_data);
        ext4_mark_inode_dirty(handle, inode);
+
 err_out:
        return retval;
 }
@@ -365,7 +426,9 @@ static int free_ext_idx(handle_t *handle, struct inode *inode,
                }
        }
        put_bh(bh);
-       ext4_free_blocks(handle, inode, block, 1, 1);
+       extend_credit_for_blkdel(handle, inode);
+       ext4_free_blocks(handle, inode, 0, block, 1,
+                        EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
        return retval;
 }
 
@@ -393,8 +456,7 @@ static int free_ext_block(handle_t *handle, struct inode *inode)
 
 }
 
-int ext4_ext_migrate(struct inode *inode, struct file *filp,
-                               unsigned int cmd, unsigned long arg)
+int ext4_ext_migrate(struct inode *inode)
 {
        handle_t *handle;
        int retval = 0, i;
@@ -404,14 +466,15 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
        struct inode *tmp_inode = NULL;
        struct list_blocks_struct lb;
        unsigned long max_entries;
+       __u32 goal;
 
-       if (!test_opt(inode->i_sb, EXTENTS))
-               /*
-                * if mounted with noextents we don't allow the migrate
-                */
-               return -EINVAL;
-
-       if ((EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
+       /*
+        * If the filesystem does not support extents, or the inode
+        * already is extent-based, error out.
+        */
+       if (!EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
+                                      EXT4_FEATURE_INCOMPAT_EXTENTS) ||
+           (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
                return -EINVAL;
 
        if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
@@ -420,24 +483,23 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
                 */
                return retval;
 
-       down_write(&EXT4_I(inode)->i_data_sem);
        handle = ext4_journal_start(inode,
                                        EXT4_DATA_TRANS_BLOCKS(inode->i_sb) +
                                        EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
-                                       2 * EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)
+                                       EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)
                                        + 1);
        if (IS_ERR(handle)) {
                retval = PTR_ERR(handle);
-               goto err_out;
+               return retval;
        }
-       tmp_inode = ext4_new_inode(handle,
-                               inode->i_sb->s_root->d_inode,
-                               S_IFREG);
+       goal = (((inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) *
+               EXT4_INODES_PER_GROUP(inode->i_sb)) + 1;
+       tmp_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
+                                  S_IFREG, 0, goal);
        if (IS_ERR(tmp_inode)) {
                retval = -ENOMEM;
                ext4_journal_stop(handle);
-               tmp_inode = NULL;
-               goto err_out;
+               return retval;
        }
        i_size_write(tmp_inode, i_size_read(inode));
        /*
@@ -454,13 +516,6 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
        ext4_orphan_add(handle, tmp_inode);
        ext4_journal_stop(handle);
 
-       ei = EXT4_I(inode);
-       i_data = ei->i_data;
-       memset(&lb, 0, sizeof(lb));
-
-       /* 32 bit block address 4 bytes */
-       max_entries = inode->i_sb->s_blocksize >> 2;
-
        /*
         * start with one credit accounted for
         * superblock modification.
@@ -469,7 +524,26 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
         * trascation that created the inode. Later as and
         * when we add extents we extent the journal
         */
+       /*
+        * Even though we take i_mutex we can still cause block
+        * allocation via mmap write to holes. If we have allocated
+        * new blocks we fail migrate.  New block allocation will
+        * clear EXT4_STATE_EXT_MIGRATE flag.  The flag is updated
+        * with i_data_sem held to prevent racing with block
+        * allocation.
+        */
+       down_read((&EXT4_I(inode)->i_data_sem));
+       EXT4_I(inode)->i_state |= EXT4_STATE_EXT_MIGRATE;
+       up_read((&EXT4_I(inode)->i_data_sem));
+
        handle = ext4_journal_start(inode, 1);
+
+       ei = EXT4_I(inode);
+       i_data = ei->i_data;
+       memset(&lb, 0, sizeof(lb));
+
+       /* 32 bit block address 4 bytes */
+       max_entries = inode->i_sb->s_blocksize >> 2;
        for (i = 0; i < EXT4_NDIR_BLOCKS; i++, blk_count++) {
                if (i_data[i]) {
                        retval = update_extent_range(handle, tmp_inode,
@@ -507,28 +581,25 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
         */
        retval = finish_range(handle, tmp_inode, &lb);
 err_out:
-       /*
-        * We are either freeing extent information or indirect
-        * blocks. During this we touch superblock, group descriptor
-        * and block bitmap. Later we mark the tmp_inode dirty
-        * via ext4_ext_tree_init. So allocate a credit of 4
-        * We may update quota (user and group).
-        *
-        * FIXME!! we may be touching bitmaps in different block groups.
-        */
-       if (ext4_journal_extend(handle,
-                       4 + 2*EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb)) != 0)
-               ext4_journal_restart(handle,
-                               4 + 2*EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb));
        if (retval)
                /*
                 * Failure case delete the extent information with the
                 * tmp_inode
                 */
                free_ext_block(handle, tmp_inode);
-       else
-               retval = ext4_ext_swap_inode_data(handle, inode,
-                                                       tmp_inode, retval);
+       else {
+               retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode);
+               if (retval)
+                       /*
+                        * if we fail to swap inode data free the extent
+                        * details of the tmp inode
+                        */
+                       free_ext_block(handle, tmp_inode);
+       }
+
+       /* We mark the tmp_inode dirty via ext4_ext_tree_init. */
+       if (ext4_journal_extend(handle, 1) != 0)
+               ext4_journal_restart(handle, 1);
 
        /*
         * Mark the tmp_inode as of size zero
@@ -556,11 +627,8 @@ err_out:
        tmp_inode->i_nlink = 0;
 
        ext4_journal_stop(handle);
-
-       up_write(&EXT4_I(inode)->i_data_sem);
-
-       if (tmp_inode)
-               iput(tmp_inode);
+       unlock_new_inode(tmp_inode);
+       iput(tmp_inode);
 
        return retval;
 }