Btrfs: cleanup xattr code
[safe/jmp/linux-2.6] / fs / btrfs / inode.c
index 2ed2dea..8adfe05 100644 (file)
@@ -38,6 +38,7 @@
 #include <linux/xattr.h>
 #include <linux/posix_acl.h>
 #include <linux/falloc.h>
+#include "compat.h"
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
@@ -47,7 +48,6 @@
 #include "volumes.h"
 #include "ordered-data.h"
 #include "xattr.h"
-#include "compat.h"
 #include "tree-log.h"
 #include "ref-cache.h"
 #include "compression.h"
@@ -101,10 +101,9 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
        u64 total;
        u64 used;
        u64 thresh;
-       unsigned long flags;
        int ret = 0;
 
-       spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
+       spin_lock(&root->fs_info->delalloc_lock);
        total = btrfs_super_total_bytes(&root->fs_info->super_copy);
        used = btrfs_super_bytes_used(&root->fs_info->super_copy);
        if (for_del)
@@ -116,7 +115,7 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
 
        if (used + root->fs_info->delalloc_bytes + num_required > thresh)
                ret = -ENOSPC;
-       spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
+       spin_unlock(&root->fs_info->delalloc_lock);
        return ret;
 }
 
@@ -125,7 +124,7 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
  * the btree.  The caller should have done a btrfs_drop_extents so that
  * no overlapping inline items exist in the btree
  */
-static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
+static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
                                struct btrfs_root *root, struct inode *inode,
                                u64 start, size_t size, size_t compressed_size,
                                struct page **compressed_pages)
@@ -149,7 +148,8 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
                cur_size = compressed_size;
        }
 
-       path = btrfs_alloc_path(); if (!path)
+       path = btrfs_alloc_path();
+       if (!path)
                return -ENOMEM;
 
        btrfs_set_trans_block_group(trans, inode);
@@ -157,7 +157,6 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
        key.objectid = inode->i_ino;
        key.offset = start;
        btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
-       inode_add_bytes(inode, size);
        datasize = btrfs_file_extent_calc_inline_size(cur_size);
 
        inode_add_bytes(inode, size);
@@ -166,7 +165,6 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
        BUG_ON(ret);
        if (ret) {
                err = ret;
-               printk("got bad ret %d\n", ret);
                goto fail;
        }
        leaf = path->nodes[0];
