path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
-printk("cache block group %Lu\n", block_group->key.objectid);
        key.objectid = block_group->key.objectid;
        key.flags = 0;
        key.offset = 0;
 static u64 leaf_range(struct btrfs_root *root)
 {
        u64 size = BTRFS_LEAF_DATA_SIZE(root);
-       size = size / (sizeof(struct btrfs_extent_item) +
-                      sizeof(struct btrfs_item));
+       do_div(size, sizeof(struct btrfs_extent_item) +
+               sizeof(struct btrfs_item));
        return size;
 }
 
        goto again;
 }
 
+static u64 div_factor(u64 num, int factor)
+{
+       num *= factor;
+       do_div(num, 10);
+       return num;
+}
+
 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
                                                 struct btrfs_block_group_cache
                                                 *hint, u64 search_start,
                if (shint->data == data) {
                        used = btrfs_block_group_used(&shint->item);
                        if (used + shint->pinned <
-                           (shint->key.offset * factor) / 10) {
+                           div_factor(shint->key.offset, factor)) {
                                return shint;
                        }
                }
        }
        if (hint && hint->data == data) {
                used = btrfs_block_group_used(&hint->item);
-               if (used + hint->pinned < (hint->key.offset * factor) / 10) {
+               if (used + hint->pinned <
+                   div_factor(hint->key.offset, factor)) {
                        return hint;
                }
-               if (used >= (hint->key.offset * 8) / 10) {
+               if (used >= div_factor(hint->key.offset, 8)) {
                        radix_tree_tag_clear(radix,
                                             hint->key.objectid +
                                             hint->key.offset - 1,
                                cache[i]->key.offset;
                        used = btrfs_block_group_used(&cache[i]->item);
                        if (used + cache[i]->pinned <
-                           (cache[i]->key.offset * factor) / 10) {
+                           div_factor(cache[i]->key.offset, factor)) {
                                found_group = cache[i];
                                goto found;
                        }
-                       if (used >= (cache[i]->key.offset * 8) / 10) {
+                       if (used >= div_factor(cache[i]->key.offset, 8)) {
                                radix_tree_tag_clear(radix,
                                                     cache[i]->key.objectid +
                                                     cache[i]->key.offset - 1,
                goto again;
        }
        if (!found_group) {
-printk("find block group bailing to zero data %d\n", data);
                ret = radix_tree_gang_lookup(radix,
                                             (void **)&found_group, 0, 1);
                if (ret == 0) {
        ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
                                0, 1);
        if (ret != 0) {
-printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
                BUG();
        }
        BUG_ON(ret != 0);
                                }
                        }
                        if (cache->data != data &&
-                           old_val < cache->key.offset / 2) {
-printk("changing block group %Lu from %d to %d\n", cache->key.objectid, cache->data, data);
+                           old_val < (cache->key.offset >> 1)) {
                                cache->data = data;
                                radix_tree_delete(cache->radix,
                                                  cache->key.objectid +
                                                      blocknr + i);
                                }
                        }
-                       if (old_val < cache->key.offset / 2 &&
-                           old_val + num >= cache->key.offset / 2) {
-printk("group %Lu now available\n", cache->key.objectid);
+                       if (old_val < (cache->key.offset >> 1) &&
+                           old_val + num >= (cache->key.offset >> 1)) {
                                radix_tree_tag_set(cache->radix,
                                                   cache->key.objectid +
                                                   cache->key.offset - 1,
                        }
                        if (start_found)
                                limit = last_block +
-                                       block_group->key.offset / 2;
+                                       (block_group->key.offset >> 1);
                        else
                                limit = search_start +
-                                       block_group->key.offset / 2;
+                                       (block_group->key.offset >> 1);
                        ret = btrfs_next_leaf(root, path);
                        if (ret == 0)
                                continue;
        struct btrfs_key key;
        struct btrfs_key found_key;
        struct btrfs_leaf *leaf;
-       u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
+       u64 group_size_blocks;
        u64 used;
 
+       group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
+               root->fs_info->sb->s_blocksize_bits;
        root = info->extent_root;
        key.objectid = 0;
        key.offset = group_size_blocks;
                                        (void *)cache);
                BUG_ON(ret);
                used = btrfs_block_group_used(bi);
-               if (used < (key.offset * 8) / 10) {
+               if (used < div_factor(key.offset, 8)) {
                        radix_tree_tag_set(radix, found_key.objectid +
                                           found_key.offset - 1,
                                           BTRFS_BLOCK_GROUP_AVAIL);
 
 
        for (i = 0; i < num_pages; i++) {
                offset = pos & (PAGE_CACHE_SIZE -1);
-               this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
+               this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes);
                /* FIXME, one block at a time */
 
                mutex_lock(&root->fs_info->fs_mutex);
                cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
                wait_on_page_writeback(pages[i]);
                offset = pos & (PAGE_CACHE_SIZE -1);
-               this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
+               this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes);
                if (!page_has_buffers(pages[i])) {
                        create_empty_buffers(pages[i],
                                             root->fs_info->sb->s_blocksize,
 
        while(count > 0) {
                size_t offset = pos & (PAGE_CACHE_SIZE - 1);
-               size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
+               size_t write_bytes = min(count,
+                                        (size_t)PAGE_CACHE_SIZE - offset);
                size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
                                        PAGE_CACHE_SHIFT;