Commit a76a3cd4 authored by Yan Zheng's avatar Yan Zheng Committed by Chris Mason

Btrfs: Count space allocated to file in bytes

This patch makes btrfs count space allocated to file in bytes instead
of 512 byte sectors.

Everything else in btrfs uses a byte count instead of sector sizes or
blocks sizes, so this fits better.
Signed-off-by: default avatarYan Zheng <zheng.yan@oracle.com>
parent a62b9401
...@@ -411,7 +411,7 @@ struct btrfs_inode_item { ...@@ -411,7 +411,7 @@ struct btrfs_inode_item {
/* transid that last touched this inode */ /* transid that last touched this inode */
__le64 transid; __le64 transid;
__le64 size; __le64 size;
__le64 nblocks; __le64 nbytes;
__le64 block_group; __le64 block_group;
__le32 nlink; __le32 nlink;
__le32 uid; __le32 uid;
...@@ -1017,7 +1017,7 @@ BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64); ...@@ -1017,7 +1017,7 @@ BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64); BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64); BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64); BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64); BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64); BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32); BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32); BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
...@@ -1814,15 +1814,6 @@ void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name, ...@@ -1814,15 +1814,6 @@ void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
int btrfs_merge_bio_hook(struct page *page, unsigned long offset, int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
size_t size, struct bio *bio); size_t size, struct bio *bio);
static inline void dec_i_blocks(struct inode *inode, u64 dec)
{
dec = dec >> 9;
if (dec <= inode->i_blocks)
inode->i_blocks -= dec;
else
inode->i_blocks = 0;
}
unsigned long btrfs_force_ra(struct address_space *mapping, unsigned long btrfs_force_ra(struct address_space *mapping,
struct file_ra_state *ra, struct file *file, struct file_ra_state *ra, struct file *file,
pgoff_t offset, pgoff_t last_index); pgoff_t offset, pgoff_t last_index);
......
...@@ -3930,7 +3930,7 @@ static int noinline replace_one_extent(struct btrfs_trans_handle *trans, ...@@ -3930,7 +3930,7 @@ static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
BUG_ON(ret); BUG_ON(ret);
btrfs_release_path(root, path); btrfs_release_path(root, path);
inode->i_blocks += extent_len >> 9; inode_add_bytes(inode, extent_len);
ext_offset = 0; ext_offset = 0;
num_bytes -= extent_len; num_bytes -= extent_len;
......
...@@ -193,7 +193,7 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans, ...@@ -193,7 +193,7 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
leaf = path->nodes[0]; leaf = path->nodes[0];
ei = btrfs_item_ptr(leaf, path->slots[0], ei = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
inode->i_blocks += (offset + size - found_end) >> 9; inode_add_bytes(inode, offset + size - found_end);
} }
if (found_end < offset) { if (found_end < offset) {
ptr = btrfs_file_extent_inline_start(ei) + found_size; ptr = btrfs_file_extent_inline_start(ei) + found_size;
...@@ -203,7 +203,7 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans, ...@@ -203,7 +203,7 @@ static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
insert: insert:
btrfs_release_path(root, path); btrfs_release_path(root, path);
datasize = offset + size - key.offset; datasize = offset + size - key.offset;
inode->i_blocks += datasize >> 9; inode_add_bytes(inode, datasize);
datasize = btrfs_file_extent_calc_inline_size(datasize); datasize = btrfs_file_extent_calc_inline_size(datasize);
ret = btrfs_insert_empty_item(trans, root, path, &key, ret = btrfs_insert_empty_item(trans, root, path, &key,
datasize); datasize);
...@@ -713,7 +713,8 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -713,7 +713,8 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
extent); extent);
if (btrfs_file_extent_disk_bytenr(leaf, if (btrfs_file_extent_disk_bytenr(leaf,
extent)) { extent)) {
dec_i_blocks(inode, old_num - new_num); inode_sub_bytes(inode, old_num -
new_num);
} }
btrfs_set_file_extent_num_bytes(leaf, extent, btrfs_set_file_extent_num_bytes(leaf, extent,
new_num); new_num);
...@@ -724,14 +725,17 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -724,14 +725,17 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
u32 new_size; u32 new_size;
new_size = btrfs_file_extent_calc_inline_size( new_size = btrfs_file_extent_calc_inline_size(
inline_limit - key.offset); inline_limit - key.offset);
dec_i_blocks(inode, (extent_end - key.offset) - inode_sub_bytes(inode, extent_end -
(inline_limit - key.offset)); inline_limit);
btrfs_truncate_item(trans, root, path, btrfs_truncate_item(trans, root, path,
new_size, 1); new_size, 1);
} }
} }
/* delete the entire extent */ /* delete the entire extent */
if (!keep) { if (!keep) {
if (found_inline)
inode_sub_bytes(inode, extent_end -
key.offset);
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
/* TODO update progress marker and return */ /* TODO update progress marker and return */
BUG_ON(ret); BUG_ON(ret);
...@@ -743,8 +747,7 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -743,8 +747,7 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
u32 new_size; u32 new_size;
new_size = btrfs_file_extent_calc_inline_size( new_size = btrfs_file_extent_calc_inline_size(
extent_end - end); extent_end - end);
dec_i_blocks(inode, (extent_end - key.offset) - inode_sub_bytes(inode, end - key.offset);
(extent_end - end));
ret = btrfs_truncate_item(trans, root, path, ret = btrfs_truncate_item(trans, root, path,
new_size, 0); new_size, 0);
BUG_ON(ret); BUG_ON(ret);
...@@ -791,9 +794,7 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -791,9 +794,7 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
} }
btrfs_release_path(root, path); btrfs_release_path(root, path);
if (disk_bytenr != 0) { if (disk_bytenr != 0) {
inode->i_blocks += inode_add_bytes(inode, extent_end - end);
btrfs_file_extent_num_bytes(leaf,
extent) >> 9;
} }
} }
...@@ -801,7 +802,8 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -801,7 +802,8 @@ int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
u64 disk_bytenr = le64_to_cpu(old.disk_bytenr); u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
if (disk_bytenr != 0) { if (disk_bytenr != 0) {
dec_i_blocks(inode, le64_to_cpu(old.num_bytes)); inode_sub_bytes(inode,
le64_to_cpu(old.num_bytes));
ret = btrfs_free_extent(trans, root, ret = btrfs_free_extent(trans, root,
disk_bytenr, disk_bytenr,
le64_to_cpu(old.disk_num_bytes), le64_to_cpu(old.disk_num_bytes),
......
...@@ -652,7 +652,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end) ...@@ -652,7 +652,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
BUG_ON(ret); BUG_ON(ret);
btrfs_release_path(root, path); btrfs_release_path(root, path);
inode->i_blocks += ordered_extent->len >> 9; inode_add_bytes(inode, ordered_extent->len);
unlock_extent(io_tree, ordered_extent->file_offset, unlock_extent(io_tree, ordered_extent->file_offset,
ordered_extent->file_offset + ordered_extent->len - 1, ordered_extent->file_offset + ordered_extent->len - 1,
GFP_NOFS); GFP_NOFS);
...@@ -1104,7 +1104,7 @@ void btrfs_read_locked_inode(struct inode *inode) ...@@ -1104,7 +1104,7 @@ void btrfs_read_locked_inode(struct inode *inode)
inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
inode->i_generation = BTRFS_I(inode)->generation; inode->i_generation = BTRFS_I(inode)->generation;
inode->i_rdev = 0; inode->i_rdev = 0;
...@@ -1184,7 +1184,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, ...@@ -1184,7 +1184,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans,
btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
inode->i_ctime.tv_nsec); inode->i_ctime.tv_nsec);
btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
btrfs_set_inode_transid(leaf, item, trans->transid); btrfs_set_inode_transid(leaf, item, trans->transid);
btrfs_set_inode_rdev(leaf, item, inode->i_rdev); btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
...@@ -1679,7 +1679,7 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -1679,7 +1679,7 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
num_dec = (orig_num_bytes - num_dec = (orig_num_bytes -
extent_num_bytes); extent_num_bytes);
if (root->ref_cows && extent_start != 0) if (root->ref_cows && extent_start != 0)
dec_i_blocks(inode, num_dec); inode_sub_bytes(inode, num_dec);
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
} else { } else {
extent_num_bytes = extent_num_bytes =
...@@ -1690,7 +1690,7 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -1690,7 +1690,7 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
if (extent_start != 0) { if (extent_start != 0) {
found_extent = 1; found_extent = 1;
if (root->ref_cows) if (root->ref_cows)
dec_i_blocks(inode, num_dec); inode_sub_bytes(inode, num_dec);
} }
root_gen = btrfs_header_generation(leaf); root_gen = btrfs_header_generation(leaf);
root_owner = btrfs_header_owner(leaf); root_owner = btrfs_header_owner(leaf);
...@@ -1700,8 +1700,8 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -1700,8 +1700,8 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
u32 size = new_size - found_key.offset; u32 size = new_size - found_key.offset;
if (root->ref_cows) { if (root->ref_cows) {
dec_i_blocks(inode, item_end + 1 - inode_sub_bytes(inode, item_end + 1 -
found_key.offset - size); new_size);
} }
size = size =
btrfs_file_extent_calc_inline_size(size); btrfs_file_extent_calc_inline_size(size);
...@@ -1709,8 +1709,8 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -1709,8 +1709,8 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
size, 1); size, 1);
BUG_ON(ret); BUG_ON(ret);
} else if (root->ref_cows) { } else if (root->ref_cows) {
dec_i_blocks(inode, item_end + 1 - inode_sub_bytes(inode, item_end + 1 -
found_key.offset); found_key.offset);
} }
} }
delete: delete:
...@@ -2514,7 +2514,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, ...@@ -2514,7 +2514,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
inode->i_gid = current->fsgid; inode->i_gid = current->fsgid;
inode->i_mode = mode; inode->i_mode = mode;
inode->i_ino = objectid; inode->i_ino = objectid;
inode->i_blocks = 0; inode_set_bytes(inode, 0);
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_item); struct btrfs_inode_item);
...@@ -3557,7 +3557,8 @@ static int btrfs_getattr(struct vfsmount *mnt, ...@@ -3557,7 +3557,8 @@ static int btrfs_getattr(struct vfsmount *mnt,
struct inode *inode = dentry->d_inode; struct inode *inode = dentry->d_inode;
generic_fillattr(inode, stat); generic_fillattr(inode, stat);
stat->blksize = PAGE_CACHE_SIZE; stat->blksize = PAGE_CACHE_SIZE;
stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9); stat->blocks = (inode_get_bytes(inode) +
BTRFS_I(inode)->delalloc_bytes) >> 9;
return 0; return 0;
} }
......
...@@ -99,7 +99,7 @@ static noinline int create_subvol(struct btrfs_root *root, char *name, ...@@ -99,7 +99,7 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
inode_item->generation = cpu_to_le64(1); inode_item->generation = cpu_to_le64(1);
inode_item->size = cpu_to_le64(3); inode_item->size = cpu_to_le64(3);
inode_item->nlink = cpu_to_le32(1); inode_item->nlink = cpu_to_le32(1);
inode_item->nblocks = cpu_to_le64(1); inode_item->nbytes = cpu_to_le64(root->leafsize);
inode_item->mode = cpu_to_le32(S_IFDIR | 0755); inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
btrfs_set_root_bytenr(&root_item, leaf->start); btrfs_set_root_bytenr(&root_item, leaf->start);
...@@ -671,7 +671,7 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd) ...@@ -671,7 +671,7 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
btrfs_release_path(root, path); btrfs_release_path(root, path);
if (ret == 0) { if (ret == 0) {
inode->i_mtime = inode->i_ctime = CURRENT_TIME; inode->i_mtime = inode->i_ctime = CURRENT_TIME;
inode->i_blocks = src->i_blocks; inode_set_bytes(inode, inode_get_bytes(src));
btrfs_i_size_write(inode, src->i_size); btrfs_i_size_write(inode, src->i_size);
BTRFS_I(inode)->flags = BTRFS_I(src)->flags; BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
ret = btrfs_update_inode(trans, root, inode); ret = btrfs_update_inode(trans, root, inode);
......
...@@ -113,7 +113,7 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans, ...@@ -113,7 +113,7 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
inode_item->generation = cpu_to_le64(1); inode_item->generation = cpu_to_le64(1);
inode_item->size = cpu_to_le64(3); inode_item->size = cpu_to_le64(3);
inode_item->nlink = cpu_to_le32(1); inode_item->nlink = cpu_to_le32(1);
inode_item->nblocks = cpu_to_le64(1); inode_item->nbytes = cpu_to_le64(root->leafsize);
inode_item->mode = cpu_to_le32(S_IFDIR | 0755); inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
btrfs_set_root_bytenr(&root_item, leaf->start); btrfs_set_root_bytenr(&root_item, leaf->start);
...@@ -598,8 +598,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, ...@@ -598,8 +598,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
ret = overwrite_item(trans, root, path, eb, slot, key); ret = overwrite_item(trans, root, path, eb, slot, key);
BUG_ON(ret); BUG_ON(ret);
/* btrfs_drop_extents changes i_blocks, update it here */ /* btrfs_drop_extents changes i_bytes & i_blocks, update it here */
inode->i_blocks += (extent_end - start) >> 9; inode_add_bytes(inode, extent_end - start);
btrfs_update_inode(trans, root, inode); btrfs_update_inode(trans, root, inode);
out: out:
if (inode) if (inode)
......
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