Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2814096lqp; Mon, 25 Mar 2024 09:56:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUnLLN/mFocwfEOcr8X8e4oh+puncRgOBBrmmwHjrilUSRbZuc8i/VSTltg6ISc3pskbSmHHoTHpXcsYzc1pASW5FcKJtmhevRY7LFcXw== X-Google-Smtp-Source: AGHT+IE2Ey7nolgW+Fudc4nTfSXbWAW0r4g7fgque5AXyx6LmuK7TzAnKFb7Ia5TiyNdzZ9eNHsb X-Received: by 2002:a05:6a21:6d84:b0:1a3:bd7e:c42c with SMTP id wl4-20020a056a216d8400b001a3bd7ec42cmr478693pzb.8.1711385767526; Mon, 25 Mar 2024 09:56:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711385767; cv=pass; d=google.com; s=arc-20160816; b=Rs7FS7kRduEJ75LqZH/p499tLlRKeUDVTD+aBY6lGtAgo6cgklCHw6MYu5MI/aF8Uz hfU3KXMo12qVJbDfqhzc21QDspAcnRO1IO1TKD5BIHO3og4F6xpNhGqDkupyBGvV5ew+ 0bJopmI2qH3plfQkSpVHKvYLFQ583D14EeLPX1HaMlHowfd4mA2PkY05nHO5HvlSoK1+ 2pMHkC8ghR2NFNWTSp7XwKlsEugxduKmODnvlQzZL7K++c8qfsBCOUfUQKUHSH7y8izb hRtF8E0jCjPFO6zJqclWYRq0r+1+m7oo+URAr/0vEQQ9A8LArmW40TeLfADLZIQZGe6E badw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=EgMCYmFkhuYojEFo2nYez3F6tl2yguGZg3Z/AU6fEKk=; fh=sPilOYAT4QfMgYxGH2LjRrcqbLUzvrQqMQUicHZMufQ=; b=0m+uZPDKFgUyLzNW9jFUQpS6XrHhTdQxE9UHAilPeZZ9FhpjN/kEirj58z5vuwrhW5 0tKdurkOWpIBOrEbxQiQmuLsJAWj36r7c3poOiLVRd6uu3m/G3FPxH/29jkYbdEwJLly 4M/E35DiwZxC2SMdhqJwWp+1xXE43Q8ihi5Y8mOLG6InvOpRrnetpwSn9X+Do4Kg7Nnh 0vNWqbGZW0sf3ugklZfUWe2jERrgcob4ToymYDAbgtHmh+YLDLl50sAgoG+LhajIit3Q NZqwhX3USIOCXuTUxfkFhK3Ly55872bEevZMkMaQu3DUhbovzgosuVujbs9/ZtxFX5aR J0tA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JOvqTvIb; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-117321-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117321-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 23-20020a631357000000b005e47afd5c60si7882300pgt.646.2024.03.25.09.56.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 09:56:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-117321-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=JOvqTvIb; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-117321-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117321-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 BDAFA304617 for ; Mon, 25 Mar 2024 16:48:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8E1971384A6; Mon, 25 Mar 2024 15:02:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JOvqTvIb" Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 04E1F7EEFE for ; Mon, 25 Mar 2024 15:02:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711378954; cv=none; b=kAkeafhZplu4oWI283FtBkAjVli9dyQOW4cto76dTTz7aknmDI4XeVjiZUf2sAQfneVEzmC+JwVBQEHR5sJ8BdUD+aAr38KrVbrruF9t9OaMbfxCZUiQfA0dzV5iQQzPelHVUw/ib3EQqhZhYeygYm01b9efohVJ8xjw91bYT68= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711378954; c=relaxed/simple; bh=k0xDgOSMDQbGv76F+USpe31dZqIKJwFIb1GGpeF3ZQY=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=VOxJR9sEDYnQyFFdfi6VT3uL/bPzgHNPaeABp+/0tNsbZ+rb1YH2JYDYfWzWHn3KF4AD2bDhF1E5SooOZiXWcumShrb6uWngKSrFcxumT+C9AM3N3Z2OWgHYqIAIHVOJZfp9vw+1Uje+dnoGPeBKtSq2GC+3Olsi9y5t1qfYA84= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JOvqTvIb; arc=none smtp.client-ip=209.85.167.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3c396fec63aso1523458b6e.0 for ; Mon, 25 Mar 2024 08:02:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711378952; x=1711983752; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=EgMCYmFkhuYojEFo2nYez3F6tl2yguGZg3Z/AU6fEKk=; b=JOvqTvIb61W4pCNOgTje3IMF8PbMbGVfLFgfTLsFSxkL4T6E7LSQFoOCGQ2r7ZUlO3 lS7hSxXlG3B6i+Tp2OcY1E+lKnO3mtQZHeNvxdVRl11cVxkSd1aYA6AnWy/ILhuIPtN+ jF/OpqRS9JVgMuXjsPsk42CBjJHvTEHKRuNoV2Ej5efbNk8MoXshNjX/k6ghUnBbtchR mTaTRdajD8FOJLEPzEEUV9VWys3cBnvU9b6+p5vOa7GiRmVCZBUB6j7ibDP0Lv8xh/ug hkl9s+tOlg1RaC19Sm577V0TMB0v2PXRMPYfKUtNsm90gLAev/stKntPHAMscKBI4DFX dLxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711378952; x=1711983752; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EgMCYmFkhuYojEFo2nYez3F6tl2yguGZg3Z/AU6fEKk=; b=elC65A/AMi/ERcsMnOAtJs+6liBUGQcH8sypED4JyboP5Ldx2t2CTSQMlXTxhg+ENa 0MxFqiJYwklWlHt18qfFaOYzvtt6JOrUc/4nL0yuIsJukDBSnrylzWyC0be4DQCoW7tr iYk122Joss9kRwM7wK4K+sKBPYVXYmcoQ3CWzVfbdsmTLsEsa8WAUmg1D9tG71DoptV4 +1m3O5kRSpLrugamkwwf4zUb6S+e47ZzwMi8Dhe5wQ3Ou34shG3qjr7cFVLhnscvP1H8 q16pC+sypErdDdctrXzn813VQ4JhnBbzAFVzRNTRANABP9Ux2KImMjPnOZQOexIffNNz XdPg== X-Gm-Message-State: AOJu0YxOiWv3IZatOxAG0gFOjPPmmKZNTLQC9Wk1eaOYCqlCv9nUbrck AxwGMCb3gOPVNZNl93K8PbKGOMW11VJlTJPuY0eimSZhUFdRYW0nqBU3kiHLzl0++uysc77s78U DUCpG4g9GS/MeHZGiBj1arwo2PsA= X-Received: by 2002:a05:6808:ec8:b0:3c2:39d1:f111 with SMTP id q8-20020a0568080ec800b003c239d1f111mr56649oiv.48.1711378952070; Mon, 25 Mar 2024 08:02:32 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240319212316.4193790-1-daeho43@gmail.com> <712f380c-68ef-4743-bd9b-7342e838ced7@kernel.org> In-Reply-To: From: Daeho Jeong Date: Mon, 25 Mar 2024 08:02:21 -0700 Message-ID: Subject: Re: [f2fs-dev] [PATCH v3] f2fs: prevent writing without fallocate() for pinned files To: Chao Yu Cc: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com, Daeho Jeong Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Mar 22, 2024 at 9:26=E2=80=AFPM Chao Yu wrote: > > On 2024/3/21 1:42, Daeho Jeong wrote: > > On Wed, Mar 20, 2024 at 2:38=E2=80=AFAM 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 alloca= ted > >>> 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, struc= t 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 =3D=3D F2FS_GET_BLO= CK_DIO))) { > >>> - if (unlikely(f2fs_cp_error(sbi))) { > >>> + (is_hole || (f2fs_lfs_mode(sbi) && flag =3D=3D F2FS_GET_BLO= CK_DIO && > >>> + !f2fs_is_pinned_file(inode)))) { > >>> + if (unlikely(f2fs_cp_error(sbi)) || > >>> + (f2fs_is_pinned_file(inode) && is_hole && > >>> + flag !=3D F2FS_GET_BLOCK_PRE_DIO)) { > >>> err =3D -EIO; > >>> goto sync_out; > >>> } > >>> @@ -3378,6 +3381,8 @@ static int prepare_write_begin(struct f2fs_sb_i= nfo *sbi, > >>> f2fs_map_lock(sbi, flag); > >>> locked =3D true; > >>> } else if ((pos & PAGE_MASK) >=3D i_size_read(inode)) { > >>> + if (f2fs_is_pinned_file(inode)) > >>> + return -EIO; > >>> f2fs_map_lock(sbi, flag); > >>> locked =3D 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 =3D -EIO; > >>> + goto out; > >>> + } > >>> + > >>> if (locked) { > >>> err =3D 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_fa= ult *vmf) > >>> struct inode *inode =3D file_inode(vmf->vma->vm_file); > >>> struct f2fs_sb_info *sbi =3D F2FS_I_SB(inode); > >>> struct dnode_of_data dn; > >>> - bool need_alloc =3D true; > >>> + bool need_alloc =3D !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 to write on only fallocate()-ed space. > > if (f2fs_sb_has_blkzoned(sbi) && F2FS_HAS_BLOCKS(inode)) { > ret =3D -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 =3D 0; > >>> vm_fault_t ret; > >>> > >>> @@ -114,19 +114,15 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct v= m_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 =3D 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 =3D f2fs_get_dnode_of_data(&dn, page->index, LOOKU= P_NODE); > >>> f2fs_put_dnode(&dn); > >>> } > >>> -#endif > >>> + > >>> if (err) { > >>> unlock_page(page); > >>> goto out_sem; > >>> @@ -4611,6 +4607,10 @@ static int f2fs_preallocate_blocks(struct kioc= b *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 =3D F2FS_BLK_ALIGN(pos); > >>> map.m_len =3D F2FS_BYTES_TO_BLK(pos + count);