Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp682641pxb; Mon, 25 Oct 2021 16:35:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAFkS9IlyeP2ub/bXHE8G1AlbS71IdY+GCCpPhf+saBB4R0ro5vr2vn5KP9OyOZOSt1f5F X-Received: by 2002:a17:90b:4f8c:: with SMTP id qe12mr6753500pjb.237.1635204920399; Mon, 25 Oct 2021 16:35:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635204920; cv=none; d=google.com; s=arc-20160816; b=pw8uVUBh3XgamuMwuYiXcDErNsCUFJrIm5VUfwv98aHfsiiHzXVWGieWJHM1o1DAaj RGpOIbU0MWlOuupCItdF+R8xtAirsOfvmgFl5O0SML4QhKJ9RyAtuI47lJTOeCCfIORo y8s+yAkixMsM5EuP0W8nb+0yMQP+8qwvK8OWxr3SkvalOBFqRBRhEQ++UuHRNUo/mIX/ PXRHZIN2ciZfky/irfK3Retx3ZjuNMJieM8Mem9OafwKucsid8lBIK9CPtoWjSJKYDXT xe4vKGt/js4Lh5K+SKFd5F8hlpwsvPpRuB2i2NmXhyG3pjUTeGdfY9OqTIvge/L7+mSN IwoA== 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=85yQWckmO0GUPzXT/fPHFJ4dS/JMV514NfTnmFX7xAY=; b=NtuIp3FZVWR7BdEErdFAwUiFbna5I2KRopwmGNMvGmv27Ujfk/WiM1ytlNTMqYZb4N 9j4jWIcO8vQ1lJyBEX6vCPbXrBh3TXeKyGEAbvmnh0wBDktehcrdfeSVLoqndZdLk+wP qUQkBEoZrFxjBr1XIFTN3UdnJSmXUEttrftoj0sbjjzfyb8VOhzNJJlCO5bCDROhgFfy sKOYJzlBcHLesQbxCzr9pidCZD2KqUYn/7SJS66Ik83T1Z7j04tAAPSTroDRGlqKr95u b5xuMQ0cFY6BLFBxAnuyZiB7vTps0vzlrUAL5NUn4tylRrvp/36qohHCHEVv4JuaTyI1 8HaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EFhcIvLq; 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 q19si23459196pll.156.2021.10.25.16.35.06; Mon, 25 Oct 2021 16:35:20 -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=EFhcIvLq; 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 S234618AbhJYTYk (ORCPT + 99 others); Mon, 25 Oct 2021 15:24:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:40072 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233950AbhJYTXC (ORCPT ); Mon, 25 Oct 2021 15:23:02 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 18C15610CF; Mon, 25 Oct 2021 19:20:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635189631; bh=iUbjssYaGWQmErBdQjjg8t1hZVck5GDkW15piU+dZLA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EFhcIvLqemOsHOblPMU1hIm1mvulKNJEtAgE8F40lPxnqYDI3SfMwOo1MS/b5JDUx 57oOnmgeaJDKER/7RsDisLESPy7ycaQkMYesP0wfjuk1JCvLDk73Btk3kyt7FZjw+W pYOqpOPG+D9rsWTrXguHIMQZxsUikvaCbtyS2W7E= 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 4.9 34/50] ocfs2: fix data corruption after conversion from inline format Date: Mon, 25 Oct 2021 21:14:21 +0200 Message-Id: <20211025190939.146428887@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211025190932.542632625@linuxfoundation.org> References: <20211025190932.542632625@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 @@ -6891,7 +6891,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; @@ -6900,26 +6900,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; } } @@ -6939,7 +6930,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, @@ -6963,15 +6955,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; @@ -6982,20 +6967,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); @@ -7026,8 +7006,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) @@ -7051,8 +7031,6 @@ out_commit: out: if (data_ac) ocfs2_free_alloc_context(data_ac); -free_pages: - kfree(pages); return ret; }