@@ -182,7 +180,7 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
        if (use_compress) {
                struct page *cpage;
                int i = 0;
-               while(compressed_size > 0) {
+               while (compressed_size > 0) {
                        cpage = compressed_pages[i];
                        cur_size = min_t(unsigned long, compressed_size,
                                       PAGE_CACHE_SIZE);
@@ -332,6 +330,7 @@ static noinline int compress_file_range(struct inode *inode,
        u64 disk_num_bytes;
        u64 blocksize = root->sectorsize;
        u64 actual_end;
+       u64 isize = i_size_read(inode);
        int ret = 0;
        struct page **pages = NULL;
        unsigned long nr_pages;
@@ -345,12 +344,12 @@ static noinline int compress_file_range(struct inode *inode,
 
        orig_start = start;
 
+       actual_end = min_t(u64, isize, end + 1);
 again:
        will_compress = 0;
        nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
        nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
 
-       actual_end = min_t(u64, i_size_read(inode), end + 1);
        total_compressed = actual_end - start;
 
        /* we want to make sure that amount of ram required to uncompress
@@ -488,7 +487,7 @@ again:
                add_async_extent(async_cow, start, num_bytes,
                                 total_compressed, pages, nr_pages_ret);
 
-               if (start + num_bytes < end) {
+               if (start + num_bytes < end && start + num_bytes < actual_end) {
                        start += num_bytes;
                        pages = NULL;
                        cond_resched();
@@ -519,8 +518,7 @@ free_pages_out:
                WARN_ON(pages[i]->mapping);
                page_cache_release(pages[i]);
        }
-       if (pages)
-               kfree(pages);
+       kfree(pages);
 
        goto out;
 }
@@ -549,7 +547,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
 
        trans = btrfs_join_transaction(root, 1);
 
-       while(!list_empty(&async_cow->extents)) {
+       while (!list_empty(&async_cow->extents)) {
                async_extent = list_entry(async_cow->extents.next,
                                          struct async_extent, list);
                list_del(&async_extent->list);
@@ -562,8 +560,8 @@ static noinline int submit_compressed_extents(struct inode *inode,
                        unsigned long nr_written = 0;
 
                        lock_extent(io_tree, async_extent->start,
-                                   async_extent->start + async_extent->ram_size - 1,
-                                   GFP_NOFS);
+                                   async_extent->start +
+                                   async_extent->ram_size - 1, GFP_NOFS);
 
                        /* allocate blocks */
                        cow_file_range(inode, async_cow->locked_page,
@@ -581,7 +579,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
                        if (!page_started)
                                extent_write_locked_range(io_tree,
                                                  inode, async_extent->start,
-                                                 async_extent->start +
+                                                 async_extent->start +
                                                  async_extent->ram_size - 1,
                                                  btrfs_get_extent,
                                                  WB_SYNC_ALL);
@@ -618,7 +616,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
                set_bit(EXTENT_FLAG_PINNED, &em->flags);
                set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
 
-               while(1) {
+               while (1) {
                        spin_lock(&em_tree->lock);
                        ret = add_extent_mapping(em_tree, em);
                        spin_unlock(&em_tree->lock);
@@ -651,11 +649,11 @@ static noinline int submit_compressed_extents(struct inode *inode,
                                             NULL, 1, 1, 0, 1, 1, 0);
 
                ret = btrfs_submit_compressed_write(inode,
-                                        async_extent->start,
-                                        async_extent->ram_size,
-                                        ins.objectid,
-                                        ins.offset, async_extent->pages,
-                                        async_extent->nr_pages);
+                                   async_extent->start,
+                                   async_extent->ram_size,
+                                   ins.objectid,
+                                   ins.offset, async_extent->pages,
+                                   async_extent->nr_pages);
 
                BUG_ON(ret);
                trans = btrfs_join_transaction(root, 1);
@@ -696,6 +694,7 @@ static noinline int cow_file_range(struct inode *inode,
        u64 cur_alloc_size;
        u64 blocksize = root->sectorsize;
        u64 actual_end;
+       u64 isize = i_size_read(inode);
        struct btrfs_key ins;
        struct extent_map *em;
        struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
@@ -705,7 +704,7 @@ static noinline int cow_file_range(struct inode *inode,
        BUG_ON(!trans);
        btrfs_set_trans_block_group(trans, inode);
 
-       actual_end = min_t(u64, i_size_read(inode), end + 1);
+       actual_end = min_t(u64, isize, end + 1);
 
        num_bytes = (end - start + blocksize) & ~(blocksize - 1);
        num_bytes = max(blocksize,  num_bytes);
@@ -734,14 +733,13 @@ static noinline int cow_file_range(struct inode *inode,
 
        btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
 
-       while(disk_num_bytes > 0) {
+       while (disk_num_bytes > 0) {
                cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
                ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
                                           root->sectorsize, 0, alloc_hint,
                                           (u64)-1, &ins, 1);
-               if (ret) {
-                       BUG();
-               }
+               BUG_ON(ret);
+
                em = alloc_extent_map(GFP_NOFS);
                em->start = start;
                em->orig_start = em->start;
@@ -754,7 +752,7 @@ static noinline int cow_file_range(struct inode *inode,
                em->bdev = root->fs_info->fs_devices->latest_bdev;
                set_bit(EXTENT_FLAG_PINNED, &em->flags);
 
-               while(1) {
+               while (1) {
                        spin_lock(&em_tree->lock);
                        ret = add_extent_mapping(em_tree, em);
                        spin_unlock(&em_tree->lock);
@@ -771,11 +769,16 @@ static noinline int cow_file_range(struct inode *inode,
                                               ram_size, cur_alloc_size, 0);
                BUG_ON(ret);
 
-               if (disk_num_bytes < cur_alloc_size) {
-                       printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
-                              cur_alloc_size);
-                       break;
+               if (root->root_key.objectid ==
+                   BTRFS_DATA_RELOC_TREE_OBJECTID) {
+                       ret = btrfs_reloc_clone_csums(inode, start,
+                                                     cur_alloc_size);
+                       BUG_ON(ret);
                }
+
+               if (disk_num_bytes < cur_alloc_size)
+                       break;
+
                /* we're not doing compressed IO, don't unlock the first
                 * page (which the caller expects to stay locked), don't
                 * clear any dirty bits and don't set any writeback bits
@@ -834,9 +837,8 @@ static noinline void async_cow_submit(struct btrfs_work *work)
            waitqueue_active(&root->fs_info->async_submit_wait))
                wake_up(&root->fs_info->async_submit_wait);
 
-       if (async_cow->inode) {
+       if (async_cow->inode)
                submit_compressed_extents(async_cow->inode, async_cow);
-       }
 }
 
 static noinline void async_cow_free(struct btrfs_work *work)
@@ -863,7 +865,7 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
 
        clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
                         EXTENT_DELALLOC, 1, 0, GFP_NOFS);
-       while(start < end) {
+       while (start < end) {
                async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
                async_cow->inode = inode;
                async_cow->root = root;
@@ -883,13 +885,6 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
                async_cow->work.ordered_free = async_cow_free;
                async_cow->work.flags = 0;
 
-               while(atomic_read(&root->fs_info->async_submit_draining) &&
-                     atomic_read(&root->fs_info->async_delalloc_pages)) {
-                       wait_event(root->fs_info->async_submit_wait,
-                            (atomic_read(&root->fs_info->async_delalloc_pages)
-                             == 0));
-               }
-
                nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
                        PAGE_CACHE_SHIFT;
                atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
@@ -903,7 +898,7 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
                            limit));
                }
 
-               while(atomic_read(&root->fs_info->async_submit_draining) &&
+               while (atomic_read(&root->fs_info->async_submit_draining) &&
                      atomic_read(&root->fs_info->async_delalloc_pages)) {
                        wait_event(root->fs_info->async_submit_wait,
                          (atomic_read(&root->fs_info->async_delalloc_pages) ==
@@ -917,6 +912,26 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
        return 0;
 }
 
+static noinline int csum_exist_in_range(struct btrfs_root *root,
+                                       u64 bytenr, u64 num_bytes)
+{
+       int ret;
+       struct btrfs_ordered_sum *sums;
+       LIST_HEAD(list);
+
+       ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
+                                      bytenr + num_bytes - 1, &list);
+       if (ret == 0 && list_empty(&list))
+               return 0;
+
+       while (!list_empty(&list)) {
+               sums = list_entry(list.next, struct btrfs_ordered_sum, list);
+               list_del(&sums->list);
+               kfree(sums);
+       }
+       return 1;
+}
+
 /*
  * when nowcow writeback call back.  This checks for snapshots or COW copies
  * of the extents that exist in the file, and COWs the file as required.
@@ -978,6 +993,7 @@ next_slot:
 
                nocow = 0;
                disk_bytenr = 0;
+               num_bytes = 0;
                btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
 
                if (found_key.objectid > inode->i_ino ||
@@ -996,7 +1012,6 @@ next_slot:
 
                if (extent_type == BTRFS_FILE_EXTENT_REG ||
                    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
-                       struct btrfs_block_group_cache *block_group;
                        disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
                        extent_end = found_key.offset +
                                btrfs_file_extent_num_bytes(leaf, fi);
@@ -1004,21 +1019,29 @@ next_slot:
                                path->slots[0]++;
                                goto next_slot;
                        }
+                       if (disk_bytenr == 0)
+                               goto out_check;
                        if (btrfs_file_extent_compression(leaf, fi) ||
                            btrfs_file_extent_encryption(leaf, fi) ||
                            btrfs_file_extent_other_encoding(leaf, fi))
                                goto out_check;
-                       if (disk_bytenr == 0)
-                               goto out_check;
                        if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
                                goto out_check;
-                       if (btrfs_cross_ref_exist(trans, root, disk_bytenr))
+                       if (btrfs_extent_readonly(root, disk_bytenr))
                                goto out_check;
-                       block_group = btrfs_lookup_block_group(root->fs_info,
-                                                              disk_bytenr);
-                       if (!block_group || block_group->ro)
+                       if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
+                                                 disk_bytenr))
                                goto out_check;
                        disk_bytenr += btrfs_file_extent_offset(leaf, fi);
+                       disk_bytenr += cur_offset - found_key.offset;
+                       num_bytes = min(end + 1, extent_end) - cur_offset;
+                       /*
+                        * force cow if csum exists in the range.
+                        * this ensure that csum for a given extent are
+                        * either valid or do not exist.
+                        */
+                       if (csum_exist_in_range(root, disk_bytenr, num_bytes))
+                               goto out_check;
                        nocow = 1;
                } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
                        extent_end = found_key.offset +
@@ -1051,8 +1074,6 @@ out_check:
                        cow_start = (u64)-1;
                }
 
-               disk_bytenr += cur_offset - found_key.offset;
-               num_bytes = min(end + 1, extent_end) - cur_offset;
                if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
                        struct extent_map *em;
                        struct extent_map_tree *em_tree;
@@ -1115,19 +1136,17 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
                              u64 start, u64 end, int *page_started,
                              unsigned long *nr_written)
 {
-       struct btrfs_root *root = BTRFS_I(inode)->root;
        int ret;
 
-       if (btrfs_test_opt(root, NODATACOW) ||
-           btrfs_test_flag(inode, NODATACOW))
+       if (btrfs_test_flag(inode, NODATACOW))
                ret = run_delalloc_nocow(inode, locked_page, start, end,
-                                        page_started, 0, nr_written);
+                                        page_started, 1, nr_written);
        else if (btrfs_test_flag(inode, PREALLOC))
                ret = run_delalloc_nocow(inode, locked_page, start, end,
-                                        page_started, 1, nr_written);
+                                        page_started, 0, nr_written);
        else
                ret = cow_file_range_async(inode, locked_page, start, end,
-                                    page_started, nr_written);
+                                          page_started, nr_written);
 
        return ret;
 }
@@ -1137,20 +1156,24 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  * bytes in this file, and to maintain the list of inodes that
  * have pending delalloc work to be done.
  */
-int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
+static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
                       unsigned long old, unsigned long bits)
 {
-       unsigned long flags;
+       /*
+        * set_bit and clear bit hooks normally require _irqsave/restore
+        * but in this case, we are only testeing for the DELALLOC
+        * bit, which is only set or cleared with irqs on
+        */
        if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
                struct btrfs_root *root = BTRFS_I(inode)->root;
-               spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
+               spin_lock(&root->fs_info->delalloc_lock);
                BTRFS_I(inode)->delalloc_bytes += end - start + 1;
                root->fs_info->delalloc_bytes += end - start + 1;
                if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
                        list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
                                      &root->fs_info->delalloc_inodes);
                }
-               spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
+               spin_unlock(&root->fs_info->delalloc_lock);
        }
        return 0;
 }
@@ -1158,17 +1181,24 @@ int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
 /*
  * extent_io.c clear_bit_hook, see set_bit_hook for why
  */
-int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
+static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
                         unsigned long old, unsigned long bits)
 {
+       /*
+        * set_bit and clear bit hooks normally require _irqsave/restore
+        * but in this case, we are only testeing for the DELALLOC
+        * bit, which is only set or cleared with irqs on
+        */
        if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
                struct btrfs_root *root = BTRFS_I(inode)->root;
-               unsigned long flags;
 
-               spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
+               spin_lock(&root->fs_info->delalloc_lock);
                if (end - start + 1 > root->fs_info->delalloc_bytes) {
-                       printk("warning: delalloc account %Lu %Lu\n",
-                              end - start + 1, root->fs_info->delalloc_bytes);
+                       printk(KERN_INFO "btrfs warning: delalloc account "
+                              "%llu %llu\n",
+                              (unsigned long long)end - start + 1,
+                              (unsigned long long)
+                              root->fs_info->delalloc_bytes);
                        root->fs_info->delalloc_bytes = 0;
                        BTRFS_I(inode)->delalloc_bytes = 0;
                } else {
@@ -1179,7 +1209,7 @@ int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
                    !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
                        list_del_init(&BTRFS_I(inode)->delalloc_inodes);
                }
-               spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
+               spin_unlock(&root->fs_info->delalloc_lock);
        }
        return 0;
 }
@@ -1208,9 +1238,8 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
        ret = btrfs_map_block(map_tree, READ, logical,
                              &map_length, NULL, 0);
 
-       if (map_length < length + size) {
+       if (map_length < length + size)
                return 1;
-       }
        return 0;
 }
 
@@ -1222,13 +1251,14 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  * At IO completion time the cums attached on the ordered extent record
  * are inserted into the btree
  */
-int __btrfs_submit_bio_start(struct inode *inode, int rw, struct bio *bio,
-                         int mirror_num, unsigned long bio_flags)
+static int __btrfs_submit_bio_start(struct inode *inode, int rw,
+                                   struct bio *bio, int mirror_num,
+                                   unsigned long bio_flags)
 {
        struct btrfs_root *root = BTRFS_I(inode)->root;
        int ret = 0;
 
-       ret = btrfs_csum_one_bio(root, inode, bio);
+       ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
        BUG_ON(ret);
        return 0;
 }
@@ -1241,7 +1271,7 @@ int __btrfs_submit_bio_start(struct inode *inode, int rw, struct bio *bio,
  * At IO completion time the cums attached on the ordered extent record
  * are inserted into the btree
  */
-int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
+static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
                          int mirror_num, unsigned long bio_flags)
 {
        struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -1249,31 +1279,32 @@ int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
 }
 
 /*
- * extent_io.c submission hook. This does the right thing for csum calculation on write,
- * or reading the csums from the tree before a read
+ * extent_io.c submission hook. This does the right thing for csum calculation
+ * on write, or reading the csums from the tree before a read
  */
-int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
+static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
                          int mirror_num, unsigned long bio_flags)
 {
        struct btrfs_root *root = BTRFS_I(inode)->root;
        int ret = 0;
        int skip_sum;
 
+       skip_sum = btrfs_test_flag(inode, NODATASUM);
+
        ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
        BUG_ON(ret);
 
-       skip_sum = btrfs_test_opt(root, NODATASUM) ||
-               btrfs_test_flag(inode, NODATASUM);
-
        if (!(rw & (1 << BIO_RW))) {
-
-               if (bio_flags & EXTENT_BIO_COMPRESSED)
+               if (bio_flags & EXTENT_BIO_COMPRESSED) {
                        return btrfs_submit_compressed_read(inode, bio,
                                                    mirror_num, bio_flags);
-               else if (!skip_sum)
-                       btrfs_lookup_bio_sums(root, inode, bio);
+               else if (!skip_sum)
+                       btrfs_lookup_bio_sums(root, inode, bio, NULL);
                goto mapit;
        } else if (!skip_sum) {
+               /* csum items have already been cloned */
+               if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
+                       goto mapit;
                /* we're doing a write, do the async checksumming */
                return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
                                   inode, rw, bio, mirror_num,
@@ -1299,17 +1330,16 @@ static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
        btrfs_set_trans_block_group(trans, inode);
        list_for_each(cur, list) {
                sum = list_entry(cur, struct btrfs_ordered_sum, list);
-               btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
-                                      inode, sum);
+               btrfs_csum_file_blocks(trans,
+                      BTRFS_I(inode)->root->fs_info->csum_root, sum);
        }
        return 0;
 }
 
 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
 {
-       if ((end & (PAGE_CACHE_SIZE - 1)) == 0) {
+       if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
                WARN_ON(1);
-       }
        return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
                                   GFP_NOFS);
 }
