From nobody Wed Apr 8 21:37:44 2026 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 0CC01ECAAA1 for ; Fri, 28 Oct 2022 17:05:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231283AbiJ1RFU (ORCPT ); Fri, 28 Oct 2022 13:05:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230370AbiJ1REU (ORCPT ); Fri, 28 Oct 2022 13:04:20 -0400 Received: from relayaws-01.paragon-software.com (relayaws-01.paragon-software.com [35.157.23.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D32D22D5F0; Fri, 28 Oct 2022 10:03:56 -0700 (PDT) Received: from relayfre-01.paragon-software.com (unknown [172.30.72.12]) by relayaws-01.paragon-software.com (Postfix) with ESMTPS id 9BA2D218D; Fri, 28 Oct 2022 17:01:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paragon-software.com; s=mail; t=1666976475; bh=7CPKDDVsSnfhQv2FIDQRZDeSJd/QzjNRDoHBpF6DsHc=; h=Date:Subject:From:To:CC:References:In-Reply-To; b=NHENqJ9QiBWV1u1KbsanQe8xs5FBr8mTnQhxFnENOfFJMFR3mLFu9crLcdCHSBHuH 0dywRoB4lG8wZlfLFs4ad8ZiC/6j/Iki0Wc2aLbofhBI9fAqPCGPJ0bImQcqUwWEXh 5UMefF0H9LrLoAieK/OQjaYQcF89i8H5rXFc1zXk= Received: from dlg2.mail.paragon-software.com (vdlg-exch-02.paragon-software.com [172.30.1.105]) by relayfre-01.paragon-software.com (Postfix) with ESMTPS id 97A68DD; Fri, 28 Oct 2022 17:03:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paragon-software.com; s=mail; t=1666976634; bh=7CPKDDVsSnfhQv2FIDQRZDeSJd/QzjNRDoHBpF6DsHc=; h=Date:Subject:From:To:CC:References:In-Reply-To; b=ZtwaZy3jXCrm0t26DkS0rm4OCn2jQRYOZ/50MvH6FSnC1I05902RZAwzuTt22iS0n y2927ENmvKF44rdhcUtPpMdoEm0l7B00MjKSwopN/CT9Un/uAScaNI+MdkIrYDB+sS 0NIVY9BTIykVJvLMpF8PQuPP8AJZHYCZKsxnOifE= Received: from [172.30.8.65] (172.30.8.65) by vdlg-exch-02.paragon-software.com (172.30.1.105) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.7; Fri, 28 Oct 2022 20:03:54 +0300 Message-ID: <5f006fed-743e-336a-ea14-699c376215e7@paragon-software.com> Date: Fri, 28 Oct 2022 20:03:53 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: [PATCH 05/14] fs/ntfs3: Fixing wrong logic in attr_set_size and ntfs_fallocate Content-Language: en-US From: Konstantin Komarov To: CC: , References: In-Reply-To: Content-Transfer-Encoding: quoted-printable X-Originating-IP: [172.30.8.65] X-ClientProxiedBy: vdlg-exch-02.paragon-software.com (172.30.1.105) To vdlg-exch-02.paragon-software.com (172.30.1.105) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8"; format="flowed" There were 2 problems: - in some cases we lost dirty flag; - cluster allocation can be called even when it wasn't needed. Fixes xfstest generic/465 Signed-off-by: Konstantin Komarov --- fs/ntfs3/attrib.c | 25 +++++++++++-------------- fs/ntfs3/file.c | 30 ++++++++++++++++++------------ fs/ntfs3/index.c | 9 +++++++++ fs/ntfs3/inode.c | 17 +++++------------ 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/fs/ntfs3/attrib.c b/fs/ntfs3/attrib.c index eda83a37a0c3..91ea73e6f4fe 100644 --- a/fs/ntfs3/attrib.c +++ b/fs/ntfs3/attrib.c @@ -414,6 +414,7 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE= type, CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn; CLST next_svcn, pre_alloc =3D -1, done =3D 0; bool is_ext, is_bad =3D false; + bool dirty =3D false; u32 align; struct MFT_REC *rec; =20 @@ -434,8 +435,10 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYP= E type, return err; =20 /* Return if file is still resident. */ - if (!attr_b->non_res) + if (!attr_b->non_res) { + dirty =3D true; goto ok1; + } =20 /* Layout of records may be changed, so do a full search. */ goto again; @@ -458,7 +461,7 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE= type, =20 if (keep_prealloc && new_size < old_size) { attr_b->nres.data_size =3D cpu_to_le64(new_size); - mi_b->dirty =3D true; + mi_b->dirty =3D dirty =3D true; goto ok; } =20 @@ -504,7 +507,7 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE= type, =20 if (new_alloc <=3D old_alloc) { attr_b->nres.data_size =3D cpu_to_le64(new_size); - mi_b->dirty =3D true; + mi_b->dirty =3D dirty =3D true; goto ok; } =20 @@ -595,7 +598,7 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE= type, next_svcn =3D le64_to_cpu(attr->nres.evcn) + 1; new_alloc_tmp =3D (u64)next_svcn << cluster_bits; attr_b->nres.alloc_size =3D cpu_to_le64(new_alloc_tmp); - mi_b->dirty =3D true; + mi_b->dirty =3D dirty =3D true; =20 if (next_svcn >=3D vcn && !to_allocate) { /* Normal way. Update attribute and exit. */ @@ -681,7 +684,7 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE= type, old_valid =3D old_size =3D old_alloc =3D (u64)vcn << cluster_bits; attr_b->nres.valid_size =3D attr_b->nres.data_size =3D attr_b->nres.alloc_size =3D cpu_to_le64(old_size); - mi_b->dirty =3D true; + mi_b->dirty =3D dirty =3D true; goto again_1; } =20 @@ -743,7 +746,7 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE= type, attr_b->nres.valid_size =3D attr_b->nres.alloc_size; } - mi_b->dirty =3D true; + mi_b->dirty =3D dirty =3D true; =20 err =3D run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen, true); @@ -804,16 +807,9 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYP= E type, if (ret) *ret =3D attr_b; =20 - /* Update inode_set_bytes. */ if (((type =3D=3D ATTR_DATA && !name_len) || (type =3D=3D ATTR_ALLOC && name =3D=3D I30_NAME))) { - bool dirty =3D false; - - if (ni->vfs_inode.i_size !=3D new_size) { - ni->vfs_inode.i_size =3D new_size; - dirty =3D true; - } - + /* Update inode_set_bytes. */ if (attr_b->non_res) { new_alloc =3D le64_to_cpu(attr_b->nres.alloc_size); if (inode_get_bytes(&ni->vfs_inode) !=3D new_alloc) { @@ -822,6 +818,7 @@ int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE= type, } } =20 + /* Don't forget to update duplicate information in parent. */ if (dirty) { ni->ni_flags |=3D NI_FLAG_UPDATE_PARENT; mark_inode_dirty(&ni->vfs_inode); diff --git a/fs/ntfs3/file.c b/fs/ntfs3/file.c index 63aef132e529..511e58f2b0f8 100644 --- a/fs/ntfs3/file.c +++ b/fs/ntfs3/file.c @@ -337,7 +337,6 @@ static int ntfs_extend(struct inode *inode, loff_t pos,= size_t count, err =3D ntfs_set_size(inode, end); if (err) goto out; - inode->i_size =3D end; } =20 if (extend_init && !is_compressed(ni)) { @@ -588,12 +587,14 @@ static long ntfs_fallocate(struct file *file, int mod= e, loff_t vbo, loff_t len) if (err) goto out; =20 - /* - * Allocate clusters, do not change 'valid' size. - */ - err =3D ntfs_set_size(inode, new_size); - if (err) - goto out; + if (new_size > i_size) { + /* + * Allocate clusters, do not change 'valid' size. + */ + err =3D ntfs_set_size(inode, new_size); + if (err) + goto out; + } =20 if (is_supported_holes) { CLST vcn =3D vbo >> sbi->cluster_bits; @@ -635,6 +636,8 @@ static long ntfs_fallocate(struct file *file, int mode,= loff_t vbo, loff_t len) &ni->file.run, i_size, &ni->i_valid, true, NULL); ni_unlock(ni); + } else if (new_size > i_size) { + inode->i_size =3D new_size; } } =20 @@ -678,7 +681,7 @@ int ntfs3_setattr(struct user_namespace *mnt_userns, st= ruct dentry *dentry, goto out; =20 if (ia_valid & ATTR_SIZE) { - loff_t oldsize =3D inode->i_size; + loff_t newsize, oldsize; =20 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { /* Should never be here, see ntfs_file_open(). */ @@ -686,16 +689,19 @@ int ntfs3_setattr(struct user_namespace *mnt_userns, = struct dentry *dentry, goto out; } inode_dio_wait(inode); + oldsize =3D inode->i_size; + newsize =3D attr->ia_size; =20 - if (attr->ia_size <=3D oldsize) - err =3D ntfs_truncate(inode, attr->ia_size); - else if (attr->ia_size > oldsize) - err =3D ntfs_extend(inode, attr->ia_size, 0, NULL); + if (newsize <=3D oldsize) + err =3D ntfs_truncate(inode, newsize); + else + err =3D ntfs_extend(inode, newsize, 0, NULL); =20 if (err) goto out; =20 ni->ni_flags |=3D NI_FLAG_UPDATE_PARENT; + inode->i_size =3D newsize; } =20 setattr_copy(mnt_userns, inode, attr); diff --git a/fs/ntfs3/index.c b/fs/ntfs3/index.c index a2e1e07b5bb8..35369ae5c438 100644 --- a/fs/ntfs3/index.c +++ b/fs/ntfs3/index.c @@ -1445,6 +1445,9 @@ static int indx_add_allocate(struct ntfs_index *indx,= struct ntfs_inode *ni, goto out1; } =20 + if (in->name =3D=3D I30_NAME) + ni->vfs_inode.i_size =3D data_size; + *vbn =3D bit << indx->idx2vbn_bits; =20 return 0; @@ -1978,6 +1981,9 @@ static int indx_shrink(struct ntfs_index *indx, struc= t ntfs_inode *ni, if (err) return err; =20 + if (in->name =3D=3D I30_NAME) + ni->vfs_inode.i_size =3D new_data; + bpb =3D bitmap_size(bit); if (bpb * 8 =3D=3D nbits) return 0; @@ -2461,6 +2467,9 @@ int indx_delete_entry(struct ntfs_index *indx, struct= ntfs_inode *ni, =20 err =3D attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len, &indx->alloc_run, 0, NULL, false, NULL); + if (in->name =3D=3D I30_NAME) + ni->vfs_inode.i_size =3D 0; + err =3D ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len, false, NULL); run_close(&indx->alloc_run); diff --git a/fs/ntfs3/inode.c b/fs/ntfs3/inode.c index 405afb54cc19..78ec3e6bbf67 100644 --- a/fs/ntfs3/inode.c +++ b/fs/ntfs3/inode.c @@ -550,17 +550,6 @@ static noinline int ntfs_get_block_vbo(struct inode *i= node, u64 vbo, clear_buffer_new(bh); clear_buffer_uptodate(bh); =20 - /* Direct write uses 'create=3D0'. */ - if (!create && vbo >=3D ni->i_valid) { - /* Out of valid. */ - return 0; - } - - if (vbo >=3D inode->i_size) { - /* Out of size. */ - return 0; - } - if (is_resident(ni)) { ni_lock(ni); err =3D attr_data_read_resident(ni, page); @@ -624,7 +613,6 @@ static noinline int ntfs_get_block_vbo(struct inode *in= ode, u64 vbo, } } else if (vbo >=3D valid) { /* Read out of valid data. */ - /* Should never be here 'cause already checked. */ clear_buffer_mapped(bh); } else if (vbo + bytes <=3D valid) { /* Normal read. */ @@ -974,6 +962,11 @@ int ntfs_write_end(struct file *file, struct address_s= pace *mapping, dirty =3D true; } =20 + if (pos + err > inode->i_size) { + inode->i_size =3D pos + err; + dirty =3D true; + } + if (dirty) mark_inode_dirty(inode); } --=20 2.37.0