Commit fda2832f authored by Qu Wenruo's avatar Qu Wenruo Committed by Josef Bacik

btrfs: cleanup for open-coded alignment

Though most of the btrfs codes are using ALIGN macro for page alignment,
there are still some codes using open-coded alignment like the
following:
------
        u64 mask = ((u64)root->stripesize - 1);
        u64 ret = (val + mask) & ~mask;
------
Or even hidden one:
------
        num_bytes = (end - start + blocksize) & ~(blocksize - 1);
------

Sometimes these open-coded alignment is not so easy to understand for
newbie like me.

This commit changes the open-coded alignment to the ALIGN macro for a
better readability.

Also there is a previous patch from David Sterba with similar changes,
but the patch is for 3.2 kernel and seems not merged.
http://www.spinics.net/lists/linux-btrfs/msg12747.html

Cc: David Sterba <dave@jikos.cz>
Signed-off-by: default avatarQu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: default avatarJosef Bacik <jbacik@fusionio.com>
parent 8c4ce81e
...@@ -3431,7 +3431,7 @@ int btrfs_check_data_free_space(struct inode *inode, u64 bytes) ...@@ -3431,7 +3431,7 @@ int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
int ret = 0, committed = 0, alloc_chunk = 1; int ret = 0, committed = 0, alloc_chunk = 1;
/* make sure bytes are sectorsize aligned */ /* make sure bytes are sectorsize aligned */
bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); bytes = ALIGN(bytes, root->sectorsize);
if (root == root->fs_info->tree_root || if (root == root->fs_info->tree_root ||
BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
...@@ -3526,7 +3526,7 @@ void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) ...@@ -3526,7 +3526,7 @@ void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
struct btrfs_space_info *data_sinfo; struct btrfs_space_info *data_sinfo;
/* make sure bytes are sectorsize aligned */ /* make sure bytes are sectorsize aligned */
bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); bytes = ALIGN(bytes, root->sectorsize);
data_sinfo = root->fs_info->data_sinfo; data_sinfo = root->fs_info->data_sinfo;
spin_lock(&data_sinfo->lock); spin_lock(&data_sinfo->lock);
...@@ -5607,10 +5607,7 @@ static u64 stripe_align(struct btrfs_root *root, ...@@ -5607,10 +5607,7 @@ static u64 stripe_align(struct btrfs_root *root,
struct btrfs_block_group_cache *cache, struct btrfs_block_group_cache *cache,
u64 val, u64 num_bytes) u64 val, u64 num_bytes)
{ {
u64 mask; u64 ret = ALIGN(val, root->stripesize);
u64 ret;
mask = ((u64)root->stripesize - 1);
ret = (val + mask) & ~mask;
return ret; return ret;
} }
......
...@@ -2686,7 +2686,7 @@ static int __extent_read_full_page(struct extent_io_tree *tree, ...@@ -2686,7 +2686,7 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
iosize = min(extent_map_end(em) - cur, end - cur + 1); iosize = min(extent_map_end(em) - cur, end - cur + 1);
cur_end = min(extent_map_end(em) - 1, end); cur_end = min(extent_map_end(em) - 1, end);
iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); iosize = ALIGN(iosize, blocksize);
if (this_bio_flag & EXTENT_BIO_COMPRESSED) { if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
disk_io_size = em->block_len; disk_io_size = em->block_len;
sector = em->block_start >> 9; sector = em->block_start >> 9;
...@@ -2977,7 +2977,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, ...@@ -2977,7 +2977,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,
BUG_ON(extent_map_end(em) <= cur); BUG_ON(extent_map_end(em) <= cur);
BUG_ON(end < cur); BUG_ON(end < cur);
iosize = min(extent_map_end(em) - cur, end - cur + 1); iosize = min(extent_map_end(em) - cur, end - cur + 1);
iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1); iosize = ALIGN(iosize, blocksize);
sector = (em->block_start + extent_offset) >> 9; sector = (em->block_start + extent_offset) >> 9;
bdev = em->bdev; bdev = em->bdev;
block_start = em->block_start; block_start = em->block_start;
...@@ -3664,7 +3664,7 @@ int extent_invalidatepage(struct extent_io_tree *tree, ...@@ -3664,7 +3664,7 @@ int extent_invalidatepage(struct extent_io_tree *tree,
u64 end = start + PAGE_CACHE_SIZE - 1; u64 end = start + PAGE_CACHE_SIZE - 1;
size_t blocksize = page->mapping->host->i_sb->s_blocksize; size_t blocksize = page->mapping->host->i_sb->s_blocksize;
start += (offset + blocksize - 1) & ~(blocksize - 1); start += ALIGN(offset, blocksize);
if (start > end) if (start > end)
return 0; return 0;
...@@ -3783,7 +3783,7 @@ static struct extent_map *get_extent_skip_holes(struct inode *inode, ...@@ -3783,7 +3783,7 @@ static struct extent_map *get_extent_skip_holes(struct inode *inode,
len = last - offset; len = last - offset;
if (len == 0) if (len == 0)
break; break;
len = (len + sectorsize - 1) & ~(sectorsize - 1); len = ALIGN(len, sectorsize);
em = get_extent(inode, NULL, 0, offset, len, 0); em = get_extent(inode, NULL, 0, offset, len, 0);
if (IS_ERR_OR_NULL(em)) if (IS_ERR_OR_NULL(em))
return em; return em;
......
...@@ -510,8 +510,7 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode, ...@@ -510,8 +510,7 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
loff_t isize = i_size_read(inode); loff_t isize = i_size_read(inode);
start_pos = pos & ~((u64)root->sectorsize - 1); start_pos = pos & ~((u64)root->sectorsize - 1);
num_bytes = (write_bytes + pos - start_pos + num_bytes = ALIGN(write_bytes + pos - start_pos, root->sectorsize);
root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
end_of_last_block = start_pos + num_bytes - 1; end_of_last_block = start_pos + num_bytes - 1;
err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
......
...@@ -233,8 +233,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans, ...@@ -233,8 +233,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
u64 isize = i_size_read(inode); u64 isize = i_size_read(inode);
u64 actual_end = min(end + 1, isize); u64 actual_end = min(end + 1, isize);
u64 inline_len = actual_end - start; u64 inline_len = actual_end - start;
u64 aligned_end = (end + root->sectorsize - 1) & u64 aligned_end = ALIGN(end, root->sectorsize);
~((u64)root->sectorsize - 1);
u64 data_len = inline_len; u64 data_len = inline_len;
int ret; int ret;
...@@ -391,7 +390,7 @@ static noinline int compress_file_range(struct inode *inode, ...@@ -391,7 +390,7 @@ static noinline int compress_file_range(struct inode *inode,
* a compressed extent to 128k. * a compressed extent to 128k.
*/ */
total_compressed = min(total_compressed, max_uncompressed); total_compressed = min(total_compressed, max_uncompressed);
num_bytes = (end - start + blocksize) & ~(blocksize - 1); num_bytes = ALIGN(end - start + 1, blocksize);
num_bytes = max(blocksize, num_bytes); num_bytes = max(blocksize, num_bytes);
total_in = 0; total_in = 0;
ret = 0; ret = 0;
...@@ -490,15 +489,13 @@ static noinline int compress_file_range(struct inode *inode, ...@@ -490,15 +489,13 @@ static noinline int compress_file_range(struct inode *inode,
* up to a block size boundary so the allocator does sane * up to a block size boundary so the allocator does sane
* things * things
*/ */
total_compressed = (total_compressed + blocksize - 1) & total_compressed = ALIGN(total_compressed, blocksize);
~(blocksize - 1);
/* /*
* one last check to make sure the compression is really a * one last check to make sure the compression is really a
* win, compare the page count read with the blocks on disk * win, compare the page count read with the blocks on disk
*/ */
total_in = (total_in + PAGE_CACHE_SIZE - 1) & total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
~(PAGE_CACHE_SIZE - 1);
if (total_compressed >= total_in) { if (total_compressed >= total_in) {
will_compress = 0; will_compress = 0;
} else { } else {
...@@ -856,7 +853,7 @@ static noinline int __cow_file_range(struct btrfs_trans_handle *trans, ...@@ -856,7 +853,7 @@ static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
BUG_ON(btrfs_is_free_space_inode(inode)); BUG_ON(btrfs_is_free_space_inode(inode));
num_bytes = (end - start + blocksize) & ~(blocksize - 1); num_bytes = ALIGN(end - start + 1, blocksize);
num_bytes = max(blocksize, num_bytes); num_bytes = max(blocksize, num_bytes);
disk_num_bytes = num_bytes; disk_num_bytes = num_bytes;
...@@ -4015,7 +4012,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -4015,7 +4012,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
u64 extent_num_bytes = 0; u64 extent_num_bytes = 0;
u64 extent_offset = 0; u64 extent_offset = 0;
u64 item_end = 0; u64 item_end = 0;
u64 mask = root->sectorsize - 1;
u32 found_type = (u8)-1; u32 found_type = (u8)-1;
int found_extent; int found_extent;
int del_item; int del_item;
...@@ -4039,7 +4035,8 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -4039,7 +4035,8 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
* extent just the way it is. * extent just the way it is.
*/ */
if (root->ref_cows || root == root->fs_info->tree_root) if (root->ref_cows || root == root->fs_info->tree_root)
btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0); btrfs_drop_extent_cache(inode, ALIGN(new_size,
root->sectorsize), (u64)-1, 0);
/* /*
* This function is also used to drop the items in the log tree before * This function is also used to drop the items in the log tree before
...@@ -4118,10 +4115,9 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -4118,10 +4115,9 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
if (!del_item) { if (!del_item) {
u64 orig_num_bytes = u64 orig_num_bytes =
btrfs_file_extent_num_bytes(leaf, fi); btrfs_file_extent_num_bytes(leaf, fi);
extent_num_bytes = new_size - extent_num_bytes = ALIGN(new_size -
found_key.offset + root->sectorsize - 1; found_key.offset,
extent_num_bytes = extent_num_bytes & root->sectorsize);
~((u64)root->sectorsize - 1);
btrfs_set_file_extent_num_bytes(leaf, fi, btrfs_set_file_extent_num_bytes(leaf, fi,
extent_num_bytes); extent_num_bytes);
num_dec = (orig_num_bytes - num_dec = (orig_num_bytes -
...@@ -4357,9 +4353,8 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) ...@@ -4357,9 +4353,8 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
struct extent_map *em = NULL; struct extent_map *em = NULL;
struct extent_state *cached_state = NULL; struct extent_state *cached_state = NULL;
struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
u64 mask = root->sectorsize - 1; u64 hole_start = ALIGN(oldsize, root->sectorsize);
u64 hole_start = (oldsize + mask) & ~mask; u64 block_end = ALIGN(size, root->sectorsize);
u64 block_end = (size + mask) & ~mask;
u64 last_byte; u64 last_byte;
u64 cur_offset; u64 cur_offset;
u64 hole_size; u64 hole_size;
...@@ -4392,7 +4387,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) ...@@ -4392,7 +4387,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
break; break;
} }
last_byte = min(extent_map_end(em), block_end); last_byte = min(extent_map_end(em), block_end);
last_byte = (last_byte + mask) & ~mask; last_byte = ALIGN(last_byte , root->sectorsize);
if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
struct extent_map *hole_em; struct extent_map *hole_em;
hole_size = last_byte - cur_offset; hole_size = last_byte - cur_offset;
...@@ -6111,8 +6106,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -6111,8 +6106,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
} else if (found_type == BTRFS_FILE_EXTENT_INLINE) { } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
size_t size; size_t size;
size = btrfs_file_extent_inline_len(leaf, item); size = btrfs_file_extent_inline_len(leaf, item);
extent_end = (extent_start + size + root->sectorsize - 1) & extent_end = ALIGN(extent_start + size, root->sectorsize);
~((u64)root->sectorsize - 1);
} }
if (start >= extent_end) { if (start >= extent_end) {
...@@ -6184,8 +6178,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ...@@ -6184,8 +6178,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
size - extent_offset); size - extent_offset);
em->start = extent_start + extent_offset; em->start = extent_start + extent_offset;
em->len = (copy_size + root->sectorsize - 1) & em->len = ALIGN(copy_size, root->sectorsize);
~((u64)root->sectorsize - 1);
em->orig_block_len = em->len; em->orig_block_len = em->len;
em->orig_start = em->start; em->orig_start = em->start;
if (compress_type) { if (compress_type) {
......
...@@ -484,7 +484,6 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, ...@@ -484,7 +484,6 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
struct btrfs_key *key) struct btrfs_key *key)
{ {
int found_type; int found_type;
u64 mask = root->sectorsize - 1;
u64 extent_end; u64 extent_end;
u64 start = key->offset; u64 start = key->offset;
u64 saved_nbytes; u64 saved_nbytes;
...@@ -501,7 +500,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, ...@@ -501,7 +500,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
extent_end = start + btrfs_file_extent_num_bytes(eb, item); extent_end = start + btrfs_file_extent_num_bytes(eb, item);
else if (found_type == BTRFS_FILE_EXTENT_INLINE) { else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
size = btrfs_file_extent_inline_len(eb, item); size = btrfs_file_extent_inline_len(eb, item);
extent_end = (start + size + mask) & ~mask; extent_end = ALIGN(start + size, root->sectorsize);
} else { } else {
ret = 0; ret = 0;
goto out; goto out;
......
...@@ -4556,8 +4556,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, ...@@ -4556,8 +4556,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
num_stripes = 1; num_stripes = 1;
stripe_index = 0; stripe_index = 0;
stripe_nr_orig = stripe_nr; stripe_nr_orig = stripe_nr;
stripe_nr_end = (offset + *length + map->stripe_len - 1) & stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
(~(map->stripe_len - 1));
do_div(stripe_nr_end, map->stripe_len); do_div(stripe_nr_end, map->stripe_len);
stripe_end_offset = stripe_nr_end * map->stripe_len - stripe_end_offset = stripe_nr_end * map->stripe_len -
(offset + *length); (offset + *length);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment