Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3590280rwb; Mon, 3 Oct 2022 18:10:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5diragI7JuMuINljzB9TGXDFX0odjgk8v8+Gp37PKHzEbqXAz4TOgAiM3+uHbZ7x5msVwe X-Received: by 2002:a17:907:7f93:b0:781:dbee:dece with SMTP id qk19-20020a1709077f9300b00781dbeedecemr17035041ejc.323.1664845845947; Mon, 03 Oct 2022 18:10:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664845845; cv=none; d=google.com; s=arc-20160816; b=omwIyEY4wZDCyFZiMHGAsSfwFT87u8h3aNCl4CMgkIry6i9SwoRcIVVhNzV/cUVmQ4 tOcDWIMnQ0c7Nellj/HfU+vE5ZeTBIm5mym/1V4r95Jon15qTZK5Rx/QT3WFnZcOR3rH iQr/q5FmRsORwIOHCyeeQameCLhpS3ojqr82CyA0lmVCICE+kLToBTDijaQ1FYW7+BIv w8fnwQnjclH+uIH9mIr3v/3E6IPmBt+7ZrrBqWwyjz+MJtimh4nTgWIsqWpGNCUlrB8i 5HDT9DdCH/RNHgY9JJgSgY80OzsIAHZMcr6EzNuyf+hkkAXdi3IS0vNej2Seh7kXx4TK a2RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nFpalgC2PipcKYFloSxcYBTIza3q51aA8hBhul9K57k=; b=wCVCqPqjWXFYJldVo7yWLyiU0AcxXbkUAXQ32FxzTRWCSYsisLpPlp73ghF9w2fQSq 7n0VPDRgllvCpme/m7t58h3lU0TgLVDDktvnD31rhYQKyiOXKOAbkfd7jJovzDkp9tj6 4sXMcZdzfujDMzzCDXPGWDnFSWGW2KazI4juW7ZHRf7pe3oBYYv9uyQk+eD2DicIUXad zPfIBwg6+Jlc5asq36RV6r7hVhiho3E/77pi38AmHsPnFemc3L0USE77D4wH54H2IvHY PmKMpWWUf/orMzVFdMFtjzeb1y6KYs7w7hBcoGziCV8jvvlt6Wj2aNBpF342uz3mFOYK 3OIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=IoyvBYIK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id wg4-20020a17090705c400b0072aeac5f74bsi10911691ejb.119.2022.10.03.18.10.19; Mon, 03 Oct 2022 18:10:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=IoyvBYIK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbiJDALF (ORCPT + 99 others); Mon, 3 Oct 2022 20:11:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229659AbiJDALA (ORCPT ); Mon, 3 Oct 2022 20:11:00 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A54910EB for ; Mon, 3 Oct 2022 17:10:59 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id i7-20020a17090a65c700b0020ad9666a86so39702pjs.0 for ; Mon, 03 Oct 2022 17:10:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=nFpalgC2PipcKYFloSxcYBTIza3q51aA8hBhul9K57k=; b=IoyvBYIKhuayD2YjwY0cYq3QNOX0EYFBF5M7BH8m+XtYhTHMfzzSwhucaco5m+FIEQ R4zM92wUCzJHSSWqPk77EHNZswj/QmuyrBRzyIsO2KqI2qngRPyLCxrggNXItyPC1T+F POhvhH7Dk/yxY6zWoUHOu2yklP0imtm12TnLgIjw1aouVaLkSDR5qGu2xlhAZQwfds1H ISJV56+r5a/EFdUZ2KeDJPRuSif4/He5XMWozQiVohHVmWMHwrr7sKeC5Q2VukUk21c7 vp0U+87ltVCwsoXajMZkXnaFHIW8um7pb4kiA8IV2nwg2UeA0eCNGY2QBXsnTdW205PQ IkFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=nFpalgC2PipcKYFloSxcYBTIza3q51aA8hBhul9K57k=; b=r+ccZvlZ55l/FJSGiO/3kVEMo36EnWLqyeUMtnc2Cgw1kNXY7Os12kcOi+6bejmHxK GOEBIIBRp4JH/jwT+44YJV6Y5rgnN7LKN8JqEIy8nH+YyjHbf0m8wSaF2M3oA+Y7YltT ouE0WE+cZgT6CLa0iSAfT8ai50nBOlMbTh9i4OQdR5Z/fHxslaiTC+89gHWh+mjP6ABu svW9Wfpv3TYXtafcWiJCdhmeLjQik3iJ0sBaWQna4DbBAtrQrSjY5Zirdbh5zII6LdAd ZltD+mpTPy8v6JaeC/0XCtlBTHSAnQiczXz78QPktYJOrVoCHnAe8yfsu/4oMBxySmP0 4vKA== X-Gm-Message-State: ACrzQf1uIe0di6WO3uHU0BVcjM34oAqFw9GHjSHKxN158g9REMS9oC4T 89cfAYUVmwBlgyIqwjyyVJzQHL/TgPg= X-Received: by 2002:a17:90b:88e:b0:20a:79e9:1a0e with SMTP id bj14-20020a17090b088e00b0020a79e91a0emr12730132pjb.34.1664842258879; Mon, 03 Oct 2022 17:10:58 -0700 (PDT) Received: from daehojeong-desktop.mtv.corp.google.com ([2620:15c:211:201:3d16:3dd7:b030:41a5]) by smtp.gmail.com with ESMTPSA id i3-20020a626d03000000b0052dfe83e19csm8266974pfc.16.2022.10.03.17.10.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Oct 2022 17:10:58 -0700 (PDT) From: Daeho Jeong To: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com Cc: Daeho Jeong Subject: [PATCH v3 2/2] f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE Date: Mon, 3 Oct 2022 17:10:50 -0700 Message-Id: <20221004001050.3269377-2-daeho43@gmail.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221004001050.3269377-1-daeho43@gmail.com> References: <20221004001050.3269377-1-daeho43@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daeho Jeong introduce a new ioctl to replace the whole content of a file atomically, which means it induces truncate and content update at the same time. We can start it with F2FS_IOC_START_ATOMIC_REPLACE and complete it with F2FS_IOC_COMMIT_ATOMIC_WRITE. Or abort it with F2FS_IOC_ABORT_ATOMIC_WRITE. Signed-off-by: Daeho Jeong --- v3: move i_size change after setting atomic write flag v2: add undefined ioctl number reported by --- fs/f2fs/data.c | 3 +++ fs/f2fs/f2fs.h | 1 + fs/f2fs/file.c | 20 ++++++++++++++------ fs/f2fs/segment.c | 14 +++++++++++++- include/uapi/linux/f2fs.h | 1 + 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index 6cd29a575105..d3d32db3a25d 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -3438,6 +3438,9 @@ static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi, else if (*blk_addr != NULL_ADDR) return 0; + if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) + goto reserve_block; + /* Look for the block in the original inode */ err = __find_data_block(inode, index, &ori_blk_addr); if (err) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 539da7f12cfc..2c49da12d6d8 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -764,6 +764,7 @@ enum { FI_COMPRESS_RELEASED, /* compressed blocks were released */ FI_ALIGNED_WRITE, /* enable aligned write */ FI_COW_FILE, /* indicate COW file */ + FI_ATOMIC_REPLACE, /* indicate atomic replace */ FI_MAX, /* max flag, never be used */ }; diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index ce2336d2f688..66d62ea42c73 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -1982,7 +1982,7 @@ static int f2fs_ioc_getversion(struct file *filp, unsigned long arg) return put_user(inode->i_generation, (int __user *)arg); } -static int f2fs_ioc_start_atomic_write(struct file *filp) +static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) { struct inode *inode = file_inode(filp); struct user_namespace *mnt_userns = file_mnt_user_ns(filp); @@ -2051,10 +2051,6 @@ static int f2fs_ioc_start_atomic_write(struct file *filp) f2fs_write_inode(inode, NULL); - isize = i_size_read(inode); - fi->original_i_size = isize; - f2fs_i_size_write(fi->cow_inode, isize); - spin_lock(&sbi->inode_lock[ATOMIC_FILE]); sbi->atomic_files++; spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); @@ -2064,6 +2060,16 @@ static int f2fs_ioc_start_atomic_write(struct file *filp) clear_inode_flag(fi->cow_inode, FI_INLINE_DATA); f2fs_up_write(&fi->i_gc_rwsem[WRITE]); + isize = i_size_read(inode); + fi->original_i_size = isize; + if (truncate) { + set_inode_flag(inode, FI_ATOMIC_REPLACE); + truncate_inode_pages_final(inode->i_mapping); + f2fs_i_size_write(inode, 0); + isize = 0; + } + f2fs_i_size_write(fi->cow_inode, isize); + f2fs_update_time(sbi, REQ_TIME); fi->atomic_write_task = current; stat_update_max_atomic_write(inode); @@ -4082,7 +4088,9 @@ static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) case FS_IOC_GETVERSION: return f2fs_ioc_getversion(filp, arg); case F2FS_IOC_START_ATOMIC_WRITE: - return f2fs_ioc_start_atomic_write(filp); + return f2fs_ioc_start_atomic_write(filp, false); + case F2FS_IOC_START_ATOMIC_REPLACE: + return f2fs_ioc_start_atomic_write(filp, true); case F2FS_IOC_COMMIT_ATOMIC_WRITE: return f2fs_ioc_commit_atomic_write(filp); case F2FS_IOC_ABORT_ATOMIC_WRITE: diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index 143b7ea0fb8e..c524538a9013 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -263,14 +263,26 @@ static void __complete_revoke_list(struct inode *inode, struct list_head *head, bool revoke) { struct revoke_entry *cur, *tmp; + pgoff_t start_index = 0; + bool truncate = is_inode_flag_set(inode, FI_ATOMIC_REPLACE); list_for_each_entry_safe(cur, tmp, head, list) { - if (revoke) + if (revoke) { __replace_atomic_write_block(inode, cur->index, cur->old_addr, NULL, true); + } else if (truncate) { + f2fs_truncate_hole(inode, start_index, cur->index); + start_index = cur->index + 1; + } + list_del(&cur->list); kmem_cache_free(revoke_entry_slab, cur); } + + if (!revoke && truncate) { + f2fs_do_truncate_blocks(inode, start_index * PAGE_SIZE, false); + clear_inode_flag(inode, FI_ATOMIC_REPLACE); + } } static int __f2fs_commit_atomic_write(struct inode *inode) diff --git a/include/uapi/linux/f2fs.h b/include/uapi/linux/f2fs.h index 3121d127d5aa..955d440be104 100644 --- a/include/uapi/linux/f2fs.h +++ b/include/uapi/linux/f2fs.h @@ -42,6 +42,7 @@ struct f2fs_comp_option) #define F2FS_IOC_DECOMPRESS_FILE _IO(F2FS_IOCTL_MAGIC, 23) #define F2FS_IOC_COMPRESS_FILE _IO(F2FS_IOCTL_MAGIC, 24) +#define F2FS_IOC_START_ATOMIC_REPLACE _IO(F2FS_IOCTL_MAGIC, 25) /* * should be same as XFS_IOC_GOINGDOWN. -- 2.38.0.rc1.362.ged0d419d3c-goog