Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp724115pxb; Mon, 25 Oct 2021 17:30:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxezDXpL5MIDnUiG+JCr6rPHlgzJbctzKUSR+IffONzP6+Oqt7mrDrLDDAvQ3Yjmr8jn7qI X-Received: by 2002:a63:6ac2:: with SMTP id f185mr16360869pgc.326.1635208202664; Mon, 25 Oct 2021 17:30:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635208202; cv=none; d=google.com; s=arc-20160816; b=lUp7ctG1qtC714JrF5A8teN3BwfdfgpBVX1V35wTgigix3BJXb2Y2SjXgVuKnB1M6A 44BycNC0xipWq0jG/QtrE7GS2VS5pGObxsBTRp7opBCczkOuzA/sLnZstzIrviOc3tL4 vCzV1P47lOs2L1znkBbaR7JpKMJuyUni6qWlN/u9vwJXcQR1uQUnZhEo+vEvc41gziiO bGDHIUqtOHGxEU6+gRHlswrqvqBTEZpuJqEWCLGaYhurBbuTiimkVGPZphTayAR+MnLU 00vwSDhbjXsIvjFtvmzl01GWmdl7a27G2Wra2avfPDv2YW0DZ2F6u8yhxjj9S8MiW6ro Joyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Sjpy53wuY6DARqCPlKv9GV4DM6ftHSoYVFoEb/pYg6s=; b=Ylsrg9MqwbpbFsRyaF6x+9xJpJcwd+zHqdBewHoeuuna6hlRvTb+cN8hIQNpxusv8X BTCwTgKQoq6er52905Zb6umV6+xudPeO8mFm4OQ1ztMwGmXsBDhdtrPMK9v6k3OHAY8o gbrlTkymZe285eY1iFzuH0akDA4YH37ChdAS+PDZ/S2NwqtUT0souGLq+RQ7Sw1oDsh4 eeekyaWwcweL4/J/4zGQIBqLeg9gTDZQRB10dYRVhVl9d3awPgQFKKRBOUM/ajQYNAuP lb16/KOc1daCFki0tuqfhID08UV+rlueqPo3uFVhIZkqJ6IlZRvljfZSI93ErmkIZMgE P5IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=l0oGqrfw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a15si28870111pgw.185.2021.10.25.17.29.49; Mon, 25 Oct 2021 17:30:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=l0oGqrfw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237517AbhJYTwl (ORCPT + 99 others); Mon, 25 Oct 2021 15:52:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:36294 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237996AbhJYTrC (ORCPT ); Mon, 25 Oct 2021 15:47:02 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6B9B561073; Mon, 25 Oct 2021 19:40:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635190817; bh=iBX99063r03N7t1VGXnXNI18MO3as3WefmmBAgdIE3s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l0oGqrfwWuMk4a4HuwxGI+A3/AQu1ogCMSMEpyUWAj14SU/rx6ZY4bNS4YL8OuH5j 3RBnidlBNEgkXNsiZiLIVz4ua/H6G+0j/tPAb1da4EvvPZm/4O8MrXNcYBtaUe7p+4 rRr5X7N/X4LvcMNTq4zD9/cOJi2G1RccjrkYBi7A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Kara , Joseph Qi , Gang He , Mark Fasheh , Joel Becker , Junxiao Bi , Changwei Ge , Jun Piao , "Markov, Andrey" , Andrew Morton , Linus Torvalds Subject: [PATCH 5.14 078/169] ocfs2: fix data corruption after conversion from inline format Date: Mon, 25 Oct 2021 21:14:19 +0200 Message-Id: <20211025191027.271927861@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211025191017.756020307@linuxfoundation.org> References: <20211025191017.756020307@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jan Kara commit 5314454ea3ff6fc746eaf71b9a7ceebed52888fa upstream. Commit 6dbf7bb55598 ("fs: Don't invalidate page buffers in block_write_full_page()") uncovered a latent bug in ocfs2 conversion from inline inode format to a normal inode format. The code in ocfs2_convert_inline_data_to_extents() attempts to zero out the whole cluster allocated for file data by grabbing, zeroing, and dirtying all pages covering this cluster. However these pages are beyond i_size, thus writeback code generally ignores these dirty pages and no blocks were ever actually zeroed on the disk. This oversight was fixed by commit 693c241a5f6a ("ocfs2: No need to zero pages past i_size.") for standard ocfs2 write path, inline conversion path was apparently forgotten; the commit log also has a reasoning why the zeroing actually is not needed. After commit 6dbf7bb55598, things became worse as writeback code stopped invalidating buffers on pages beyond i_size and thus these pages end up with clean PageDirty bit but with buffers attached to these pages being still dirty. So when a file is converted from inline format, then writeback triggers, and then the file is grown so that these pages become valid, the invalid dirtiness state is preserved, mark_buffer_dirty() does nothing on these pages (buffers are already dirty) but page is never written back because it is clean. So data written to these pages is lost once pages are reclaimed. Simple reproducer for the problem is: xfs_io -f -c "pwrite 0 2000" -c "pwrite 2000 2000" -c "fsync" \ -c "pwrite 4000 2000" ocfs2_file After unmounting and mounting the fs again, you can observe that end of 'ocfs2_file' has lost its contents. Fix the problem by not doing the pointless zeroing during conversion from inline format similarly as in the standard write path. [akpm@linux-foundation.org: fix whitespace, per Joseph] Link: https://lkml.kernel.org/r/20210930095405.21433-1-jack@suse.cz Fixes: 6dbf7bb55598 ("fs: Don't invalidate page buffers in block_write_full_page()") Signed-off-by: Jan Kara Reviewed-by: Joseph Qi Tested-by: Joseph Qi Acked-by: Gang He Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Changwei Ge Cc: Jun Piao Cc: "Markov, Andrey" Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/ocfs2/alloc.c | 46 ++++++++++++---------------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) --- a/fs/ocfs2/alloc.c +++ b/fs/ocfs2/alloc.c @@ -7045,7 +7045,7 @@ void ocfs2_set_inode_data_inline(struct int ocfs2_convert_inline_data_to_extents(struct inode *inode, struct buffer_head *di_bh) { - int ret, i, has_data, num_pages = 0; + int ret, has_data, num_pages = 0; int need_free = 0; u32 bit_off, num; handle_t *handle; @@ -7054,26 +7054,17 @@ int ocfs2_convert_inline_data_to_extents struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; struct ocfs2_alloc_context *data_ac = NULL; - struct page **pages = NULL; - loff_t end = osb->s_clustersize; + struct page *page = NULL; struct ocfs2_extent_tree et; int did_quota = 0; has_data = i_size_read(inode) ? 1 : 0; if (has_data) { - pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), - sizeof(struct page *), GFP_NOFS); - if (pages == NULL) { - ret = -ENOMEM; - mlog_errno(ret); - return ret; - } - ret = ocfs2_reserve_clusters(osb, 1, &data_ac); if (ret) { mlog_errno(ret); - goto free_pages; + goto out; } } @@ -7093,7 +7084,8 @@ int ocfs2_convert_inline_data_to_extents } if (has_data) { - unsigned int page_end; + unsigned int page_end = min_t(unsigned, PAGE_SIZE, + osb->s_clustersize); u64 phys; ret = dquot_alloc_space_nodirty(inode, @@ -7117,15 +7109,8 @@ int ocfs2_convert_inline_data_to_extents */ block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); - /* - * Non sparse file systems zero on extend, so no need - * to do that now. - */ - if (!ocfs2_sparse_alloc(osb) && - PAGE_SIZE < osb->s_clustersize) - end = PAGE_SIZE; - - ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); + ret = ocfs2_grab_eof_pages(inode, 0, page_end, &page, + &num_pages); if (ret) { mlog_errno(ret); need_free = 1; @@ -7136,20 +7121,15 @@ int ocfs2_convert_inline_data_to_extents * This should populate the 1st page for us and mark * it up to date. */ - ret = ocfs2_read_inline_data(inode, pages[0], di_bh); + ret = ocfs2_read_inline_data(inode, page, di_bh); if (ret) { mlog_errno(ret); need_free = 1; goto out_unlock; } - page_end = PAGE_SIZE; - if (PAGE_SIZE > osb->s_clustersize) - page_end = osb->s_clustersize; - - for (i = 0; i < num_pages; i++) - ocfs2_map_and_dirty_page(inode, handle, 0, page_end, - pages[i], i > 0, &phys); + ocfs2_map_and_dirty_page(inode, handle, 0, page_end, page, 0, + &phys); } spin_lock(&oi->ip_lock); @@ -7180,8 +7160,8 @@ int ocfs2_convert_inline_data_to_extents } out_unlock: - if (pages) - ocfs2_unlock_and_free_pages(pages, num_pages); + if (page) + ocfs2_unlock_and_free_pages(&page, num_pages); out_commit: if (ret < 0 && did_quota) @@ -7205,8 +7185,6 @@ out_commit: out: if (data_ac) ocfs2_free_alloc_context(data_ac); -free_pages: - kfree(pages); return ret; }