From patchwork Sun Dec 24 10:49:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schubert X-Patchwork-Id: 13504217 Received: from outbound-ip168b.ess.barracuda.com (outbound-ip168b.ess.barracuda.com [209.222.82.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3455020FA; Sun, 24 Dec 2023 10:50:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ddn.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ddn.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ddn.com header.i=@ddn.com header.b="WUFlNtP8" Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2169.outbound.protection.outlook.com [104.47.73.169]) by mx-outbound22-58.us-east-2b.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sun, 24 Dec 2023 10:49:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KngvBWb+PmbuF1XkqH4w2y3FZePt4KkvJYro2ed4qO3BliOMWx2OjZeSvUarKgfITIJKJL/ZWrcGUjDhxnBKPSvxhoAAvP7Xm5DBEtzANYKz188aZ1X4M5OQCmMQzngW1H+QWOcgsK6cMjw96WT279nzJOZsV7ifV3NXBc5XfgtbliiDYAPcX52fCbWm5/bYUZ4DuomkAlNmDXijwlXHjbZgm0YvZw0qrl4Z25hbmm1hf23GLCath72KAL+tkVB1FQwhxXlSnIVTmkKsn1/ZDWfyW0RdA+tncQPYZOx91GZoNm6Yse5wkWlBAXbHp/7WdpAlBixQTe8Q0dNJtZ4OLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=bFmWOcQ5bLEZvSGFgGcd1Y7BisfNVQzPPuC+ciLqEuA=; b=TVMO+O6vsNQzVx9uZm9jpPBu8C5QBNOnu0GzFS8lXAHq87ervCYHFsWsoev8kA6K8ayPRml+s9u3261VydiMFUTp9hm423ygQmRigPnx6NkoBM9SLQP1RU3yTvQ84OnVkzSJlswy7LqBlABQeC3iDpgSm+ya3IYkPguMwtCvgZimKqA/Wib2HJ3D6a5LGiY0eKIEW+xePEWMYs3lxVIJEtZ5gWtJmCx4adEgUpOmxMvzqspg2jXQFs0NHwt7I0MeVee4ZuFoX56MB843A6ihK9JheDjJHXmU7PDHD2lKwoTAtx7Fbs+4CHOaxgdLa94U6N63OG3j5VAr7hETYph2dw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 50.222.100.11) smtp.rcpttodomain=ddn.com smtp.mailfrom=ddn.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ddn.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ddn.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bFmWOcQ5bLEZvSGFgGcd1Y7BisfNVQzPPuC+ciLqEuA=; b=WUFlNtP8yjpBBULbd+JKa8TgWFFFMXQQlznAoQ2NSZKwGcw0cyQY8ANOHRMUnnzzUFxaP3y7xglkjdhpAqrsz2NNiGPSnNhqKECXkEEVs7tT2W11gShKJlsDlKI1TXAcTW9qqksgCY6EDmg+0HVu7X3VZb4b+o+p2xoE62SJdig= Received: from DM6PR13CA0035.namprd13.prod.outlook.com (2603:10b6:5:bc::48) by PH7PR19MB8185.namprd19.prod.outlook.com (2603:10b6:510:2f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.24; Sun, 24 Dec 2023 10:49:45 +0000 Received: from DM6NAM04FT044.eop-NAM04.prod.protection.outlook.com (2603:10b6:5:bc:cafe::35) by DM6PR13CA0035.outlook.office365.com (2603:10b6:5:bc::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.12 via Frontend Transport; Sun, 24 Dec 2023 10:49:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 50.222.100.11) smtp.mailfrom=ddn.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ddn.com; Received-SPF: Pass (protection.outlook.com: domain of ddn.com designates 50.222.100.11 as permitted sender) receiver=protection.outlook.com; client-ip=50.222.100.11; helo=uww-mx01.datadirectnet.com; pr=C Received: from uww-mx01.datadirectnet.com (50.222.100.11) by DM6NAM04FT044.mail.protection.outlook.com (10.13.159.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.14 via Frontend Transport; Sun, 24 Dec 2023 10:49:44 +0000 Received: from localhost (unknown [10.68.0.8]) by uww-mx01.datadirectnet.com (Postfix) with ESMTP id 7A75620C684C; Sun, 24 Dec 2023 03:50:46 -0700 (MST) From: Bernd Schubert To: linux-fsdevel@vger.kernel.org Cc: bernd.schubert@fastmail.fm, miklos@szeredi.hu, dsingh@ddn.com, amir73il@gmail.com, Bernd Schubert , Hao Xu , stable@vger.kernel.org Subject: [PATCH 1/4] fuse: Fix VM_MAYSHARE and direct_io_allow_mmap Date: Sun, 24 Dec 2023 11:49:11 +0100 Message-Id: <20231224104914.49316-2-bschubert@ddn.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231224104914.49316-1-bschubert@ddn.com> References: <20231224104914.49316-1-bschubert@ddn.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM04FT044:EE_|PH7PR19MB8185:EE_ X-MS-Office365-Filtering-Correlation-Id: 388a6288-fc7f-485e-15b6-08dc046e0a76 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AJxU6R1+PlynnzrRI7InELZDpJJ43dw6BwNp/Zy5cObkO6K4iS+i6k8NM4X/RZgmiQxBYGJnWXyTX+nZyLNrBHxRacYfN5NJJApcfsEjK62oRVzJ44+DchDTXqKFNcGaIcSXSnz25FW1nNyuZBT4qfmi3zxV9mC0VFHGUr4F5KNm94/c1P0soGDz5I/uZb3zp3YyBZYew4hsMBFuYt4mqKA4qiunS8dv0TrEzDblhVx1RT84kt80DXjCElOIHF5J1/zr/4xj+ayEURg/4Nqiwef4GOiIFo6sAh98SszUAsUunRqBJCFwgWTQlMDOhrFpDyZfRqx4gXOLBiQikAN6T7+k3beLoCccJAdL0CpRV2n1Gg+YFrOcQYLeMf/Pot+1NndkYLxcGnh9wST7vgkixB7GCLvhW34J/WPvEDasnhZmHVs1GxfsHKSievLwePBsOUbElL7MpQehRBJIYBQ2H2DphBuAa0BisRfiY/EwClAW5ZRTaCdJvQfX2i0TaidSUdJn7ROBdeVIfzMhfUVkKkgkIzLOcS7n72pU+lLcNWQaavL0vAzwrtWcmXwzfTektDGdHZEmac6NSp5cVrGnXZHIUEc8JXkqqe3Fl+iILxT1NuBq3DyesON6GZMmZtLWC/6nPlN7VPfeSQZK0rzMoNXHK2Ets+jv2h1hy9JRY6yn7+3MoTMB98kIcM7KHppOedcvf+xdVfpWkrbskcktC0kpliQiJQotaYsKsSqiJYd+XnOpZzS7o9dtgZ0kwuR4EqNXSnXmjJVY3VuCcNdfG7+lY8KAPZWxinqWQBDLalY= X-Forefront-Antispam-Report: CIP:50.222.100.11;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:uww-mx01.datadirectnet.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39840400004)(346002)(376002)(396003)(136003)(230922051799003)(230173577357003)(230273577357003)(1800799012)(64100799003)(451199024)(186009)(82310400011)(36840700001)(46966006)(2906002)(6666004)(478600001)(4326008)(8936002)(8676002)(6916009)(316002)(54906003)(70586007)(70206006)(5660300002)(86362001)(47076005)(83380400001)(36860700001)(336012)(6266002)(40480700001)(81166007)(36756003)(41300700001)(356005)(2616005)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: YoqzXS4kKwYAEQ8jSrUbHljJmX60VTzCtGM5ZjlYCz7JUDir7tK6LLqhRkRqzb9lQvsII2qZXyVi2ATb3YQQiDgWJqQCBTOHK7TTIBkYJCkKk4UmTfxM0zR7WNBMq1y35qAQJ/CxV2V5RFB6XsF1vgeBa44Vv50wI+ScFA0noEDMdn3Ctk62n++B7lGd/nFWuhX9/psmNpMU4Fwpdl6XoqeZWXb8n6ftNU9mFMiHdPJYc7nFcrHQa0HeydMU0UxMBdrLpYLS3aQOWDKk11C0jc5wdppFPVVORDgZUuenpH+wN7F5rVx6yRI2X2Yy+GAAG1RewX3h89X4eQmXLWRHOJ0ShUqYiWaEtf9BoRGMgUYegoHyzOXkD3/40xETmQsImkiTctDV2S1mflQ2lt0pemRSaRq1Bbfdfi1rWnVp8DDWXofKqd7245tWSufDqbbqbvA4ma6gFzn+xDmFI0KP1IZtzof+vMeVAROhf+Y1KuQe0f1rL+8KP5BIHoGg0oQjqJDv0VOHcwG0Bb2pn7O6uQO8fM/OQ/PoUvTBaOndxNaL2G4PAkkimPtSCAUhOScwx3SCGIT0Cy4oZglfiyBv6Se8o3d8SR21n123AxOx5o0GoHkUzSqaIbvO6ivpymyBBWNRjTfuwkkle8IYk5mD0g== X-OriginatorOrg: ddn.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Dec 2023 10:49:44.7411 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 388a6288-fc7f-485e-15b6-08dc046e0a76 X-MS-Exchange-CrossTenant-Id: 753b6e26-6fd3-43e6-8248-3f1735d59bb4 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=753b6e26-6fd3-43e6-8248-3f1735d59bb4;Ip=[50.222.100.11];Helo=[uww-mx01.datadirectnet.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM04FT044.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR19MB8185 X-BESS-ID: 1703414989-105690-12427-18446-1 X-BESS-VER: 2019.1_20231221.2126 X-BESS-Apparent-Source-IP: 104.47.73.169 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVoamxsZAVgZQ0MIgzcww0TjZ1N jcwtLS2MjY0jQlJcUo0SzNxDjRyDRJqTYWAEFu/ZtBAAAA X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.253033 [from cloudscan21-10.us-east-2b.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.00 using account:ESS124931 scores of KILL_LEVEL=7.0 tests=BSF_BESS_OUTBOUND X-BESS-BRTS-Status: 1 There were multiple issues with direct_io_allow_mmap: - fuse_link_write_file() was missing, resulting in warnings in fuse_write_file_get() and EIO from msync() - "vma->vm_ops = &fuse_file_vm_ops" was not set, but especially fuse_page_mkwrite is needed. The semantics of invalidate_inode_pages2() is so far not clearly defined in fuse_file_mmap. It dates back to commit 3121bfe76311 ("fuse: fix "direct_io" private mmap") Though, as direct_io_allow_mmap is a new feature, that was for MAP_PRIVATE only. As invalidate_inode_pages2() is calling into fuse_launder_folio() and writes out dirty pages, it should be safe to call invalidate_inode_pages2 for MAP_PRIVATE and MAP_SHARED as well. Cc: Hao Xu Cc: stable@vger.kernel.org Fixes: e78662e818f9 ("fuse: add a new fuse init flag to relax restrictions in no cache mode") Signed-off-by: Bernd Schubert Reviewed-by: Amir Goldstein --- fs/fuse/file.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index a660f1f21540a..174aa16407c4b 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -2475,7 +2475,10 @@ static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) invalidate_inode_pages2(file->f_mapping); - return generic_file_mmap(file, vma); + if (!(vma->vm_flags & VM_MAYSHARE)) { + /* MAP_PRIVATE */ + return generic_file_mmap(file, vma); + } } if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) From patchwork Sun Dec 24 10:49:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schubert X-Patchwork-Id: 13504219 Received: from outbound-ip191a.ess.barracuda.com (outbound-ip191a.ess.barracuda.com [209.222.82.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B9EA2565 for ; Sun, 24 Dec 2023 11:23:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ddn.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ddn.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ddn.com header.i=@ddn.com header.b="ePAQgytH" Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by mx-outbound8-113.us-east-2a.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sun, 24 Dec 2023 11:23:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SF64B82m6VyJvTw7675vKeHPKa0vw5C/3vL/0e05X2WSjRM/Xz57F6L/XWoLKk4wyw52ynUQN/eWRfe9YKlj4whOqkKtOUcwVUUHTifvUF8UpgMvauS9Hqk5WMRoiT0lYp7Svz8RtcXn6joZ1ivFCIIYQ5+ArStTnT9604oFGQpwhz966qdHvnAuGMP60gIUll42/fFN8D+yHJJxAO2kMchRuKy4mMt3NlKrVokHT8U/P3ILAt/SjCv7mWjFyMBwh/jAjouIDTP7CNHhA7EMdQX/3SqJe2eQujN7Z1M51Fi9yNuzPg8XJ31l8hGl8XDW9JIUljOxT/IcL0frubxHnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JtUsS3++EPdQQ3bmPIiTTKoLjxFW6sQ8iIUw5Wezmr8=; b=ItD4JhnlCanfe4/Hw2G/IL+0CkJ/FkoC1QAgB1iJPWNMYkiwdJ3DFgzWpzwrd3yyqWQSsTrfgeMaO/+lN1JvYTQpMPzVLLZZLhtQvRVU2r0mS4j4gZU6lUl7Xn03Y7dPlzeNavp5idjpD3mXdQoZA7+r+jqWEzRJ7iNkfbzvW8raayT+VEtIlPJ+pXtcJXun46SFHwLheliJCmVTis7dSd9q2fwBGLTmmpfgKdRCJ2lxr1hTtOV+2qsRxJVW91q0dPLZL8H8n5uhrytmMmNI7+5XG8AiKfwcgAgl4tMqSefDfP9i/NfW/d2XXunBXtmepfmCfMbijKtnKjFfhs9Irw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 50.222.100.11) smtp.rcpttodomain=ddn.com smtp.mailfrom=ddn.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ddn.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ddn.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JtUsS3++EPdQQ3bmPIiTTKoLjxFW6sQ8iIUw5Wezmr8=; b=ePAQgytHDBaB8zbEQXHe2Uz1X8XPyzKWH73StmN8MmL+miyU4yjlG/ucGnC5A5kmYneMLbaEdpyku65VKdHRaR4tvJXiGGNEJzjg4McKApq7cIwcEar2Dm2MNYqxDiz5MholB7f+pXEtVF/st1bs2B4LZuyYCIEQqcfnguY05rA= Received: from BY5PR03CA0008.namprd03.prod.outlook.com (2603:10b6:a03:1e0::18) by LV8PR19MB8371.namprd19.prod.outlook.com (2603:10b6:408:206::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.24; Sun, 24 Dec 2023 10:49:46 +0000 Received: from DM6NAM04FT059.eop-NAM04.prod.protection.outlook.com (2603:10b6:a03:1e0:cafe::21) by BY5PR03CA0008.outlook.office365.com (2603:10b6:a03:1e0::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.24 via Frontend Transport; Sun, 24 Dec 2023 10:49:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 50.222.100.11) smtp.mailfrom=ddn.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ddn.com; Received-SPF: Pass (protection.outlook.com: domain of ddn.com designates 50.222.100.11 as permitted sender) receiver=protection.outlook.com; client-ip=50.222.100.11; helo=uww-mx01.datadirectnet.com; pr=C Received: from uww-mx01.datadirectnet.com (50.222.100.11) by DM6NAM04FT059.mail.protection.outlook.com (10.13.158.176) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.16 via Frontend Transport; Sun, 24 Dec 2023 10:49:45 +0000 Received: from localhost (unknown [10.68.0.8]) by uww-mx01.datadirectnet.com (Postfix) with ESMTP id 7608320C684E; Sun, 24 Dec 2023 03:50:47 -0700 (MST) From: Bernd Schubert To: linux-fsdevel@vger.kernel.org Cc: bernd.schubert@fastmail.fm, miklos@szeredi.hu, dsingh@ddn.com, amir73il@gmail.com, Bernd Schubert Subject: [PATCH 2/4] fuse: Create helper function if DIO write needs exclusive lock Date: Sun, 24 Dec 2023 11:49:12 +0100 Message-Id: <20231224104914.49316-3-bschubert@ddn.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231224104914.49316-1-bschubert@ddn.com> References: <20231224104914.49316-1-bschubert@ddn.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM04FT059:EE_|LV8PR19MB8371:EE_ X-MS-Office365-Filtering-Correlation-Id: b08b90ce-f5f1-4371-adfb-08dc046e0ad0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HJmx0s6VhGeju7siwMIeGv+qcmcA0jpdZM6BpQyaOFNeVYGFdZquKzmstPK+wV3Qg5eWUaA3YOjo9FwPrq2PdNQqG44MVugyLygX4AbtwNBqeOH+8FslsChXGDXoH+3scll5wv6KcD6Pg0QDPY2Rj1/1dIFoCixOjbm685HeiUsjtldGnmJ0NdwtWqVQLfijDaCG3Pr8CadpizXKSN21l7q3d0cg7BS7kLuH7fUQjM4kW2H2xi9LzJ1JFVMJMn39NDZbppFgCgAsGdvpEl6KbryflMwdU/GiWwGrAjD8KPuYehW/lD3OlJ+UtmQZuH6VQ6eIL35buAwDN1+x1kKGQRLkDm1ehzMWNBE7a2WkKmTZRFuGWIiBlld223X/IcVZFTVLKkpRaKMTZLcS8g/VKv8gamWl+8hVBrpia/rs3usEu8lh8AmDh43Nw+bW+7474Y4tkTc5RygBGDpIYp+rma2H0VYG5f1Pcs4KEaKqMCLG2kZ6x+MnGQhyuBOD3mYU+5hbHlMcUN+LWsU6/IROdshIqr145+aLhF4EBmpoFl4Gk1U4mI9pl7ZffHwYlw+t6bJEGe2p0I+W9mGdDOB9yt4LsZKQeCkzjsi9woyysLfiBiOZfzCgJ/lmyBOswjW2Ph3lxOb3p69SH2AsUKIDdBzrnHU5ujGlAKs7WY09I4oUagq4m3bgQud1fbxDEoVb+5CGLVRd56isww54tsqu1fnml9mkJOFHpwT8vKbPww9xz8SuxWtFr7R3cHmxOxrQ X-Forefront-Antispam-Report: CIP:50.222.100.11;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:uww-mx01.datadirectnet.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(376002)(39840400004)(136003)(346002)(396003)(230922051799003)(1800799012)(451199024)(82310400011)(186009)(64100799003)(36840700001)(46966006)(336012)(2616005)(1076003)(26005)(6266002)(6666004)(6916009)(47076005)(36860700001)(5660300002)(4326008)(83380400001)(8676002)(8936002)(41300700001)(2906002)(478600001)(316002)(70206006)(70586007)(36756003)(86362001)(356005)(81166007)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: jEd4kv5kk+4gKQo1bGUJVVT1IaTUoDy/vuYQD+I22+1qj/I9Mz2fowgyK7v8VbwRXYktVULeiEAc3OogzKTE/sZv1zZgbWAzUxH0QuEtwDTiasZgOHimJKjU7yhzJgDU26DMQHAeKseBs1VUFc2oaw7O7ikXtjNEDaYLvdJ3v62gsUmfDKQxXG+cKhj8+jGLa803mXqBKh8/ES7w1zhVO3n2OecJ5rfMIdeIIwM1cfjhfF51E4SbXHKRzFyC+7nKv7KfmtJ7dNKOs3iscVG8S3YMVU27ldRu583ZexYJWDq2xS6TWijUJcBGbcDskIXm86tYKdVJtivkQN9JAqs9HOvQctVeaIe75ZByfHnHm3IKVaC520D8wNNouFIDkC1sRBnhGjwHoGUg97MX8dcXaApvQntOC3qDxl23tfnimNW2G6zc1gAKYrEJCBNSzGQhJe/FZkRSUuud+XXLpoBtJ9OWLpDNLENyjVwq5HtqY3ia14CTpqIQFNtrS9TeH6meA2a2zsR7x+v08MbA33Ur/1DS2NO6eFBpJWnZEUvMMc2jUEcnoV+ewwYa+Xh6NHgoGYcMiXscljqBtC2IcCfqUsP4uvUZhqErgZ5CwZSGk6cBdY7+Hr1xcQDWNsUEr4DMCCRtaJCNR3rmLqdXWUpbmQ== X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Dec 2023 10:49:45.3348 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b08b90ce-f5f1-4371-adfb-08dc046e0ad0 X-MS-Exchange-CrossTenant-Id: 753b6e26-6fd3-43e6-8248-3f1735d59bb4 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=753b6e26-6fd3-43e6-8248-3f1735d59bb4;Ip=[50.222.100.11];Helo=[uww-mx01.datadirectnet.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM04FT059.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR19MB8371 X-OriginatorOrg: ddn.com X-BESS-ID: 1703416983-102161-26459-3586-1 X-BESS-VER: 2019.1_20231221.2126 X-BESS-Apparent-Source-IP: 104.47.66.41 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVsZmlpZAVgZQMNXE1CDFIsXSxD jFJM3CINXUMtXMIi3FPNE8zcDIxNxQqTYWAI3wnmFBAAAA X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.253034 [from cloudscan16-212.us-east-2b.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.00 using account:ESS124931 scores of KILL_LEVEL=7.0 tests=BSF_BESS_OUTBOUND X-BESS-BRTS-Status: 1 This makes the code a bit easier to read and allows to more easily add more conditions when an exclusive lock is needed. Signed-off-by: Bernd Schubert --- fs/fuse/file.c | 64 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 174aa16407c4b..546254aaab19f 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -1298,6 +1298,45 @@ static ssize_t fuse_perform_write(struct kiocb *iocb, struct iov_iter *ii) return res; } +static bool fuse_io_past_eof(struct kiocb *iocb, struct iov_iter *iter) +{ + struct inode *inode = file_inode(iocb->ki_filp); + + return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode); +} + +/* + * @return true if an exclusive lock for direct IO writes is needed + */ +static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from) +{ + struct file *file = iocb->ki_filp; + struct fuse_file *ff = file->private_data; + struct inode *inode = file_inode(iocb->ki_filp); + + /* server side has to advise that it supports parallel dio writes */ + if (!(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES)) + return true; + + /* append will need to know the eventual eof - always needs an + * exclusive lock + */ + if (iocb->ki_flags & IOCB_APPEND) + return true; + + /* combination opf page access and direct-io difficult, shared + * locks actually introduce a conflict. + */ + if (get_fuse_conn(inode)->direct_io_allow_mmap) + return true; + + /* parallel dio beyond eof is at least for now not supported */ + if (fuse_io_past_eof(iocb, from)) + return true; + + return false; +} + static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) { struct file *file = iocb->ki_filp; @@ -1557,26 +1596,12 @@ static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) return res; } -static bool fuse_direct_write_extending_i_size(struct kiocb *iocb, - struct iov_iter *iter) -{ - struct inode *inode = file_inode(iocb->ki_filp); - - return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode); -} - static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) { struct inode *inode = file_inode(iocb->ki_filp); - struct file *file = iocb->ki_filp; - struct fuse_file *ff = file->private_data; struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); ssize_t res; - bool exclusive_lock = - !(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES) || - get_fuse_conn(inode)->direct_io_allow_mmap || - iocb->ki_flags & IOCB_APPEND || - fuse_direct_write_extending_i_size(iocb, from); + bool exclusive_lock = fuse_dio_wr_exclusive_lock(iocb, from); /* * Take exclusive lock if @@ -1590,10 +1615,10 @@ static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) else { inode_lock_shared(inode); - /* A race with truncate might have come up as the decision for - * the lock type was done without holding the lock, check again. + /* + * Previous check was without any lock and might have raced. */ - if (fuse_direct_write_extending_i_size(iocb, from)) { + if (fuse_dio_wr_exclusive_lock(iocb, from)) { inode_unlock_shared(inode); inode_lock(inode); exclusive_lock = true; @@ -2467,7 +2492,8 @@ static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) return fuse_dax_mmap(file, vma); if (ff->open_flags & FOPEN_DIRECT_IO) { - /* Can't provide the coherency needed for MAP_SHARED + /* + * Can't provide the coherency needed for MAP_SHARED * if FUSE_DIRECT_IO_ALLOW_MMAP isn't set. */ if ((vma->vm_flags & VM_MAYSHARE) && !fc->direct_io_allow_mmap) From patchwork Sun Dec 24 10:49:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schubert X-Patchwork-Id: 13504258 Received: from outbound-ip191b.ess.barracuda.com (outbound-ip191b.ess.barracuda.com [209.222.82.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7F7D63A6 for ; Sun, 24 Dec 2023 12:23:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ddn.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ddn.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ddn.com header.i=@ddn.com header.b="Ol5R+qVG" Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2101.outbound.protection.outlook.com [104.47.55.101]) by mx-outbound45-33.us-east-2c.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sun, 24 Dec 2023 12:23:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OuDLisyot9oPPcKmY71GHlmrgVTfDeAlRauxYJH4MdSF0wYUImQxbwzDfRwgRoevY9gROI8bEHZM/nnCr40SGrcfJAPkWtsRtMo7Rq2+EpT0W/g0R24Nssp5Bg2bSOMu3F+/N5Az8I0hKWEgAdvR0Li+OuMgoVSY01aZnpoaw0CYV49MVnmbKd9e6xrLHmPWr/RZgmqydImPyWg2GNVx4/nQ2Wzj2Bkdxz81Dgd3+HL/sqkWnhiB7IB8OAM3FGFvY9z2zl97T3Y8mPgARs0g4ms/iaulDayYG1ZGfbQp4y55BTB9+JRMSCinYaxnPcGxAuQ8Cpo4aulnRv1KUfwA6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Eqev4BKDzXIKjXcBnqvBj4Bw1+YgK0I7XfDRge+9fpY=; b=dY5Joba6tQXDz/5eDbfrcSnb3BBk1imciqS+7qaIVch3Ha69JNv3slK4oSUPW2g5ixWtRoXgKeilb920A0X3DbOLCBeSO2q2vZ5s8mumkBW8jIbnZoBsxYHGd8N7bVwAsjZ2xC2YTsvnMAsLHKWopW1/I/qjB9keoe7tmNYx+tkwYMlUdVWCj9jPxihv+mGj6qWadQFXj+fS2Rg0vt6m/43/XI5xG5aeSW5Ld+IHVXwjWLhaCGq6QyZJCVzckCjnilqMuBn4YEeKiJ+vSWx2qaZdtrcFO8n4bey4oLRS6/XpjGiVsbCGWSRWgGVRgZQoM5RbF5PsUfkEe2arb/WbMw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 50.222.100.11) smtp.rcpttodomain=ddn.com smtp.mailfrom=ddn.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ddn.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ddn.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Eqev4BKDzXIKjXcBnqvBj4Bw1+YgK0I7XfDRge+9fpY=; b=Ol5R+qVGWTFQG/oH0X+1uME6FpW/AUZh2F220dEAYxjW3RAJxFytoqNY7lHvwIQIiaRabHkN3r+cK9Sg35XR0uehL5eZK8FMZxsBIzJ/2d6xgwZmto9r3JrO1BAfveIIpAURGgHR0KzHVedijEWWRItJcwpatyPrU0f/KuaByzE= Received: from BY3PR04CA0006.namprd04.prod.outlook.com (2603:10b6:a03:217::11) by LV2PR19MB5984.namprd19.prod.outlook.com (2603:10b6:408:14e::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.24; Sun, 24 Dec 2023 10:49:47 +0000 Received: from DM6NAM04FT023.eop-NAM04.prod.protection.outlook.com (2603:10b6:a03:217:cafe::fa) by BY3PR04CA0006.outlook.office365.com (2603:10b6:a03:217::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.24 via Frontend Transport; Sun, 24 Dec 2023 10:49:47 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 50.222.100.11) smtp.mailfrom=ddn.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ddn.com; Received-SPF: Pass (protection.outlook.com: domain of ddn.com designates 50.222.100.11 as permitted sender) receiver=protection.outlook.com; client-ip=50.222.100.11; helo=uww-mx01.datadirectnet.com; pr=C Received: from uww-mx01.datadirectnet.com (50.222.100.11) by DM6NAM04FT023.mail.protection.outlook.com (10.13.158.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.16 via Frontend Transport; Sun, 24 Dec 2023 10:49:46 +0000 Received: from localhost (unknown [10.68.0.8]) by uww-mx01.datadirectnet.com (Postfix) with ESMTP id 7014420C684D; Sun, 24 Dec 2023 03:50:48 -0700 (MST) From: Bernd Schubert To: linux-fsdevel@vger.kernel.org Cc: bernd.schubert@fastmail.fm, miklos@szeredi.hu, dsingh@ddn.com, amir73il@gmail.com, Bernd Schubert Subject: [PATCH 3/4] fuse: Add fuse_dio_lock/unlock helper functions Date: Sun, 24 Dec 2023 11:49:13 +0100 Message-Id: <20231224104914.49316-4-bschubert@ddn.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231224104914.49316-1-bschubert@ddn.com> References: <20231224104914.49316-1-bschubert@ddn.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM04FT023:EE_|LV2PR19MB5984:EE_ X-MS-Office365-Filtering-Correlation-Id: 298936c4-9107-4d9a-fee8-08dc046e0b5c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: THIZ/rohXpJWrjPzGHglp1mlfFZJvKXCx8XDaFiuADazXWVa7fg38gXoyBxGmf9Uxczrk6NrgplPTf/CW6PoXwTUX2RVenCYUGG9DKnoi1nY3OMqjHfTRfuFqm0eZCpfo7HBs5v5W0lIIRdvgXOx9KtmPRyQdMReOz2Sxk+14AGL8hh6RiIw4oZR8Im7J2Ogj3+dx7wP9jANXU7YasHBNYJWg2O5LFVb0GEXc5FGfNbqVdAG/1hYuq/kdpEychlMi0enRtdhm6gD4VfldpBki+b8eCfjx0To4ZKl/vqbAAuP1eJ8V4aleCK/22IKfoFIC2HpbCXR+1N+zk7i4V04lkMIIeucDRZcUcoWA9hJqICSqZfjRV/r7D/pNvkX7WLhlfyGJIrIkPB6bYLOTtAJ14P5DTeToomp4Ko4EZ5q7N65WF55luDoleHIJHnXM56VSFJsbAHmEGx/6Y044eHxc25VzxBdoOKvo3KWC1jQklKRZ4xppc4lFq6zWIFNT8PxVXDa0ZAu59vbwX8b29er2nUxYb+Gt6faGx3KDGJz3x9StfHpLmQa07p3uAKGh7qH1CTvztc5isk0Jv09nNNZHi5/rllVDCwFdOgfw3GXjIbXbdm6y5tfP1Hs2mmPDE01gWHAVvfBxO9C551DabEB0R9xXb6t98Szg1RNh3e05pnCDJA553N4YToYmENiI1pmi5l8o0RK8zmcrt6c/e2/NxxfeFcOo/6vlLSgwMK+P/ciwkR7wtIhC3dqUkhCFsSV X-Forefront-Antispam-Report: CIP:50.222.100.11;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:uww-mx01.datadirectnet.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39840400004)(346002)(136003)(396003)(376002)(230922051799003)(64100799003)(186009)(451199024)(82310400011)(1800799012)(36840700001)(46966006)(336012)(83380400001)(47076005)(2616005)(1076003)(6266002)(26005)(36860700001)(8676002)(316002)(6916009)(70586007)(70206006)(5660300002)(4326008)(8936002)(6666004)(478600001)(2906002)(41300700001)(81166007)(356005)(86362001)(36756003)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: q127Ls6MfMNBAQPssM0fQ8ofKbMKZDtko9267WVoC5vK6RsKgOdp5cRqhsPNHTvjNl4VBzRot4KBtiF4SujC5AumjyA+7K2t+rKrhmwyxRZj766HXr6VNOuf49c7wCTu87VghhmJsKoqUHvh9ABVjX7eqvYqgr+v7ITQ6599R/DEOTH2GR4mfbdnc0+P5nCS86anrRBnhpfIdymjXH3/25oRz3wCqQK3R3l5SIKMCpGgGTGwE76NxajpDkvGto7T5CUwnEFmSbyUDBcuiiY7SdZmquJ/jEIvMVnKNPKWikdxjcdATF9NEQ9o8+Z2PpwZYDCTXrXzWTwEaCPBvrWrDXt/7gpY2s4PK3dZL37FJ7O8X33XvbF3NyhdnBhBNolczIFtIUuJtRPXB19D6SRH939uj1/B65REPn+SSO/PLcUuy0RvUZ9EiIkJ/a3FNPPmxmCqvanUM2ahmXtbhzEyXXPCffFcFr2xM4gcJ7O/8eeBD41IWdkhevfKsqUvQA7WBt2LUc56xRUHbJCR9zkMUxNGHEfsZcj2F8uKPYF/IoeUj0c1nbJLhBGsqx046NjMzNdy2NMERLu6sz8SDsvKn0blgNGFTUL9Fuc22FNol4jAnnzUhXgaHgbvHfZJQwQ1KV5ICONqBZqrJdEcj+n+8Q== X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Dec 2023 10:49:46.2544 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 298936c4-9107-4d9a-fee8-08dc046e0b5c X-MS-Exchange-CrossTenant-Id: 753b6e26-6fd3-43e6-8248-3f1735d59bb4 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=753b6e26-6fd3-43e6-8248-3f1735d59bb4;Ip=[50.222.100.11];Helo=[uww-mx01.datadirectnet.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM04FT023.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR19MB5984 X-OriginatorOrg: ddn.com X-BESS-ID: 1703420586-111553-7266-1775-1 X-BESS-VER: 2019.1_20231221.2126 X-BESS-Apparent-Source-IP: 104.47.55.101 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVkYW5pZAVgZQ0CjJwMDY3Dg1xd DIMtEsydDQMDkpJdHM2DTFNC3R2MRSqTYWABAC9HVBAAAA X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.253035 [from cloudscan19-8.us-east-2b.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.00 using account:ESS124931 scores of KILL_LEVEL=7.0 tests=BSF_BESS_OUTBOUND X-BESS-BRTS-Status: 1 So far this is just a helper to remove complex locking logic out of fuse_direct_write_iter. Especially needed by the next patch in the series to that adds the fuse inode cache IO mode and adds in even more locking complexity. Signed-off-by: Bernd Schubert --- fs/fuse/file.c | 61 ++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 546254aaab19f..abc93415ec7e3 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -1337,6 +1337,37 @@ static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from return false; } +static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from, + bool *exclusive) +{ + struct inode *inode = file_inode(iocb->ki_filp); + + *exclusive = fuse_dio_wr_exclusive_lock(iocb, from); + if (*exclusive) { + inode_lock(inode); + } else { + inode_lock_shared(inode); + /* + * Previous check was without inode lock and might have raced, + * check again. + */ + if (fuse_io_past_eof(iocb, from)) { + inode_unlock_shared(inode); + inode_lock(inode); + *exclusive = true; + } + } +} + +static void fuse_dio_unlock(struct inode *inode, bool exclusive) +{ + if (exclusive) { + inode_unlock(inode); + } else { + inode_unlock_shared(inode); + } +} + static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) { struct file *file = iocb->ki_filp; @@ -1601,30 +1632,9 @@ static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) struct inode *inode = file_inode(iocb->ki_filp); struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); ssize_t res; - bool exclusive_lock = fuse_dio_wr_exclusive_lock(iocb, from); - - /* - * Take exclusive lock if - * - Parallel direct writes are disabled - a user space decision - * - Parallel direct writes are enabled and i_size is being extended. - * - Shared mmap on direct_io file is supported (FUSE_DIRECT_IO_ALLOW_MMAP). - * This might not be needed at all, but needs further investigation. - */ - if (exclusive_lock) - inode_lock(inode); - else { - inode_lock_shared(inode); - - /* - * Previous check was without any lock and might have raced. - */ - if (fuse_dio_wr_exclusive_lock(iocb, from)) { - inode_unlock_shared(inode); - inode_lock(inode); - exclusive_lock = true; - } - } + bool exclusive; + fuse_dio_lock(iocb, from, &exclusive); res = generic_write_checks(iocb, from); if (res > 0) { if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { @@ -1635,10 +1645,7 @@ static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) fuse_write_update_attr(inode, iocb->ki_pos, res); } } - if (exclusive_lock) - inode_unlock(inode); - else - inode_unlock_shared(inode); + fuse_dio_unlock(inode, exclusive); return res; } From patchwork Sun Dec 24 10:49:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schubert X-Patchwork-Id: 13504218 Received: from outbound-ip191a.ess.barracuda.com (outbound-ip191a.ess.barracuda.com [209.222.82.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C64B23A8 for ; Sun, 24 Dec 2023 11:22:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=ddn.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ddn.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ddn.com header.i=@ddn.com header.b="GFa8MA/9" Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2169.outbound.protection.outlook.com [104.47.59.169]) by mx-outbound8-113.us-east-2a.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sun, 24 Dec 2023 11:22:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OCoeLnK5Bv/fnTiqJvJBf3cq33HxfZfwIf9nJCxb46w6t9/gcR30I0VzxD89VH99PAm0BaleNU9OKLNuKREYXxvKXukBPT1SAZrmEXxXx2U8qCLeDlBpsm+KLp6On3UaPENg+odgBfxQtUqxFQh2DjU83mV/ZERvIsAdopmZjwB16jSZPKiH5LsvurIBflLijzOxvEraXNam0HkvzLBykz3FYVJNCMfPl0bpdDOiwgcmaXNAP7Y/LbctR9SvOTu5NBFXyKZ8x49kO47nnatYD3N+IBLnxwKYCxV8c7qa3zo0FkYxPPWf65H5GxePX2H+eqHoz36yf+/fwBhMx/C5qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rRIsNDIS7VV+4mxJ/eLurBoiVaBypvLK+Cu89jZBzzM=; b=XsYKTZPezL3xgkpHqqF/hQwH1apRwOB76U2xzpGQfl5STki+eXvEVwPaCgnIBa98fuPWWAd45yEytOf+J79aVYwmFRLlLPTp1+ddby9RDa1wed6xaQOBm5qLkGWV+HipXuOTbeusyMI3NaEObaTW8z/ayIeKLrcrWae5MRGcysE2wrjbVCZiRGzOdyrvpiuzBpchJVXGcIXrRugKbQaPEuFRoZefP2SwIXHgxlqMwk0BKvdY5DstWYv0GoYaJqaG53P/jIopLNHBrpZpGJAMqwkBCh1E3yGBwIn2dVzduBiMKepR9lpLznbRnAR+nh7nOyTJ05zuZL50TSxAaqN5qw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 50.222.100.11) smtp.rcpttodomain=ddn.com smtp.mailfrom=ddn.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ddn.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ddn.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=rRIsNDIS7VV+4mxJ/eLurBoiVaBypvLK+Cu89jZBzzM=; b=GFa8MA/9vlEvaoO6BAMFmIjrbpV0AKF5dPw2b2R8ry+cGsJqwmQqWF//2NRnpRTDND7mQCEU0XIwKSya9J8tfMpIh6+iKmzufm2mJm6N9JRc8COdEmvAb3Sy1ROIQr2j9T2Bx9VmKjUZhiRmoGt0hDDD5e1JMkqvZ1xWz9qHjqc= Received: from MW4P222CA0007.NAMP222.PROD.OUTLOOK.COM (2603:10b6:303:114::12) by IA0PR19MB7728.namprd19.prod.outlook.com (2603:10b6:208:40c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.26; Sun, 24 Dec 2023 10:49:48 +0000 Received: from MW2NAM04FT062.eop-NAM04.prod.protection.outlook.com (2603:10b6:303:114:cafe::98) by MW4P222CA0007.outlook.office365.com (2603:10b6:303:114::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.24 via Frontend Transport; Sun, 24 Dec 2023 10:49:48 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 50.222.100.11) smtp.mailfrom=ddn.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ddn.com; Received-SPF: Pass (protection.outlook.com: domain of ddn.com designates 50.222.100.11 as permitted sender) receiver=protection.outlook.com; client-ip=50.222.100.11; helo=uww-mx01.datadirectnet.com; pr=C Received: from uww-mx01.datadirectnet.com (50.222.100.11) by MW2NAM04FT062.mail.protection.outlook.com (10.13.31.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7135.14 via Frontend Transport; Sun, 24 Dec 2023 10:49:47 +0000 Received: from localhost (unknown [10.68.0.8]) by uww-mx01.datadirectnet.com (Postfix) with ESMTP id 6B01520C684C; Sun, 24 Dec 2023 03:50:49 -0700 (MST) From: Bernd Schubert To: linux-fsdevel@vger.kernel.org Cc: bernd.schubert@fastmail.fm, miklos@szeredi.hu, dsingh@ddn.com, amir73il@gmail.com, Bernd Schubert Subject: [PATCH 4/4] fuse: introduce inode io modes Date: Sun, 24 Dec 2023 11:49:14 +0100 Message-Id: <20231224104914.49316-5-bschubert@ddn.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231224104914.49316-1-bschubert@ddn.com> References: <20231224104914.49316-1-bschubert@ddn.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MW2NAM04FT062:EE_|IA0PR19MB7728:EE_ X-MS-Office365-Filtering-Correlation-Id: cfd00f4a-c414-4445-623b-08dc046e0c41 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sEnoP74ULssvoXE4rb3wusE05GMKXuveA42RxUM+68i1/DNBvCZz+QYvunoZN+D7qrGbNVBr/ldGliDWWt2tP8s3OA4W5bY4mX9AWnwEK11LkAp/DpDU0zj/jO3r4pNh8YdcCYRM204pvNUd3ENkOzUMXuC6V8s32eFj4285UjyR16SKHgNnSPPcm1RJydML240w42fEHYyxwjpMg7iuu83FlkRuyq1TTSOgPsgtabjvjYFGZmBFw3Ex/ZZCe7OauZ/hywPU6fzKhRWDEPYixMn+5Tq9fLs4jem4DgcFn0C/kCy6yf425YAw24L5MMjndmTVkbMC/uYGL1PBYkyBW+ncOeaIguLRT08TExYhcjTda6iHN3Cas0FVVCNu4QaQs7JSrohu7i+LhYRwjXwm9GDVGpM4h+r+WEgh9l86T06u3aU6CHAKqaPcgn4APTUK4P3EYTAoQSc94//zKu9K9d4z+jsuFR1L6c1zE7C14BgH+gOa8H64/6JfALLHo5twWN0UQn2y6eEPUKkXspt7rgHmzraKPSN+oWuneR0Va1ebGDX54e1ACd+Jl1cmT+VyiYXtdg/dEDvZNdcJGPnaSVolMcUR+DWsVyH/G3ujnFKRpKFuvGdVsv72lkenc7ZRSSmC44rLyhkFec74b13bRZuqzlBIpml9als/fHATLKAvIN7TWbHYqSKweEe+1YQc2PRkf4rlCVsRLN+2ziafR5e9e5WBCSDAQpehtQe9TEWeBxN6+q9lp5xWuu/7cKMz X-Forefront-Antispam-Report: CIP:50.222.100.11;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:uww-mx01.datadirectnet.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(346002)(396003)(39840400004)(376002)(136003)(230922051799003)(451199024)(1800799012)(64100799003)(82310400011)(186009)(46966006)(36840700001)(8936002)(86362001)(316002)(6916009)(70206006)(8676002)(70586007)(478600001)(6666004)(30864003)(5660300002)(2906002)(4326008)(41300700001)(1076003)(26005)(40480700001)(83380400001)(36860700001)(336012)(36756003)(6266002)(47076005)(81166007)(356005)(2616005)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: lTDNGNDZuCNl0m9rlNNsVgVp185eRS83qvkEqrpVJ5277jW9SuxzhxUqfhNw2ttKiE00X5f+H+sfkcjLiRm6esIwYSFlab3jfgndcdtiuNIEBW/x+Hm/tBwl80uowa4iiZE6QTjXPROKx7ZRWafB8hj6jRyRXmQFRdoXNLXb3bHbBFiJFVjqCB45KuY3h+CaIO/XyuB/jsLzso9KhUte6xjm53TyGzzjDJtwnwlVwjihY3A2PkU9gu+lYFu2yVz7vrOQNv2dVFa/4S8q3oPK2Wq7TOVS3c3FJkA7pHBab4WBTWm96sSddYK6JcdVs0nFharjYNnENkDXYZeLXjDnRTDCrBBFsy9IHWxlumzCG3c3ZAP/pzKuzSzXhR+RCYgrHEBdqRlBrZCupdKeTrN/CO3vam4sEpQQNgPl4hEfsoGvDiOLcso0uiqZ4h5Wwv/glQu2VnwGJOr2itB9juJdey8v6qCS/gT3j5Y7/8aehjGJy64fCQj01EymVRKvaO46W6yKf4NMMXJu3vVO4qoL3ty7AKbijF3opSETPPNQbhDELc3E5MCZ4kKY7cMFXSWFcD83m+P+Ppbn+niSHHr+UoroDVRgdYn1G/KqBvxmCBBPyfdyw3kfFNu9yiRSdnqpa/j3SkP9ky09eA4BNYy04g== X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Dec 2023 10:49:47.7355 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cfd00f4a-c414-4445-623b-08dc046e0c41 X-MS-Exchange-CrossTenant-Id: 753b6e26-6fd3-43e6-8248-3f1735d59bb4 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=753b6e26-6fd3-43e6-8248-3f1735d59bb4;Ip=[50.222.100.11];Helo=[uww-mx01.datadirectnet.com] X-MS-Exchange-CrossTenant-AuthSource: MW2NAM04FT062.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR19MB7728 X-OriginatorOrg: ddn.com X-BESS-ID: 1703416933-102161-17024-10994-1 X-BESS-VER: 2019.1_20231221.2126 X-BESS-Apparent-Source-IP: 104.47.59.169 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVobGBoaWQGYGUNQ4JdEsNcnQ3C zNLDnNLCk5JTHRwsQ4Jc3AxDzVPMnUXKk2FgA+yUcIQgAAAA== X-BESS-Outbound-Spam-Score: 0.50 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.253034 [from cloudscan23-100.us-east-2b.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.50 BSF_RULE7568M META: Custom Rule 7568M 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.50 using account:ESS124931 scores of KILL_LEVEL=7.0 tests=BSF_RULE7568M, BSF_BESS_OUTBOUND X-BESS-BRTS-Status: 1 From: Amir Goldstein The fuse inode io mode is determined by the mode of its open files/mmaps and parallel dio. - caching io mode - files open in caching mode or mmap on direct_io file - direct io mode - no files open in caching mode and no files mmaped - parallel dio mode - direct io mode with parallel dio in progress We use a new FOPEN_CACHE_IO flag to explicitly mark a file that was open in caching mode. direct_io mmap uses page cache, so first mmap will mark the file as FOPEN_DIRECT_IO|FOPEN_CACHE_IO (i.e. mixed mode) and inode will enter the caching io mode. If the server opens the file with flags FOPEN_DIRECT_IO|FOPEN_CACHE_IO, the inode enters caching io mode already on open. This allows executing parallel dio when inode is not in caching mode even if shared mmap is allowed, but no mmaps have been performed on the inode in question. An mmap on direct_io file now waits for in-progress parallel dio writes, so FOPEN_PARALLEL_DIRECT_WRITES is enabled again by this commit. Open in caching mode falls back to direct io mode if parallel dio is in progress. Signed-off-by: Bernd Schubert Signed-off-by: Amir Goldstein --- fs/fuse/file.c | 160 ++++++++++++++++++++++++++++++++++++-- fs/fuse/fuse_i.h | 76 +++++++++++++++++- include/uapi/linux/fuse.h | 2 + 3 files changed, 230 insertions(+), 8 deletions(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index abc93415ec7e3..fb0b571daaf55 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -104,10 +104,100 @@ static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args, kfree(ra); } +static bool fuse_file_is_direct_io(struct file *file) +{ + struct fuse_file *ff = file->private_data; + + return ff->open_flags & FOPEN_DIRECT_IO || file->f_flags & O_DIRECT; +} + +/* Request access to submit new io to inode via open file */ +static bool fuse_file_io_open(struct file *file, struct inode *inode) +{ + struct fuse_file *ff = file->private_data; + struct fuse_inode *fi = get_fuse_inode(inode); + bool ok = true; + + if (!S_ISREG(inode->i_mode) || FUSE_IS_DAX(inode)) + return true; + + /* Set explicit FOPEN_CACHE_IO flag for file open in caching mode */ + if (!fuse_file_is_direct_io(file)) + ff->open_flags |= FOPEN_CACHE_IO; + + spin_lock(&fi->lock); + /* First caching file open enters caching inode io mode */ + if (ff->open_flags & FOPEN_CACHE_IO) { + ok = fuse_inode_get_io_cache(fi); + if (!ok) { + /* fallback to open in direct io mode */ + pr_debug("failed to open file in caching mode; falling back to direct io mode.\n"); + ff->open_flags &= ~FOPEN_CACHE_IO; + ff->open_flags |= FOPEN_DIRECT_IO; + } + } + spin_unlock(&fi->lock); + + return ok; +} + +/* Request access to submit new io to inode via mmap */ +static int fuse_file_io_mmap(struct fuse_file *ff, struct inode *inode) +{ + struct fuse_inode *fi = get_fuse_inode(inode); + + if (WARN_ON(!S_ISREG(inode->i_mode) || FUSE_IS_DAX(inode))) + return -ENODEV; + + spin_lock(&fi->lock); + /* + * First mmap of direct_io file enters caching inode io mode, blocks + * new parallel dio writes and waits for the in-progress parallel dio + * writes to complete. + */ + if (!(ff->open_flags & FOPEN_CACHE_IO)) { + while (!fuse_inode_get_io_cache(fi)) { + /* + * Setting the bit advises new direct-io writes + * to use an exclusive lock - without it the wait below + * might be forever. + */ + set_bit(FUSE_I_CACHE_IO_MODE, &fi->state); + spin_unlock(&fi->lock); + wait_event_interruptible(fi->direct_io_waitq, + fuse_is_io_cache_allowed(fi)); + spin_lock(&fi->lock); + } + ff->open_flags |= FOPEN_CACHE_IO; + } + spin_unlock(&fi->lock); + + return 0; +} + +/* No more pending io and no new io possible to inode via open/mmapped file */ +static void fuse_file_io_release(struct fuse_file *ff, struct inode *inode) +{ + struct fuse_inode *fi = get_fuse_inode(inode); + + if (!S_ISREG(inode->i_mode) || FUSE_IS_DAX(inode)) + return; + + spin_lock(&fi->lock); + /* Last caching file close exits caching inode io mode */ + if (ff->open_flags & FOPEN_CACHE_IO) + fuse_inode_put_io_cache(fi); + spin_unlock(&fi->lock); +} + static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir) { if (refcount_dec_and_test(&ff->count)) { struct fuse_args *args = &ff->release_args->args; + struct inode *inode = ff->release_args->inode; + + if (inode) + fuse_file_io_release(ff, inode); if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) { /* Do nothing when client does not implement 'open' */ @@ -199,6 +289,9 @@ void fuse_finish_open(struct inode *inode, struct file *file) struct fuse_file *ff = file->private_data; struct fuse_conn *fc = get_fuse_conn(inode); + /* The file open mode determines the inode io mode */ + fuse_file_io_open(file, inode); + if (ff->open_flags & FOPEN_STREAM) stream_open(inode, file); else if (ff->open_flags & FOPEN_NONSEEKABLE) @@ -1305,6 +1398,37 @@ static bool fuse_io_past_eof(struct kiocb *iocb, struct iov_iter *iter) return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode); } +/* + * New parallal dio allowed only if inode is not in caching mode and + * denies new opens in caching mode. + */ +static bool fuse_file_shared_dio_start(struct inode *inode) +{ + struct fuse_inode *fi = get_fuse_inode(inode); + bool ok; + + if (WARN_ON(!S_ISREG(inode->i_mode) || FUSE_IS_DAX(inode))) + return false; + + spin_lock(&fi->lock); + ok = fuse_inode_deny_io_cache(fi); + spin_unlock(&fi->lock); + return ok; +} + +/* Allow new opens in caching mode after last parallel dio end */ +static void fuse_file_shared_dio_end(struct inode *inode) +{ + struct fuse_inode *fi = get_fuse_inode(inode); + bool allow_cached_io; + + spin_lock(&fi->lock); + allow_cached_io = fuse_inode_allow_io_cache(fi); + spin_unlock(&fi->lock); + if (allow_cached_io) + wake_up(&fi->direct_io_waitq); +} + /* * @return true if an exclusive lock for direct IO writes is needed */ @@ -1313,6 +1437,7 @@ static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from struct file *file = iocb->ki_filp; struct fuse_file *ff = file->private_data; struct inode *inode = file_inode(iocb->ki_filp); + struct fuse_inode *fi = get_fuse_inode(inode); /* server side has to advise that it supports parallel dio writes */ if (!(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES)) @@ -1324,11 +1449,9 @@ static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from if (iocb->ki_flags & IOCB_APPEND) return true; - /* combination opf page access and direct-io difficult, shared - * locks actually introduce a conflict. - */ - if (get_fuse_conn(inode)->direct_io_allow_mmap) - return true; + /* shared locks are not allowed with parallel page cache IO */ + if (test_bit(FUSE_I_CACHE_IO_MODE, &fi->state)) + return false; /* parallel dio beyond eof is at least for now not supported */ if (fuse_io_past_eof(iocb, from)) @@ -1349,9 +1472,11 @@ static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from, inode_lock_shared(inode); /* * Previous check was without inode lock and might have raced, - * check again. + * check again. fuse_file_shared_dio_start() should be performed + * only after taking shared inode lock. */ - if (fuse_io_past_eof(iocb, from)) { + if (fuse_io_past_eof(iocb, from) || + !fuse_file_shared_dio_start(inode)) { inode_unlock_shared(inode); inode_lock(inode); *exclusive = true; @@ -1364,6 +1489,7 @@ static void fuse_dio_unlock(struct inode *inode, bool exclusive) if (exclusive) { inode_unlock(inode); } else { + fuse_file_shared_dio_end(inode); inode_unlock_shared(inode); } } @@ -2493,11 +2619,16 @@ static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) { struct fuse_file *ff = file->private_data; struct fuse_conn *fc = ff->fm->fc; + int rc; /* DAX mmap is superior to direct_io mmap */ if (FUSE_IS_DAX(file_inode(file))) return fuse_dax_mmap(file, vma); + /* + * FOPEN_DIRECT_IO handling is special compared to O_DIRECT, + * as does not allow MAP_SHARED mmap without FUSE_DIRECT_IO_ALLOW_MMAP. + */ if (ff->open_flags & FOPEN_DIRECT_IO) { /* * Can't provide the coherency needed for MAP_SHARED @@ -2508,10 +2639,23 @@ static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) invalidate_inode_pages2(file->f_mapping); + /* + * First mmap of direct_io file enters caching inode io mode. + * Also waits for parallel dio writers to go into serial mode + * (exclusive instead of shared lock). + */ + rc = fuse_file_io_mmap(ff, file_inode(file)); + if (rc) + return rc; + if (!(vma->vm_flags & VM_MAYSHARE)) { /* MAP_PRIVATE */ return generic_file_mmap(file, vma); } + } else if (file->f_flags & O_DIRECT) { + rc = fuse_file_io_mmap(ff, file_inode(file)); + if (rc) + return rc; } if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) @@ -3280,7 +3424,9 @@ void fuse_init_file_inode(struct inode *inode, unsigned int flags) INIT_LIST_HEAD(&fi->write_files); INIT_LIST_HEAD(&fi->queued_writes); fi->writectr = 0; + fi->iocachectr = 0; init_waitqueue_head(&fi->page_waitq); + init_waitqueue_head(&fi->direct_io_waitq); fi->writepages = RB_ROOT; if (IS_ENABLED(CONFIG_FUSE_DAX)) diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 1df83eebda927..5774585f6de3e 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -111,7 +111,7 @@ struct fuse_inode { u64 attr_version; union { - /* Write related fields (regular file only) */ + /* read/write io cache (regular file only) */ struct { /* Files usable in writepage. Protected by fi->lock */ struct list_head write_files; @@ -123,9 +123,15 @@ struct fuse_inode { * (FUSE_NOWRITE) means more writes are blocked */ int writectr; + /** Number of files/maps using page cache */ + int iocachectr; + /* Waitq for writepage completion */ wait_queue_head_t page_waitq; + /* waitq for direct-io completion */ + wait_queue_head_t direct_io_waitq; + /* List of writepage requestst (pending or sent) */ struct rb_root writepages; }; @@ -187,6 +193,8 @@ enum { FUSE_I_BAD, /* Has btime */ FUSE_I_BTIME, + /* Wants or already has page cache IO */ + FUSE_I_CACHE_IO_MODE, }; struct fuse_conn; @@ -1349,6 +1357,72 @@ int fuse_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry, struct fileattr *fa); /* file.c */ +/* + * Request an open in caching mode. + * Return true if in caching mode. + */ +static inline bool fuse_inode_get_io_cache(struct fuse_inode *fi) +{ + assert_spin_locked(&fi->lock); + if (fi->iocachectr < 0) + return false; + fi->iocachectr++; + if (fi->iocachectr == 1) + set_bit(FUSE_I_CACHE_IO_MODE, &fi->state); + + return true; +} + +/* + * Release an open in caching mode. + * Return true if no more files open in caching mode. + */ +static inline bool fuse_inode_put_io_cache(struct fuse_inode *fi) +{ + assert_spin_locked(&fi->lock); + if (WARN_ON(fi->iocachectr <= 0)) + return false; + + if (--fi->iocachectr == 0) { + clear_bit(FUSE_I_CACHE_IO_MODE, &fi->state); + return true; + } + + return false; +} + +/* + * Requets to deny new opens in caching mode. + * Return true if denying new opens in caching mode. + */ +static inline bool fuse_inode_deny_io_cache(struct fuse_inode *fi) +{ + assert_spin_locked(&fi->lock); + if (fi->iocachectr > 0) + return false; + fi->iocachectr--; + return true; +} + +/* + * Release a request to deny open in caching mode. + * Return true if allowing new opens in caching mode. + */ +static inline bool fuse_inode_allow_io_cache(struct fuse_inode *fi) +{ + assert_spin_locked(&fi->lock); + if (WARN_ON(fi->iocachectr >= 0)) + return false; + return ++(fi->iocachectr) == 0; +} + +/* + * Return true if allowing new opens in caching mode. + */ +static inline bool fuse_is_io_cache_allowed(struct fuse_inode *fi) +{ + return READ_ONCE(fi->iocachectr) >= 0; +} struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, unsigned int open_flags, bool isdir); diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index e7418d15fe390..66a4bd8d767d4 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -353,6 +353,7 @@ struct fuse_file_lock { * FOPEN_STREAM: the file is stream-like (no file position at all) * FOPEN_NOFLUSH: don't flush data cache on close (unless FUSE_WRITEBACK_CACHE) * FOPEN_PARALLEL_DIRECT_WRITES: Allow concurrent direct writes on the same inode + * FOPEN_CACHE_IO: using cache for this open file (incl. mmap on direct_io) */ #define FOPEN_DIRECT_IO (1 << 0) #define FOPEN_KEEP_CACHE (1 << 1) @@ -361,6 +362,7 @@ struct fuse_file_lock { #define FOPEN_STREAM (1 << 4) #define FOPEN_NOFLUSH (1 << 5) #define FOPEN_PARALLEL_DIRECT_WRITES (1 << 6) +#define FOPEN_CACHE_IO (1 << 7) /** * INIT request/reply flags