From patchwork Thu Jul 9 09:47:07 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dingdinghua X-Patchwork-Id: 29625 Return-Path: X-Original-To: patchwork-incoming@bilbo.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (ozlabs.org [203.10.76.45]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "mx.ozlabs.org", Issuer "CA Cert Signing Authority" (verified OK)) by bilbo.ozlabs.org (Postfix) with ESMTPS id DA745B7067 for ; Thu, 9 Jul 2009 19:47:16 +1000 (EST) Received: by ozlabs.org (Postfix) id CD166DDDF8; Thu, 9 Jul 2009 19:47:16 +1000 (EST) Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by ozlabs.org (Postfix) with ESMTP id 6B43ADDDE7 for ; Thu, 9 Jul 2009 19:47:16 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756021AbZGIJrO (ORCPT ); Thu, 9 Jul 2009 05:47:14 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1756598AbZGIJrO (ORCPT ); Thu, 9 Jul 2009 05:47:14 -0400 Received: from wa-out-1112.google.com ([209.85.146.182]:8948 "EHLO wa-out-1112.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756021AbZGIJrO (ORCPT ); Thu, 9 Jul 2009 05:47:14 -0400 Received: by wa-out-1112.google.com with SMTP id j5so6076wah.21 for ; Thu, 09 Jul 2009 02:47:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer; bh=pIpAhulTfFdEk/LbqRFidz93qPDcoLElYj5dTHg+qic=; b=fERynlpbDYaYUVVAb+zbpHx4//nITpIEukPZLGOwpTkQD0dGA8krUwNN4nqUmjC/MN xpyIpPTeGqdFDSwoLZLCCwaWRzXfyWWLW0yV4FaAthbR9e9At18/qwKWidflgNJjxTmE JssDvKi1PPWfz4xemIydytD0TqzsBBunvvU7s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=ad20aMXv2E1yteJXPVWXwhkB/swDGi3CVEKKQArp9Plfs1gcUlNfUzd1Bv6KC8VwPY /tsyqwsyD5bN6DXvTf5fMvwAOJUi7Ob62lLY+AcRTvHdNcjilvTZwhDUI8Z0I8yATLTa J24iGTylEEA8vMmjmhHY5IA57UYRGceALL7Fg= Received: by 10.115.106.18 with SMTP id i18mr994311wam.57.1247132833769; Thu, 09 Jul 2009 02:47:13 -0700 (PDT) Received: from localhost ([159.226.39.251]) by mx.google.com with ESMTPS id j39sm16760372waf.45.2009.07.09.02.47.11 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 09 Jul 2009 02:47:13 -0700 (PDT) From: dingdinghua To: linux-ext4@vger.kernel.org Cc: dingdinghua Subject: [PATCH] fix race bwtween write_metadata_buffer and get_write_access Date: Thu, 9 Jul 2009 17:47:07 +0800 Message-Id: <1247132827-29614-1-git-send-email-dingdinghua85@gmail.com> X-Mailer: git-send-email 1.6.0.4 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org At committing phase, we call jbd2_journal_write_metadata_buffer to prepare log block's buffer_head, in this function, new_bh->b_data is set to b_frozen_data or bh_in->b_data. We call "jbd_unlock_bh_state(bh_in)" too early, since at this point , we haven't file bh_in to BJ_shadow list, and we may set new_bh->b_data to bh_in->b_data, at this time, another thread may call get write access of bh_in, modify bh_in->b_data and dirty it. So , if new_bh->b_data is set to bh_in->b_data, the committing transaction may flush the newly modified buffer content to disk, preserve work done in jbd2_journal_get_write_access is useless. jbd also has this problem. Signed-off-by: dingdinghua --- fs/jbd/journal.c | 20 +++++++++++--------- fs/jbd2/journal.c | 20 +++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/fs/jbd/journal.c b/fs/jbd/journal.c index 737f724..ff5dcb5 100644 --- a/fs/jbd/journal.c +++ b/fs/jbd/journal.c @@ -287,6 +287,7 @@ int journal_write_metadata_buffer(transaction_t *transaction, struct page *new_page; unsigned int new_offset; struct buffer_head *bh_in = jh2bh(jh_in); + journal_t *journal = transaction->t_journal; /* * The buffer really shouldn't be locked: only the current committing @@ -300,6 +301,11 @@ int journal_write_metadata_buffer(transaction_t *transaction, J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); + /* keep subsequent assertions sane */ + new_bh->b_state = 0; + init_buffer(new_bh, NULL, NULL); + atomic_set(&new_bh->b_count, 1); + new_jh = journal_add_journal_head(new_bh); /* This sleeps */ /* * If a new transaction has already done a buffer copy-out, then @@ -361,14 +367,6 @@ repeat: kunmap_atomic(mapped_data, KM_USER0); } - /* keep subsequent assertions sane */ - new_bh->b_state = 0; - init_buffer(new_bh, NULL, NULL); - atomic_set(&new_bh->b_count, 1); - jbd_unlock_bh_state(bh_in); - - new_jh = journal_add_journal_head(new_bh); /* This sleeps */ - set_bh_page(new_bh, new_page, new_offset); new_jh->b_transaction = NULL; new_bh->b_size = jh2bh(jh_in)->b_size; @@ -385,7 +383,11 @@ repeat: * copying is moved to the transaction's shadow queue. */ JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); - journal_file_buffer(jh_in, transaction, BJ_Shadow); + spin_lock(&journal->j_list_lock); + __journal_file_buffer(jh_in, transaction, BJ_Shadow); + spin_unlock(&journal->j_list_lock); + jbd_unlock_bh_state(bh_in); + JBUFFER_TRACE(new_jh, "file as BJ_IO"); journal_file_buffer(new_jh, transaction, BJ_IO); diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c index 18bfd5d..a601417 100644 --- a/fs/jbd2/journal.c +++ b/fs/jbd2/journal.c @@ -297,6 +297,7 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction, unsigned int new_offset; struct buffer_head *bh_in = jh2bh(jh_in); struct jbd2_buffer_trigger_type *triggers; + journal_t *journal = transaction->t_journal; /* * The buffer really shouldn't be locked: only the current committing @@ -310,6 +311,11 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction, J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); + /* keep subsequent assertions sane */ + new_bh->b_state = 0; + init_buffer(new_bh, NULL, NULL); + atomic_set(&new_bh->b_count, 1); + new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ /* * If a new transaction has already done a buffer copy-out, then @@ -388,14 +394,6 @@ repeat: kunmap_atomic(mapped_data, KM_USER0); } - /* keep subsequent assertions sane */ - new_bh->b_state = 0; - init_buffer(new_bh, NULL, NULL); - atomic_set(&new_bh->b_count, 1); - jbd_unlock_bh_state(bh_in); - - new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ - set_bh_page(new_bh, new_page, new_offset); new_jh->b_transaction = NULL; new_bh->b_size = jh2bh(jh_in)->b_size; @@ -412,7 +410,11 @@ repeat: * copying is moved to the transaction's shadow queue. */ JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); - jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); + spin_lock(&journal->j_list_lock); + __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); + spin_unlock(&journal->j_list_lock); + jbd_unlock_bh_state(bh_in); + JBUFFER_TRACE(new_jh, "file as BJ_IO"); jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);