@@ -1320,7 +1350,7 @@ struct btrfs_writepage_fixup {
        struct btrfs_work work;
 };
 
-void btrfs_writepage_fixup_worker(struct btrfs_work *work)
+static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
 {
        struct btrfs_writepage_fixup *fixup;
        struct btrfs_ordered_extent *ordered;
@@ -1379,7 +1409,7 @@ out_page:
  * to fix it up.  The async helper will wait for ordered extents, set
  * the delalloc bit and make it safe to write the page.
  */
-int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
+static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
 {
        struct inode *inode = page->mapping->host;
        struct btrfs_writepage_fixup *fixup;
@@ -1533,7 +1563,7 @@ nocow:
        return 0;
 }
 
-int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
+static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
                                struct extent_state *state, int uptodate)
 {
        return btrfs_finish_ordered_io(page->mapping->host, start, end);
@@ -1552,10 +1582,11 @@ struct io_failure_record {
        u64 start;
        u64 len;
        u64 logical;
+       unsigned long bio_flags;
        int last_mirror;
 };
 
-int btrfs_io_failed_hook(struct bio *failed_bio,
+static int btrfs_io_failed_hook(struct bio *failed_bio,
                         struct page *page, u64 start, u64 end,
                         struct extent_state *state)
 {
@@ -1570,7 +1601,6 @@ int btrfs_io_failed_hook(struct bio *failed_bio,
        int ret;
        int rw;
        u64 logical;
-       unsigned long bio_flags = 0;
 
        ret = get_state_private(failure_tree, start, &private);
        if (ret) {
@@ -1580,6 +1610,7 @@ int btrfs_io_failed_hook(struct bio *failed_bio,
                failrec->start = start;
                failrec->len = end - start + 1;
                failrec->last_mirror = 0;
+               failrec->bio_flags = 0;
 
                spin_lock(&em_tree->lock);
                em = lookup_extent_mapping(em_tree, start, failrec->len);
@@ -1595,8 +1626,10 @@ int btrfs_io_failed_hook(struct bio *failed_bio,
                }
                logical = start - em->start;
                logical = em->block_start + logical;
-               if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
-                       bio_flags = EXTENT_BIO_COMPRESSED;
+               if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
+                       logical = em->block_start;
+                       failrec->bio_flags = EXTENT_BIO_COMPRESSED;
+               }
                failrec->logical = logical;
                free_extent_map(em);
                set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
@@ -1611,13 +1644,13 @@ int btrfs_io_failed_hook(struct bio *failed_bio,
                              failrec->logical, failrec->len);
        failrec->last_mirror++;
        if (!state) {
-               spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
+               spin_lock(&BTRFS_I(inode)->io_tree.lock);
                state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
                                                    failrec->start,
                                                    EXTENT_LOCKED);
                if (state && state->start != failrec->start)
                        state = NULL;
-               spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
+               spin_unlock(&BTRFS_I(inode)->io_tree.lock);
        }
        if (!state || failrec->last_mirror > num_copies) {
                set_state_private(failure_tree, failrec->start, 0);
@@ -1633,6 +1666,7 @@ int btrfs_io_failed_hook(struct bio *failed_bio,
        bio->bi_sector = failrec->logical >> 9;
        bio->bi_bdev = failed_bio->bi_bdev;
        bio->bi_size = 0;
+
        bio_add_page(bio, page, failrec->len, start - page_offset(page));
        if (failed_bio->bi_rw & (1 << BIO_RW))
                rw = WRITE;
@@ -1641,7 +1675,7 @@ int btrfs_io_failed_hook(struct bio *failed_bio,
 
        BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
                                                      failrec->last_mirror,
-                                                     bio_flags);
+                                                     failrec->bio_flags);
        return 0;
 }
 
@@ -1649,7 +1683,7 @@ int btrfs_io_failed_hook(struct bio *failed_bio,
  * each time an IO finishes, we do a fast check in the IO failure tree
  * to see if we need to process or clean up an io_failure_record
  */
-int btrfs_clean_io_failures(struct inode *inode, u64 start)
+static int btrfs_clean_io_failures(struct inode *inode, u64 start)
 {
        u64 private;
        u64 private_failure;
@@ -1682,7 +1716,7 @@ int btrfs_clean_io_failures(struct inode *inode, u64 start)
  * if there's a match, we allow the bio to finish.  If not, we go through
  * the io_failure_record routines to find good copies
  */
-int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
+static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
                               struct extent_state *state)
 {
        size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
@@ -1693,30 +1727,38 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
        int ret;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        u32 csum = ~(u32)0;
-       unsigned long flags;
 
-       if (btrfs_test_opt(root, NODATASUM) ||
-           btrfs_test_flag(inode, NODATASUM))
+       if (PageChecked(page)) {
+               ClearPageChecked(page);
+               goto good;
+       }
+       if (btrfs_test_flag(inode, NODATASUM))
                return 0;
+
+       if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
+           test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
+               clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
+                                 GFP_NOFS);
+               return 0;
+       }
+
        if (state && state->start == start) {
                private = state->private;
                ret = 0;
        } else {
                ret = get_state_private(io_tree, start, &private);
        }
-       local_irq_save(flags);
-       kaddr = kmap_atomic(page, KM_IRQ0);
-       if (ret) {
+       kaddr = kmap_atomic(page, KM_USER0);
+       if (ret)
                goto zeroit;
-       }
+
        csum = btrfs_csum_data(root, kaddr + offset, csum,  end - start + 1);
        btrfs_csum_final(csum, (char *)&csum);
-       if (csum != private) {
+       if (csum != private)
                goto zeroit;
-       }
-       kunmap_atomic(kaddr, KM_IRQ0);
-       local_irq_restore(flags);
 
+       kunmap_atomic(kaddr, KM_USER0);
+good:
        /* if the io failure tree for this inode is non-empty,
         * check to see if we've recovered from a failed IO
         */
@@ -1724,13 +1766,13 @@ int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
        return 0;
 
 zeroit:
-       printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
-              page->mapping->host->i_ino, (unsigned long long)start, csum,
-              private);
+       printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
+              "private %llu\n", page->mapping->host->i_ino,
+              (unsigned long long)start, csum,
+              (unsigned long long)private);
        memset(kaddr + offset, 1, end - start + 1);
        flush_dcache_page(page);
-       kunmap_atomic(kaddr, KM_IRQ0);
-       local_irq_restore(flags);
+       kunmap_atomic(kaddr, KM_USER0);
        if (private == 0)
                return 0;
        return -EIO;
@@ -1808,10 +1850,6 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
        struct inode *inode;
        int ret = 0, nr_unlink = 0, nr_truncate = 0;
 
-       /* don't do orphan cleanup if the fs is readonly. */
-       if (root->fs_info->sb->s_flags & MS_RDONLY)
-               return;
-
        path = btrfs_alloc_path();
        if (!path)
                return;
@@ -1966,21 +2004,17 @@ void btrfs_read_locked_inode(struct inode *inode)
 
        inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
        BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
+       BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
        inode->i_generation = BTRFS_I(inode)->generation;
        inode->i_rdev = 0;
        rdev = btrfs_inode_rdev(leaf, inode_item);
 
        BTRFS_I(inode)->index_cnt = (u64)-1;
+       BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
 
        alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
-       BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
-                                                      alloc_group_block);
-       BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
-       if (!BTRFS_I(inode)->block_group) {
-               BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
-                                                NULL, 0,
-                                                BTRFS_BLOCK_GROUP_METADATA, 0);
-       }
+       BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
+                                               alloc_group_block, 0);
        btrfs_free_path(path);
        inode_item = NULL;
 
