From patchwork Wed Jul 21 17:16:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 59483 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 774F8B70A6 for ; Thu, 22 Jul 2010 03:16:42 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753465Ab0GURQM (ORCPT ); Wed, 21 Jul 2010 13:16:12 -0400 Received: from ksp.mff.cuni.cz ([195.113.26.206]:35827 "EHLO atrey.karlin.mff.cuni.cz" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753213Ab0GURQL (ORCPT ); Wed, 21 Jul 2010 13:16:11 -0400 Received: by atrey.karlin.mff.cuni.cz (Postfix, from userid 4043) id DE548F134A; Wed, 21 Jul 2010 19:16:09 +0200 (CEST) Date: Wed, 21 Jul 2010 19:16:09 +0200 From: Jan Kara To: tytso@mit.edu, Ric Wheeler , Christoph Hellwig , Mingming Cao , djwong@us.ibm.com, linux-ext4 , linux-kernel , Keith Mannthey , Mingming Cao Subject: Re: [RFC] ext4: Don't send extra barrier during fsync if there are no dirty pages. Message-ID: <20100721171609.GC1215@atrey.karlin.mff.cuni.cz> References: <20100429235102.GC15607@tux1.beaverton.ibm.com> <1272934667.2544.3.camel@mingming-laptop> <4BE02C45.6010608@redhat.com> <20100504154553.GA22777@infradead.org> <20100630124832.GA1333@thunk.org> <4C2B44C0.3090002@redhat.com> <20100630134429.GE1333@thunk.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20100630134429.GE1333@thunk.org> User-Agent: Mutt/1.5.18 (2008-05-17) Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Hi, > On Wed, Jun 30, 2010 at 09:21:04AM -0400, Ric Wheeler wrote: > > > > The problem with not issuing a cache flush when you have dirty meta > > data or data is that it does not have any tie to the state of the > > volatile write cache of the target storage device. > > We track whether or not there is any metadata updates associated with > the inode already; if it does, we force a journal commit, and this > implies a barrier operation. > > The case we're talking about here is one where either (a) there is no > journal, or (b) there have been no metadata updates (I'm simplifying a > little here; in fact we track whether there have been fdatasync()- vs > fsync()- worthy metadata updates), and so there hasn't been a journal > commit to do the cache flush. > > In this case, we want to track when is the last time an fsync() has > been issued, versus when was the last time data blocks for a > particular inode have been pushed out to disk. > > To use an example I used as motivation for why we might want an > fsync2(int fd[], int flags[], int num) syscall, consider the situation > of: > > fsync(control_fd); > fdatasync(data_fd); > > The first fsync() will have executed a cache flush operation. So when > we do the fdatasync() (assuming that no metadata needs to be flushed > out to disk), there is no need for the cache flush operation. > > If we had an enhanced fsync command, we would also be able to > eliminate a second journal commit in the case where data_fd also had > some metadata that needed to be flushed out to disk. Current implementation already avoids journal commit because of fdatasync(data_fd). We remeber a transaction ID when inode metadata has last been updated and do not force a transaction commit if it is already committed. Thus the first fsync might force a transaction commit but second fdatasync likely won't. We could actually improve the scheme to work for data as well. I wrote a proof-of-concept patches (attached) and they nicely avoid second barrier when doing: echo "aaa" >file1; echo "aaa" >file2; fsync file2; fsync file1 Ted, would you be interested in something like this? Honza From 4fe836d5554dc8cb0732af1f4e5b317d7e59febf Mon Sep 17 00:00:00 2001 From: Jan Kara Date: Wed, 21 Jul 2010 19:01:51 +0200 Subject: [PATCH 2/2] ext4: Send barriers on fsync only when needed It isn't necessary to send a barrier to disk for fsync of file 'f' when we already sent one after all the data of 'f' have been written. Implement logic to detect this condition and avoid sending barrier in this case. We use counters of submitted and completed IO barriers for a block device. When a page is written to the block device, we store current number of barriers submitted in the inode. When we handle fsync, we check whether the number of completed barriers is at least that large. Signed-off-by: Jan Kara --- fs/ext4/ext4.h | 4 +++- fs/ext4/fsync.c | 19 +++++++++++++++++-- fs/ext4/inode.c | 4 ++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 19a4de5..cc67e72 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -832,10 +832,12 @@ struct ext4_inode_info { /* * Transactions that contain inode's metadata needed to complete - * fsync and fdatasync, respectively. + * fsync and fdatasync, respectively and barrier id when we last + * wrote data to this file. */ tid_t i_sync_tid; tid_t i_datasync_tid; + unsigned i_data_bid; }; /* diff --git a/fs/ext4/fsync.c b/fs/ext4/fsync.c index 592adf2..d8a6995 100644 --- a/fs/ext4/fsync.c +++ b/fs/ext4/fsync.c @@ -57,6 +57,21 @@ static void ext4_sync_parent(struct inode *inode) } } +static int ext4_need_issue_data_flush(struct inode *inode) +{ + struct ext4_inode_info *ei = EXT4_I(inode); + journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; + int comp_bid, inode_bid = ei->i_data_bid; + + if (!(journal->j_flags & JBD2_BARRIER)) + return 0; + comp_bid = atomic_read(&inode->i_sb->s_bdev->bd_barriers_completed); + /* inode_bid < completed_bid safe against wrapping */ + if (inode_bid - comp_bid < 0) + return 0; + return 1; +} + /* * akpm: A new design for ext4_sync_file(). * @@ -126,11 +141,11 @@ int ext4_sync_file(struct file *file, int datasync) */ if (ext4_should_writeback_data(inode) && (journal->j_fs_dev != journal->j_dev) && - (journal->j_flags & JBD2_BARRIER)) + ext4_need_issue_data_flush(inode)) blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL, BLKDEV_IFL_WAIT); ret = jbd2_log_wait_commit(journal, commit_tid); - } else if (journal->j_flags & JBD2_BARRIER) + } else if (ext4_need_issue_data_flush(inode)) blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL, BLKDEV_IFL_WAIT); return ret; diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 42272d6..8d57aae 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -2758,6 +2758,10 @@ static int ext4_writepage(struct page *page, } else ret = block_write_full_page(page, noalloc_get_block_write, wbc); + /* Make sure we read current value of bd_barriers_sent */ + smp_rmb(); + EXT4_I(inode)->i_data_bid = + atomic_read(&inode->i_sb->s_bdev->bd_barriers_sent); return ret; } -- 1.6.4.2