From patchwork Thu Jun 16 09:23:00 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: liubo X-Patchwork-Id: 886572 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p5G9NB2h022794 for ; Thu, 16 Jun 2011 09:23:12 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756195Ab1FPJXI (ORCPT ); Thu, 16 Jun 2011 05:23:08 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:50682 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1753935Ab1FPJXG (ORCPT ); Thu, 16 Jun 2011 05:23:06 -0400 Received: from tang.cn.fujitsu.com (tang.cn.fujitsu.com [10.167.250.3]) by song.cn.fujitsu.com (Postfix) with ESMTP id 5EF48170126; Thu, 16 Jun 2011 17:23:03 +0800 (CST) Received: from mailserver.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id p5G9N0Ef015814; Thu, 16 Jun 2011 17:23:02 +0800 Received: from localhost.localdomain ([10.167.225.27]) by mailserver.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.1FP4) with ESMTP id 2011061617224329-589249 ; Thu, 16 Jun 2011 17:22:43 +0800 From: Liu Bo To: Cc: , Subject: [PATCH 2/2] Btrfs: fix 'ls -lis' mismatch Date: Thu, 16 Jun 2011 17:23:00 +0800 Message-Id: <1308216180-13132-2-git-send-email-liubo2009@cn.fujitsu.com> X-Mailer: git-send-email 1.6.5.2 In-Reply-To: <1308216180-13132-1-git-send-email-liubo2009@cn.fujitsu.com> References: <1308216180-13132-1-git-send-email-liubo2009@cn.fujitsu.com> X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.1FP4|July 25, 2010) at 2011-06-16 17:22:43, Serialize by Router on mailserver/fnst(Release 8.5.1FP4|July 25, 2010) at 2011-06-16 17:22:45, Serialize complete at 2011-06-16 17:22:45 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Thu, 16 Jun 2011 09:23:12 +0000 (UTC) As btrfs uses delay allocation mechanism and data=order mode, there can be a period window, during which we sub delalloc_bytes and add_inode_bytes, and we may get a value of '0' referred to inode's blocks via 'ls -lis'. ino:291 blocks:198656 i_blocks:0 i_bytes:0 delalloc_bytes:101711872 ino:291 blocks:198656 i_blocks:0 i_bytes:0 delalloc_bytes:101711872 <--------- ino:291 blocks:0 i_blocks:0 i_bytes:0 delalloc_bytes:0 | THE ino:291 blocks:0 i_blocks:0 i_bytes:0 delalloc_bytes:0 | WINDOW <--------- ino:291 blocks:819200 i_blocks:819200 i_bytes:0 delalloc_bytes:0 This may make btrfs's users confused. Hence, we use anther counter for the number of delalloc bytes in flight that are accounted for in coordination with inode_add_bytes to ensure correct output results. Signed-off-by: Liu Bo --- fs/btrfs/btrfs_inode.h | 8 ++++++-- fs/btrfs/inode.c | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h index 31337df..1e0dc82 100644 --- a/fs/btrfs/btrfs_inode.h +++ b/fs/btrfs/btrfs_inode.h @@ -93,9 +93,13 @@ struct btrfs_inode { */ u64 logged_trans; - /* total number of bytes pending delalloc, used by stat to calc the - * real block usage of the file + /* + * total number of bytes pending delalloc, used by stat to + * calc the real block usage of the file. */ + u64 pending_bytes; + + /* total number of bytes pending delalloc */ u64 delalloc_bytes; /* total number of bytes that may be used for this inode for diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index e01a084..70bd01d 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -106,6 +106,13 @@ static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, return err; } +static void btrfs_inode_add_bytes(struct inode *inode, loff_t bytes) +{ + if (BTRFS_I(inode)->pending_bytes >= bytes) + BTRFS_I(inode)->pending_bytes -= bytes; + inode_add_bytes(inode, bytes); +} + /* * this does all the hard work for inserting an inline extent into * the btree. The caller should have done a btrfs_drop_extents so that @@ -144,7 +151,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); datasize = btrfs_file_extent_calc_inline_size(cur_size); - inode_add_bytes(inode, size); + btrfs_inode_add_bytes(inode, size); ret = btrfs_insert_empty_item(trans, root, path, &key, datasize); BUG_ON(ret); @@ -1346,6 +1353,7 @@ static int btrfs_set_bit_hook(struct inode *inode, spin_lock(&root->fs_info->delalloc_lock); BTRFS_I(inode)->delalloc_bytes += len; + BTRFS_I(inode)->pending_bytes += len; root->fs_info->delalloc_bytes += len; if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) { list_add_tail(&BTRFS_I(inode)->delalloc_inodes, @@ -1685,7 +1693,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, btrfs_mark_buffer_dirty(leaf); - inode_add_bytes(inode, num_bytes); + btrfs_inode_add_bytes(inode, num_bytes); ins.objectid = disk_bytenr; ins.offset = disk_num_bytes; @@ -6726,6 +6734,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) ei->last_trans = 0; ei->last_sub_trans = 0; ei->logged_trans = 0; + ei->pending_bytes = 0; ei->delalloc_bytes = 0; ei->reserved_bytes = 0; ei->disk_i_size = 0; @@ -6901,7 +6910,7 @@ static int btrfs_getattr(struct vfsmount *mnt, 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; + BTRFS_I(inode)->pending_bytes) >> 9; return 0; }