From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.99 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796276624; b=2y1TxDl/45tArwRZDfgqOQy0O6cZx1735MdxE0iq0CgwQnba2WxvW/PjOPMvboAN6uZb fcR44NwuvdkttM3Fx1zYm2HhU+C0R3cgI/1JJweS25S3nw0jt3IxcCApaQtXLwX92PuB5 qyKxjrqVaaq+x3Dvk0nmRbFb6T6gY2NMfUFGHOAXk55hoTQoTLO7Vg7fuVpAiYNY36a76 0McGc3uyff09YyRdCrImCgKeJPq4cu9feyqXJLGgKs4kSp1Wz+3Q1iPUvMYoZQmbNnvX+ bd4urIszWujZYfhjX2hsb/glOimEd19+qChdOb2DrEl4ctAAjYFbhQfATyG7asAvrWg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796276624; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:Message-ID: Date:MIME-Version:User-Agent:Subject:To:Cc:References:From: In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=moOMO7pYl9pJaPuR6pKLk8Xarp9K8ltDVm/aQCkv1I8=; b=XFl3GkFoxc1UX7ls1D10RXx+BM17KOMDhGt107rpe3JiHsODJSMPcFWB6RO62R89uj3f orb1iZFH1cBH261un4wRJ16cIy4mIxwbCAI/PteqljOp+7rMHJIyIDLFxatHHso3EmECU 8u53ces5DHcoS6+TX6Ph0SFlq7DYSJWiz2fimkMA6jMxH2JNMlm1DnGP2CWFvwEZ3DdNe PMSp3b3GGK6CbsK/q7sbwuM2y0HW3BukqpNLy3poDX3WkXtvSUd/t5V6pmxuSU3s7NpeB UhccqBaCY4y51yTN6hCBVlEAUDcGen72LLUcHYSit2MZi9CsHbnzpNa0k38fxRDUQGA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=UiUD9zoq; arc=none smtp.remote-ip=115.124.30.99 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58C7E606A322 for ; Thu, 3 Dec 2026 05:43:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 58C7E606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=UiUD9zoq DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796276620; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=moOMO7pYl9pJaPuR6pKLk8Xarp9K8ltDVm/aQCkv1I8=; b=UiUD9zoqHBB3DMsstLhONyZEYE8WbClactNWBlirSgD6rGR3AI4g5sQdvQmo/Cebo5Y/L96HxvGu09eLxKOWuPDmmAn5fVGn04kcHfBvmchHa+LFUUHD6tCCbp0SP3ISGfX0c8eC1qZNCLIPDLF/3aXEc0i9BWfkHlZ9mXEqbv4= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037026112;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0XGD47gm_1796276619; Received: from 30.221.129.217(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XGD47gm_1796276619 cluster:ay36) by smtp.aliyun-inc.com; Thu, 03 Dec 2026 13:43:39 +0800 Message-ID: Date: Thu, 3 Dec 2026 13:43:38 +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: sashiko-reviews@lists.linux.dev, Heming Zhao , Christoph Hellwig Cc: Mark Fasheh , Joel Becker , Andrew Morton , "ocfs2-devel@lists.linux.dev" , "linux-kernel@vger.kernel.org" References: <20261202143507.1754776-1-joseph.qi@linux.alibaba.com> <20261202143507.1754776-8-joseph.qi@linux.alibaba.com> From: Joseph Qi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/2/26 11:09 PM, sashiko-bot@kernel.org wrote: > Thank you for your contribution! Sashiko AI review found 2 potential > issues to consider: > - [High] ocfs2: stranded OCFS2_DIO_ORPHANED_FL on failed or short > extending direct writes > - [Medium] ocfs2: w_desc kvcalloc churn and O(N^2) scans in > ocfs2_dio_wr_map_blocks() > -- > > commit 842dc9fe75b01af0050260d527cbd4edb7427f5b > Author: Heming Zhao > > ocfs2: switch dio write path from buffer_head to iomap > > This commit converts the ocfs2 direct I/O write path from the legacy > buffer_head API to iomap, introducing multi-cluster block mapping in > ocfs2_dio_wr_map_blocks() and moving orphan management into > ocfs2_file_write_iter() and ocfs2_dio_end_io(). > >> 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 > > [ ... ] > >> @@ -2755,6 +2797,548 @@ static int ocfs2_write_end(const struct kiocb *iocb, > > [ ... ] > >> +/* >> + * The most a single mapping handed out for a direct write spans. >> + * It bounds the memory of the write context, one descriptor per >> + * cluster. >> + */ >> +#define OCFS2_DIO_MAX_MAP_BYTES (16U << 20) >> +/* A mapping always leaves room for at least one whole cluster. */ >> +static_assert(OCFS2_DIO_MAX_MAP_BYTES > OCFS2_MAX_CLUSTERSIZE); > > [ ... ] > >> +static int ocfs2_dio_wr_map_blocks(struct inode *inode, >> + struct ocfs2_map_block *map, >> + unsigned int flags, void *owner) >> +{ > > [ ... ] > >> + /* >> + * Only take what is of the same kind as what is at the start: >> + * all of it is a hole, or all of it is unwritten, or all of >> + * it is written, in one extent. >> + */ >> + probe = *map; >> + probe.flags = 0; >> + ret = ocfs2_map_blocks(inode, &probe, 0); >> + if (ret) { >> + mlog_errno(ret); >> + goto unlock; >> + } >> + map->len = min_t(u64, map->len, probe.len); >> + len = map->len << blkbits; >> + >> + /* >> + * Note, Zeroing the gap between i_size and the write is done >> + * once per dio by ocfs2_dio_zero_eof() before the io is issued. >> + */ >> + >> + /* >> + * Remember what this attempt covers: if it fails after it has >> + * zeroed the head/tail folios, the folios have to be cleaned up >> + * behind the locks, see out below. touched_end stays at pos when >> + * nothing was zeroed, and is 0 when the write context was never >> + * reached. >> + */ >> + ret = ocfs2_write_begin_nolock(inode->i_mapping, pos, len, >> + OCFS2_WRITE_DIRECT, NULL, >> + (void **)&wc, di_bh, NULL, owner, >> + &touched_end); >> + if (ret) { >> + if (ret != -ENOSPC && ret != -EDQUOT) >> + mlog_errno(ret); >> + goto unlock; >> + } >> + >> + /* >> + * The write context says how many of the clusters are really there. >> + * It is within what we asked for, what is mapped is up to the end of >> + * the last of them. >> + */ >> + desc = &wc->w_desc[0]; > > [Severity: Medium] > Does passing up to OCFS2_DIO_MAX_MAP_BYTES (16MB) to > ocfs2_write_begin_nolock() cause vmalloc churn on wc->w_desc and quadratic > descriptor scans when allocating into a hole on fragmented free space? > > With 4KB clusters, a 16MB span has w_clen = 4096, so > ocfs2_alloc_write_ctxt() allocates a 96KB w_desc array via kvcalloc() > (falling back to vmalloc) while holding ocfs2_inode_lock() and > ip_alloc_sem, even though ocfs2_dio_wr_map_blocks() already clamped > map->len to a single uniform extent via ocfs2_map_blocks() and only reads > wc->w_desc[0]. When filling a 16MB hole where ocfs2_dio_alloc_clusters() > only obtains 1 contiguous cluster per call, iomap_iter() advances by 1 > cluster and calls ocfs2_dio_wr_map_blocks() again for the remaining hole, > repeating the kvcalloc() and re-scanning all remaining clusters in > ocfs2_populate_write_desc() and ocfs2_dio_trim_write_ctxt() up to 4,096 > times for a single 16MB write. > A performance issue in case a fragmented 16MB, not a correctness bug. I'd rather leave it un-resolved in this series. > [ ... ] > >> +void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end) >> +{ >> + struct ocfs2_inode_info *oi = OCFS2_I(inode); >> + struct buffer_head *di_bh = NULL; >> + >> + if (READ_ONCE(oi->ip_dio_orphan_pid) != task_pid_nr(current)) >> + return; >> + >> + /* >> + * @end is 0 when this write added no orphan entry; leave any entry a >> + * previous write of this task left for the recovery. >> + */ >> + if (!end) >> + return; >> + >> + if (ocfs2_inode_lock(inode, &di_bh, 1) < 0) { >> + WRITE_ONCE(oi->ip_dio_orphan_pid, 0); >> + return; >> + } >> + >> + /* >> + * Reaching @end means i_size covers every cluster this write >> + * allocated, so the entry can go. Otherwise keep it for the truncate >> + * recovery in ocfs2_add_inode_to_orphan() and give up ownership so a >> + * later write by this task cannot remove it without truncating. A >> + * physical cluster count cannot show this on a sparse file, so only >> + * i_size reaching @end is trusted. >> + */ >> + if (i_size_read(inode) >= end) >> + ocfs2_dio_clear_orphan_locked(inode, di_bh); >> + else >> + WRITE_ONCE(oi->ip_dio_orphan_pid, 0); >> + >> + ocfs2_inode_unlock(inode, 1); >> + brelse(di_bh); >> +} > > [ ... ] > >> @@ -3304,10 +3635,156 @@ int ocfs2_iomap_next(const struct iomap_iter *iter, struct iomap *iomap, > > [ ... ] > >> +static int ocfs2_dio_end_io(struct kiocb *iocb, ssize_t size, >> + int error, unsigned int flags, int level) >> +{ > > [ ... ] > >> + if (error) { >> + /* >> + * Do not convert on error: dio->size still counts bios that >> + * failed, and a short write may leave the cluster tail >> + * unzeroed, so converting would expose stale disk blocks. >> + * Leave the extents unwritten (reads return zeroes) and the >> + * orphan entry for recovery, drop the zeroed folios, and >> + * report the error, as ext4 and xfs do. Give up orphan >> + * ownership only if this write owns it: an async completion >> + * runs in a worker and must not clobber the pid of a >> + * concurrent extending write that added the entry. >> + */ >> + if (READ_ONCE(OCFS2_I(inode)->ip_dio_orphan_pid) == >> + task_pid_nr(current)) >> + WRITE_ONCE(OCFS2_I(inode)->ip_dio_orphan_pid, 0); >> + ocfs2_dio_cleanup_pages(inode, &unwritten, false); >> + ocfs2_free_unwritten_list(inode, &unwritten); >> + ocfs2_rw_unlock(inode, level); >> + return error; >> + } > > [Severity: High] > Does leaving OCFS2_DIO_ORPHANED_FL and the dio orphan entry on disk when an > extending direct write fails or completes a short buffered fallback prevent > the inode from being deleted if it is later unlinked? > > When a cluster-extending direct write adds the inode to the orphan dir in > ocfs2_file_write_iter() and then fails (for example, an extending write at > EOF with an unmapped user buffer that hits -EFAULT in > bio_iov_iter_get_pages(), or an -ENOSPC in ocfs2_dio_wr_map_blocks()) or > bounces on -ENOTBLK to a short buffered write where i_size_read(inode) < > dio_end, both ocfs2_dio_end_io() and ocfs2_dio_clear_orphan() clear > ip_dio_orphan_pid and leave OCFS2_DIO_ORPHANED_FL set on disk: > > fs/ocfs2/inode.c:ocfs2_delete_inode() { > ... > /* Skip inode deletion and wait for dio orphan entry recovered > * first */ > if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) { > ocfs2_cleanup_delete_inode(inode, 0); > goto bail_unlock_inode; > } > ... > } > > If the file is subsequently unlinked without another extending direct write > occurring first, ocfs2_delete_inode() refuses to wipe the inode while > periodic background orphan scans (ORPHAN_NO_NEED_TRUNCATE) skip dio- > entries, leaking the unlinked inode, its allocated clusters and its orphan > entries on disk for the lifetime of the mount. In addition, if the write > failed before allocating new clusters or did a short buffered fallback on a > file with fallocate(FALLOC_FL_KEEP_SIZE) extents past EOF, leaving > OCFS2_DIO_ORPHANED_FL set causes the next extending direct write to > truncate those preallocated extents in ocfs2_add_inode_to_orphan(). Also, > if __iomap_dio_rw() returns -ENOTBLK and ocfs2_dio_upgrade_rw_lock() fails, > rw_level becomes -1 before jumping to out:, skipping > ocfs2_dio_clear_orphan() and leaving ip_dio_orphan_pid set to current->pid. > This is a pre-existing one, and I'd rather leave it un-resolved in this series. Thanks, Joseph