@@ -2046,19 +2080,18 @@ static void fill_inode_item(struct btrfs_trans_handle *trans,
 
        btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
        btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
+       btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
        btrfs_set_inode_transid(leaf, item, trans->transid);
        btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
        btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
-       btrfs_set_inode_block_group(leaf, item,
-                                   BTRFS_I(inode)->block_group->key.objectid);
+       btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
 }
 
 /*
  * copy everything in the in-memory inode into the btree.
  */
-int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
-                             struct btrfs_root *root,
-                             struct inode *inode)
+noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
+                               struct btrfs_root *root, struct inode *inode)
 {
        struct btrfs_inode_item *inode_item;
        struct btrfs_path *path;
@@ -2133,7 +2166,7 @@ int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
                                  inode->i_ino,
                                  dir->i_ino, &index);
        if (ret) {
-               printk("failed to delete reference to %.*s, "
+               printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
                       "inode %lu parent %lu\n", name_len, name,
                       inode->i_ino, dir->i_ino);
                goto err;
@@ -2216,7 +2249,12 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
        struct btrfs_trans_handle *trans;
        unsigned long nr = 0;
 
-       if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
+       /*
+        * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
+        * the root of a subvolume or snapshot
+        */
+       if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
+           inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
                return -ENOTEMPTY;
        }
 
