Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3082527lqp; Mon, 25 Mar 2024 20:40:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVqtjgsbJe+t3y8j5Oe5mIAmJhYZmJ0Hbd6/0SuHZTDr/TQ5ql6mz7jszUEPuhprfdQ3b9nGgqRs+Vt+DVreWzrpgs2BT/L1RoEK/XKEQ== X-Google-Smtp-Source: AGHT+IGS7uVrtrSMw3PjImxNNTeu0CRl39FujwMfrAc9TFmjercIcAtBdoEpBN6bNFQI59GGq746 X-Received: by 2002:a05:6a20:2589:b0:1a3:c38d:9609 with SMTP id k9-20020a056a20258900b001a3c38d9609mr7060310pzd.3.1711424400439; Mon, 25 Mar 2024 20:40:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711424400; cv=pass; d=google.com; s=arc-20160816; b=eZQUKaQDu/mUUn2RF4dv1IqWQrXwrbCIZckPwK0YEbYXuaLhNp0otupOPxf6cWYvzd kucUjRcWMqQ36SJG7FfwdOPf06WK2rx7EG3DZx2hLvIoczCJeMGKyiIM7lPQi90CK9DB 6tOrr89SRZ5Glo0P2eHrhpi7He5uZG7vc01oceVaC0M+tkooKmuWrBTY+GDU2hkZ+ACz Tk29y6qcczFlF5xmeV03VZA0LlViNfEsVQZhyu5bl3tfiNGoa0vlaOmnqkO9UavznGt1 O4pzNuex/ifSAfuNaOEGXWifuLHCpOBXEl4G5rXqSnPO9jkY8fS/yUzqB+G+51X+Fzz5 sF2w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=N/ybcAWlUCHmoySSU86mwFcgprc/w7f5uICPA/CHnbQ=; fh=ZaK2vudNijy95H0GvyYVBWTmhTw8nJYwWXkLewFdkfY=; b=OroSPi9NpntbDlY3j7V1tVugYJBVyiObsqZk4FqEpkM0L2IWJLZLbRlpG5f0saOngx 1xxmyZgSuPbJYtQHmsAEREzCFBDLGoQO8NuEJeX2XT8RXFZ5YP/lNHNgmQOW9eXhc7uP PtRpLFZwWzoTGdHk2Puhb3m2M5RONGo8zvQtvff5qGn8xLwg+PmkvWUXh52K5FvI9Gt1 BAME6s5d1kDfgLhojDQ+Ve4JbmxyY1NvxkjgmpHeA9Bnm3p/TYowOs/3/DTtiDLoCMRQ vC/gvULY3FbeU09V70hPcfZX9r0DR6L+3ThOfun/Eoz+BnpJOmSR+ndIFZssbuDMI4Ga QG4g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IhxLN1Id; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-118345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118345-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id i5-20020a17090a974500b002a07c1d4551si1740639pjw.184.2024.03.25.20.40.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 20:40:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=IhxLN1Id; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-118345-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118345-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 204E72E4662 for ; Tue, 26 Mar 2024 03:40:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 221FC129A8D; Tue, 26 Mar 2024 03:39:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IhxLN1Id" 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 2F6B8128801 for ; Tue, 26 Mar 2024 03:39:37 +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=1711424377; cv=none; b=sBrGes4ngiBHY8PyWTfmMsnHsyYJbftTbDiEVWhzOknGMuC5BK0Ny3YcTiyIk2k9UiqYuAoRpOHAcu+A+xOze9NTM1BQtBQ+M3lMqIHQ6bdClkt5KKoi33FqyyZzwyck2CFHVjyOPLKznDHbtMKYauYm8/9zHDPG5U5iJDWZLdc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711424377; c=relaxed/simple; bh=P3vreY2sz09wZD7BtqL79mQRFOWcQ2qQmdy79Frd0Q4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=JRsOjUgvLg+OrbzU1tSTFUHLEUQpiq3dqfn9VNTsm7MTqFE+UpQu0fhARnbIXhykyBL9aqHlvIP7lK3C+tYHmKzJqez8ld0ZPd6yOBTRXwYBJ+ceJWszpsEaLhKuhboZ1D/8gRi7dVUDfVH9Me60jJyg+bM8Ho/PFDH8Kum+Lxw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IhxLN1Id; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDBADC43390; Tue, 26 Mar 2024 03:39:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711424377; bh=P3vreY2sz09wZD7BtqL79mQRFOWcQ2qQmdy79Frd0Q4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=IhxLN1IdkHiZ64LTHLbvCtomJaVRZtRfO2u0R5NhiFGnaaCNZchQvv+HLwhXo9j72 qMZWHzmrfjiUQz/+AAsc0jsYdhUbCNn0P97P749R1l6+TxAIRgWiykAw9dZDDk5Zwk hCLzQKIKQ3DnHgsFPtW8vhqvLjm3AU0cy9wdQj17n2LpTJTXjTnOsUpp1iQWYot0Ib I+ISYRzTx68ay9l8zMbVNQ97VQpF/lHHPM9clxQ0Nin44os951PUuYDOQwOIRLSulZ iUnT23G/MyVu4hpKPdmrdp+pQWSjwVX8Na8bu5vizMSIzaVvU2Y4/hp/c2AqQbgZRt SEPxYmLBTbHnw== Message-ID: <30419139-6fdd-48df-b32a-9db7575cebb8@kernel.org> Date: Tue, 26 Mar 2024 11:39:33 +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 Yu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2024/3/25 23:02, Daeho Jeong wrote: > On Fri, Mar 22, 2024 at 9:26 PM Chao Yu wrote: >> >> 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: > > You're right. I missed it. Maybe I think we should keep the concept > consistent across devices regardless of zoned storage support. How > about preventing file pinning for already written inodes across all > device types? I am changing the pinfile concept by allowing the users I'm okay with that, or we can tries to migrate data block of target file from seq-zone to conv-zone or regular-device before setting it w/ pin flag... Thanks, > to write on only fallocate()-ed space. > >> >> 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);