writeback: get rid of wbc->for_writepages
[linux-2.6.git] / fs / btrfs / ordered-data.c
index b5745bb..7b2f401 100644 (file)
@@ -39,11 +39,11 @@ static u64 entry_end(struct btrfs_ordered_extent *entry)
 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);
 
@@ -67,13 +67,13 @@ static struct rb_node *tree_insert(struct rb_root *root, u64 file_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;
@@ -88,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;
@@ -102,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;
@@ -165,8 +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 disk_len, int nocow,
-                            int compressed)
+                            u64 start, u64 len, u64 disk_len, int type)
 {
        struct btrfs_ordered_inode_tree *tree;
        struct rb_node *node;
@@ -183,10 +182,8 @@ int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
        entry->len = len;
        entry->disk_len = disk_len;
        entry->inode = inode;
-       if (nocow)
-               set_bit(BTRFS_ORDERED_NOCOW, &entry->flags);
-       if (compressed)
-               set_bit(BTRFS_ORDERED_COMPRESSED, &entry->flags);
+       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);
@@ -196,10 +193,8 @@ int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
 
        node = tree_insert(&tree->tree, file_offset,
                           &entry->rb_node);
-       if (node) {
-               printk("warning dup entry from add_ordered_extent\n");
-               BUG();
-       }
+       BUG_ON(node);
+
        set_extent_ordered(&BTRFS_I(inode)->io_tree, file_offset,
                           entry_end(entry) - 1, GFP_NOFS);
 
@@ -285,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);
@@ -315,6 +310,16 @@ int btrfs_remove_ordered_extent(struct inode *inode,
 
        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);
@@ -342,7 +347,8 @@ int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only)
                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_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);
@@ -374,6 +380,68 @@ int btrfs_wait_ordered_extents(struct btrfs_root *root, int nocow_only)
 }
 
 /*
+ * 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
@@ -392,7 +460,7 @@ 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);
+       btrfs_fdatawrite_range(inode->i_mapping, start, end, WB_SYNC_ALL);
        if (wait) {
                wait_event(entry->wait, test_bit(BTRFS_ORDERED_COMPLETE,
                                                 &entry->flags));
@@ -421,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;
@@ -450,10 +523,7 @@ 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;
@@ -491,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;
@@ -552,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;
@@ -580,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)) {
+               if (test->file_offset > entry_end(ordered))
                        i_size_test = test->file_offset;
-               }
        } else {
                i_size_test = i_size_read(inode);
        }
@@ -609,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;
@@ -626,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;
@@ -672,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);
 }
@@ -730,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;
+}