@@ -2234,9 +2272,8 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
        /* now the directory is empty */
        err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
                                 dentry->d_name.name, dentry->d_name.len);
-       if (!err) {
+       if (!err)
                btrfs_i_size_write(inode, 0);
-       }
 
 fail_trans:
        nr = trans->blocks_used;
@@ -2249,6 +2286,7 @@ fail:
        return err;
 }
 
+#if 0
 /*
  * when truncating bytes in a file, it is possible to avoid reading
  * the leaves that contain only checksum items.  This can be the
@@ -2416,6 +2454,8 @@ out:
        return ret;
 }
 
+#endif
+
 /*
  * this can truncate away extent items, csum items and directory items.
  * It starts at a high offset and removes keys until it can't find
@@ -2465,14 +2505,11 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 
        btrfs_init_path(path);
 
-       ret = drop_csum_leaves(trans, root, path, inode, new_size);
-       BUG_ON(ret);
-
 search_again:
        ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
-       if (ret < 0) {
+       if (ret < 0)
                goto error;
-       }
+
        if (ret > 0) {
                /* there are no items in the tree for us to truncate, we're
                 * done
@@ -2484,7 +2521,7 @@ search_again:
                path->slots[0]--;
        }
 
-       while(1) {
+       while (1) {
                fi = NULL;
                leaf = path->nodes[0];
                btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
@@ -2515,25 +2552,19 @@ search_again:
                        }
                        item_end--;
                }
-               if (found_type == BTRFS_CSUM_ITEM_KEY) {
-                       ret = btrfs_csum_truncate(trans, root, path,
-                                                 new_size);
-                       BUG_ON(ret);
-               }
                if (item_end < new_size) {
-                       if (found_type == BTRFS_DIR_ITEM_KEY) {
+                       if (found_type == BTRFS_DIR_ITEM_KEY)
                                found_type = BTRFS_INODE_ITEM_KEY;
-                       } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
-                               found_type = BTRFS_CSUM_ITEM_KEY;
-                       } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
+                       else if (found_type == BTRFS_EXTENT_ITEM_KEY)
+                               found_type = BTRFS_EXTENT_DATA_KEY;
+                       else if (found_type == BTRFS_EXTENT_DATA_KEY)
                                found_type = BTRFS_XATTR_ITEM_KEY;
-                       } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
+                       else if (found_type == BTRFS_XATTR_ITEM_KEY)
                                found_type = BTRFS_INODE_REF_KEY;
-                       } else if (found_type) {
+                       else if (found_type)
                                found_type--;
-                       } else {
+                       else
                                break;
-                       }
                        btrfs_set_key_type(&key, found_type);
                        goto next;
                }
@@ -2615,7 +2646,7 @@ delete:
                                pending_del_nr++;
                                pending_del_slot = path->slots[0];
                        } else {
-                               printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
+                               BUG();
                        }
                } else {
                        break;
@@ -2850,7 +2881,7 @@ void btrfs_delete_inode(struct inode *inode)
        btrfs_wait_ordered_range(inode, 0, (u64)-1);
 
        btrfs_i_size_write(inode, 0);
-       trans = btrfs_start_transaction(root, 1);
+       trans = btrfs_join_transaction(root, 1);
 
        btrfs_set_trans_block_group(trans, inode);
        ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
@@ -2897,9 +2928,10 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
                                    namelen, 0);
        if (IS_ERR(di))
                ret = PTR_ERR(di);
-       if (!di || IS_ERR(di)) {
+
+       if (!di || IS_ERR(di))
                goto out_err;
-       }
+
        btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
 out:
        btrfs_free_path(path);
@@ -2948,6 +2980,7 @@ static noinline void init_btrfs_i(struct inode *inode)
        bi->i_default_acl = NULL;
 
        bi->generation = 0;
+       bi->sequence = 0;
        bi->last_trans = 0;
        bi->logged_trans = 0;
        bi->delalloc_bytes = 0;
@@ -2962,7 +2995,6 @@ static noinline void init_btrfs_i(struct inode *inode)
                             inode->i_mapping, GFP_NOFS);
        INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
        btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
-       mutex_init(&BTRFS_I(inode)->csum_mutex);
        mutex_init(&BTRFS_I(inode)->extent_mutex);
        mutex_init(&BTRFS_I(inode)->log_mutex);
 }
@@ -2979,8 +3011,8 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
 static int btrfs_find_actor(struct inode *inode, void *opaque)
 {
        struct btrfs_iget_args *args = opaque;
-       return (args->ino == inode->i_ino &&
-               args->root == BTRFS_I(inode)->root);
+       return args->ino == inode->i_ino &&
+               args->root == BTRFS_I(inode)->root;
 }
 
 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
@@ -3042,15 +3074,14 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
        return inode;
 }
 
-static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
-                                  struct nameidata *nd)
+struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
 {
-       struct inode * inode;
+       struct inode *inode;
        struct btrfs_inode *bi = BTRFS_I(dir);
        struct btrfs_root *root = bi->root;
        struct btrfs_root *sub_root = root;
        struct btrfs_key location;
-       int ret, new, do_orphan = 0;
+       int ret, new;
 
        if (dentry->d_name.len > BTRFS_NAME_LEN)
                return ERR_PTR(-ENAMETOOLONG);
@@ -3071,17 +3102,21 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
                inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
                if (IS_ERR(inode))
                        return ERR_CAST(inode);
-
-               /* the inode and parent dir are two different roots */
-               if (new && root != sub_root) {
-                       igrab(inode);
-                       sub_root->inode = inode;
-                       do_orphan = 1;
-               }
        }
