Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4327505pxj; Tue, 8 Jun 2021 11:36:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzmjGXsSZrWCtYQr70lgbOZlC1ZG6eIKtIXEuM4P5ZMC7ZUC+p7p6/8aEpBMtUWKuN4jpTW X-Received: by 2002:a17:906:b0c8:: with SMTP id bk8mr10353884ejb.412.1623177360971; Tue, 08 Jun 2021 11:36:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623177360; cv=none; d=google.com; s=arc-20160816; b=IB+ef/ToEanSOT/ra/MPzP/5NAaybPshJb3Jkwpfd+K7i4YGw/8RVdRKCakskThzvV zNA2Kj9Xa970PnDNDaf/sIzI1uV6mT+fduO5ATHtu6wLneVy3/ChJ6BBARd9J5ngzP+B +Vxlo8hlc8MtfGUlv1D9ZVShhhLoimDi1U8r11KvcX0mJzLCNoMBP3iyjEaMM/+0yXac OL3toxvotw3GJzIa118cAjPVIyAWvZBJfGj3jq1L2mPdagBbTPvWbMfI7kc5I1yd+sZE C/BX11d6mCIbsVtGj0lTEUl3wpn2Y3WVzJAHsoBhlF9uk+yjS9OAdW62zL6zHE36d/vN wzKg== 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=eJyueqT99bPXaBx/E3HtXxCAMlj35JbBn9ygPRi6qlA=; b=E4MMyvzygYVZ0SO9vIHE+OeNW7+MlRZ3ZDugfxXaj/XIwm81pRseaOF5TxflQpPTVk 3lf8M71+9uBZ/kE+ymd+4bDIYR4bHXTSCJQgiDLyCXd+59OtvURGjKhP7Y1GePoZ1fyk zJb4SnydBYXFeRzyHs7TF2lsWk0ypj4meiTEvMwkh49TfNJFugHzAKJpNa3G4Qo24uIS mkbjY3CVMQBR2UZMX0q6mrwVSdf3v8EKtalFydBts4/UGvorfuJWRF8p+k9WdRP6nvfK Xi4s/X5JuQB1YUv1fuT18ScG6Jk0pIAHplBwjZm8mt0/xhV7VlSH09Xca1L6d/Zsvore EB1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Vxo5uhEA; 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 y15si335746ede.450.2021.06.08.11.35.37; Tue, 08 Jun 2021 11:36:00 -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=Vxo5uhEA; 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 S234663AbhFHSe4 (ORCPT + 99 others); Tue, 8 Jun 2021 14:34:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:57488 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234415AbhFHSd1 (ORCPT ); Tue, 8 Jun 2021 14:33:27 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A163F613C5; Tue, 8 Jun 2021 18:31:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177073; bh=ecE+wuI6c979x5yVE+UxXl3++LSPOw+3JRb+KM6H2+w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vxo5uhEARV/gjRL6FTF0rgqNbqxMaowcIVctvhL6fuIRZxbX9XrfmKPLpmdTy3sdC owjy2AmIIFQGVSyi6rDKZuo2Kc8QGHL+COR2sKfK/sB3tW4LPDqDbujHYt7xytk3NS sRvDQtfqbuq3VPZoGBgOqVwTVPYmSBEBT3h0BWes= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Junxiao Bi , Joseph Qi , Jan Kara , Mark Fasheh , Joel Becker , Changwei Ge , Gang He , Jun Piao , Andrew Morton , Linus Torvalds Subject: [PATCH 4.14 22/47] ocfs2: fix data corruption by fallocate Date: Tue, 8 Jun 2021 20:27:05 +0200 Message-Id: <20210608175931.209696915@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175930.477274100@linuxfoundation.org> References: <20210608175930.477274100@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: Junxiao Bi commit 6bba4471f0cc1296fe3c2089b9e52442d3074b2e upstream. When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size. Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily. qemu-img convert -p -t none -T none -f qcow2 $qcow_image \ -O qcow2 -o compat=1.1 $qcow_image.conv The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size. fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0 fallocate(11, 0, 2276196352, 65536) = 0 v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html v2: https://lore.kernel.org/linux-fsdevel/20210525093034.GB4112@quack2.suse.cz/T/ Link: https://lkml.kernel.org/r/20210528210648.9124-1-junxiao.bi@oracle.com Signed-off-by: Junxiao Bi Reviewed-by: Joseph Qi Cc: Jan Kara Cc: Mark Fasheh Cc: Joel Becker Cc: Changwei Ge Cc: Gang He Cc: Jun Piao Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/ocfs2/file.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -1862,6 +1862,45 @@ out: } /* + * zero out partial blocks of one cluster. + * + * start: file offset where zero starts, will be made upper block aligned. + * len: it will be trimmed to the end of current cluster if "start + len" + * is bigger than it. + */ +static int ocfs2_zeroout_partial_cluster(struct inode *inode, + u64 start, u64 len) +{ + int ret; + u64 start_block, end_block, nr_blocks; + u64 p_block, offset; + u32 cluster, p_cluster, nr_clusters; + struct super_block *sb = inode->i_sb; + u64 end = ocfs2_align_bytes_to_clusters(sb, start); + + if (start + len < end) + end = start + len; + + start_block = ocfs2_blocks_for_bytes(sb, start); + end_block = ocfs2_blocks_for_bytes(sb, end); + nr_blocks = end_block - start_block; + if (!nr_blocks) + return 0; + + cluster = ocfs2_bytes_to_clusters(sb, start); + ret = ocfs2_get_clusters(inode, cluster, &p_cluster, + &nr_clusters, NULL); + if (ret) + return ret; + if (!p_cluster) + return 0; + + offset = start_block - ocfs2_clusters_to_blocks(sb, cluster); + p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset; + return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS); +} + +/* * Parts of this function taken from xfs_change_file_space() */ static int __ocfs2_change_file_space(struct file *file, struct inode *inode, @@ -1871,7 +1910,7 @@ static int __ocfs2_change_file_space(str { int ret; s64 llen; - loff_t size; + loff_t size, orig_isize; struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); struct buffer_head *di_bh = NULL; handle_t *handle; @@ -1902,6 +1941,7 @@ static int __ocfs2_change_file_space(str goto out_inode_unlock; } + orig_isize = i_size_read(inode); switch (sr->l_whence) { case 0: /*SEEK_SET*/ break; @@ -1909,7 +1949,7 @@ static int __ocfs2_change_file_space(str sr->l_start += f_pos; break; case 2: /*SEEK_END*/ - sr->l_start += i_size_read(inode); + sr->l_start += orig_isize; break; default: ret = -EINVAL; @@ -1963,6 +2003,14 @@ static int __ocfs2_change_file_space(str default: ret = -EINVAL; } + + /* zeroout eof blocks in the cluster. */ + if (!ret && change_size && orig_isize < size) { + ret = ocfs2_zeroout_partial_cluster(inode, orig_isize, + size - orig_isize); + if (!ret) + i_size_write(inode, size); + } up_write(&OCFS2_I(inode)->ip_alloc_sem); if (ret) { mlog_errno(ret); @@ -1979,9 +2027,6 @@ static int __ocfs2_change_file_space(str goto out_inode_unlock; } - if (change_size && i_size_read(inode) < size) - i_size_write(inode, size); - inode->i_ctime = inode->i_mtime = current_time(inode); ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); if (ret < 0)