From patchwork Mon Aug 5 10:48:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Yuezhang.Mo@sony.com" X-Patchwork-Id: 13753504 Received: from mx07-001d1705.pphosted.com (mx07-001d1705.pphosted.com [185.132.183.11]) (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 85A3C1311AC for ; Mon, 5 Aug 2024 10:49:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=185.132.183.11 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722854947; cv=fail; b=fXvQvGrWHI0ygUFdQATvtXxBkzlE9LxLs8HZ/Os27PJajuw5cT4XCC4NeBdrO1qwOqZteVW4tve+sg93o0CLFVWeelhtDToEECiVAWD4CCyadjGCDfT4zWxe0cmacOiED0+zkinrR+vs2TKQuIPTZX2S6IjqGMjiHB+yVCMN1RU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722854947; c=relaxed/simple; bh=OD6UZKaCwRrFfJJRRZWO6tmH7Q0WAYHjRZZkqSwtSoM=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=dKrHPU95ZI8qlMmWtCtUwAdLT3t0aXVJOeyrLvZoBK1Knw5HzNZjs9Q8ZrAmmenahxL1mNAdiKc4bTXDNspGFZOq1gnhlu+CLTdGLvMQ40MMHcq5HpyOR5LrL1W4gvxk39sugdE45rp+b0YyfGt0s6EAq3MN1SNqPr+LNId5vnc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sony.com; spf=pass smtp.mailfrom=sony.com; dkim=pass (2048-bit key) header.d=sony.com header.i=@sony.com header.b=mBMitNzZ; arc=fail smtp.client-ip=185.132.183.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=sony.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sony.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sony.com header.i=@sony.com header.b="mBMitNzZ" Received: from pps.filterd (m0209325.ppops.net [127.0.0.1]) by mx08-001d1705.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 475AVqnj022436; Mon, 5 Aug 2024 10:48:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sony.com; h=cc :content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=S1; bh=bqJnNHbI6Rvhs1iL3tBU8E41+JYVH GEDkpwskQUgpjM=; b=mBMitNzZlHRR3pqIHLwogSFaqi0xYq3BdjcWn9CorXILE KdZSYIFtI0ljOBIoOoHNVZaZvVEhDHBsqaebahbIlr+xO+Uy8w7TrzPYHhulyqKm 4IB2Qvz+9pLrokpZfSVq21XDKOxfxQKWAbCm67kGUt1gi82FFKBEorA9F/Fu1tPL GiaKBLqfqoX0FXTOuHtkwYHIwYEUlq1QnZmz8ujrJDnQWI6T7ztNrgwfzzHcsw9n e/98m7fderEbzCEVjNVmJi9ZEQ1uPbAk0WI+ayi7H2aV0JjnSSRsT6vs9FxJUaYE +35iRF9urO+YoDpnYEnQEtQVQNEdhwXLXXZ8eUHsg== Received: from hk3pr03cu002.outbound.protection.outlook.com (mail-eastasiaazlp17011031.outbound.protection.outlook.com [40.93.128.31]) by mx08-001d1705.pphosted.com (PPS) with ESMTPS id 40s9sxhpfg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 05 Aug 2024 10:48:41 +0000 (GMT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=g4kDCX45U0PjT5vWOS4TFz0Zz/Xq6acO8bmwbFHsjXQYyDry10ClrTrStbwDXL3yapibkDB8pkmbDgQNsDNz7h11jtIRqVl8lgf53A5PSbTPdu6ZRyMhuaJ2+HrcsG3b6aHQuIWJeVftzDrZ8+pWbBq8x/5qtsH7ORFh5TNafJtSgGJiDFjnLfUBmC7DA4BlrYyh7/FABL08YsR7f5IykYrNwo8gdkbAdtVEZzdHX7uputlRRAM9qPf4nDt8a91S/fTrDJCV2WhtIPydBgruZoatWfRkKqH2/XOSKTKda0k5aBl6M+N9l8Fb8zBCVVT8YANAIEyhUE1EJqOK9IVfzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=bqJnNHbI6Rvhs1iL3tBU8E41+JYVHGEDkpwskQUgpjM=; b=DWNoXfeE7MksMfcjBe9MLJrATXtl8Ncxar3OS0WNgsf+iNP3S9XAjFtISeCTd4mthX2rpqMMyhW9umBqqheOuHrTkdzNyZYsKxV6p8fTar0M2DeVhNpdO/0FhS7b3y/UkZdATOuAd/8DQ5+qMXItBaCR3bdBGTtGPc8VbCbsCuoMiNlgOYQ9jzcIryPn5iis0LSzZTr96XsGVUBmedr3d82nr3npw5GOVYVPYMbD2oae0boRAj8qDwplOK5b0XSpTJ4jUiKEkqClD5U2F5JN2jH1cohwJ2XJXzrMT1pkefr3e8sQgs++aauOtGsnTHXa9ezY3FZHt3DtogLR4BlvCQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=sony.com; dmarc=pass action=none header.from=sony.com; dkim=pass header.d=sony.com; arc=none Received: from PUZPR04MB6316.apcprd04.prod.outlook.com (2603:1096:301:fc::7) by SEYPR04MB5932.apcprd04.prod.outlook.com (2603:1096:101:8d::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.21; Mon, 5 Aug 2024 10:48:34 +0000 Received: from PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::409e:64d3:cee0:7b06]) by PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::409e:64d3:cee0:7b06%4]) with mapi id 15.20.7828.016; Mon, 5 Aug 2024 10:48:33 +0000 From: "Yuezhang.Mo@sony.com" To: "linkinjeon@kernel.org" , "sj1557.seo@samsung.com" CC: "linux-fsdevel@vger.kernel.org" , "Wataru.Aoyama@sony.com" Subject: [PATCH v3] exfat: move extend valid_size into ->page_mkwrite() Thread-Topic: [PATCH v3] exfat: move extend valid_size into ->page_mkwrite() Thread-Index: AQHa5yRRdo/FBEhmQUq8IBWuW2EmUw== Date: Mon, 5 Aug 2024 10:48:33 +0000 Message-ID: Accept-Language: en-US, zh-CN Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PUZPR04MB6316:EE_|SEYPR04MB5932:EE_ x-ms-office365-filtering-correlation-id: 06cf8ae6-6589-445d-30d4-08dcb53c26f9 x-proofpoint-id: d8690225-876f-412f-87c6-a7cb45557a4c x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0;ARA:13230040|366016|1800799024|376014|38070700018; x-microsoft-antispam-message-info: =?iso-8859-1?q?q3vARDJsxnkPgG60YAIoKEVaZZ?= =?iso-8859-1?q?YtpZFSXG6QBZISbaSppljVzDf2SkHcU1yvmflNL3KIWpPp0ZcEVs52rgcxSH?= =?iso-8859-1?q?zbBvf0I0iFE8j5NtBrWs6g0QI53AX1Viyd6txtW9eZbSqKhWfx9gSoKdkQin?= =?iso-8859-1?q?ranSdf0Tfyfa0o6BAbhrGlEeM2B3qCsQzXXcHRgJ3tLLpWDotpR35aALDa6d?= =?iso-8859-1?q?tyKxALX1GgPS5MQLoFTq8GGFgamq1NPGCpAp8+oG2gjGms6O1Yd/nfVLm0Sr?= =?iso-8859-1?q?6Vylo2mRSAJn8hqezFnd5zzUdSivODGUJ5p6I47ksZ88pq+m2TDJSzeUdXmn?= =?iso-8859-1?q?XrHglUP0/UO3TDYvPyhYSWCO54Qsg6N/Qe6e/bwYxaccNqaw3lT14F+gy3Jf?= =?iso-8859-1?q?BjMOW82vWo5aYHKa2AUhS3fAusRQVcWmUfjc9B4EcEZ8jcx1yfe2ho3iGRNP?= =?iso-8859-1?q?SZtHhEaCKt9kfAgLm9dvG0x+vWxYWPEPCKKvJPVENleCB9yqPZuqdZwhoJNz?= =?iso-8859-1?q?vZBEZVhrj9vW5/sSxhwgWrprHeSUsQTiT/71BSayEudm38O/pbx9tH5vydLE?= =?iso-8859-1?q?MvmpavU4Ju6jLF0Z/P8QHFKLuGmWXtLwOzGI7DPaAXYoZfP/Y6dy9DYvM4a7?= =?iso-8859-1?q?E+Geg07Am+PFZ2HaxPNyEldRa8hOb8AgCRR3my/8AgszVN+1RBf5BszAfjCr?= =?iso-8859-1?q?pTpz7MqRGsQp7GMjYXtS72DYwHxYMiPMR5YqZvjYBPYNMwnN+vJWTbkjHdme?= =?iso-8859-1?q?2ND/EMe16b4vwH28nHjkNTrcZv8eBSySBBaVKAkV2AF8PM8TO5A5UiGdjQ0d?= =?iso-8859-1?q?ln9ZPRNldNzXSqs2xP/SREQU+FAoLGZZn8RUT5UnFwBqIRZU/hI4PUWAMWlK?= =?iso-8859-1?q?DKZeSnuWopPwanvnhf/Xp659GCbyNGnITVGqZn13Krp2HTsf4zzeyLMCbopt?= =?iso-8859-1?q?3F2Bpe7WBenkD7DlKfAKD79BiJ0YcBU+D1iOO5LPi1KIygOlcEHeP/BFYJhg?= =?iso-8859-1?q?GCFI8MhmxnYzllKZsd2FhTHCyx2fk+ibnYaQCosjVzxXl8FBR6TIwgaJiaT/?= =?iso-8859-1?q?IHk0HmFUt/FVh5qwIdgJjqpwVPSx5qS91SGHceR96s5/Ai1TbAK1y/+8k3d3?= =?iso-8859-1?q?evqJt3zW/sWvvEA+mpEZiky+33+vtdpAaBv4IBzf4QLf0c1PiPAZLkoStx+i?= =?iso-8859-1?q?ZnpbmeY1BIukp4LVtKZNkTUUvaOnWCZpLkMWgX2ZvMtwGEikOEKsEFHz9t7R?= =?iso-8859-1?q?iEbtaezQHA1VLNrybtLJg3hrk5kjHbf88d441Oq6fO1KIPyDslCVh8wXQUgl?= =?iso-8859-1?q?ZYnK5l+JpoV8htR1YL55tjwcx/Loxycboe9dC//hrAaYkwF/bN7mRkHRzYSh?= =?iso-8859-1?q?gu55CfnjeuiecDcEFi7YR88GAkfTdlBlxu1kn8uYP0e2MW7fHQg+G6KnO75N?= =?iso-8859-1?q?R0U7i7g0sReBBffa0wRZQ27g=3D=3D?= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR04MB6316.apcprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(376014)(38070700018);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?CpKF4W0go8kwgiGdGG4fxsU?= =?iso-8859-1?q?/BbFqwziRAXXjf5FDigxHa1GTDX6KlddhE1uh3ZAitnOMFc9fv+l+fAEpIsn?= =?iso-8859-1?q?0DBCw+H8N098TAOdxxes0Rgrr3VS7C2p3pMVI4QLX+2lgpeM35nrifi0vJuK?= =?iso-8859-1?q?0+OJNsNu0uQm3U5pSYXCfbKQKBYKbODStJXBxaYE89gqF5EId73yJtoMB1qw?= =?iso-8859-1?q?JpjTGMsBWxRKqJ4lsfeLbN7bcotrrtLinFk9pbngkp/+Je5v4WvzybXJwZr2?= =?iso-8859-1?q?/oh8D3JtgUxbDX1Yg12QIVjMu1Q813YwDxFK8LKmHJWCeHm2KWtYeZFn4NMc?= =?iso-8859-1?q?Z0qK16kvtD3LGevmwmQw5tupggraFziU9odFhgN+q7xRjrRBIUirwe6nrxww?= =?iso-8859-1?q?UDI0WZ4/HQhbSXe0mpUvo3I6Kgz994d0AVh+sCdJSj+pl5BeWwESenRiNy81?= =?iso-8859-1?q?mGBhcVt+zFLblgolxU054g5ZBtQIPa2/sAKPWGhi3oddZui9oI32mCUI8ghP?= =?iso-8859-1?q?5Wfq/TEUwAlWZSyGrhxegmH86iCWrszRsDErzPNJITIirJybzGupXQpZHYuj?= =?iso-8859-1?q?SRpV/Na6W50VxLcAJoSrU5qBmDaexeZpRiYZRmrupBC5H1K+3hDnLT1eOrRx?= =?iso-8859-1?q?v2GqDZCIeA/RnpO95yKwkBTDxk21DCfS7Ppwqs1OeJjBO4FeoxBxdLsynMcG?= =?iso-8859-1?q?ppS91QFItleftQhYaY7wunGrGSW1oINj+xD2IBKUCJWAuJjFnAlsf75iqXAL?= =?iso-8859-1?q?+L8KDtJykuqQjao9oM74vfK3DHasYlw79QmgjUh3vc+Pd6beEX9obL5pMIkb?= =?iso-8859-1?q?XpPteQmwMMixaYG620vCwtWnhkPcXnbv6lWpF8WRF/3jy4TycYp+kjw3OjQ1?= =?iso-8859-1?q?6LVaSCV1etnhpwgQS0od4/twDXhZmQFr1G+vMOGFvR76YIqfJijMsUWaKoOS?= =?iso-8859-1?q?avtpcZz+tg4H/Z+aWOQm7QS8ifUg6Xbdu+ArMPOErILOCyEW+fjPnK3Lh+Gc?= =?iso-8859-1?q?0W8HBJ92/4m7Jxbb6ldWCB853OS8o3yylViE+ktMxYbX6pUCOtdhASEkBUgo?= =?iso-8859-1?q?yYLQMJvUg/rOkcNu6oPGEkRvq2MYoaqTrDM/YxIWYkK6qjCNPYC7l2M/5EhG?= =?iso-8859-1?q?vsWmXOdjRz3bmvMRywoRZzSL0vvsTBdH0WD09+WhUV/vS7OAFBs8EkWla4xE?= =?iso-8859-1?q?4nvFxzURsSbg5Cy9YcgRQyu18O0DKRk8YwurtJYSbDfMIF5y/81Sbx/o/paD?= =?iso-8859-1?q?9aREFanEtUmGt3nxEpsIag5CPswDE3eXR9kWAAPyEKaSpGkiZQY7ITGjcQwM?= =?iso-8859-1?q?kyPJd9WW2tRvxYdAuL3QNbT9g051Mmk1nSY10HoE4HnUYX0m93mAziWX7S6R?= =?iso-8859-1?q?xPFUcTqDat3fLs8hlVeHSzOP+pebGj6NgAqpRXu0bPffSifu8sMW6UdQBD/0?= =?iso-8859-1?q?tL9JcDE10ImBOq9L5IfNIAKEsaZjK4VQCCpATcWzOKpIL4lESt3tQwiK17n/?= =?iso-8859-1?q?Jg+pqjyfntYkmlL+RKP2aAaBrdxarvpAvOMDUQCulglTpZn1GuCdItfYQVcM?= =?iso-8859-1?q?TxR3LYJIKZpJxTtwiY1s6NjQbG8APIkGtuOx1cH2/dCAhiAhc/HP7xkiUBnE?= =?iso-8859-1?q?UtJps/JQ5yjASo7RfxxQFxRASRKfDPBnbwiWVE7jS/EuO26NpxhPnuw+NiVk?= =?iso-8859-1?q?=3D?= Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: BdDqc1XW9DdiYH2Jkn3Yls42++NLjoaFcvGTAPS8wP4gdPFKyl0k4bE4yftmWegtCVS6qtvr/LCnujhbvXNKzrpZM9xj6mxHuaxjY5qExgTT7JqsfRaSJa9j8cjqhFAmj89jHdMyRlXUc4xKiZ4GUlwF075nBVELPrrNJ1pLN/a5zBwUOBLiZowv3fA3uVIsQCat7ujv/LekLHqJQAlVn4AgM7W5X3UeHXJhtvkXB7XHwk5rMcIbTtePEM6StEspTQjNjInewBIj10cvHr6bnvkp98YrqLl3u4AP6WtxnRWhvIEBf2GdUxioiOzprf5iwv/QpUsYLDOQG/V64FNw4T4NH1dsj+0xDcHS1ZrDIf05HLjPD3aAV9ZFQh9CMsQ9xF4PcGGnAaez1ooUuotGGXMiE3BPWgU4IJB//28V1Ep5oflZnKK7GwXB/YDaSGKyNF88EB93Ptss5SrmoZ/KSQvaWK9gdcuoR470Fdd2O9hexYH3MwkLlBZXz9pws6XCPlQ5lVK8Z2lE1C3bWaaBVQtWl5LJQCSuROhq62kOOP7o74L8sDym1MqIztc6egIvwR/4QhpretoukS+i48U+ypKT6pNiqidcAipkDX1SLyXzWYMM83ttksHa6gqY/OSy X-OriginatorOrg: sony.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PUZPR04MB6316.apcprd04.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 06cf8ae6-6589-445d-30d4-08dcb53c26f9 X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Aug 2024 10:48:33.6169 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 66c65d8a-9158-4521-a2d8-664963db48e4 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: N/dHhnRZtpv8mq/xxHnjLv6B2NofU5bXMXSdHhczC2Obl2zAggDeUYfMU6CQvO9qh+JEluAGDIOktq6vSyDEHA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEYPR04MB5932 X-Proofpoint-GUID: Gtj_0gLe7LbTz_dmBfE12LP8d3puTeUY X-Proofpoint-ORIG-GUID: Gtj_0gLe7LbTz_dmBfE12LP8d3puTeUY X-Sony-Outbound-GUID: Gtj_0gLe7LbTz_dmBfE12LP8d3puTeUY X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-04_14,2024-08-02_01,2024-05-17_01 It is not a good way to extend valid_size to the end of the mmap area by writing zeros in mmap. Because after calling mmap, no data may be written, or only a small amount of data may be written to the head of the mmap area. This commit moves extending valid_size to exfat_page_mkwrite(). In exfat_page_mkwrite() only extend valid_size to the starting position of new data writing, which reduces unnecessary writing of zeros. If the block is not mapped and is marked as new after being mapped for writing, block_write_begin() will zero the page cache corresponding to the block, so there is no need to call zero_user_segment() in exfat_file_zeroed_range(). And after moving extending valid_size to exfat_page_mkwrite(), the data written by mmap will be copied to the page cache but the page cache may be not mapped to the disk. Calling zero_user_segment() will cause the data written by mmap to be cleared. So this commit removes calling zero_user_segment() from exfat_file_zeroed_range() and renames exfat_file_zeroed_range() to exfat_extend_valid_size(). Signed-off-by: Yuezhang Mo --- Changes for v2: - Remove a unnecessary check from exfat_file_mmap() Changes for v3: - Fix the potential deadlock - Change to use ->valid_size to determine whether exfat_block_page_mkwrite() needs to be called fs/exfat/exfat_fs.h | 1 + fs/exfat/file.c | 92 ++++++++++++++++++++++++++++++++------------- fs/exfat/inode.c | 5 +++ 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index ecc5db952deb..1d207eee3197 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -516,6 +516,7 @@ int __exfat_write_inode(struct inode *inode, int sync); int exfat_write_inode(struct inode *inode, struct writeback_control *wbc); void exfat_evict_inode(struct inode *inode); int exfat_block_truncate_page(struct inode *inode, loff_t from); +int exfat_block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf); /* exfat/nls.c */ unsigned short exfat_toupper(struct super_block *sb, unsigned short a); diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 64c31867bc76..781b4d4dbda1 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -526,32 +526,32 @@ int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) return blkdev_issue_flush(inode->i_sb->s_bdev); } -static int exfat_file_zeroed_range(struct file *file, loff_t start, loff_t end) +static int exfat_extend_valid_size(struct file *file, loff_t new_valid_size) { int err; + loff_t pos; struct inode *inode = file_inode(file); + struct exfat_inode_info *ei = EXFAT_I(inode); struct address_space *mapping = inode->i_mapping; const struct address_space_operations *ops = mapping->a_ops; - while (start < end) { - u32 zerofrom, len; + pos = ei->valid_size; + while (pos < new_valid_size) { + u32 len; struct page *page = NULL; - zerofrom = start & (PAGE_SIZE - 1); - len = PAGE_SIZE - zerofrom; - if (start + len > end) - len = end - start; + len = PAGE_SIZE - (pos & (PAGE_SIZE - 1)); + if (pos + len > new_valid_size) + len = new_valid_size - pos; - err = ops->write_begin(file, mapping, start, len, &page, NULL); + err = ops->write_begin(file, mapping, pos, len, &page, NULL); if (err) goto out; - zero_user_segment(page, zerofrom, zerofrom + len); - - err = ops->write_end(file, mapping, start, len, len, page, NULL); + err = ops->write_end(file, mapping, pos, len, len, page, NULL); if (err < 0) goto out; - start += len; + pos += len; balance_dirty_pages_ratelimited(mapping); cond_resched(); @@ -579,7 +579,7 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) goto unlock; if (pos > valid_size) { - ret = exfat_file_zeroed_range(file, valid_size, pos); + ret = exfat_extend_valid_size(file, pos); if (ret < 0 && ret != -ENOSPC) { exfat_err(inode->i_sb, "write: fail to zero from %llu to %llu(%zd)", @@ -613,26 +613,66 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) return ret; } -static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma) +static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf) { - int ret; + int err; + struct vm_area_struct *vma = vmf->vma; + struct file *file = vma->vm_file; struct inode *inode = file_inode(file); struct exfat_inode_info *ei = EXFAT_I(inode); - loff_t start = ((loff_t)vma->vm_pgoff << PAGE_SHIFT); - loff_t end = min_t(loff_t, i_size_read(inode), - start + vma->vm_end - vma->vm_start); + struct folio *folio = page_folio(vmf->page); + vm_fault_t ret = VM_FAULT_LOCKED; - if ((vma->vm_flags & VM_WRITE) && ei->valid_size < end) { - ret = exfat_file_zeroed_range(file, ei->valid_size, end); - if (ret < 0) { - exfat_err(inode->i_sb, - "mmap: fail to zero from %llu to %llu(%d)", - start, end, ret); - return ret; + sb_start_pagefault(inode->i_sb); + file_update_time(file); + + if (ei->valid_size < folio_pos(folio) + folio_size(folio)) { + if (!inode_trylock(inode)) { + ret = VM_FAULT_RETRY; + goto out; } + + if (ei->valid_size < folio_pos(folio)) { + err = exfat_extend_valid_size(file, folio_pos(folio)); + if (err < 0) { + ret = vmf_fs_error(err); + inode_unlock(inode); + goto out; + } + } + + err = exfat_block_page_mkwrite(vma, vmf); + inode_unlock(inode); + if (err) + ret = vmf_fs_error(err); + } else { + folio_lock(folio); + if (folio->mapping != file->f_mapping) { + folio_unlock(folio); + ret = VM_FAULT_NOPAGE; + goto out; + } + + folio_mark_dirty(folio); + folio_wait_stable(folio); } - return generic_file_mmap(file, vma); +out: + sb_end_pagefault(inode->i_sb); + return ret; +} + +static const struct vm_operations_struct exfat_file_vm_ops = { + .fault = filemap_fault, + .map_pages = filemap_map_pages, + .page_mkwrite = exfat_page_mkwrite, +}; + +static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma) +{ + file_accessed(file); + vma->vm_ops = &exfat_file_vm_ops; + return 0; } const struct file_operations exfat_file_operations = { diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c index dd894e558c91..804de7496a7f 100644 --- a/fs/exfat/inode.c +++ b/fs/exfat/inode.c @@ -564,6 +564,11 @@ int exfat_block_truncate_page(struct inode *inode, loff_t from) return block_truncate_page(inode->i_mapping, from, exfat_get_block); } +int exfat_block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) +{ + return block_page_mkwrite(vma, vmf, exfat_get_block); +} + static const struct address_space_operations exfat_aops = { .dirty_folio = block_dirty_folio, .invalidate_folio = block_invalidate_folio,