Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1549067lqp; Fri, 22 Mar 2024 21:27:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWkWRuTnZOomAMRRDjZ7yV7n5mCvmRiO0SM3wNzursYZ8G8v3+EkIF6tQtvuU3DhZM5aQOkcc/H2xk9WPy0xOgxQMltPvZa6dQzZKYwFQ== X-Google-Smtp-Source: AGHT+IF9cWt3ol3us1PgvCH6gVv2ww7U0vqL6Xu54fq2deM1NQ1SVbTtIAhxKe6u4MPTCY2btTWt X-Received: by 2002:a17:903:22cb:b0:1dd:7e2:b9c2 with SMTP id y11-20020a17090322cb00b001dd07e2b9c2mr1974810plg.10.1711168021076; Fri, 22 Mar 2024 21:27:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711168021; cv=pass; d=google.com; s=arc-20160816; b=dyc5G8EjaeAtkLGaAebKHA5QIAoKJK3Dt3aEosvJM+ojusFJarEl00OmjjcGxsGTOe mAZjmxqQVOcIFzYs+etkiDWsqyfVu/c7LKkm2mztxpKHMOnVjbdeXTiZR45HvLs6hU8q b18vEokRuWFXqSVAGeQ82Hj12cVopxMuDVkJUnzlb4+1sH9UuBD4r+NAGU5a7xQ5wSy7 ZtRGjkkDryi18/SoHVW07dcFaEBD//+S2bQYuGpO/KKMpuWg0Mzs/Rrc9hSA8l429eUl a0kdM9K2kbX845P1YESKXBKye/++3R3ZZD0JoRIsza5u6zpTvkbL6DYudDgWGxRabn/O Sn4g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=fDobeJ1Q8yo+RbPEMeiHtYxH1H8ClCKf9HbBmOgnCkQ=; fh=ZaK2vudNijy95H0GvyYVBWTmhTw8nJYwWXkLewFdkfY=; b=YF62Cgd76eSUPCUw4EBI28LwVjXG89sLUbSAYMvgfq1ddP5aa+TeDHlcecdw9lz1xC 11WdmQgwmcT86qioK31dvuIRathKQ4ACA1Sr8zWdilsdigQJLzzlazUl9JDnw2IM3IjT VbH63l1zlDbu3kttdNkPd3VRIMgiiKn7Z6CPYWP6kaXU0tBM9Wxd/kXMF4MyLwu2WEBa c8Bu01YMm2JJSG0gVQlCBWY7iSY6Yi+2C/o/9BdyCM4dMDcGtmHuO8wguXZoMv60K3aY wgk19H6cotwyEAxtEd/wVBJXdVOsn7Y6nDTQmuGEhBwCIzv1ZOSndagImUUtXpem3nek LmXQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SLle9KSs; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-112224-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112224-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id b17-20020a170902d51100b001dd6c4a3e18si892960plg.474.2024.03.22.21.27.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 21:27:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112224-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SLle9KSs; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-112224-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112224-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 58564B21FA2 for ; Sat, 23 Mar 2024 04:26:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 94842440C; Sat, 23 Mar 2024 04:26:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SLle9KSs" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 757DA17F3 for ; Sat, 23 Mar 2024 04:26:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711168008; cv=none; b=VoX+O9E+GSXIi29oh6OstRjNslomw4i8bjI8aiGRLGuYiCtnOxJBpCwLj+0EoWVO4hSOOOS9T//pkZX2oUSv+KG5Mp5HlqOoENM6eOXm0pwtgS0MeJV+cpp0Ryjg7+JE8bVf4wREMUEsDvKTbX1a3ruEGj3283JtInJi7vnB2jY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711168008; c=relaxed/simple; bh=yK8dDTZSWtZlDKKXpzS7heTkO7u+N3dxFz037MPPjsU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=TUjZgmcrFtZ2m02Ji4iMQcGEuP9ey7DIMGby7RoDEXkh+osIgVHx0rpGTbq8ohYDp8/XoHtRbyHk4NptnEhw1IoOG0BjFwDM+2cUfAhPE6y2cXfxtoYlBFJTdL+YAOkqbuGbvobCqtiRDfDrqBzx69KTloEqkTdcmiVorUFcGNM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SLle9KSs; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3DB7C433F1; Sat, 23 Mar 2024 04:26:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711168007; bh=yK8dDTZSWtZlDKKXpzS7heTkO7u+N3dxFz037MPPjsU=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=SLle9KSsmR+UuzL9V1wpIGnLAEh6nUthenpr4LTm++VcCDQ3VPOL/8KE97NgOEaxp k/BF1+XW4zUT4nt+M3vneuwQjuAMGdQPxUanXr+jYRPTrdE9iSgcnn6rHb3ug5MvUv nPuOpBVlMzOgfcCpThNznSTTfKiTozyX5AgF664SRCPs3Qz/pTbHwUtZsQnMJQXorn 1nwovNsFO4RPZkcv8ImDu6ALT4+VN/fndWa7JyfF7ogns+q27NVLMSgxRJqb2/R+kC jjQUB2mxBUKQtuxVlcAaMNGZ1nVNP5GZCskD1ftXaaZyFV/MKBEuUvH8x1ewioaSkk oCc5CCilLFTxw== Message-ID: Date: Sat, 23 Mar 2024 12:26:47 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [f2fs-dev] [PATCH v3] f2fs: prevent writing without fallocate() for pinned files To: Daeho Jeong Cc: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com, Daeho Jeong References: <20240319212316.4193790-1-daeho43@gmail.com> <712f380c-68ef-4743-bd9b-7342e838ced7@kernel.org> Content-Language: en-US From: Chao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k1bwUj/wOcqWu+ZdnQa3zbfvMI9o/kFlOu8iTGlD8sNjJK+Y/fPK3znFqoqqKmSFZ aiRALjAZH6ufspvYAJEJE9eZSX7Rtdyt30MMHQARAQABwsFfBBgBCgAJBQJWLOm1AhsMAAoJ EKTPgB1/p52GPpoP/2LOn/5KSkGHGmdjzRoQHBTdm2YV1YwgADg52/mU68Wo6viStZqcVEnX 3ALsWeETod3qeBCJ/TR2C6hnsqsALkXMFFJTX8aRi/E4WgBqNvNgAkWGsg5XKB3JUoJmQLqe CGVCT1OSQA/gTEfB8tTZAGFwlw1D3W988CiGnnRb2EEqU4pEuBoQir0sixJzFWybf0jjEi7P pODxw/NCyIf9GNRNYByUTVKnC7C51a3b1gNs10aTUmRfQuu+iM5yST5qMp4ls/yYl5ybr7N1 zSq9iuL13I35csBOn13U5NE67zEb/pCFspZ6ByU4zxChSOTdIJSm4/DEKlqQZhh3FnVHh2Ld eG/Wbc1KVLZYX1NNbXTz7gBlVYe8aGpPNffsEsfNCGsFDGth0tC32zLT+5/r43awmxSJfx2P 5aGkpdszvvyZ4hvcDfZ7U5CBItP/tWXYV0DDl8rCFmhZZw570vlx8AnTiC1v1FzrNfvtuxm3 92Qh98hAj3cMFKtEVbLKJvrc2AO+mQlS7zl1qWblEhpZnXi05S1AoT0gDW2lwe54VfT3ySon 8Klpbp5W4eEoY21tLwuNzgUMxmycfM4GaJWNCncKuMT4qGVQO9SPFs0vgUrdBUC5Pn5ZJ46X mZA0DUz0S8BJtYGI0DUC/jAKhIgy1vAx39y7sAshwu2VILa71tXJ In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2024/3/21 1:42, Daeho Jeong wrote: > On Wed, Mar 20, 2024 at 2:38 AM Chao Yu wrote: >> >> On 2024/3/20 5:23, Daeho Jeong wrote: >>> From: Daeho Jeong >>> >>> In a case writing without fallocate(), we can't guarantee it's allocated >>> in the conventional area for zoned stroage. >>> >>> Signed-off-by: Daeho Jeong >>> --- >>> v2: covered the direct io case >>> v3: covered the mkwrite case >>> --- >>> fs/f2fs/data.c | 14 ++++++++++++-- >>> fs/f2fs/file.c | 16 ++++++++-------- >>> 2 files changed, 20 insertions(+), 10 deletions(-) >>> >>> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c >>> index c21b92f18463..d3e5ab2736a6 100644 >>> --- a/fs/f2fs/data.c >>> +++ b/fs/f2fs/data.c >>> @@ -1584,8 +1584,11 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag) >>> >>> /* use out-place-update for direct IO under LFS mode */ >>> if (map->m_may_create && >>> - (is_hole || (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO))) { >>> - if (unlikely(f2fs_cp_error(sbi))) { >>> + (is_hole || (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO && >>> + !f2fs_is_pinned_file(inode)))) { >>> + if (unlikely(f2fs_cp_error(sbi)) || >>> + (f2fs_is_pinned_file(inode) && is_hole && >>> + flag != F2FS_GET_BLOCK_PRE_DIO)) { >>> err = -EIO; >>> goto sync_out; >>> } >>> @@ -3378,6 +3381,8 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi, >>> f2fs_map_lock(sbi, flag); >>> locked = true; >>> } else if ((pos & PAGE_MASK) >= i_size_read(inode)) { >>> + if (f2fs_is_pinned_file(inode)) >>> + return -EIO; >>> f2fs_map_lock(sbi, flag); >>> locked = true; >>> } >>> @@ -3407,6 +3412,11 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi, >>> >>> if (!f2fs_lookup_read_extent_cache_block(inode, index, >>> &dn.data_blkaddr)) { >>> + if (f2fs_is_pinned_file(inode)) { >>> + err = -EIO; >>> + goto out; >>> + } >>> + >>> if (locked) { >>> err = f2fs_reserve_block(&dn, index); >>> goto out; >>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c >>> index 82277e95c88f..4db3b21c804b 100644 >>> --- a/fs/f2fs/file.c >>> +++ b/fs/f2fs/file.c >>> @@ -57,7 +57,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) >>> struct inode *inode = file_inode(vmf->vma->vm_file); >>> struct f2fs_sb_info *sbi = F2FS_I_SB(inode); >>> struct dnode_of_data dn; >>> - bool need_alloc = true; >>> + bool need_alloc = !f2fs_is_pinned_file(inode); >> >> Will this check races w/ pinfile get|set? > > Do you mean "set/clear" case? I believe "set" case is okay, since we Yup, > can't set if the inode already has a data block. For "clear" case, I However, we can set pinfile on written inode in regular block device: if (f2fs_sb_has_blkzoned(sbi) && F2FS_HAS_BLOCKS(inode)) { ret = -EFBIG; goto out; } Should we add the logic only if blkzoned feture is enabled? > believe mkwrite failure is okay in racy conditions caused by clearing > the pin flag. What do you think? Or we can use filemap_invalidate_lock() in f2fs_ioc_set_pin_file() to avoid the race condition? Thanks, > >> >> Thanks, >> >>> int err = 0; >>> vm_fault_t ret; >>> >>> @@ -114,19 +114,15 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) >>> goto out_sem; >>> } >>> >>> + set_new_dnode(&dn, inode, NULL, NULL, 0); >>> if (need_alloc) { >>> /* block allocation */ >>> - set_new_dnode(&dn, inode, NULL, NULL, 0); >>> err = f2fs_get_block_locked(&dn, page->index); >>> - } >>> - >>> -#ifdef CONFIG_F2FS_FS_COMPRESSION >>> - if (!need_alloc) { >>> - set_new_dnode(&dn, inode, NULL, NULL, 0); >>> + } else { >>> err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE); >>> f2fs_put_dnode(&dn); >>> } >>> -#endif >>> + >>> if (err) { >>> unlock_page(page); >>> goto out_sem; >>> @@ -4611,6 +4607,10 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter, >>> return ret; >>> } >>> >>> + /* For pinned files, it should be fallocate()-ed in advance. */ >>> + if (f2fs_is_pinned_file(inode)) >>> + return 0; >>> + >>> /* Do not preallocate blocks that will be written partially in 4KB. */ >>> map.m_lblk = F2FS_BLK_ALIGN(pos); >>> map.m_len = F2FS_BYTES_TO_BLK(pos + count);