From patchwork Sat Feb 17 06:38:32 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: 13561264 Received: from mx08-001d1705.pphosted.com (mx08-001d1705.pphosted.com [185.183.30.70]) (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 BCE5B1BDC4 for ; Sat, 17 Feb 2024 06:39:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=185.183.30.70 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708151964; cv=fail; b=fFhFt9s1EtzctKAEoMdUlJkDQAcK0ntPcw4L+ld6boAuBbBREbiW11ynG0lqjdOXLlYQLxdwrAFFiqeLp241/Bq8qM9R9t9RanCnVHWs1DRJEYIokUKsuidK7FxyCwrWe3mC0t5VgD0AyWTpPkmpDXJYj2XjFpPInQbztpzKm2k= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708151964; c=relaxed/simple; bh=do9PFALg5/5fC8MWUOD1+So/MW/EWvCmxTUwObNJbVg=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=qt4ijXcP8y9biT8JRb9nN0pHxfhKd9aDVgYEy1WVEOX0JSxXGSQ8Q+slS7Y8I8zHpeXKK8eH/CVmM3mLrJR13CROENaBDeWb4Pld5xxCHjuNZsxRqln2sS6Z4P6kWiVCJiJvCWaBcJzcpnOjxBMsXW+S6AH1naTWuqLTX8w7kJw= 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=B9KrxIzY; arc=fail smtp.client-ip=185.183.30.70 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="B9KrxIzY" Received: from pps.filterd (m0209319.ppops.net [127.0.0.1]) by mx08-001d1705.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41H6ciFa006812; Sat, 17 Feb 2024 06:38:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sony.com; h=from : to : cc : subject : date : message-id : content-type : mime-version; s=S1; bh=duyxEWRGGkfuQFQE5nrFqcsiPxo0qajJLvPPl4d7bC0=; b=B9KrxIzYCGj0M8Nr+m1LfdpyD72nP46kw960J6Qf27PEhZ/Xkz/9z6nOkrH12zgSnveW ZXhprYN1FOMG7OKxroGNnRn8T9TM96sK2UWFc8NTrDtG6UbwUc/CmnQIIeFbMnESd7mx E81rAqs3tHz/+seXz/oiBygvpTPydOYwetVLTkxbiZB9tgBUUyWDNwVQGdrJZpm6A7bn 24HzKbIHYSIw1BpkK7uZkLd8BvxerHlYyO+ZptCCK5QodT3CdJNqOl6ARM1+mCO8/TS+ 1b1bm6KiuLbG9YQHacNrj+RA+cK3bx6/tXCzp/RnHs+aylSy4GEyMzLqEFdcZX518XRA aQ== Received: from apc01-sg2-obe.outbound.protection.outlook.com (mail-sgaapc01lp2107.outbound.protection.outlook.com [104.47.26.107]) by mx08-001d1705.pphosted.com (PPS) with ESMTPS id 3wan6sr25e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 17 Feb 2024 06:38:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gc1ri41HkUyYejJzEmI0UZ9Wg6NmQvxOWCMFF0+3rH5AMDGG8/7mJ7wuFTVUhcddLqQLQ3VE0sxZfu0pOKZpBcfnNnCMJglbcNlN9pEFHwd+7O4WMlJ9va3jcdcGWZ5DBSZc5CDfRcKss0tnC/VhDPhm0uSutYwsu3j/hetOmNyagjSun6V1fK4+MG2gjsvbVy1tvc5XJYemb//+4zfcewTvWIv8ZzpKNhCJahSeobaUGClWQKneBUzuoaKWyna/ca6j9RN+IDTzHdyhXrh3G3fXCTXlZ9PicBUEPMN0yp+nS7TAqriORoHJsiDGpQAGh8QO+BdYjmAetXr6YKmtRQ== 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=duyxEWRGGkfuQFQE5nrFqcsiPxo0qajJLvPPl4d7bC0=; b=RndKXiDIQ5rpXS+DKJWwYWO+iBvM1nwu60avIXZVqRqCw/kTkUxgHSvm5CnQdrkmTpAtbRXZFy6ByADBm5zkrk/h8VCdYyFM1wevkd/NQkrHcg8dO4Eu0O+bPn2gwSpkOr9zB+Ox3+kmDySKiqPK8OA/3U3+khewjjhA5fyKxvAOW02rRx7HJ9mwhOsYkClT/EVvBQ96D+ntSN60TPzXGoA9lkZWQi9bYnhGfedqWWNIay62GIYMBQMBrGof0EKBa6RgJBdOrUEm4E4ni8Wvx0UoUTlgWw2Wj9jPCx3rRiZoL9I4RULgJbcspk3tGrIIwkF9FYBfgRtcjO3mc5mD9g== 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 SEZPR04MB8358.apcprd04.prod.outlook.com (2603:1096:101:241::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.32; Sat, 17 Feb 2024 06:38:32 +0000 Received: from PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::f0fc:7116:6105:88b2]) by PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::f0fc:7116:6105:88b2%7]) with mapi id 15.20.7292.029; Sat, 17 Feb 2024 06:38:32 +0000 From: "Yuezhang.Mo@sony.com" To: "linkinjeon@kernel.org" , "sj1557.seo@samsung.com" CC: "linux-fsdevel@vger.kernel.org" , "erichong@qnap.com" , "Andy.Wu@sony.com" , "Wataru.Aoyama@sony.com" Subject: [PATCH v1] exfat: fix appending discontinuous clusters to empty file Thread-Topic: [PATCH v1] exfat: fix appending discontinuous clusters to empty file Thread-Index: Adpg0nMFwt+zG1LXR1ylXU/l2ZhIKwAmK47A Date: Sat, 17 Feb 2024 06:38:32 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: yes X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PUZPR04MB6316:EE_|SEZPR04MB8358:EE_ x-ms-office365-filtering-correlation-id: c1403c7a-0be6-4c2c-e9d3-08dc2f830f4b x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 7/4stVjq1m1iT3BlU07pVUoqWN1pImBpWwHgbjcpSHHQ8Rd/CahujiewpWAKdYNcZwRI8XH9NpwptsDFKIUf8FXBpuwTq8OX01ULl+hgv8PDs67mLWCFlSjk9j+7lk3UGcH4xe5I38grXV2RkqF6PHXqssZAMDRdn9wfvrdnVt3ombuJ37EyWmykYzWS0RhwbsT+B1m4BdGDLiuBb9pfi/SMhaYGjKYoYKF1/WWa7qNUDyuGlyguB58MbEXCgi8eS7bMXEvQ2ZbW0P0U88Vy7XkyS84TpXV7GyDrgzf0jxnDAkhquMK5WPxWqIgZqkr0XE/Zl/a7LXu+SCnmggp5OReSptFhUnN7FDYIBX3xsrpsR1bMszULfwnXpkJgeSQX/XgEYiKf3IV6yWjDHZtl/xxwhdffcjRBLL4OYnqHILipygOvZQGeAO3+IVKt5LLvDJP5PzOX6KGA99MnBvmiBd3bks/qUFq0QPQ43UPKqfPVUT9dE0Z8QDtwZi/lYiOHHSA6R6aziEkn0cxgOaUJgE3uhd6Lijc5czrqJwz9CbU/mHLT8qGmQcL3aHThgKbjhrcPn4H5Bs4p+NGM/1IxedpEjk9f9ISGBMXlIOnXwvs= 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:(13230031)(366004)(346002)(136003)(396003)(39860400002)(376002)(230922051799003)(451199024)(186009)(64100799003)(1800799012)(2906002)(5660300002)(8676002)(52536014)(4326008)(8936002)(83380400001)(107886003)(26005)(38100700002)(122000001)(86362001)(33656002)(82960400001)(38070700009)(99936003)(64756008)(54906003)(66446008)(76116006)(66946007)(66556008)(66476007)(110136005)(316002)(6506007)(7696005)(9686003)(966005)(478600001)(71200400001)(55016003)(41300700001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?oCs+mtMimztVyD0Pvba6HgqqHhZQ?= =?utf-8?q?sOWmCo1pNRP+rGTFF03w5Sq/3/0jsDHtqx41Jh3Ulsia/25960CHexfZHalq24Xic?= =?utf-8?q?6EaJQjx8L3es0AE0U5eeiQ9npL1CAq/7RbxH4OaX4QtCFHP/+TPGjPGcF8dZvV/C5?= =?utf-8?q?WElxf/TJM+umK1tbnEBZgZvmg650H4NoVkEVjr8W3r4CIQjDa8DWwxvz4KXWbfgc0?= =?utf-8?q?cvF6+3SzzDLVL7onxbnVNLhEaL8zlWgglP5/buk+gIalLb7me1tem7alECCTULYXh?= =?utf-8?q?lnBpZU6eOe1EQpGsU6YASxBSsR38Eq8hLA/Qa0iyUIHnU6GEh2HTy3KC0DkmQxZsD?= =?utf-8?q?EtB/HI8/WGh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iV4Iwidp85hHtu9pdF5U0YkmhOoM01VrKjkzwERZfezjXG/D?= =?utf-8?q?YnwMA7qjxB12WnY1lwwCUvGI+pyKKxy0oG9BHNQbStyMcM2LAv3yEkCmGhWsq5Mno?= =?utf-8?q?ZGK+bq25vjiXqpllwe2sQoJoezWr7i9itmQIxIsDn1tu0pnGltPyqJwPtplmQwluv?= =?utf-8?q?HoTHvBCJY0BSlJijbd00dzYXMZPaR7E2zAAphBbkOTAHmRM0paETE1FEeDUpD2scb?= =?utf-8?q?kwNU72Mfdw6u31DWS6RCrc2d9tGqqFDNc7lWLxpLUMAHNVEn2yv0hf3FxoCzHih6V?= =?utf-8?q?dPpga8la+5ew?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: US/B343kx4jYpkEMWq2bkSKAkPk20MXEPSbihMp+anasPF/n1NKz3qs/z8KHxx3E/q6P6sDpwQ8gIoU8wPJJ5GKg0E4cQ8sj8iB5FACc8YDhdM2/p4ZP+QQhL9DyABuOfnd/6xBXKV3mHm8Fs4Ew+3WdQQsW/+60HUj/MZFi/9drEewsy/X9ePQXjewrrPYILq7gPMC7hBNuWT4UwehuVB0tTHC3ek6sjilY5GRtSuqA1Bmh37a7IBISknFQI7wEnTVGo7CZeMowpBpXvDtQgyorLzn274/vQ/agSOkoGa7+xtI5Sw0wXUwwPG+NjCwUn838VNr/IaIRehBo8ymVsDSnNmbrLaFSSd+0icnf/wiW8YhoBq9oUbUZ1nk/z6q/7JEDE1Vps3YjQTfuD+QgFL4u3bh71e9bNPIv9wceyXnYdqI508hgcQDhFxjUc/eBdW+j+0tM9KsWgN+ov4fPUDHdEWmYBdLZWeEdm1ouOCg7i4Oua/d3cSKqlH6wlPYcq0SxQdE0TBxseMaCd3+8njsVgN7COGLFcDwQbwYlv785yKNFBiq/ZTkBa6HBH3bEjlg33hKFb+4nFV/HVi29SRL2JZrI79xnMWCR6XGQIBknmB+mSkvbzOCftu9KAGJK 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: c1403c7a-0be6-4c2c-e9d3-08dc2f830f4b X-MS-Exchange-CrossTenant-originalarrivaltime: 17 Feb 2024 06:38:32.3566 (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: zJ4WwynzK2h9snl/4bW95++g1OQpTRt71hWHhWhb65+ghmajMbtyRC5DUtEum+eoM9J7NOGOMXCgFQrbFy8gjA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEZPR04MB8358 X-Proofpoint-GUID: iNPtpOOkX2FxQznt55VDJCytUlNemug3 X-Proofpoint-ORIG-GUID: iNPtpOOkX2FxQznt55VDJCytUlNemug3 X-Proofpoint-UnRewURL: 0 URL was un-rewritten Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Sony-Outbound-GUID: iNPtpOOkX2FxQznt55VDJCytUlNemug3 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-17_03,2024-02-16_01,2023-05-22_02 Eric Hong found that when using ftruncate to expand an empty file, exfat_ent_set() will fail if discontinuous clusters are allocated. The reason is that the empty file does not have a cluster chain, but exfat_ent_set() attempts to append the newly allocated cluster to the cluster chain. In addition, exfat_find_last_cluster() only supports finding the last cluster in a non-empty file. So this commit adds a check whether the file is empty. If the file is empty, exfat_find_last_cluster() and exfat_ent_set() are no longer called as they do not need to be called. Fixes: f55c096f62f1 ("exfat: do not zero the extended part") Reported-by: Eric Hong Closes: https://github.com/namjaejeon/linux-exfat-oot/issues/66 Signed-off-by: Yuezhang Mo --- fs/exfat/file.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index d25a96a148af..cc00f1a7a1e1 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -35,13 +35,18 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) if (new_num_clusters == num_clusters) goto out; - exfat_chain_set(&clu, ei->start_clu, num_clusters, ei->flags); - ret = exfat_find_last_cluster(sb, &clu, &last_clu); - if (ret) - return ret; + if (num_clusters) { + exfat_chain_set(&clu, ei->start_clu, num_clusters, ei->flags); + ret = exfat_find_last_cluster(sb, &clu, &last_clu); + if (ret) + return ret; + + clu.dir = last_clu + 1; + } else { + last_clu = EXFAT_EOF_CLUSTER; + clu.dir = EXFAT_EOF_CLUSTER; + } - clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ? - EXFAT_EOF_CLUSTER : last_clu + 1; clu.size = 0; clu.flags = ei->flags; @@ -51,17 +56,19 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) return ret; /* Append new clusters to chain */ - if (clu.flags != ei->flags) { - exfat_chain_cont_cluster(sb, ei->start_clu, num_clusters); - ei->flags = ALLOC_FAT_CHAIN; - } - if (clu.flags == ALLOC_FAT_CHAIN) - if (exfat_ent_set(sb, last_clu, clu.dir)) - goto free_clu; - - if (num_clusters == 0) + if (num_clusters) { + if (clu.flags != ei->flags) + if (exfat_chain_cont_cluster(sb, ei->start_clu, num_clusters)) + goto free_clu; + + if (clu.flags == ALLOC_FAT_CHAIN) + if (exfat_ent_set(sb, last_clu, clu.dir)) + goto free_clu; + } else ei->start_clu = clu.dir; + ei->flags = clu.flags; + out: inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); /* Expanded range not zeroed, do not update valid_size */