Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp773357rwb; Tue, 4 Oct 2022 10:24:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6/3k33bSzaXFZWZEsrIn2PnN1f75+cSVHD7iZueZe7h4en5AEH6AxL8EiTTj59+z+GK5IT X-Received: by 2002:a17:903:230b:b0:177:e667:7862 with SMTP id d11-20020a170903230b00b00177e6677862mr28308660plh.154.1664904241828; Tue, 04 Oct 2022 10:24:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664904241; cv=none; d=google.com; s=arc-20160816; b=bQ7cBzC/p2U6db+6ipEfXRfD3GnFHV6lSwleHV9VSQodqkJg4qzosoYlkxmCclK75M dDi2wp4O3okCnrDx9dJqKUybBXp2AN4aTLXIs89SULE6jo1H4MXWM7aS2GQ5pjimaV7u BeNWh4s/wDsifnpyDUPQyGODC9AnxFCxLJvo692acs5SyRg8NHHmNpOTt4biqC+4u0Jf dbWjNBJhrFe5MPFW8/xDV196RPGWAen/Z9pUhSlL3zVMwUR3rQ6ma11pAWKJ4UOO2Xj7 j2h1psjK6vGzHLfHih7wDMkRlRFmZkWWadLE9eWKlI92fHGVoLR2lF7dl0nJFb4sdW1c WYyw== 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=TR19ErKJDLX3klMzWsPtAp4FJxjfq89DPkRaUC4/w+k=; b=C44CuGMHIqVu7zzj9qBLER705PBg7H/UyJnlPQ691ji45FSSKe7VhnflMIN6zZxJOz lLzQvd20eyHlAkbR2xrfk5f0azaXqJkB2eq/hdYMAS+g0hEbO1j5peEggXIhbiJb2gPS TaSJQSLtPt+xaC/ilQ0rg5L81jHWQEhDMSKi9CSyyl/dnnFOP6ssPzL88V/U4FW9zZ0S YhsPRu25Lj9/19sUyDvdhLtmPItsHyMcH/OePlxQ7qSIa8Cq6VQmlODSoqJYhdueREhB 7xHKNCgFcXG6oEjzsUB6MFiyQj3M6n00hrlcbffgpQ+lMJNLo6TChMMybgFHTe+m4PZt Pvmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DEHpmf1s; 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 e11-20020a170903240b00b00179e70b9e6csi13554201plo.519.2022.10.04.10.23.50; Tue, 04 Oct 2022 10:24:01 -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=DEHpmf1s; 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 S230029AbiJDROG (ORCPT + 99 others); Tue, 4 Oct 2022 13:14:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229999AbiJDROD (ORCPT ); Tue, 4 Oct 2022 13:14:03 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68C025D11B for ; Tue, 4 Oct 2022 10:14:02 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id d11so13211126pll.8 for ; Tue, 04 Oct 2022 10:14:02 -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=TR19ErKJDLX3klMzWsPtAp4FJxjfq89DPkRaUC4/w+k=; b=DEHpmf1sd2XqvwkfLMcqkDucCoP0pV4XqvkpBJzOj0ETtG7DmeRoTYh1u1Sthrxbd9 57xk0Yu2LWut/tZV2c1dPxVgpiVu0fBOocIvx+dlAeTiNJAEcuFN41R3EsnWmpl5jPpT NQagkZGx4UT8rvuAlmvvgFclT72bZYXPAEzKEKfW2VFUwvwaQAg4Sl7Ht20NcY0ST76/ 731S17/FHSB6vZE05CB+715uxe2uQv350gEZ6Gw/qS2nWCdoueCbF4A3mz2WakSFxgmq J/wCmPnMVftkOaWlTS5YJ0hudCVFpxiI56rTC6Uo0hTX7M/l11MjETdr7BNVTHrccNf0 tg4A== 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=TR19ErKJDLX3klMzWsPtAp4FJxjfq89DPkRaUC4/w+k=; b=7go23sph7NuuE9HKERaGBj8GsFD4QUcMY4D2Y7yt1fF8bMv8yqWprT2AjCZDK4HnpY sKl68bJzhxlHRgv2pnKUDvADW3rVW+OXuK9b1+pkmoNftE2h8i1T34hn0D7sU/CQVkJM yIcn/jNvrkIl5HM3D7PYx9VpcN5nUWzSHamrU99SH8TiBCwDdeyoLHb5hV05zNgTSvxd uELo6zcVGcEXIZPtOl44kXHrIUdIBtsGy05gaQsy2WKbUtnQEMGSTYr0rREI2cdUPb9v /gdtrrQXoABqj60si0Lgu7nwaGjIQcaB//nFYT89licF8iDK49NhlprLqg/ldk6mkkG5 7Q4Q== X-Gm-Message-State: ACrzQf1q2exDSD7Rg/42QLxD70RIDnss51fdhrbL3q+r+M8GW5UIssIn qMsWYwbtnp+H2uQY+6ycULKej5L9tWs= X-Received: by 2002:a17:902:ebc7:b0:17e:7378:1da8 with SMTP id p7-20020a170902ebc700b0017e73781da8mr14408668plg.152.1664903641497; Tue, 04 Oct 2022 10:14:01 -0700 (PDT) Received: from daehojeong-desktop.mtv.corp.google.com ([2620:15c:211:201:9291:ff24:9df5:5be7]) by smtp.gmail.com with ESMTPSA id p63-20020a622942000000b00561c55c56a7sm1683693pfp.94.2022.10.04.10.14.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 10:14:00 -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 v4 2/2] f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE Date: Tue, 4 Oct 2022 10:13:51 -0700 Message-Id: <20221004171351.3678194-2-daeho43@gmail.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog In-Reply-To: <20221004171351.3678194-1-daeho43@gmail.com> References: <20221004171351.3678194-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 abb55cd418c1..e4f7efbdc027 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -264,14 +264,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