+       return inode;
+}
 
-       if (unlikely(do_orphan))
-               btrfs_orphan_cleanup(sub_root);
+static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
+                                  struct nameidata *nd)
+{
+       struct inode *inode;
+
+       if (dentry->d_name.len > BTRFS_NAME_LEN)
+               return ERR_PTR(-ENAMETOOLONG);
+
+       inode = btrfs_lookup_dentry(dir, dentry);
+       if (IS_ERR(inode))
+               return ERR_CAST(inode);
 
        return d_splice_alias(inode, dentry);
 }
@@ -3137,7 +3172,6 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
                        return 0;
                filp->f_pos = 2;
        }
-
        path = btrfs_alloc_path();
        path->reada = 2;
 
@@ -3167,6 +3201,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
                                path->slots[0]++;
                        }
                }
+
                advance = 1;
                item = btrfs_item_nr(leaf, slot);
                btrfs_item_key_to_cpu(leaf, &found_key, slot);
@@ -3202,16 +3237,25 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
 
                        d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
                        btrfs_dir_item_key_to_cpu(leaf, di, &location);
+
+                       /* is this a reference to our own snapshot? If so
+                        * skip it
+                        */
+                       if (location.type == BTRFS_ROOT_ITEM_KEY &&
+                           location.objectid == root->root_key.objectid) {
+                               over = 0;
+                               goto skip;
+                       }
                        over = filldir(dirent, name_ptr, name_len,
                                       found_key.offset, location.objectid,
                                       d_type);
 
