Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp2001060rwr; Fri, 21 Apr 2023 02:47:49 -0700 (PDT) X-Google-Smtp-Source: AKy350aq1xtC1dgyCJEWsisG7yr6wmv/g/UIKSpfYnB53DoBAKgTiKY0lkkCMRB5mvwJS+pgznw1 X-Received: by 2002:a05:6a20:a226:b0:ef:929f:6979 with SMTP id u38-20020a056a20a22600b000ef929f6979mr3636572pzk.0.1682070469708; Fri, 21 Apr 2023 02:47:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682070469; cv=none; d=google.com; s=arc-20160816; b=jpEOaIlvXO3U+Nr4cWZ9BdQqLuJXaP6c+6xZKmZYrUjTWNSSJrykA7Oc8mktLXAgMP e74KawAmSlJk/z5zxMaMzruGxmrCgrB193gqw9wz3HUDQJ+QXaH5OOH0BnCnQJurIKx0 Hmuw/NH6SuR+4XofSrpUyFBluBSJ9tfNy5a3sbhHm92z0SfTyQH7qyaVNmsBNl9gQePi vjQg87hVBqqHXk0xiAnkRTsnhjC/d1hg44SQkt++PbUonIPCaLAtNSQ1eNekElXNDLOM 8QV6vOMRxOVwyPyu3pOOTZMj9bUVC+v7Md71Cs4mByFiUPpL/8MH4E9YHsFHD499kgqh GF5Q== 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=n2loVQHnXEIbRgzz0wwtKakik0SwqtSHBFppUgBXyaA=; b=MsAuZbsYEmA3xK3agRyn2yaYKPlJ0iqEwZkfVTANluQ3onjsowjxxTIF3SaJa3BpCq +3Oa3OF66ClXwJf3PXwx6UcVEhoilIfqKcRcgefFOkqqsMliIPdPVQsAFr8njQcl5h5e wLxNa6pC758Vb8aV8jbtX1E1/BohcqA12dTkZwWV/4d9rMca5rFFlVxreGor42loEZhX SX6JYx2VcJ3LcFrO+JxcDjHkxzO0H7JLq8OHWji9YABf/Zdww+MdLmhBTtfewe+dkLcT n4vEqdwOHQJhH24TpG82bdF6QqK5VKdA3FCgOJJ8ox6pH4HJm/PkEPxbK3kIdz8eWHZ8 qDvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=ig1YH4PM; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 i28-20020aa796fc000000b0063b6e71d1f1si1880291pfq.40.2023.04.21.02.47.34; Fri, 21 Apr 2023 02:47:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=20221208 header.b=ig1YH4PM; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-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 S231469AbjDUJrJ (ORCPT + 99 others); Fri, 21 Apr 2023 05:47:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232016AbjDUJrE (ORCPT ); Fri, 21 Apr 2023 05:47:04 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4C1BBBB2; Fri, 21 Apr 2023 02:46:35 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-63b73203e0aso13007206b3a.1; Fri, 21 Apr 2023 02:46:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682070394; x=1684662394; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n2loVQHnXEIbRgzz0wwtKakik0SwqtSHBFppUgBXyaA=; b=ig1YH4PMLoWN8eaoh/N6KGCMQwWD1ucvBaT9UF34yNxuNnbrJt3nJSIpFm9pFr2Eht M5O3DU1USYqaO1bLFzuy8aa+phJoAYYxvQQhuztBPbIfdb6O/WlaBus1SS2VzAoao62r OKSjplRtMGGhFZP7DQJpVNNSPSiwIY0cZ4/f1I4/mavAifnQC+sY1Habn2LLQv6Fd7mO k1ZIKVtU0i7u86oxmXbUU05nDhhjjx2bBS8cgpXRxIxowLlfUjr9b7tgIjSFePOcZXi2 nPFD6bv7aCJNrxYEMC6NoTpeaftEnGElUjisuG4tOBIOvkAMbhr903QFSmdG83WNKa4l ANag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682070394; x=1684662394; 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:message-id:reply-to; bh=n2loVQHnXEIbRgzz0wwtKakik0SwqtSHBFppUgBXyaA=; b=Fx/ViFnVZ2XJIRi7n/Vy4olBhunNhUbx1m16Ku9HouQiGIs2OwdO4b1DUoBMEwQfTo gV+yhf62GT/4kx8at5F8+f1wC+5RkdlHEB+rq2gnyl470WxM8wErrO8t99cQeXklm+ax Ihp6V0h9WXuFghLOpIdr6QlF7aTNC+diSydYMuqJKmDq0J+oIjYayT8a173m5Vfu+HfI aRCIuE0R+Y2/il/Gqhd7eaG1s+SpLdoD3wS8wRrtRHij3qWlnabeAvGUejXIfeWjBUFk HRsWVGvHvfuqIeCJyolxcIARo6nDDJ6oCszlYj2F1X2nm5CeVonGy4Ur68aH0Eq53A+3 KarQ== X-Gm-Message-State: AAQBX9cPwDFg0el3RP9b/t/p3SNYaCcC8UF+tEH5rd17QDPa8Sol6hDT /esVDoyXxGDiVEIyeLTw3n/AeFhS31s= X-Received: by 2002:a05:6a20:918c:b0:e9:4683:284a with SMTP id v12-20020a056a20918c00b000e94683284amr6414904pzd.4.1682070394733; Fri, 21 Apr 2023 02:46:34 -0700 (PDT) Received: from rh-tp.. ([2406:7400:63:2dd2:8818:e6e1:3a73:368c]) by smtp.gmail.com with ESMTPSA id w35-20020a631623000000b0051f15c575fesm2295376pgl.87.2023.04.21.02.46.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Apr 2023 02:46:34 -0700 (PDT) From: "Ritesh Harjani (IBM)" To: linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org Cc: Jan Kara , Christoph Hellwig , "Darrick J . Wong" , Ojaswin Mujoo , Disha Goel , "Ritesh Harjani (IBM)" , Christoph Hellwig Subject: [PATCHv6 2/9] fs/buffer.c: Add generic_buffers_fsync*() implementation Date: Fri, 21 Apr 2023 15:16:12 +0530 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-ext4@vger.kernel.org Some of the higher layers like iomap takes inode_lock() when calling generic_write_sync(). Also writeback already happens from other paths without inode lock, so it's difficult to say that we really need sync_mapping_buffers() to take any inode locking here. Having said that, let's add generic_buffers_fsync/_noflush() implementation in buffer.c with no inode_lock/unlock() for now so that filesystems like ext2 and ext4's nojournal mode can use it. Ext4 when got converted to iomap for direct-io already copied it's own variant of __generic_file_fsync() without lock. This patch adds generic_buffers_fsync() & generic_buffers_fsync_noflush() implementations for use in filesystems like ext2 & ext4 respectively. Later we can review other filesystems as well to see if we can make generic_buffers_fsync/_noflush() which does not take any inode_lock() as the default path. Tested-by: Disha Goel Reviewed-by: Christoph Hellwig Signed-off-by: Ritesh Harjani (IBM) --- fs/buffer.c | 70 +++++++++++++++++++++++++++++++++++++ include/linux/buffer_head.h | 4 +++ 2 files changed, 74 insertions(+) diff --git a/fs/buffer.c b/fs/buffer.c index 9e1e2add541e..05febfbaecdc 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -593,6 +593,76 @@ int sync_mapping_buffers(struct address_space *mapping) } EXPORT_SYMBOL(sync_mapping_buffers); +/** + * generic_buffers_fsync_noflush - generic buffer fsync implementation + * for simple filesystems with no inode lock + * + * @file: file to synchronize + * @start: start offset in bytes + * @end: end offset in bytes (inclusive) + * @datasync: only synchronize essential metadata if true + * + * This is a generic implementation of the fsync method for simple + * filesystems which track all non-inode metadata in the buffers list + * hanging off the address_space structure. + */ +int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end, + bool datasync) +{ + struct inode *inode = file->f_mapping->host; + int err; + int ret; + + err = file_write_and_wait_range(file, start, end); + if (err) + return err; + + ret = sync_mapping_buffers(inode->i_mapping); + if (!(inode->i_state & I_DIRTY_ALL)) + goto out; + if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) + goto out; + + err = sync_inode_metadata(inode, 1); + if (ret == 0) + ret = err; + +out: + /* check and advance again to catch errors after syncing out buffers */ + err = file_check_and_advance_wb_err(file); + if (ret == 0) + ret = err; + return ret; +} +EXPORT_SYMBOL(generic_buffers_fsync_noflush); + +/** + * generic_buffers_fsync - generic buffer fsync implementation + * for simple filesystems with no inode lock + * + * @file: file to synchronize + * @start: start offset in bytes + * @end: end offset in bytes (inclusive) + * @datasync: only synchronize essential metadata if true + * + * This is a generic implementation of the fsync method for simple + * filesystems which track all non-inode metadata in the buffers list + * hanging off the address_space structure. This also makes sure that + * a device cache flush operation is called at the end. + */ +int generic_buffers_fsync(struct file *file, loff_t start, loff_t end, + bool datasync) +{ + struct inode *inode = file->f_mapping->host; + int ret; + + ret = generic_buffers_fsync_noflush(file, start, end, datasync); + if (!ret) + ret = blkdev_issue_flush(inode->i_sb->s_bdev); + return ret; +} +EXPORT_SYMBOL(generic_buffers_fsync); + /* * Called when we've recently written block `bblock', and it is known that * `bblock' was for a buffer_boundary() buffer. This means that the block at diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h index 8f14dca5fed7..105b67c2a7a8 100644 --- a/include/linux/buffer_head.h +++ b/include/linux/buffer_head.h @@ -211,6 +211,10 @@ int inode_has_buffers(struct inode *); void invalidate_inode_buffers(struct inode *); int remove_inode_buffers(struct inode *inode); int sync_mapping_buffers(struct address_space *mapping); +int generic_buffers_fsync_noflush(struct file *file, loff_t start, loff_t end, + bool datasync); +int generic_buffers_fsync(struct file *file, loff_t start, loff_t end, + bool datasync); void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len); static inline void clean_bdev_bh_alias(struct buffer_head *bh) -- 2.39.2