From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.218.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796284166; b=bkyPSVMhovn8a+Vbqu3Ru6bTK3l06Cv7e/a2L1Fbwq/eUuNas1aU9TA6D7aOk13da7Gg BJKyf4mXUypOmyjWZ1fcHmEgaX0FM2IDbHYEaei3ND8G8QreN0mta9z1lRYy2RjNx4YWI hyfztDE61C5gKispYqQmbq7C1tl/5b5uQGOSTzcUpx4PYz2idTVacJF3sJZ3qEdOyM3OX mjRDvtESZSIuYsFpIyzUpzFBmuJ6vxgyhwSkGGA238Mjw3qBOxu2Z6GP3nfiYeqYJwB/h bxI1pN/gt8EPC/gEPIXqyicSbA+NgsncyFPXU+9iQEqs9xiX4HQLRQB6gk1yHKUYdaw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796284166; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References:From: Content-Language:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=vfZIULogg+4koJQbd5UatPvVdPEOD1OgBPvhb1QjaAg=; b=ixdKLY0sjaPbH+I7X1vrW908dlvFiMbh2Iy2YY8nflkVj//pWqCfzCLQtNhIzFDXXC1A Gf7Kl90gRaxqmKkpLW7NHszrSkVETmnd0JHw/Zz30CKLM+p6IvRX2lQJl9x0sH1BEmKAS HdsEnjZScskyqobUfNnjx1+78jwFwYnpzzbxZAdpyNa3w2pPKL8GNCMqGYMmixkSVbEyX aPjf7LNtqETAmYWGzcmXUAjIj8K9mTLbKM8IeNudZU4SYN08SQ2/vNFxfTLoM86Vzb8tj VZlVuhu/69BmdxEkF/nOKtC6bzzl3I41nbis1H0ddkli1pqwNzK/xXh3nTtD8MgMLIA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=IpLHDa02; arc=none smtp.remote-ip=209.85.218.41 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08750606A322 for ; Thu, 3 Dec 2026 07:49:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 08750606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.a=rsa-sha256 header.s=google header.b=IpLHDa02 Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-c524aaf5f23so4996866b.0 for ; Wed, 02 Dec 2026 23:49:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1796284164; x=1796888964; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to:content-type; bh=vfZIULogg+4koJQbd5UatPvVdPEOD1OgBPvhb1QjaAg=; b=IpLHDa02aubYWL4R/radk9czmqdfnJbE+wP7xt0yO+XVBEAuHCnUbUKoPf4u1SAwTP v6x5BqeNfe/AhRoiX6D5Al2tLea2Pu0Mo1qRS0PfAE8mp2E1Q3Q7MVwqsKaTzbBS+Ocm DbwyCnDb4BYvaDAiASjw26WDx/d8kx+4cCy2b+r9kQe/HoSdSELlsio+dYiJqkpfaZpg 87VgyFDUvOrYo0/uoOPVge5NXxZsMEtnxKtmjeBdQBIWRQE+ZGcmYLUczMTfcCv+4ORg ytxln5wdvQ6VC3iMHTufwDeddWwGAKMWWhins1rYAF+DiJdjLgGT3Tg5h+BN/QpObBh/ zQsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796284164; x=1796888964; h=content-transfer-encoding:content-type:in-reply-to:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vfZIULogg+4koJQbd5UatPvVdPEOD1OgBPvhb1QjaAg=; b=uX3O4LL73vmMeANHV+CzyX5uubKHhI/pt7MdQaKutrwrglmUXQ7lMQhSHo6jWwkohe +fPQRc5OGd7wMPZ03KAQ2zn34l7urAf4ahb+baHOkenXtrEZH5jHcRonWLrZKUQJM+oS LdFgN33XYDit2aovX23yuG8QI5TwgdMQETI5jKgAC7y79abI0PIZu6y+RF/uBfxt4kAU teFadx+Tk51OsuXPA+QLvAEiSY/sQmq8jlA7x5a2YgdG7fG0qxt0lqYTOVpwKW7+X8Yt UHVNLeK4Y5QPa6bdFsTJkWYiBS45D78pGzvEibScMhkXeRlXMQGsMfc2fQaqQLzjLg2a SRMA== X-Forwarded-Encrypted: i=1; AL+hErEvxzhvG2gEblwKujGJWAk0wG/EQx00J0HyDdyhjYiSMnDd7IDSmmfraKn5jPAS0GHyENTDpo8HD9m4/1A=@vger.kernel.org X-Gm-Message-State: APuU4Z+QNjXidds/jFll+id3ioQetcQjnyyBVUjqCKnK6Pm/u/HL+Q7C rhoEFqIaxsUwDywHPsDvQ82kobNWLuGIX0vVyN/FBwL3f2ojgoNtNtlhugm8gzO+AGU= X-Gm-Gg: AZFdTZehVHiAPo81LhKyvfMs5rB/dlcvPHaYrAv3O7aiTZOoOWFwxtjmnpO8tfRkNTM BkVwLOfjetv76yiTMwNN7SXYLFCYXMQWNGzldhRpdb7YondTgWZ4BFFQWra3QOJTbY03EDWcjRx wP2+0PYE1QDy+0qac+D/J8YeME1tocndjWx6O/dT3T98NpF2YlCpirpKYtUuwhwbgfn9HhKNYL7 do4Cxq5hCIYkUET1HQDaJUPI2xUJBsP53O701TBN3ozjMMmhOwhvvY6fUo1oGIrklmh5Evlk+F0 Z8M+DahMeFQx6hlNinZRqLgnD2zREYRobwoLJnDylhyvr2rGLqfH5vfRNQ1EkFMtBCDrhuBsHM1 LTTWVw48BaAyrX8Tm7ARF7PuxcmyfIytgbJL2cV/8uvtjH9tvRC/xMWzw7KZ9AQL0Qllu5HNfpE 6u+b+Dv9hHjb0wZvRdMQVKYHthqc8+Hs09foETuRTejmo62L6U3FhCaZsIzB5sM3NOa6tt3BG4d dPYZduRXj3IFNfUAwdcirGlDk7zGT/HVa/1/75L3SG7gqVyvct1bdsjUTop5eulpt9Ok2r7scbP 6N1+LcLO/NUo82s7P+9lMmcJ63GwyIPo8w== X-Received: by 2002:a17:907:a608:b0:c52:3341:b4f1 with SMTP id a640c23a62f3a-c524ab93d7emr74540766b.1.1796284164374; Wed, 02 Dec 2026 23:49:24 -0800 (PST) Received: from [10.202.112.30] ([202.127.77.110]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7d10d11sm3019258a91.6.2026.12.02.23.49.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Dec 2026 23:49:23 -0800 (PST) Message-ID: Date: Thu, 3 Dec 2026 15:49:18 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v15 07/10] ocfs2: switch dio write path from buffer_head to iomap To: Joseph Qi Cc: Andrew Morton , Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, hch@lst.de References: <20261202143507.1754776-1-joseph.qi@linux.alibaba.com> <20261202143507.1754776-8-joseph.qi@linux.alibaba.com> From: Heming Zhao Content-Language: en-US In-Reply-To: <20261202143507.1754776-8-joseph.qi@linux.alibaba.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 12/2/26 22:35, Joseph Qi wrote: > From: Heming Zhao > > Replace ocfs2_dio_wr_get_block() with ocfs2_dio_wr_map_blocks(), which > hands iomap a mapping of as many clusters as can be described by a single > struct iomap: up to 16MB, stopped at the first cluster that is not of the > same kind as the first one. ocfs2_iomap_begin() serves writes through it, > and ocfs2_dio_end_io() converts the unwritten extents the write recorded > and updates the inode size. > > In the buffer_head path, ocfs2_dio_wr_get_block() stores dwc in > bh->b_private, which lets it detect the first call for an I/O chunk and > invoke ocfs2_add_inode_to_orphan() to protect the extent allocation. > iomap provides no similar private field, so ocfs2_add_inode_to_orphan() > moves to ocfs2_file_write_iter(). > > It is also impossible to pass the dwc to iomap_dio_ops.end_io: the > callback cannot distinguish between I/Os initiated by different threads > concurrently, and without that context it could delete an orphan entry > created by another thread. The orphan removal therefore moves to > ocfs2_file_write_iter() as well, where inode_lock() serializes the writers > and IOMAP_DIO_FORCE_WAIT makes an extending write complete before the > function returns, so i_size is already updated when the entry is removed. > > iomap calls ->iomap_begin() once per mapping but ->end_io() once per I/O, > and the latter only gets the kiocb, so the kiocb is the identity of the > I/O: ocfs2_file_write_iter() passes it in iomap_iter.private, > ocfs2_iomap_next() forwards it to ocfs2_dio_wr_map_blocks(), and each > unwritten record stores it in ue_owner. ocfs2_dio_collect_unwritten() > finds the records of one I/O by that owner. > > A direct write waits for unwritten records belonging to another dio before > it takes the re-write fast path, not after. A completion which fails part > way through its conversion has already made some clusters written and > dropped ip_alloc_sem and the inode lock, but it still owns the records and > is about to write the zeroed head/tail buffers over those clusters. The > fast path would see them as MAPPED and hand them out, and the new data > would be overwritten by those zeroes. The records are only removed after > that cleanup, so waiting first excludes the whole window. Under > IOMAP_NOWAIT the wait becomes -EAGAIN. > > Unlike ocfs2_dio_wr_get_block(), the zeroing of the gap between i_size and > the write is done once per dio by ocfs2_dio_zero_eof() before the io is > issued. Zeroing it per mapping would zero blocks that earlier mappings of > this same dio have already written, because i_size is only updated at > completion; that made xfstests generic/538 fail. > > On failure only the first and the last cluster of the touched range are > cleaned up, through ocfs2_dio_cleanup_clusters(): those are the only > clusters ocfs2_grab_folios_direct() fills folio windows for, and the > clusters in between can hold dirty folios of other writes sharing the > cluster. ocfs2_write_range_buffers() writes the buffers directly when some > clusters may already have been converted, because generic writeback > refuses to submit folios beyond i_size. > > ocfs2_dio_clear_orphan_locked() only removes the orphan entry. Clusters > beyond i_size are left alone: there is no on-disk way to tell what this > write allocated from unwritten extents preallocated with > fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the > preallocated space as well. They stay until a later truncate or recovery. > The in-memory marker is only dropped once the entry is really gone, so a > failed write leaves it for the next extending write to recover, which > ocfs2_add_inode_to_orphan() already does before adding its own. > > The pid marker is a strict ownership token: every path that leaves the > entry behind zeroes ip_dio_orphan_pid, so a later write by the same task > cannot match it and remove the entry without the truncate recovery. > ocfs2_dio_clear_orphan() only removes the entry once i_size covers the > range the write meant to write, or nothing is allocated beyond i_size; > otherwise it leaves the entry for recovery and gives up ownership. > > Writes that span EOF may trigger an i_size update at completion, and > within i_size there can still be uncommitted metadata such as the CoW done > by ocfs2_prepare_inode_for_write(). Both set IOMAP_F_DIRTY so iomap cannot > take the FUA-only fast path, which would clear IOMAP_DIO_NEED_SYNC and skip > generic_write_sync(), leaving that metadata uncommitted for O_DSYNC. > > With the write path on iomap, ocfs2 no longer needs LEGACY_DIRECT_IO. > > A buffered write needs the rw lock EX, so every path that falls back to > generic_perform_write() upgrades first: the -ENOTBLK / zero-length bounce > inside the direct branch as well as the demoted direct branch. Under > coherency=buffered a PR lock would let another node write the same range > directly while this node does a read-modify-write in the page cache. > ocfs2_dio_upgrade_rw_lock() drops PR and takes EX rather than asking for > one upgrade, which can deadlock in the cluster lock manager when two nodes > upgrade at once. > > ip_dio_orphan_pid is reset in ocfs2_alloc_inode(), not only in the slab > constructor. The slab object is reused for a new inode, and a stale pid > that happens to be reused by another task would let an unrelated write > match the marker and delete a crash-left orphan entry without the truncate > recovery, leaking the clusters beyond i_size. > > Clearing the orphan entry takes the inode cluster lock, which blocks, so > IOCB_NOWAIT does not go there. The marker stays for a later write or for > the recovery. > > ocfs2_free_write_ctxt() runs the cached deallocs after unlocking the > folios, like ocfs2_write_end_nolock() does. Without it the error paths > that free a write context lose the extents it had cached for deletion. > > A write that hits an error does not convert its unwritten extents. > iomap keeps dio->size counting bios that failed at completion, and a > short write can leave the tail of the last cluster unzeroed, so > converting [offset, offset + size) would mark uninitialized disk blocks > written and expose stale data. Leave the extents unwritten, drop the > zeroed folios and return the error, as ext4 and xfs do; the extents read > back as zeroes until a later write converts them. > > Co-developed-by: Joseph Qi > Signed-off-by: Joseph Qi > Signed-off-by: Heming Zhao > --- > fs/ocfs2/Kconfig | 1 - > fs/ocfs2/aops.c | 1062 +++++++++++++++++++++++++++++++++------------- > fs/ocfs2/aops.h | 33 +- > fs/ocfs2/file.c | 275 ++++++++++-- > fs/ocfs2/inode.h | 8 +- > fs/ocfs2/super.c | 2 + > 6 files changed, 1032 insertions(+), 349 deletions(-) > > diff --git a/fs/ocfs2/Kconfig b/fs/ocfs2/Kconfig > index bf1678a5eb01e..74bc2008ef0bf 100644 > --- a/fs/ocfs2/Kconfig > +++ b/fs/ocfs2/Kconfig > @@ -9,7 +9,6 @@ config OCFS2_FS > select QUOTA_TREE > select FS_IOMAP > select FS_POSIX_ACL > - select LEGACY_DIRECT_IO > help > OCFS2 is a general purpose extent based shared disk cluster file > system with many similarities to ext3. It supports 64 bit inode > diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c > index 4b76be5909689..8d903c55749a9 100644 > --- a/fs/ocfs2/aops.c > +++ b/fs/ocfs2/aops.c > @@ -951,6 +951,8 @@ static void ocfs2_free_write_ctxt(struct inode *inode, > ... ... > + > +/* > + * Undo the orphan dir entry an extending direct write has added. > + * > + * Clusters beyond i_size are left alone. There is no on-disk way to tell > + * what this write allocated from unwritten extents preallocated with > + * fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the > + * preallocated space as well. They stay until a later truncate or recovery. > + * > + * TODO: ocfs2_recover_orphans() still truncates to i_size, so dead-slot > + * recovery can still free FALLOC_FL_KEEP_SIZE extents. That predates the > + * iomap conversion. > + */ > +static void ocfs2_dio_clear_orphan_locked(struct inode *inode, > + struct buffer_head *di_bh) > +{ > + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); > + struct ocfs2_inode_info *oi = OCFS2_I(inode); > + struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; > + int ret = 0; > + > + if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) > + ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0); > + if (ret < 0) { > + mlog_errno(ret); /* leave the entry to the recovery */ > + WRITE_ONCE(oi->ip_dio_orphan_pid, 0); > + return; > + } Regardless of the "ret" value, the code clears oi->ip_dio_orphan_pid. I suggest removing the above WRITE_ONCE() and return. > + > + /* > + * Only drop the marker once the entry is gone. Keeping it while the > + * entry survives lets the next extending write recover it, see > + * ocfs2_add_inode_to_orphan(). > + */ This comment also needs updating if we drop above codes. Thanks, Heming > + WRITE_ONCE(oi->ip_dio_orphan_pid, 0); > +} > + > +/* > + * Does nothing when this task is not the one which added the entry. > + * The rw_lock has to be held. @end is the end of the range this write > + * meant to write, 0 when it added no orphan entry. > + */ > +void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end) > ... ...