Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp688565pxb; Mon, 25 Oct 2021 16:41:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZ/+fRiiJNrO8zdUOnY7GOhCutqXlJAJcUDjmUXu5jdJBPuHa241cPVIQj1iI3Mvt8j8XE X-Received: by 2002:a05:6a00:16c6:b029:32d:e190:9dd0 with SMTP id l6-20020a056a0016c6b029032de1909dd0mr22161993pfc.70.1635205309969; Mon, 25 Oct 2021 16:41:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635205309; cv=none; d=google.com; s=arc-20160816; b=EVuLmAQLOfP6gX3bKsZtY8fgTOK/hz1/wQs3lo3GLro5+7kw2yEFrqVen48jS+gkqX OJqZFLvJOMAvK54GkM6LSoAlqmr8cubgmUqTg9jJ01xxJ1lGd2OjcN/KVyrMxLskbHm/ s16lC3WJFkxdkPwIgRlEhumE+fXkPd149TwGVmtxJZ8D5SGMgv6mFG9MCkP3oz0VxDUn PhIM4fWAm1KE9gEPZdjvbjauVPyNPT8DkoGRPUkkRYNieNs5zqby19QTsgsfyq6KBblu uw5zfeO1g+NIL+IpYNiYiDT11Hu8Qpa8ulrHGkflSTunpeRGUesmzw3en6YlrrGyWVbD P77w== 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=TA9SpOWvKEIx+FfJtO5WQCZ74r5jvmaH32Fo3s2on0Y=; b=0wRP28RFn3TQ+RwJHutSTyMewQSq/CYCTf8dEzRjZtgxigtl47pypYKaJomJXJWCsd i0DayjuCvknoFIO9cdriL4ysgCBW8X7LIk74zLFXh+lbN5KqZOi8NOAIt/wC7Wtsmy9i CG/QYhSdgOlqgpvaWMrnDVx/3S9V0UBCTakgfK5EJOXk1PqffSOOZGX8j2+NRuWs+3cI d+MMZz3ALp9GXNOh1i4v908amvYxBqVn2lHAplSUA52WeYXRqQsSiR3xPXee6VCdswz4 f9cENnmuQbBYJloYOuZDS+t9SFAMDyfxC1BWX+C0xdKl7hXHrJgY1KDIeAAvM9661m5J YuGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cI6xza1T; 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 u16si17533062pgi.130.2021.10.25.16.41.24; Mon, 25 Oct 2021 16:41:49 -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=cI6xza1T; 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 S235197AbhJYTa4 (ORCPT + 99 others); Mon, 25 Oct 2021 15:30:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:48094 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235347AbhJYT2M (ORCPT ); Mon, 25 Oct 2021 15:28:12 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2E25C60FE8; Mon, 25 Oct 2021 19:24:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1635189880; bh=SaBFZLhzJ/iBCfoHhVHYZLj56Nj4Rwcz/JC8rNlTGQM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cI6xza1ToHfKIQHC2JLzSdDGqNDS/ORYTd2AMCyXKcPxITEajUI8staJiQxb8IhBI H+8O6zuzD7CtGTprlQFXVoZisXEe82Fyp0a/OCu6rMm39cGj5SvBtU4aemI+MxrNZD GgazAINi8VYO7HKc4KiPpKkgXT6z8OptAbayZcVI= 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.19 15/37] ocfs2: fix data corruption after conversion from inline format Date: Mon, 25 Oct 2021 21:14:40 +0200 Message-Id: <20211025190931.269729378@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211025190926.680827862@linuxfoundation.org> References: <20211025190926.680827862@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 @@ -7048,7 +7048,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; @@ -7057,26 +7057,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; } } @@ -7096,7 +7087,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, @@ -7120,15 +7112,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; @@ -7139,20 +7124,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); @@ -7183,8 +7163,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) @@ -7208,8 +7188,6 @@ out_commit: out: if (data_ac) ocfs2_free_alloc_context(data_ac); -free_pages: - kfree(pages); return ret; }