Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4482374ybx; Mon, 4 Nov 2019 14:09:59 -0800 (PST) X-Google-Smtp-Source: APXvYqzIREAu9bl+UThPNIGM+7nTBHmyKZmx+VMvLgUhyHNHN0G8g94tNkfpFouq7E5GiSSCjw6h X-Received: by 2002:a17:906:198c:: with SMTP id g12mr15185692ejd.247.1572905399645; Mon, 04 Nov 2019 14:09:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905399; cv=none; d=google.com; s=arc-20160816; b=Wi9jrOWvs4TRS5dXRW3iP3QcH8b/0c3Br6VJkbqNanrj6ClK+jMEyrGGnN4VEfTa1H QG0rK9HKp8zmD2sr4ornrHMvjeyTvwJCGcH85JRIGjRL9SnQssK+yBtRdFhl3b6XYhOF +AiWi8KVA6GbtUkZUPlts18UeMDDRdnbbF/DktQ8OxkzwY2+WYTRk8kqpFQwrbGA8t4v kVMM38TQjkHnIKKD4kNkKFcY+g/3U+hdCjGQI0wqxzPdh1tfVlpT4FIeqS+Rkhvplm9c aNrpyP3nFrWu6iWlaCP7OtTgNATicDHWKsjbYaRrfHgezs3j6MwJ6UYlCh/nkfw0eOKh xWbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+ddGf9QXvoVtH88ntMJHKpNS6MSJ1FBOI10IlcjDARw=; b=VkLYb5whO8xHHz2yawxhOQ2MTk9Z2OfT7DBXiq3SSoYMlKi6mrgrZUShdBJPK4avLO HgCkDuGT7fL29VgW7+PUdTr2ITqD15FoiJxHf2Yh+b5Bcs9wRZRXxGaNI36u2AT4s8Y5 Khy7UXnDcac+Z2dEaJMkZ4g6gx7v3PsFXjCMvU3t5nFu+GLitFnD2kpY8/LSo2//YxP+ jJvz+nwblOth5c9SpGFuuLwnsME8BYzq8Yd21t8rDBmoJlvD9fgu0OYXeguUDufgcqR5 RxXVojFFhaPxVrXV8fPhmYudtLChvPJ+3ZYIv8j8LfGSXMP1Z2NyjGANWWhQ5+bUd28c qOiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mfNw8kcJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i19si489607ede.298.2019.11.04.14.09.32; Mon, 04 Nov 2019 14:09:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mfNw8kcJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388250AbfKDWG1 (ORCPT + 99 others); Mon, 4 Nov 2019 17:06:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:38202 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388994AbfKDWGZ (ORCPT ); Mon, 4 Nov 2019 17:06:25 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 93EB6205C9; Mon, 4 Nov 2019 22:06:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572905184; bh=n5uNfqPFplx3jfycm2S+aIa/zP6GuyposK3H0PiCmjo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mfNw8kcJyWucV9W4P63MuwJwhYWJZR+RwPgkZDCP0vWES83R9zE31tSyIN8jbxD/M rPFYqNJCnqKp2K1EsqxnrME0CwxrMR5gWiRGO3XVveIAjjqC9BJ7opemTGmJ4G8p/I RJNScuuYYQmCWYTMwqQ7m/nYNwePiZDFzg4kifxg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jia Guo , Yiwen Jiang , Mark Fasheh , Joel Becker , Junxiao Bi , Joseph Qi , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.3 058/163] ocfs2: clear zero in unaligned direct IO Date: Mon, 4 Nov 2019 22:44:08 +0100 Message-Id: <20191104212144.296491898@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212140.046021995@linuxfoundation.org> References: <20191104212140.046021995@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jia Guo [ Upstream commit 7a243c82ea527cd1da47381ad9cd646844f3b693 ] Unused portion of a part-written fs-block-sized block is not set to zero in unaligned append direct write.This can lead to serious data inconsistencies. Ocfs2 manage disk with cluster size(for example, 1M), part-written in one cluster will change the cluster state from UN-WRITTEN to WRITTEN, VFS(function dio_zero_block) doesn't do the cleaning because bh's state is not set to NEW in function ocfs2_dio_wr_get_block when we write a WRITTEN cluster. For example, the cluster size is 1M, file size is 8k and we direct write from 14k to 15k, then 12k~14k and 15k~16k will contain dirty data. We have to deal with two cases: 1.The starting position of direct write is outside the file. 2.The starting position of direct write is located in the file. We need set bh's state to NEW in the first case. In the second case, we need mapped twice because bh's state of area out file should be set to NEW while area in file not. [akpm@linux-foundation.org: coding style fixes] Link: http://lkml.kernel.org/r/5292e287-8f1a-fd4a-1a14-661e555e0bed@huawei.com Signed-off-by: Jia Guo Reviewed-by: Yiwen Jiang Cc: Mark Fasheh Cc: Joel Becker Cc: Junxiao Bi Cc: Joseph Qi Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/ocfs2/aops.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index a4c905d6b5755..3e0a93e799ea1 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -2139,13 +2139,30 @@ static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, struct ocfs2_dio_write_ctxt *dwc = NULL; struct buffer_head *di_bh = NULL; u64 p_blkno; - loff_t pos = iblock << inode->i_sb->s_blocksize_bits; + unsigned int i_blkbits = inode->i_sb->s_blocksize_bits; + loff_t pos = iblock << i_blkbits; + sector_t endblk = (i_size_read(inode) - 1) >> i_blkbits; unsigned len, total_len = bh_result->b_size; int ret = 0, first_get_block = 0; len = osb->s_clustersize - (pos & (osb->s_clustersize - 1)); len = min(total_len, len); + /* + * bh_result->b_size is count in get_more_blocks according to write + * "pos" and "end", we need map twice to return different buffer state: + * 1. area in file size, not set NEW; + * 2. area out file size, set NEW. + * + * iblock endblk + * |--------|---------|---------|--------- + * |<-------area in file------->| + */ + + if ((iblock <= endblk) && + ((iblock + ((len - 1) >> i_blkbits)) > endblk)) + len = (endblk - iblock + 1) << i_blkbits; + mlog(0, "get block of %lu at %llu:%u req %u\n", inode->i_ino, pos, len, total_len); @@ -2229,6 +2246,9 @@ static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock, if (desc->c_needs_zero) set_buffer_new(bh_result); + if (iblock > endblk) + set_buffer_new(bh_result); + /* May sleep in end_io. It should not happen in a irq context. So defer * it to dio work queue. */ set_buffer_defer_completion(bh_result); -- 2.20.1