Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp852462rdg; Fri, 13 Oct 2023 03:14:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH9i1Up2Jw3dix7IpYoXOTGzimcJlfXTDn9LGlsCVGHcZ14Gqk6YxDC3SEbhE4Jf8GT0DKa X-Received: by 2002:a17:90a:4292:b0:279:be6:bf73 with SMTP id p18-20020a17090a429200b002790be6bf73mr22727851pjg.11.1697192085048; Fri, 13 Oct 2023 03:14:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697192085; cv=none; d=google.com; s=arc-20160816; b=oQoCGqkirjSW3iHu4w/MuWTMH+O+/fKbd7gTahpQt+Pc0ypv2gKg4N6kgE8wOAzlh+ 9NY0YUeC9hPGfg/lL095mP3GMvID3sJF295q+LA9AxYKafKJyMXMisXI6narUwpslssh YWrkK8jR0xj5KiG5Kaw0MzsFpeX8qmNP1Uzxeb23ocAzrXBg//N+ymWWdOB0kPd85xPe E3AgDPhxE+ncXauwo+QlndCqbv60+Dx4YoZy2eRELnIbMmieJyTge1ujrX+VBSHseRSZ c6oty2J1x5tdvnAUuJFLcyjkoLdXFIdUDr0+k8CwvAv6qaH8ftSbWTE4F6YtqWeX6E35 Xe7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-signature; bh=ZHqOW8nf34OSl8E8wbbUPuB+73cpvAlYusVvHrKdlS8=; fh=xIyBEtf5c9l2fLbmVgffDXWkgf2Fk9ne5qKQ4RK8NBQ=; b=USFYN2vsApUkK+bCfIErPJcIleOd1QAUi/05r/eOAy/2h1JWSlBR6nZe0tG4jp+QW8 NJaRuxWRXXPXjG7ywgCaFGsr2q54XpRgW8LigenzzRMHwtYxX/88Vx+K6GkyIqgj0vfN Gq0nCRLTHmBZoVRWzx29CkMtw7NG5wGHgnabUrmW6VvCYZekbNNqwH3GjBHTG4S4asE/ Jmh+mjnRx9l+8v6nLE3kcR5y5Cq1WoPwyxzSlrsXwZ3/FEU1TcxGDn50oAGqmdfqFvf3 9ZjXW0byWJHaPXq+RiXZk6TW2qhVgIGTfDtaHy/+0G2/naB0GTKRgrngL8cLOW8QpGhF 5uWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=YNjml1P4; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id a193-20020a6390ca000000b005acd2009192si547178pge.13.2023.10.13.03.14.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 03:14:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=YNjml1P4; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id E136181BDA61; Fri, 13 Oct 2023 03:14:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229942AbjJMKOa (ORCPT + 99 others); Fri, 13 Oct 2023 06:14:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229900AbjJMKO3 (ORCPT ); Fri, 13 Oct 2023 06:14:29 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAFDCB7 for ; Fri, 13 Oct 2023 03:14:26 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 3331E1F37E; Fri, 13 Oct 2023 10:14:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1697192065; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ZHqOW8nf34OSl8E8wbbUPuB+73cpvAlYusVvHrKdlS8=; b=YNjml1P47f50HsyrTxVfNxDdZGT8ejC6dS6di4REIi8eLw8n/9clBiqPDzUGoquIiC2f8D 1/4qONZuQXIidKtX6ZdaL6t+0KRgNLxuloF93pYDp4wvAyTtkCW5P7Y/MIBbHELNu3TdPN BTJ3m8su2JCWpspPmxtJyXsFOmrQtIc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1697192065; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ZHqOW8nf34OSl8E8wbbUPuB+73cpvAlYusVvHrKdlS8=; b=yOwZTgVzzdggbX/Tpdqt8qjXWDnRSIra6mrrL6dBMxp0hPqjnVUakDYTv1JBHwuANOPT2R MH7apz7Tj2z062BQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 169CF138EF; Fri, 13 Oct 2023 10:14:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id YdlfBYEYKWUEKwAAMHmgww (envelope-from ); Fri, 13 Oct 2023 10:14:25 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 96973A05C4; Fri, 13 Oct 2023 12:14:24 +0200 (CEST) Date: Fri, 13 Oct 2023 12:14:24 +0200 From: Jan Kara To: Dave Chinner Cc: Jan Kara , Ted Tso , linux-ext4@vger.kernel.org, "Ritesh Harjani (IBM)" , Gao Xiang Subject: Re: [PATCH] ext4: Properly sync file size update after O_SYNC direct IO Message-ID: <20231013101424.ctn5zfpntu4kjyl4@quack3> References: <20231011142155.19328-1-jack@suse.cz> <20231012085937.pzfsttumi6q4g3tm@quack3> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -5.10 X-Spamd-Result: default: False [-5.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; BAYES_HAM(-3.00)[100.00%]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; TAGGED_RCPT(0.00)[]; MIME_GOOD(-0.10)[text/plain]; NEURAL_HAM_LONG(-3.00)[-1.000]; RCPT_COUNT_FIVE(0.00)[6]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.00)[-1.000]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; SUSPICIOUS_RECIPS(1.50)[]; FREEMAIL_CC(0.00)[suse.cz,mit.edu,vger.kernel.org,gmail.com,linux.alibaba.com] X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Fri, 13 Oct 2023 03:14:42 -0700 (PDT) On Fri 13-10-23 10:33:08, Dave Chinner wrote: > On Thu, Oct 12, 2023 at 10:59:37AM +0200, Jan Kara wrote: > > On Thu 12-10-23 11:26:15, Dave Chinner wrote: > > > On Wed, Oct 11, 2023 at 04:21:55PM +0200, Jan Kara wrote: > > > > Gao Xiang has reported that on ext4 O_SYNC direct IO does not properly > > > > sync file size update and thus if we crash at unfortunate moment, the > > > > file can have smaller size although O_SYNC IO has reported successful > > > > completion. The problem happens because update of on-disk inode size is > > > > handled in ext4_dio_write_iter() *after* iomap_dio_rw() (and thus > > > > dio_complete() in particular) has returned and generic_file_sync() gets > > > > called by dio_complete(). Fix the problem by handling on-disk inode size > > > > update directly in our ->end_io completion handler. > > > > > > > > References: https://lore.kernel.org/all/02d18236-26ef-09b0-90ad-030c4fe3ee20@linux.alibaba.com > > > > Reported-by: Gao Xiang > > > > Signed-off-by: Jan Kara > > > > --- > > > > fs/ext4/file.c | 139 ++++++++++++++++++------------------------------- > > > > 1 file changed, 52 insertions(+), 87 deletions(-) > > > ..... > > > > @@ -388,9 +342,28 @@ static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset, > > > > */ > > > > if (inode->i_nlink) > > > > ext4_orphan_del(NULL, inode); > > > > + return; > > > > } > > > > + /* > > > > + * If i_disksize got extended due to writeback of delalloc blocks while > > > > + * the DIO was running we could fail to cleanup the orphan list in > > > > + * ext4_handle_inode_extension(). Do it now. > > > > + */ > > > > + if (!list_empty(&EXT4_I(inode)->i_orphan) && inode->i_nlink) { > > > > + handle_t *handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); > > > > > > So this has to be called after the DIO write completes and calls > > > ext4_handle_inode_extension()? > > > > Yes, if the write was setup as extending one ('extend' is set to true in > > ext4_dio_write_iter()). > > Then that is worth a comment to document the constraint for anyone > that is trying to understand how ext4 is using the iomap DIO code. Fair enough, comment added. > > > > @@ -606,9 +570,8 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) > > > > dio_flags, NULL, 0); > > > > if (ret == -ENOTBLK) > > > > ret = 0; > > > > - > > > > if (extend) > > > > - ret = ext4_handle_inode_extension(inode, offset, ret, count); > > > > + ext4_inode_extension_cleanup(inode, ret); > > > > > > Because this doesn't wait for AIO DIO to complete and actually > > > extend the file before running the cleanup code... > > > > As Gao wrote, ext4 sets IOMAP_DIO_FORCE_WAIT if 'extend' is set (see > > ext4_dio_write_checks()) so if we get to calling > > ext4_inode_extension_cleanup() we are guaranteed the IO has already > > completed. > > Ugh. That's a pretty nasty undocumented landmine. It definitely > needs a comment (or better, a WARN_ON_ONCE()) to document that this > code -only- works if AIO is disabled. This isn't for ext4 > developers, it's for people working on the iomap code to understand > that ext4 has some really non-obvious constraints in it's DIO code > paths and that's why the landmine is not being stepped on.... OK. Ext4 has always been this way so I never felt the need to document it but you're right. I've added: if (extend) { /* * We always perform extending DIO write synchronously so by * now the IO is completed and ext4_handle_inode_extension() * was called. Cleanup the inode in case of error or race with * writeback of delalloc blocks. */ WARN_ON_ONCE(ret == -EIOCBQUEUED); ext4_inode_extension_cleanup(inode, ret); } Thanks for the suggestions! Honza -- Jan Kara SUSE Labs, CR