From patchwork Wed Jun 28 01:52:45 2023 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: 13295134 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D072EB64DC for ; Wed, 28 Jun 2023 01:53:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231167AbjF1BxH (ORCPT ); Tue, 27 Jun 2023 21:53:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231163AbjF1BxG (ORCPT ); Tue, 27 Jun 2023 21:53:06 -0400 Received: from mx08-001d1705.pphosted.com (mx08-001d1705.pphosted.com [185.183.30.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77FF02111 for ; Tue, 27 Jun 2023 18:53:04 -0700 (PDT) Received: from pps.filterd (m0209320.ppops.net [127.0.0.1]) by mx08-001d1705.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35RN1xoZ006382; Wed, 28 Jun 2023 01:52:49 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sony.com; h=from : to : cc : subject : date : message-id : mime-version : content-type : content-transfer-encoding; s=S1; bh=Ni3R7osGpPeMIBApZcw0D3jOBzZVUQ1CIoCb7NuFltI=; b=Z07ndWh7+Zdnon4PSuOdCitZC00UYRitbPn7VcZlSYjb3MBTLiE2VfRvF0cnTpbPr0o5 93pPwO/MY4VTI6JZG+It+cYIYj+6bIFKvnggG2xOHWcEin1TJ5sTHGJo+iMzLy+zRnBm EVdU2n6F1FjWjd/vHZXSfqNNrkJPWTxIBbJKD+aOffewopbAVchOLdOoxyLoDhTXCd/p F9xrvOqs1R0yHw0RveSNDfXwkQAVTP4BnuplVvhJZt6+XeYlaKi4WQxVJaHXnjYgl4JZ iAm3/I99duxzMpxK+CmMz/O/4J2svpWgZyHHmyeIXsV3XBpUoSjbpWGf8UYrLOvynY9v Hg== Received: from apc01-psa-obe.outbound.protection.outlook.com (mail-psaapc01lp2040.outbound.protection.outlook.com [104.47.26.40]) by mx08-001d1705.pphosted.com (PPS) with ESMTPS id 3rdpcm3kb3-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 28 Jun 2023 01:52:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YbeVEpoL8AYjbuPfnnF7/YK/3FOEwZCECkj+PlcetNHimHb83VAK1lcKyV7yGbSrOGLFv9h1V5r3wkecgMgIlYnfyxA6pxmOpjmZL94yhxdlZxUtjWCZbc97SsL2FRR6fu2fqZFUUISGkndI8x6DLxnr6LkyYwiZqhKdUI4wtTA5GFbmDgFzqePmEcUaSpRAeLCoP+9FkJFHZBT08Vp15O05vrXymvrS/noSkxXkbFyWG7wXzqAGWqHT2VC+ul3t77YfqUyzt881+TlsMvW7dw3DMYjcjUWThEBxZqIt8SSKhV12QAfc9lF6sijTR9OIjyauMYBKePWzqi1k/xHg9A== 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=Ni3R7osGpPeMIBApZcw0D3jOBzZVUQ1CIoCb7NuFltI=; b=lzvGZ8Nu/j1+xWCgQ2F74OzLdDAyR0g4pLpgHd8PnS0D6pu/N2hIiSn371Avgzq0W/tM1yLXlMc6TClKuruXm2kKYNIB2FkzZ+dZJwpCDaZVfeSok6OWeAJNAK+sirXG9e5rTGjPLYH0unv/JkNm6610LCfgLZCl4jCVjMdieYnRsWDwNKT6K/zrgVkWnuMvoQfDztsSolYDfkW11YdKFMj9jDgowhkPbH8jyxOmZXc+U3/fC+M+wTBTyETsHNWHJNF/Qv9dTLn2dVGAgatjMdoEB4ib+7YuIRVy7iZ2+qX3wLF/Bh9lGFRa/4z0YwHsPbotGgqv2L5T74Q3Pq17tA== 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 PSAPR04MB4389.apcprd04.prod.outlook.com (2603:1096:301:3f::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6544.15; Wed, 28 Jun 2023 01:52:45 +0000 Received: from PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::ce2d:a9dc:4955:5275]) by PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::ce2d:a9dc:4955:5275%5]) with mapi id 15.20.6521.026; Wed, 28 Jun 2023 01:52:45 +0000 From: "Yuezhang.Mo@sony.com" To: "linkinjeon@kernel.org" , "sj1557.seo@samsung.com" CC: "linux-fsdevel@vger.kernel.org" , "Andy.Wu@sony.com" , "Wataru.Aoyama@sony.com" Subject: [PATCH v2 1/2] exfat: change to get file size from DataLength Thread-Topic: [PATCH v2 1/2] exfat: change to get file size from DataLength Thread-Index: AdmpYuJ0V9XHhCJETaqF4XX9XJpueg== Date: Wed, 28 Jun 2023 01:52:45 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PUZPR04MB6316:EE_|PSAPR04MB4389:EE_ x-ms-office365-filtering-correlation-id: f273b820-30ba-4096-145a-08db777a5e75 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: oIypL9Gf7qLZChNTnDt63SKl5B/Ju9up9yFte+3RQmC9HNi5z2OxKBHyJAQSaSLMSbsp4+uchud1g/otNxcY/UQ8FCcOK2PlfqxBlRVtjwkaQb+ciQafGA6toUdRUQYxc+TdySZUiMcffPImFwb2F1G+UvgO1lp4TXIAoABV1Bubck/DfCRiBM/arquydd/z29hZz3TxLlOIp1FU0sUZOMDmmcE2oQucVI+SFyuHtA7zQ3A2gH9w95K5xL9a5wSIakU7u9jX12A8q7zFrZfGs9C542gZ9P/d+GKs1676/K3utj0L7jQgc5UhPsBCQmAAqbUEV5uNrTDdeRZ+3p1taNlXRK62eipRhKfFHQrG143OK8vKc9GQ4x3wYfKCZ2F2XegXNFd8prumaubj34aRK9CTBssxB+g8wlqy1hLAJmVHB5fUC7beAjf2uPD+5oD1C2fIWR1AUbHP1hY8XqgId25V8QPPgbETSrqTpjiHImNBcfaMrwNBV1Ey7rLEToRQ1tYxF1XCcMavsib29OzPaWcaqcE/g2+ZV+zWXfuz6aOKliRGtkWHEVwnPKGWApNWuO1PGTTgI5qUsjy1fgcoB3xi5db2OEE4myKXFl50ftM+8B+5hqmG8JyEt+VmW/sG 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:(13230028)(4636009)(396003)(136003)(346002)(366004)(376002)(39860400002)(451199021)(5660300002)(66946007)(41300700001)(52536014)(76116006)(64756008)(66446008)(66476007)(66556008)(8936002)(8676002)(4326008)(316002)(478600001)(83380400001)(38070700005)(55016003)(26005)(6506007)(2906002)(33656002)(30864003)(107886003)(9686003)(186003)(54906003)(110136005)(122000001)(38100700002)(86362001)(82960400001)(71200400001)(7696005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange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cvYi5j4QQm/jyPCeMA52LUP1Dt6VO3ONBaRiK7KfwrA3rq1iordJCCZAioV6bzLimjKkiSO/70QOxc2ef+yKcddAj8DN63LEkCNjNj4iWk6zkfubbj38fwiw8IrvwLY447vbb3QPHdF740Y2EP217S6RTGGSVijT9wYo5rz92IsSFZhgAvGeJl5kMOJsrwd7fMj+HWtxVCCwTAQIp26P47idtapjD3IqD0BWisJu0VO8o9nokY4Vqkt//OVQlpWzPh3Eu8FpzajcZbr0L9Hi0RsnqNjVRYj1DGSwktNF/J7VNshLOLdE5WqWjyjH0nd26RuDGHgqy/pjKOEC6uoLH80AI9x25j/mtD40aOzpqfPIQz1WWHYCzl+jnIJDCYi/t16h6IvfAySwLak3oRrFq7JfTGA/ndhwvedh4vHkc3ZJxLdfhlY2HRgv28OZdOxu69R1Z2kXRXUZp3ji6gZHnF6x4hjuCNxysjOyenJgDoNGJqzx25NUMSE0e+ku7Pmy5AApFowOME64IypELuWr9tbn8ki8bpS8kXWxyE7NVpBNDXcP814AKQ9wQ6gXL/3jHthx4dHNQ1Eag7G7T7PK1ngd9AhAQ76+puWQx11BHmJwISoeHlMDXFXGcaKYKHQAzc8wx0UXb5v4AAay5M03kmJcr1p5PJhgOrnqja6omt3lRlVpiyTtxTgLNGQjl9ca3DKhAWZw== 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: f273b820-30ba-4096-145a-08db777a5e75 X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Jun 2023 01:52:45.7042 (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: aPu5B+CtZq7opw3Zb4oYsIxr/kghjWgPc2E5VynQLYbJ8ym3g0bO6k0dtWXsspbSVDRWODHQqoc+z3pgmqFMDQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PSAPR04MB4389 X-Proofpoint-ORIG-GUID: SutzyGj7iNElMhXOU052r1m7T8eU2qzD X-Proofpoint-GUID: SutzyGj7iNElMhXOU052r1m7T8eU2qzD X-Sony-Outbound-GUID: SutzyGj7iNElMhXOU052r1m7T8eU2qzD X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-06-27_16,2023-06-27_01,2023-05-22_02 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org In stream extension directory entry, the ValidDataLength field describes how far into the data stream user data has been written, and the DataLength field describes the file size. Signed-off-by: Yuezhang Mo Reviewed-by: Andy Wu Reviewed-by: Aoyama Wataru --- fs/exfat/exfat_fs.h | 2 + fs/exfat/file.c | 133 +++++++++++++++++++++++++++++++++++++++++++- fs/exfat/inode.c | 92 +++++++++++++++++++++++++----- fs/exfat/namei.c | 7 ++- 4 files changed, 217 insertions(+), 17 deletions(-) diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index 729ada9e26e8..8d6248367433 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -208,6 +208,7 @@ struct exfat_dir_entry { unsigned char flags; unsigned short attr; loff_t size; + loff_t valid_size; unsigned int num_subdirs; struct timespec64 atime; struct timespec64 mtime; @@ -317,6 +318,7 @@ struct exfat_inode_info { loff_t i_size_aligned; /* on-disk position of directory entry or 0 */ loff_t i_pos; + loff_t valid_size; /* hash by i_location */ struct hlist_node i_hash_fat; /* protect bmap against truncate */ diff --git a/fs/exfat/file.c b/fs/exfat/file.c index e99183a74611..8cd14bc16857 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "exfat_raw.h" #include "exfat_fs.h" @@ -23,6 +24,7 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) return err; inode->i_ctime = inode->i_mtime = current_time(inode); + EXFAT_I(inode)->valid_size = size; mark_inode_dirty(inode); if (!IS_SYNC(inode)) @@ -143,6 +145,9 @@ int __exfat_truncate(struct inode *inode) ei->start_clu = EXFAT_EOF_CLUSTER; } + if (i_size_read(inode) < ei->valid_size) + ei->valid_size = i_size_read(inode); + if (ei->type == TYPE_FILE) ei->attr |= ATTR_ARCHIVE; @@ -379,15 +384,139 @@ 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) +{ + int err; + 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; + struct page *page; + + zerofrom = start & (PAGE_SIZE - 1); + len = PAGE_SIZE - zerofrom; + if (start + len > end) + len = end - start; + + err = ops->write_begin(file, mapping, start, 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); + if (err < 0) + goto out; + start += len; + + balance_dirty_pages_ratelimited(mapping); + cond_resched(); + } + + ei->valid_size = end; + mark_inode_dirty(inode); + +out: + return err; +} + +static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) +{ + ssize_t ret; + struct file *file = iocb->ki_filp; + struct inode *inode = file_inode(file); + struct exfat_inode_info *ei = EXFAT_I(inode); + loff_t pos = iocb->ki_pos; + loff_t valid_size; + + inode_lock(inode); + + valid_size = ei->valid_size; + + ret = generic_write_checks(iocb, iter); + if (ret < 0) + goto unlock; + + if (pos > valid_size) { + ret = exfat_file_zeroed_range(file, valid_size, pos); + if (ret < 0 && ret != -ENOSPC) { + exfat_err(inode->i_sb, + "write: fail to zero from %llu to %llu(%ld)", + valid_size, pos, ret); + } + if (ret < 0) + goto unlock; + } + + ret = __generic_file_write_iter(iocb, iter); + if (ret < 0) + goto unlock; + + if (pos + ret > i_size_read(inode)) + i_size_write(inode, pos + ret); + + if (pos + ret > ei->valid_size) + ei->valid_size = pos + ret; + + /* + * If valid_size is extended with sector-aligned length in + * exfat_get_block(), set to the writren length. + */ + if (i_size_read(inode) < ei->valid_size) + ei->valid_size = i_size_read(inode); + + mark_inode_dirty(inode); + inode_unlock(inode); + + if (pos > valid_size && iocb_is_dsync(iocb)) { + ssize_t err = vfs_fsync_range(file, valid_size, pos - 1, + iocb->ki_flags & IOCB_SYNC); + if (err < 0) + return err; + } + + return generic_write_sync(iocb, ret); + +unlock: + inode_unlock(inode); + + return ret; +} + +static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma) +{ + int ret; + 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); + + 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; + } + } + + return generic_file_mmap(file, vma); +} + const struct file_operations exfat_file_operations = { .llseek = generic_file_llseek, .read_iter = generic_file_read_iter, - .write_iter = generic_file_write_iter, + .write_iter = exfat_file_write_iter, .unlocked_ioctl = exfat_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = exfat_compat_ioctl, #endif - .mmap = generic_file_mmap, + .mmap = exfat_file_mmap, .fsync = exfat_file_fsync, .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c index 481dd338f2b8..0c6b5897518c 100644 --- a/fs/exfat/inode.c +++ b/fs/exfat/inode.c @@ -72,8 +72,8 @@ int __exfat_write_inode(struct inode *inode, int sync) if (ei->start_clu == EXFAT_EOF_CLUSTER) on_disk_size = 0; - ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size); - ep2->dentry.stream.size = ep2->dentry.stream.valid_size; + ep2->dentry.stream.valid_size = cpu_to_le64(ei->valid_size); + ep2->dentry.stream.size = cpu_to_le64(on_disk_size); if (on_disk_size) { ep2->dentry.stream.flags = ei->flags; ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu); @@ -276,6 +276,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock, sector_t last_block; sector_t phys = 0; loff_t pos; + size_t b_size = bh_result->b_size; mutex_lock(&sbi->s_lock); last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb); @@ -303,17 +304,25 @@ static int exfat_get_block(struct inode *inode, sector_t iblock, mapped_blocks = sbi->sect_per_clus - sec_offset; max_blocks = min(mapped_blocks, max_blocks); - /* Treat newly added block / cluster */ - if (iblock < last_block) - create = 0; - - if (create || buffer_delay(bh_result)) { - pos = EXFAT_BLK_TO_B((iblock + 1), sb); + pos = EXFAT_BLK_TO_B((iblock + 1), sb); + if ((create && iblock >= last_block) || buffer_delay(bh_result)) { if (ei->i_size_ondisk < pos) ei->i_size_ondisk = pos; } + map_bh(bh_result, sb, phys); + if (buffer_delay(bh_result)) + clear_buffer_delay(bh_result); + if (create) { + sector_t valid_blks; + + valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb); + if (iblock < valid_blks && iblock + max_blocks >= valid_blks) { + max_blocks = valid_blks - iblock; + goto done; + } + err = exfat_map_new_buffer(ei, bh_result, pos); if (err) { exfat_fs_error(sb, @@ -321,11 +330,40 @@ static int exfat_get_block(struct inode *inode, sector_t iblock, pos, ei->i_size_aligned); goto unlock_ret; } - } - if (buffer_delay(bh_result)) - clear_buffer_delay(bh_result); - map_bh(bh_result, sb, phys); + if (pos - sb->s_blocksize + b_size > ei->valid_size) { + ei->valid_size = pos - sb->s_blocksize + b_size; + mark_inode_dirty(inode); + } + } else { + size_t b_size = EXFAT_BLK_TO_B(max_blocks, sb); + + pos = EXFAT_BLK_TO_B(iblock, sb); + if (pos >= ei->valid_size) { + /* Read out of valid data */ + clear_buffer_mapped(bh_result); + } else if (pos + b_size <= ei->valid_size) { + /* Normal read */ + } else if (pos + sb->s_blocksize <= ei->valid_size) { + /* Normal short read */ + max_blocks = 1; + } else { + /* Read across valid size */ + if (bh_result->b_page) { + loff_t size = ei->valid_size - pos; + loff_t off = pos & (PAGE_SIZE - 1); + + set_bh_page(bh_result, bh_result->b_page, off); + err = bh_read(bh_result, 0); + if (err < 0) + goto unlock_ret; + + zero_user_segment(bh_result->b_page, off + size, + off + sb->s_blocksize); + } + max_blocks = 1; + } + } done: bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb); unlock_ret: @@ -340,6 +378,17 @@ static int exfat_read_folio(struct file *file, struct folio *folio) static void exfat_readahead(struct readahead_control *rac) { + struct address_space *mapping = rac->mapping; + struct inode *inode = mapping->host; + struct exfat_inode_info *ei = EXFAT_I(inode); + loff_t pos = readahead_pos(rac); + + /* Range cross valid_size, read it page by page. */ + if (ei->valid_size < i_size_read(inode) && + pos <= ei->valid_size && + ei->valid_size < pos + readahead_length(rac)) + return; + mpage_readahead(rac, exfat_get_block); } @@ -410,6 +459,8 @@ static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) { struct address_space *mapping = iocb->ki_filp->f_mapping; struct inode *inode = mapping->host; + struct exfat_inode_info *ei = EXFAT_I(inode); + loff_t pos = iocb->ki_pos; loff_t size = iocb->ki_pos + iov_iter_count(iter); int rw = iov_iter_rw(iter); ssize_t ret; @@ -433,8 +484,20 @@ static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) * condition of exfat_get_block() and ->truncate(). */ ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block); - if (ret < 0 && (rw & WRITE)) - exfat_write_failed(mapping, size); + if (ret < 0) { + if (rw & WRITE) + exfat_write_failed(mapping, size); + + if (ret != -EIOCBQUEUED) + return ret; + } else + size = pos + ret; + + if ((rw & READ) && pos < ei->valid_size && ei->valid_size < size) { + iov_iter_revert(iter, size - ei->valid_size); + iov_iter_zero(size - ei->valid_size, iter); + } + return ret; } @@ -534,6 +597,7 @@ static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info) ei->start_clu = info->start_clu; ei->flags = info->flags; ei->type = info->type; + ei->valid_size = info->valid_size; ei->version = 0; ei->hint_stat.eidx = 0; diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c index e0ff9d156f6f..ceaa720804b9 100644 --- a/fs/exfat/namei.c +++ b/fs/exfat/namei.c @@ -395,6 +395,7 @@ static int exfat_find_empty_entry(struct inode *inode, i_size_write(inode, size); ei->i_size_ondisk += sbi->cluster_size; ei->i_size_aligned += sbi->cluster_size; + ei->valid_size += sbi->cluster_size; ei->flags = p_dir->flags; inode->i_blocks += sbi->cluster_size >> 9; } @@ -544,6 +545,8 @@ static int exfat_add_entry(struct inode *inode, const char *path, info->size = clu_size; info->num_subdirs = EXFAT_MIN_SUBDIR; } + info->valid_size = info->size; + memset(&info->crtime, 0, sizeof(info->crtime)); memset(&info->mtime, 0, sizeof(info->mtime)); memset(&info->atime, 0, sizeof(info->atime)); @@ -645,7 +648,8 @@ static int exfat_find(struct inode *dir, struct qstr *qname, info->type = exfat_get_entry_type(ep); info->attr = le16_to_cpu(ep->dentry.file.attr); - info->size = le64_to_cpu(ep2->dentry.stream.valid_size); + info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size); + info->size = le64_to_cpu(ep2->dentry.stream.size); if ((info->type == TYPE_FILE) && (info->size == 0)) { info->flags = ALLOC_NO_FAT_CHAIN; info->start_clu = EXFAT_EOF_CLUSTER; @@ -1271,6 +1275,7 @@ static int __exfat_rename(struct inode *old_parent_inode, } i_size_write(new_inode, 0); + new_ei->valid_size = 0; new_ei->start_clu = EXFAT_EOF_CLUSTER; new_ei->flags = ALLOC_NO_FAT_CHAIN; } From patchwork Wed Jun 28 01:52:52 2023 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: 13295135 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA298EB64DC for ; Wed, 28 Jun 2023 01:53:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231172AbjF1BxR (ORCPT ); Tue, 27 Jun 2023 21:53:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231133AbjF1BxP (ORCPT ); Tue, 27 Jun 2023 21:53:15 -0400 Received: from mx07-001d1705.pphosted.com (mx07-001d1705.pphosted.com [185.132.183.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CA94297B for ; Tue, 27 Jun 2023 18:53:14 -0700 (PDT) Received: from pps.filterd (m0209325.ppops.net [127.0.0.1]) by mx08-001d1705.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35RMvwAt030254; Wed, 28 Jun 2023 01:52:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sony.com; h=from : to : cc : subject : date : message-id : mime-version : content-type : content-transfer-encoding; s=S1; bh=hkaYrArXpofIodO/hHnl8rhYT7ma2jWFcJZWfdphFJo=; b=E4405+ecoQQdEFwHTES4zIXrum1TffrlJ6p/OXmc8G6S9Rr94+cveBM3+ZWx1u85bR7j TFILwZSEmdMqLW38FTjFwnl8og/nBTjY4RYPyHmJ/4bgsDqtnSRYbtpHQcxtQ0rStamt kBVlb3ssRk6CvfyJf9SDLX4kB4cv63qcKOEQI36446O1NqtnKmxB2BLMLUvTX/wx4UP4 Dw1YKZYjhXyWxDpLea1EvbvEkKTeLRqWVX9f5qKwtSXsP4PpWvT+u827qgy1vEr5OHcS c5ijvkFZiK8yiU1FAoeA88TNd0w64+RYuiDyT7UEy4yOc2KAyaVN7Q/Tie/kqZWWGHta HQ== Received: from apc01-psa-obe.outbound.protection.outlook.com (mail-psaapc01lp2047.outbound.protection.outlook.com [104.47.26.47]) by mx08-001d1705.pphosted.com (PPS) with ESMTPS id 3rdnqy3mq2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 28 Jun 2023 01:52:58 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GkRxHuJB7zDfpUy5nmBvCFKjK0s6LjyXcgJ7JFkN4dekAsuVEv5jXOTiua/hqpEj+/xTqyBl1DObNoT8WRHjERz3F5P/sdpRzTfhzcNunpXSPUp4N/HRZ6mO94MBd+rmnead1om7tesctFoSzC3QOOcA6HjkfM+YC4l1c535qEasuGeRtmnpZ8teaQr89dNh8VVsdjvk+2Op/PkIkSqq5Q5NGfYqGXWHpgqJvwliJknei4xXG+/ziMROGkRufrpK3ZPsvGoIg4GjiKZa2WBtCSnY0GgEz/jTChHbgLBHL3mREhYUmI+L27TPsGcdTNMAjTRbQk7J3GMdOzwAon18BA== 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=hkaYrArXpofIodO/hHnl8rhYT7ma2jWFcJZWfdphFJo=; b=gxMibElQQ4Rz62DCaYWbsrnJ5RICoHXB34wykCFq0HD4I24LsQcwfDyOvPcht0x7EMEyt/3ukz4lIJfjN52yvlbmpr4f3pt0isP1aRUguRdeyzqQlJ9Zqq5E9nfuHC+aQALxOwXlD315gE2hHkpM2Z7YSaa4fAN367WV3wUvfhjE33d9MXCJn7DTa8YErp7/RNF5eSRWypwuMoigwzOI67xNG0OezJ+gwlG26TFkND7KK32/HB3sAsOmq+UBB3NdGU1K27BZeX57tCFmd9hxttq6FhdjJRkHquQedjHeCfLMvtD+6hHQ9/qMvwVaf37SbduR7AuvM0rFuX4J8ZI6GQ== 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 PSAPR04MB4389.apcprd04.prod.outlook.com (2603:1096:301:3f::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6544.15; Wed, 28 Jun 2023 01:52:53 +0000 Received: from PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::ce2d:a9dc:4955:5275]) by PUZPR04MB6316.apcprd04.prod.outlook.com ([fe80::ce2d:a9dc:4955:5275%5]) with mapi id 15.20.6521.026; Wed, 28 Jun 2023 01:52:53 +0000 From: "Yuezhang.Mo@sony.com" To: "linkinjeon@kernel.org" , "sj1557.seo@samsung.com" CC: "linux-fsdevel@vger.kernel.org" , "Andy.Wu@sony.com" , "Wataru.Aoyama@sony.com" Subject: [PATCH v2 2/2] exfat: do not zeroed the extended part Thread-Topic: [PATCH v2 2/2] exfat: do not zeroed the extended part Thread-Index: AdmpYxJaQT0eJpq9S2qk9+xpIeN52Q== Date: Wed, 28 Jun 2023 01:52:52 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PUZPR04MB6316:EE_|PSAPR04MB4389:EE_ x-ms-office365-filtering-correlation-id: ee4826d8-7cc1-4f97-5ed5-08db777a62c2 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: PpH0fyK6V9cqk9P++HN6PwKdOoCdsNELwInIlZYYqQ5ld7PXv6jGJd1eSav70yxzBartKkK6tpY9l0a7t30k2YZ5uq78FIaA3vF0iWN+0+YzYDs6r5yQZ2tIltixB1m1mGN4pFhv1L4qOOjbjtJI8MZ4Npai3x+N+s74oToyoiKFlWYFQauuHP0oMdMMXviXxVG5B37BxxYFnz9bd5Ad2YHlH4WzEWfmHB+M9BPHzelvGmspPzEgD7Pi8Q/8wOgjkRbRUNHpzUtnXZabFXJh22CaFPEr3xstUhF5P/c9dp4bUyw8U+H7L5raUUU8BLgDV5oXr4OfWIDwsxWbsKlxFEo4dw5OEhJ/ES5argjw1CtzgGe+V13nJJLT3PmQfOdOFjwJoED4A3nYetOEJ4W545vnNgKRQNfu+MmxkwAhp3qNYXmO+4yC0VQpBQ7aGi2UvhfPrEbmX21qM5zozzoV05u6LBMVa8KslPVrEH/4T/CLydlCjmq0ahCcdF+4fyn+zqpvRw1VkrdYy2/5U0MSnvqG8WXvX6zVX1O8KdY6jTy2GpHmba6Moa0LfPmF2CUV7b2kA9vA5n4CxdNqkix853RkXPIhcQLh5IzYRfXX6pgQUDR2bsUVgelL625ZFB8H 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:(13230028)(4636009)(396003)(136003)(346002)(366004)(376002)(39860400002)(451199021)(5660300002)(66946007)(41300700001)(52536014)(76116006)(64756008)(66446008)(66476007)(66556008)(8936002)(8676002)(4326008)(316002)(478600001)(83380400001)(38070700005)(55016003)(26005)(6506007)(2906002)(33656002)(107886003)(9686003)(186003)(54906003)(110136005)(122000001)(38100700002)(86362001)(82960400001)(71200400001)(7696005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-ant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mQSy3kBaEZz1iGqZR+MRvCfXiyeLAXy?= =?utf-8?q?MFBSu25cLewY1O0OZ8fyHUpjYgNbD1byZM/1nWQ7GdwH1tEnM6PcpendxsGsWs9v8?= =?utf-8?q?egXeJwNcS8DUyV7r97hWGbbGD3bBt74ckOFw17IPsGvWOLqy6KaeM5tPB18Ba1cvH?= =?utf-8?q?aicyCIPXQLpIy56vVFQa4AireuZdzBCC28WfZoAV7Gu1oWwyiZaz3sQQxX/R2HL/2?= =?utf-8?q?3SZq78/FAzVA8BhpBbP7+fG4/2oHH+KDIgmk8c3A7nUPuqEWJQrgfWqs5AEG4KA8f?= =?utf-8?q?nk+s/xQMbXzcue7dDaB4D9/KyZHomATzxUZE7EOJgCvLow4s3eLQVrk8Lyrqdpqtr?= =?utf-8?q?6W6fzMP9yDs6LdMfCFJdtRMsuDuGef5wmHqKWK7ESTsfQIJTisAd9TGvX+NDXmJhR?= =?utf-8?q?lZX07bLOA7gstNny9jqIhbT8vvCoJ8asRrOvRJ0mffs0mE05PnSGx4KaLrCbbqgDn?= =?utf-8?q?VFHMO1uqoTanLWG6EYKEsWQn64/BrJaWcXBF9/tABGbjCNfl+CQo7rSa1IFmnfnV4?= =?utf-8?q?yvq+3r8M10tB0vKNOlwu1cVc3QYDEsr0l7TtF8zpUkb5+/rhc+mkA7HjnG1BcybT8?= =?utf-8?q?SLSuzniFIXy+nzzORKwgJQ+PRRRYOHTCvEjGN7LlXgVYTypzCDwgzzub818mzj/iG?= =?utf-8?q?aTf4p+VA9P15?= MIME-Version: 1.0 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: MKDxVOurGhkR3IkS3bcygor6q/9RNKivRtZD7AoybLuT3fO0UopZMKLuC6GHq5giVR7IZe8FBx+QBA09ojbUp4oZJQeaiMK1w/6i0WsJKfgNhQMf4rDXLRewyf4FG+fV4q2NLO3lmHEWa8NITsB3rMHJSNZBpcAG9n6BKmea6MskMBZBO9AszlRZqoTbKtq2LnveHjaD7+QT4VtZD7VWLlwBxWUvHRI4VCIM74wPS8iOw7LNXjRrDzHO0WaYz2esZwec5wh3jrgRD4VwQqrzqz+jt340VlKnvCfa4JUu14J9X55fPPi5oqW5bRn09bTaC8WeO9Cll0ZOcOAS0FARwh//MlHowGLEb5dOZ5vz2MNt2nYwwW9/+j8FWwEWZQZnO/SULBt8wKF/FpT6flgzemgvy79vsM703KPjMmUD+yPifbWZOO7wQCwprw3Njfq92kPiqGgxFLMLKHEqybVBz59lpFXPA8+sDL7sywaFNSJebTaH+v5XVcELicuRNoGsfU9lMtZt0V6Hs+DxDQIOQWx5FDgPbdtZv3chHisNSP9KaI79PdKTev5dtgE1Xu61wAiq8acfOVMwL/wtGByU6eOMhWAgpFEBHEoCav3Oasj3ESc9aTSw43bwzckUYmsf8c+y3GYq+VF9ngx0k4Ur99KMeuAeOOrzNRic1z+YiipvSMgnQPtXGHpmJeDGAnQ3ocPc5GhOVXHk3zdYJUqQbQzMzkpM9QwVXdUXhLsqtAaFumMGDeAoe9j4tCf8zame2puwGmUgyliZ0AzX1M1wSwWvb/qRNTNU7FGOZDQ/fET7fIWNqO+mpkdiCF8xj+j4eIvU6AMeTK7RyFoQcjIbGA== 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: ee4826d8-7cc1-4f97-5ed5-08db777a62c2 X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Jun 2023 01:52:52.9154 (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: rQTjZ88VVESzHrpNbPxceYjA6+TnOPjUVk608O4IkAT6OCmnrVyd3z4D+Zt9awFTIPCNORRxTsMwOdfZHy5Gdw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PSAPR04MB4389 X-Proofpoint-GUID: xgKxvZL3wO7dXfllLqzk4F-st6QEpLsa X-Proofpoint-ORIG-GUID: xgKxvZL3wO7dXfllLqzk4F-st6QEpLsa X-Sony-Outbound-GUID: xgKxvZL3wO7dXfllLqzk4F-st6QEpLsa X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-06-27_16,2023-06-27_01,2023-05-22_02 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Since the read operation beyond the ValidDataLength returns zero, if we just extend the size of the file, we don't need to zero the extended part, but only change the DataLength without changing the ValidDataLength. Signed-off-by: Yuezhang Mo Reviewed-by: Andy Wu Reviewed-by: Aoyama Wataru --- fs/exfat/file.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 8cd14bc16857..62a21c45517d 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -13,7 +13,7 @@ #include "exfat_raw.h" #include "exfat_fs.h" -static int exfat_cont_expand(struct inode *inode, loff_t size) +static int exfat_expand_and_zero(struct inode *inode, loff_t size) { struct address_space *mapping = inode->i_mapping; loff_t start = i_size_read(inode), count = size - i_size_read(inode); @@ -43,6 +43,81 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) return filemap_fdatawait_range(mapping, start, start + count - 1); } +static int exfat_expand(struct inode *inode, loff_t size) +{ + int ret; + unsigned int num_clusters, new_num_clusters, last_clu; + struct exfat_inode_info *ei = EXFAT_I(inode); + struct super_block *sb = inode->i_sb; + struct exfat_sb_info *sbi = EXFAT_SB(sb); + struct exfat_chain clu; + + ret = inode_newsize_ok(inode, size); + if (ret) + return ret; + + num_clusters = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); + new_num_clusters = EXFAT_B_TO_CLU_ROUND_UP(size, sbi); + + 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; + + clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ? + EXFAT_EOF_CLUSTER : last_clu + 1; + clu.size = 0; + clu.flags = ei->flags; + + ret = exfat_alloc_cluster(inode, new_num_clusters - num_clusters, + &clu, IS_DIRSYNC(inode)); + if (ret) + 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) + ei->start_clu = clu.dir; + +out: + inode->i_ctime = inode->i_mtime = current_time(inode); + /* Expanded range not zeroed, do not update valid_size */ + i_size_write(inode, size); + + ei->i_size_aligned = round_up(size, sb->s_blocksize); + ei->i_size_ondisk = ei->i_size_aligned; + inode->i_blocks = round_up(size, sbi->cluster_size) >> 9; + + if (IS_DIRSYNC(inode)) + return write_inode_now(inode, 1); + + mark_inode_dirty(inode); + + return 0; + +free_clu: + exfat_free_cluster(inode, &clu); + return -EIO; +} + +static int exfat_cont_expand(struct inode *inode, loff_t size) +{ + if (mapping_writably_mapped(inode->i_mapping)) + return exfat_expand_and_zero(inode, size); + + return exfat_expand(inode, size); +} + static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode) { mode_t allow_utime = sbi->options.allow_utime;