+skip:
                        if (name_ptr != tmp_name)
                                kfree(name_ptr);
 
                        if (over)
                                goto nopos;
-
                        di_len = btrfs_dir_name_len(leaf, di) +
                                 btrfs_dir_data_len(leaf, di) + sizeof(*di);
                        di_cur += di_len;
@@ -3237,7 +3281,7 @@ int btrfs_write_inode(struct inode *inode, int wait)
        struct btrfs_trans_handle *trans;
        int ret = 0;
 
-       if (root->fs_info->closing > 1)
+       if (root->fs_info->btree_inode == inode)
                return 0;
 
        if (wait) {
@@ -3326,16 +3370,14 @@ out:
  * helper to find a free sequence number in a given directory.  This current
  * code is very simple, later versions will do smarter things in the btree
  */
-static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
-                                u64 *index)
+int btrfs_set_inode_index(struct inode *dir, u64 *index)
 {
        int ret = 0;
 
        if (BTRFS_I(dir)->index_cnt == (u64)-1) {
                ret = btrfs_set_inode_index_count(dir);
-               if (ret) {
+               if (ret)
                        return ret;
-               }
        }
 
        *index = BTRFS_I(dir)->index_cnt;
@@ -3348,14 +3390,11 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
                                     struct btrfs_root *root,
                                     struct inode *dir,
                                     const char *name, int name_len,
-                                    u64 ref_objectid,
-                                    u64 objectid,
-                                    struct btrfs_block_group_cache *group,
-                                    int mode, u64 *index)
+                                    u64 ref_objectid, u64 objectid,
+                                    u64 alloc_hint, int mode, u64 *index)
 {
        struct inode *inode;
        struct btrfs_inode_item *inode_item;
-       struct btrfs_block_group_cache *new_inode_group;
        struct btrfs_key *location;
        struct btrfs_path *path;
        struct btrfs_inode_ref *ref;
@@ -3373,7 +3412,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
                return ERR_PTR(-ENOMEM);
 
        if (dir) {
-               ret = btrfs_set_inode_index(dir, inode, index);
+               ret = btrfs_set_inode_index(dir, index);
                if (ret)
                        return ERR_PTR(ret);
        }
@@ -3391,13 +3430,14 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
                owner = 0;
        else
                owner = 1;
-       new_inode_group = btrfs_find_block_group(root, group, 0,
-                                      BTRFS_BLOCK_GROUP_METADATA, owner);
-       if (!new_inode_group) {
-               printk("find_block group failed\n");
-               new_inode_group = group;
+       BTRFS_I(inode)->block_group =
+                       btrfs_find_block_group(root, 0, alloc_hint, owner);
+       if ((mode & S_IFREG)) {
+               if (btrfs_test_opt(root, NODATASUM))
+                       btrfs_set_flag(inode, NODATASUM);
+               if (btrfs_test_opt(root, NODATACOW))
+                       btrfs_set_flag(inode, NODATACOW);
        }
-       BTRFS_I(inode)->block_group = new_inode_group;
 
        key[0].objectid = objectid;
        btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
@@ -3417,8 +3457,8 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
        if (objectid > root->highest_inode)
                root->highest_inode = objectid;
 
-       inode->i_uid = current->fsuid;
-       inode->i_gid = current->fsgid;
+       inode->i_uid = current_fsuid();
+       inode->i_gid = current_fsgid();
        inode->i_mode = mode;
        inode->i_ino = objectid;
        inode_set_bytes(inode, 0);
@@ -3659,7 +3699,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
        err = btrfs_check_free_space(root, 1, 0);
        if (err)
                goto fail;
-       err = btrfs_set_inode_index(dir, inode, &index);
+       err = btrfs_set_inode_index(dir, &index);
        if (err)
                goto fail;
 
@@ -3829,12 +3869,13 @@ static noinline int uncompress_inline(struct btrfs_path *path,
 
 /*
  * a bit scary, this does extent mapping from logical file offset to the disk.
- * the ugly parts come from merging extents from the disk with the
- * in-ram representation.  This gets more complex because of the data=ordered code,
+ * the ugly parts come from merging extents from the disk with the in-ram
+ * representation.  This gets more complex because of the data=ordered code,
  * where the in-ram extents might be locked pending data=ordered completion.
  *
  * This also copies inline extents directly into the page.
  */
+
 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
                                    size_t pg_offset, u64 start, u64 len,
                                    int create)
@@ -4031,7 +4072,7 @@ again:
                                    extent_map_end(em) - 1, GFP_NOFS);
                goto insert;
        } else {
-               printk("unkknown found_type %d\n", found_type);
+               printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
                WARN_ON(1);
        }
 not_found:
@@ -4043,7 +4084,11 @@ not_found_em:
 insert:
        btrfs_release_path(root, path);
        if (em->start > start || extent_map_end(em) <= start) {
-               printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
+               printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
+                      "[%llu %llu]\n", (unsigned long long)em->start,
+                      (unsigned long long)em->len,
+                      (unsigned long long)start,
+                      (unsigned long long)len);
                err = -EIO;
                goto out;
        }
@@ -4080,8 +4125,6 @@ insert:
                                }
                        } else {
                                err = -EIO;
-                               printk("failing to insert %Lu %Lu\n",
-                                      start, len);
                                free_extent_map(em);
                                em = NULL;
                        }
