writeback: get rid of wbc->for_writepages
[linux-2.6.git] / fs / btrfs / ordered-data.c
index b695f5b..7b2f401 100644 (file)
@@ -26,7 +26,6 @@
 #include "btrfs_inode.h"
 #include "extent_io.h"
 
-
 static u64 entry_end(struct btrfs_ordered_extent *entry)
 {
        if (entry->file_offset + entry->len < entry->file_offset)
@@ -34,14 +33,17 @@ static u64 entry_end(struct btrfs_ordered_extent *entry)
        return entry->file_offset + entry->len;
 }
 
+/* returns NULL if the insertion worked, or it returns the node it did find
+ * in the tree
+ */
 static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
                                   struct rb_node *node)
 {
-       struct rb_node ** p = &root->rb_node;
-       struct rb_node * parent = NULL;
+       struct rb_node **p = &root->rb_node;
+       struct rb_node *parent = NULL;
        struct btrfs_ordered_extent *entry;
 
-       while(*p) {
+       while (*p) {
                parent = *p;
                entry = rb_entry(parent, struct btrfs_ordered_extent, rb_node);
 
@@ -58,16 +60,20 @@ static struct rb_node *tree_insert(struct rb_root *root, u64 file_offset,
        return NULL;
 }
 
+/*
+ * look for a given offset in the tree, and if it can't be found return the
+ * first lesser offset
+ */
 static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
                                     struct rb_node **prev_ret)
 {
-       struct rb_node * n = root->rb_node;
+       struct rb_node *n = root->rb_node;
        struct rb_node *prev = NULL;
        struct rb_node *test;
        struct btrfs_ordered_extent *entry;
        struct btrfs_ordered_extent *prev_entry = NULL;
 
-       while(n) {
+       while (n) {
                entry = rb_entry(n, struct btrfs_ordered_extent, rb_node);
                prev = n;
                prev_entry = entry;
@@ -82,7 +88,7 @@ static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
        if (!prev_ret)
                return NULL;
 
-       while(prev && file_offset >= entry_end(prev_entry)) {
+       while (prev && file_offset >= entry_end(prev_entry)) {
                test = rb_next(prev);
                if (!test)
                        break;
@@ -96,7 +102,7 @@ static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
        if (prev)
                prev_entry = rb_entry(prev, struct btrfs_ordered_extent,
                                      rb_node);
-       while(prev && file_offset < entry_end(prev_entry)) {
+       while (prev && file_offset < entry_end(prev_entry)) {
                test = rb_prev(prev);
                if (!test)
                        break;
@@ -108,6 +114,9 @@ static struct rb_node *__tree_search(struct rb_root *root, u64 file_offset,
        return NULL;
 }
 
+/*
+ * helper to check if a given offset is inside a given entry
+ */
 static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
 {
        if (file_offset < entry->file_offset ||
@@ -116,6 +125,10 @@ static int offset_in_entry(struct btrfs_ordered_extent *entry, u64 file_offset)
        return 1;
 }
 
+/*
+ * look find the first ordered struct that has this offset, otherwise
+ * the first one less than this offset
+ */
 static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
                                          u64 file_offset)
 {
@@ -152,7 +165,7 @@ static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,
  * inserted.
  */
 int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
-                            u64 start, u64 len)
+                            u64 start, u64 len, u64 disk_len, int type)
 {
        struct btrfs_ordered_inode_tree *tree;
        struct rb_node *node;
@@ -167,20 +180,29 @@ int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
        entry->file_offset = file_offset;
        entry->start = start;
        entry->len = len;
+       entry->disk_len = disk_len;
+       entry->inode = inode;
+       if (type != BTRFS_ORDERED_IO_DONE && type != BTRFS_ORDERED_COMPLETE)
+               set_bit(type, &entry->flags);
+
        /* one ref for the tree */
        atomic_set(&entry->refs, 1);
        init_waitqueue_head(&entry->wait);
        INIT_LIST_HEAD(&entry->list);
+       INIT_LIST_HEAD(&entry->root_extent_list);
 
        node = tree_insert(&tree->tree, file_offset,
                           &entry->rb_node);
-       if (node) {
-               entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
-               atomic_inc(&entry->refs);
-       }
+       BUG_ON(node);
+
        set_extent_ordered(&BTRFS_I(inode)->io_tree, file_offset,
                           entry_end(entry) - 1, GFP_NOFS);
 
+       spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
+       list_add_tail(&entry->root_extent_list,
+                     &BTRFS_I(inode)->root->fs_info->ordered_extents);
+       spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
+
        mutex_unlock(&tree->mutex);
        BUG_ON(node);
        return 0;
@@ -258,7 +280,7 @@ int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
        struct btrfs_ordered_sum *sum;
 
        if (atomic_dec_and_test(&entry->refs)) {
-               while(!list_empty(&entry->list)) {
+               while (!list_empty(&entry->list)) {
                        cur = entry->list.next;
                        sum = list_entry(cur, struct btrfs_ordered_sum, list);
                        list_del(&sum->list);
@@ -285,12 +307,141 @@ int btrfs_remove_ordered_extent(struct inode *inode,
        rb_erase(node, &tree->tree);
        tree->last = NULL;
        set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
+
+       spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
+       list_del_init(&entry->root_extent_list);
+
+       /*
+        * we have no more ordered extents for this inode and
+        * no dirty pages.  We can safely remove it from the
+        * list of ordered extents
+        */
+       if (RB_EMPTY_ROOT(&tree->tree) &&
+           !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {
+               list_del_init(&BTRFS_I(inode)->ordered_operations);
+       }
+       spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
+
        mutex_unlock(&tree->mutex);
        wake_up(&entry->wait);
        return 0;
 }
 
 /*
+ * wait for all the ordered extents in a root.  This is done when balancing
+ * space between drives.
+ */
+int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only)
+{
+       struct list_head splice;
+       struct list_head *cur;
+       struct btrfs_ordered_extent *ordered;
+       struct inode *inode;
+
+       INIT_LIST_HEAD(&splice);
+
+       spin_lock(&root->fs_info->ordered_extent_lock);
+       list_splice_init(&root->fs_info->ordered_extents, &splice);
+       while (!list_empty(&splice)) {
+               cur = splice.next;
+               ordered = list_entry(cur, struct btrfs_ordered_extent,
+                                    root_extent_list);
+               if (nocow_only &&
+                   !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) &&
+                   !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
+                       list_move(&ordered->root_extent_list,
+                                 &root->fs_info->ordered_extents);
+                       cond_resched_lock(&root->fs_info->ordered_extent_lock);
+                       continue;
+               }
+
+               list_del_init(&ordered->root_extent_list);
+               atomic_inc(&ordered->refs);
+
+               /*
+                * the inode may be getting freed (in sys_unlink path).
+                */
+               inode = igrab(ordered->inode);
+
+               spin_unlock(&root->fs_info->ordered_extent_lock);
+
+               if (inode) {
+                       btrfs_start_ordered_extent(inode, ordered, 1);
+                       btrfs_put_ordered_extent(ordered);
+                       iput(inode);
+               } else {
+                       btrfs_put_ordered_extent(ordered);
+               }
+
+               spin_lock(&root->fs_info->ordered_extent_lock);
+       }
+       spin_unlock(&root->fs_info->ordered_extent_lock);
+       return 0;
+}
+
+/*
+ * this is used during transaction commit to write all the inodes
+ * added to the ordered operation list.  These files must be fully on
+ * disk before the transaction commits.
+ *
+ * we have two modes here, one is to just start the IO via filemap_flush
+ * and the other is to wait for all the io.  When we wait, we have an
+ * extra check to make sure the ordered operation list really is empty
+ * before we return
+ */
+int btrfs_run_ordered_operations(struct btrfs_root *root, int wait)
+{
+       struct btrfs_inode *btrfs_inode;
+       struct inode *inode;
+       struct list_head splice;
+
+       INIT_LIST_HEAD(&splice);
+
+       mutex_lock(&root->fs_info->ordered_operations_mutex);
+       spin_lock(&root->fs_info->ordered_extent_lock);
+again:
+       list_splice_init(&root->fs_info->ordered_operations, &splice);
+
+       while (!list_empty(&splice)) {
+               btrfs_inode = list_entry(splice.next, struct btrfs_inode,
+                                  ordered_operations);
+
+               inode = &btrfs_inode->vfs_inode;
+
+               list_del_init(&btrfs_inode->ordered_operations);
+
+               /*
+                * the inode may be getting freed (in sys_unlink path).
+                */
+               inode = igrab(inode);
+
+               if (!wait && inode) {
+                       list_add_tail(&BTRFS_I(inode)->ordered_operations,
+                             &root->fs_info->ordered_operations);
+               }
+               spin_unlock(&root->fs_info->ordered_extent_lock);
+
+               if (inode) {
+                       if (wait)
+                               btrfs_wait_ordered_range(inode, 0, (u64)-1);
+                       else
+                               filemap_flush(inode->i_mapping);
+                       iput(inode);
+               }
+
+               cond_resched();
+               spin_lock(&root->fs_info->ordered_extent_lock);
+       }
+       if (wait && !list_empty(&root->fs_info->ordered_operations))
+               goto again;
+
+       spin_unlock(&root->fs_info->ordered_extent_lock);
+       mutex_unlock(&root->fs_info->ordered_operations_mutex);
+
+       return 0;
+}
+
+/*
  * Used to start IO or wait for a given ordered extent to finish.
  *
  * If wait is one, this effectively waits on page writeback for all the pages
@@ -309,16 +460,17 @@ void btrfs_start_ordered_extent(struct inode *inode,
         * start IO on any dirty ones so the wait doesn't stall waiting
         * for pdflush to find them
         */
-       btrfs_fdatawrite_range(inode->i_mapping, start, end, WB_SYNC_NONE);
-       if (wait)
+       btrfs_fdatawrite_range(inode->i_mapping, start, end, WB_SYNC_ALL);
+       if (wait) {
                wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
                                                 &entry->flags));
+       }
 }
 
 /*
  * Used to wait on ordered extents across a large range of bytes.
  */
-void btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
+int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)
 {
        u64 end;
        u64 orig_end;
@@ -337,18 +489,23 @@ again:
        /* start IO across the range first to instantiate any delalloc
         * extents
         */
-       btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_NONE);
+       btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_ALL);
+
+       /* The compression code will leave pages locked but return from
+        * writepage without setting the page writeback.  Starting again
+        * with WB_SYNC_ALL will end up waiting for the IO to actually start.
+        */
+       btrfs_fdatawrite_range(inode->i_mapping, start, orig_end, WB_SYNC_ALL);
 
        btrfs_wait_on_page_writeback_range(inode->i_mapping,
                                           start >> PAGE_CACHE_SHIFT,
                                           orig_end >> PAGE_CACHE_SHIFT);
 
        end = orig_end;
-       while(1) {
+       while (1) {
                ordered = btrfs_lookup_first_ordered_extent(inode, end);
-               if (!ordered) {
+               if (!ordered)
                        break;
-               }
                if (ordered->file_offset > orig_end) {
                        btrfs_put_ordered_extent(ordered);
                        break;
@@ -366,12 +523,10 @@ again:
        }
        if (test_range_bit(&BTRFS_I(inode)->io_tree, start, orig_end,
                           EXTENT_ORDERED | EXTENT_DELALLOC, 0)) {
-               printk("inode %lu still ordered or delalloc after wait "
-                      "%llu %llu\n", inode->i_ino,
-                      (unsigned long long)start,
-                      (unsigned long long)orig_end);
+               schedule_timeout(1);
                goto again;
        }
+       return 0;
 }
 
 /*
@@ -406,7 +561,7 @@ out:
  * if none is found
  */
 struct btrfs_ordered_extent *
-btrfs_lookup_first_ordered_extent(struct inode * inode, u64 file_offset)
+btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
 {
        struct btrfs_ordered_inode_tree *tree;
        struct rb_node *node;
@@ -467,7 +622,7 @@ int btrfs_ordered_update_i_size(struct inode *inode,
         * yet
         */
        node = &ordered->rb_node;
-       while(1) {
+       while (1) {
                node = rb_prev(node);
                if (!node)
                        break;
@@ -495,9 +650,8 @@ int btrfs_ordered_update_i_size(struct inode *inode,
                 * between our ordered extent and the next one.
                 */
                test = rb_entry(node, struct btrfs_ordered_extent, rb_node);
-               if (test->file_offset > entry_end(ordered)) {
-                       i_size_test = test->file_offset - 1;
-               }
+               if (test->file_offset > entry_end(ordered))
+                       i_size_test = test->file_offset;
        } else {
                i_size_test = i_size_read(inode);
        }
@@ -509,7 +663,7 @@ int btrfs_ordered_update_i_size(struct inode *inode,
         * disk_i_size to the end of the region.
         */
        if (i_size_test > entry_end(ordered) &&
-           !test_range_bit(io_tree, entry_end(ordered), i_size_test,
+           !test_range_bit(io_tree, entry_end(ordered), i_size_test - 1,
                           EXTENT_DELALLOC, 0)) {
                new_i_size = min_t(u64, i_size_test, i_size_read(inode));
        }
@@ -524,13 +678,13 @@ out:
  * try to find a checksum.  This is used because we allow pages to
  * be reclaimed before their checksum is actually put into the btree
  */
-int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
+int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
+                          u32 *sum)
 {
        struct btrfs_ordered_sum *ordered_sum;
        struct btrfs_sector_sum *sector_sums;
        struct btrfs_ordered_extent *ordered;
        struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree;
-       struct list_head *cur;
        unsigned long num_sectors;
        unsigned long i;
        u32 sectorsize = BTRFS_I(inode)->root->sectorsize;
@@ -541,13 +695,12 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
                return 1;
 
        mutex_lock(&tree->mutex);
-       list_for_each_prev(cur, &ordered->list) {
-               ordered_sum = list_entry(cur, struct btrfs_ordered_sum, list);
-               if (offset >= ordered_sum->file_offset) {
+       list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
+               if (disk_bytenr >= ordered_sum->bytenr) {
                        num_sectors = ordered_sum->len / sectorsize;
                        sector_sums = ordered_sum->sums;
                        for (i = 0; i < num_sectors; i++) {
-                               if (sector_sums[i].offset == offset) {
+                               if (sector_sums[i].bytenr == disk_bytenr) {
                                        *sum = sector_sums[i].sum;
                                        ret = 0;
                                        goto out;
@@ -557,6 +710,7 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u32 *sum)
        }
 out:
        mutex_unlock(&tree->mutex);
+       btrfs_put_ordered_extent(ordered);
        return ret;
 }
 
@@ -586,7 +740,6 @@ int btrfs_fdatawrite_range(struct address_space *mapping, loff_t start,
                .nr_to_write = mapping->nrpages * 2,
                .range_start = start,
                .range_end = end,
-               .for_writepages = 1,
        };
        return btrfs_writepages(mapping, &wbc);
 }
@@ -644,3 +797,49 @@ int btrfs_wait_on_page_writeback_range(struct address_space *mapping,
 
        return ret;
 }
+
+/*
+ * add a given inode to the list of inodes that must be fully on
+ * disk before a transaction commit finishes.
+ *
+ * This basically gives us the ext3 style data=ordered mode, and it is mostly
+ * used to make sure renamed files are fully on disk.
+ *
+ * It is a noop if the inode is already fully on disk.
+ *
+ * If trans is not null, we'll do a friendly check for a transaction that
+ * is already flushing things and force the IO down ourselves.
+ */
+int btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
+                               struct btrfs_root *root,
+                               struct inode *inode)
+{
+       u64 last_mod;
+
+       last_mod = max(BTRFS_I(inode)->generation, BTRFS_I(inode)->last_trans);
+
+       /*
+        * if this file hasn't been changed since the last transaction
+        * commit, we can safely return without doing anything
+        */
+       if (last_mod < root->fs_info->last_trans_committed)
+               return 0;
+
+       /*
+        * the transaction is already committing.  Just start the IO and
+        * don't bother with all of this list nonsense
+        */
+       if (trans && root->fs_info->running_transaction->blocked) {
+               btrfs_wait_ordered_range(inode, 0, (u64)-1);
+               return 0;
+       }
+
+       spin_lock(&root->fs_info->ordered_extent_lock);
+       if (list_empty(&BTRFS_I(inode)->ordered_operations)) {
+               list_add_tail(&BTRFS_I(inode)->ordered_operations,
+                             &root->fs_info->ordered_operations);
+       }
+       spin_unlock(&root->fs_info->ordered_extent_lock);
+
+       return 0;
+}