@@ -4097,9 +4140,8 @@ out:
                btrfs_free_path(path);
        if (trans) {
                ret = btrfs_end_transaction(trans, root);
-               if (!err) {
+               if (!err)
                        err = ret;
-               }
        }
        if (err) {
                free_extent_map(em);
@@ -4354,48 +4396,22 @@ out:
 }
 
 /*
- * Invalidate a single dcache entry at the root of the filesystem.
- * Needed after creation of snapshot or subvolume.
- */
-void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
-                                 int namelen)
-{
-       struct dentry *alias, *entry;
-       struct qstr qstr;
-
-       alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
-       if (alias) {
-               qstr.name = name;
-               qstr.len = namelen;
-               /* change me if btrfs ever gets a d_hash operation */
-               qstr.hash = full_name_hash(qstr.name, qstr.len);
-               entry = d_lookup(alias, &qstr);
-               dput(alias);
-               if (entry) {
-                       d_invalidate(entry);
-                       dput(entry);
-               }
-       }
-}
-
-/*
  * create a new subvolume directory/inode (helper for the ioctl).
  */
-int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
-               struct btrfs_trans_handle *trans, u64 new_dirid,
-               struct btrfs_block_group_cache *block_group)
+int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
+                            struct btrfs_root *new_root, struct dentry *dentry,
+                            u64 new_dirid, u64 alloc_hint)
 {
        struct inode *inode;
        int error;
        u64 index = 0;
 
        inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
-                               new_dirid, block_group, S_IFDIR | 0700, &index);
+                               new_dirid, alloc_hint, S_IFDIR | 0700, &index);
        if (IS_ERR(inode))
                return PTR_ERR(inode);
        inode->i_op = &btrfs_dir_inode_operations;
        inode->i_fop = &btrfs_dir_file_operations;
-       new_root->inode = inode;
 
        inode->i_nlink = 1;
        btrfs_i_size_write(inode, 0);
@@ -4404,7 +4420,6 @@ int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
        if (error)
                return error;
 
-       atomic_inc(&inode->i_count);
        d_instantiate(dentry, inode);
        return 0;
 }
@@ -4459,13 +4474,15 @@ void btrfs_destroy_inode(struct inode *inode)
        }
        spin_unlock(&BTRFS_I(inode)->root->list_lock);
 
-       while(1) {
+       while (1) {
                ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
                if (!ordered)
                        break;
                else {
-                       printk("found ordered extent %Lu %Lu\n",
-                              ordered->file_offset, ordered->len);
+                       printk(KERN_ERR "btrfs found ordered "
+                              "extent %llu %llu on inode cleanup\n",
+                              (unsigned long long)ordered->file_offset,
+                              (unsigned long long)ordered->len);
                        btrfs_remove_ordered_extent(inode, ordered);
                        btrfs_put_ordered_extent(ordered);
                        btrfs_put_ordered_extent(ordered);
@@ -4542,14 +4559,15 @@ static int btrfs_getattr(struct vfsmount *mnt,
 {
        struct inode *inode = dentry->d_inode;
        generic_fillattr(inode, stat);
+       stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
        stat->blksize = PAGE_CACHE_SIZE;
        stat->blocks = (inode_get_bytes(inode) +
                        BTRFS_I(inode)->delalloc_bytes) >> 9;
        return 0;
 }
 
-static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
-                          struct inode * new_dir,struct dentry *new_dentry)
+static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
+                          struct inode *new_dir, struct dentry *new_dentry)
 {
        struct btrfs_trans_handle *trans;
        struct btrfs_root *root = BTRFS_I(old_dir)->root;
@@ -4559,11 +4577,22 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
        u64 index = 0;
        int ret;
 
+       /* we're not allowed to rename between subvolumes */
+       if (BTRFS_I(old_inode)->root->root_key.objectid !=
+           BTRFS_I(new_dir)->root->root_key.objectid)
+               return -EXDEV;
+
        if (S_ISDIR(old_inode->i_mode) && new_inode &&
            new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
                return -ENOTEMPTY;
        }
 
+       /* to rename a snapshot or subvolume, we need to juggle the
+        * backrefs.  This isn't coded yet
+        */
+       if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
+               return -EXDEV;
+
        ret = btrfs_check_free_space(root, 1, 0);
        if (ret)
                goto out_unlock;
@@ -4598,7 +4627,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
                }
 
        }
-       ret = btrfs_set_inode_index(new_dir, old_inode, &index);
+       ret = btrfs_set_inode_index(new_dir, &index);
        if (ret)
                goto out_fail;
 
@@ -4623,31 +4652,33 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root)
        struct list_head *head = &root->fs_info->delalloc_inodes;
        struct btrfs_inode *binode;
        struct inode *inode;
-       unsigned long flags;
 
-       spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
-       while(!list_empty(head)) {
+       if (root->fs_info->sb->s_flags & MS_RDONLY)
+               return -EROFS;
+
+       spin_lock(&root->fs_info->delalloc_lock);
+       while (!list_empty(head)) {
                binode = list_entry(head->next, struct btrfs_inode,
                                    delalloc_inodes);
                inode = igrab(&binode->vfs_inode);
                if (!inode)
                        list_del_init(&binode->delalloc_inodes);
-               spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
+               spin_unlock(&root->fs_info->delalloc_lock);
                if (inode) {
                        filemap_flush(inode->i_mapping);
                        iput(inode);
                }
                cond_resched();
-               spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
+               spin_lock(&root->fs_info->delalloc_lock);
        }
-       spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
+       spin_unlock(&root->fs_info->delalloc_lock);
 
        /* the filemap_flush will queue IO into the worker threads, but
         * we have to make sure the IO is actually started and that
         * ordered extents get created before we return
         */
        atomic_inc(&root->fs_info->async_submit_draining);
-       while(atomic_read(&root->fs_info->nr_async_submits) ||
+       while (atomic_read(&root->fs_info->nr_async_submits) ||
              atomic_read(&root->fs_info->async_delalloc_pages)) {
                wait_event(root->fs_info->async_submit_wait,
                   (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
@@ -4911,6 +4942,7 @@ static int btrfs_permission(struct inode *inode, int mask)
 }
 
 static struct inode_operations btrfs_dir_inode_operations = {
+       .getattr        = btrfs_getattr,
        .lookup         = btrfs_lookup,
        .create         = btrfs_create,
        .unlink         = btrfs_unlink,