Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.131 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796047376; b=GHYY4b6q42/TLNmKNk1nAyzv/wrUAlSRgsMiJFkMTls12Kg4hq2LEfmJ/PnZh1vgTVIF 0BomU1Gw4k1FT8idK08i9WEzHXM4f3VdTYDzYC9Q5yWCnaKYXErcE5DwKXhVX3Y/u56o5 hHID3Ih5oGy718zUiC7sV7S7uv90GFS3Iju2PdD4EdKC+c/Tzy4g1a6gTgdiHt0pL+kDQ WKOJ5NnJGUtbcjPw7MQk7tb/HpXHpU78lYyVNyi0VPcFoUZEDO6djtL25brIDLEbH9mgv jxQCPtE0asgFG4q/pmm+PyVDWpIvBi3ZDVFM2S3+jXmyW29hwEgoC0+a5aUnKe60jTw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796047376; 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=i86rxTEMaKbpfNlOp7YM+V/m0TNKMWzZy6Oo2toRlqo=; b=omL20lxyfiU1rjPWq3QPEkhtN87j7XF8l6Q4Zewn9Dhqo3unf9UWDwuL11Tgbuq6NyRa xTnzzdOOX/FBrWVwqyRwzMsQJ/rCWIE6HFT8W2SyakGCFjo6+KwxMsDMs8y0TldHMOMfC LdbpyxkWNrzMKjtrg+ez3Y3E8h9A+lK8eokOCESCxl9c9IQb5/pxYwLJ4q9UIjEXfwcQf qOcfK01H4i+z7Gq2NUBbkDesGlELAAdhox6jVo0+ZCmJ0eoY1G8Yb8YsuBiXDfB4SzTPi Z1rrpWqEA/Npxc1Xn/K1FjzjlRLHpeoMF+9lwytni7AUnw/jKke6HNVN6B5n6Z0SPDw== 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=plPutfMZ; arc=none smtp.remote-ip=115.124.30.131 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (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 72560606A320; Mon, 30 Nov 2026 14:02:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 72560606A320 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, unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=plPutfMZ DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796047365; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=i86rxTEMaKbpfNlOp7YM+V/m0TNKMWzZy6Oo2toRlqo=; b=plPutfMZg3PdihM3NatuAtpsQ/T+dYia+lPmqVmhy5MGiXXJ9rGYS6LDhgBb9Wmyli2gZQA83p7sY9pvYnRPi2j915MAsQbLTDJLQNEI/A/hiM1fjrFMmyoFSWG5qUAoXzmVSV3cuv6KxrqbyhLEtBkkKud0VyWKS7Af4WQGqDQ= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R251e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033040069076;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0XFmtVIF_1796047363; Received: from 30.180.33.101(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XFmtVIF_1796047363 cluster:ay36) by smtp.aliyun-inc.com; Mon, 30 Nov 2026 22:02:44 +0800 Message-ID: <7d4e982f-1196-4f50-be5d-f3e215d6e297@linux.alibaba.com> Date: Mon, 30 Nov 2026 22:02:42 +0800 X-Mailing-List: ocfs2-devel@lists.linux.dev 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 v13 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" , "ocfs2-devel@lists.linux.dev" References: <20261130100203.383093-1-joseph.qi@linux.alibaba.com> <20261130100203.383093-8-joseph.qi@linux.alibaba.com> From: Joseph Qi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 11/30/26 6:33 PM, sashiko-bot@kernel.org wrote: > Thank you for your contribution! Sashiko AI review found 2 potential > issues to consider: > - [Critical] ocfs2: bio errors ignored and short writes corrupted in > ocfs2_dio_end_io() > - [High] ocfs2: orphan entry and blocks leaked on error in > ocfs2_file_write_iter() > -- > > commit 899b48a1b8a6daa237c8f0415e63a2d83b81e29c > 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 direct I/O implementation to iomap and drops > LEGACY_DIRECT_IO. > >> diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c >> index e3e28584e7410..66be8ae74b6f6 100644 >> --- a/fs/ocfs2/aops.c >> +++ b/fs/ocfs2/aops.c > [ ... ] >> @@ -3296,10 +3598,141 @@ int ocfs2_iomap_next(const struct iomap_iter *iter, struct iomap *iomap, >> } >> >> ret = ocfs2_iomap_begin(iter->inode, iter->pos, iter->len, iter->flags, >> - iomap, srcmap); >> + iomap, srcmap, iter->private); >> return ret < 0 ? ret : 1; >> } >> >> +/* >> + * Clean up the head/tail folios which the recording mappings of a direct >> + * write have zeroed. Each record covers a run of clusters whose folios >> + * were prepared; the zeroed folios are the ones in its first and its last >> + * cluster, and only those two are cleaned, see >> + * ocfs2_dio_cleanup_clusters(). The records are still on >> + * ip_unwritten_list, so no other direct write can take these clusters over >> + * while they are cleaned up. >> + * >> + * @write_zeroes is true when some of the clusters may have been converted >> + * to written and the zeroes have to reach the disk; otherwise the pages >> + * are dropped. >> + * >> + * Must be called before ocfs2_free_unwritten_list(), which wakes up the >> + * writes waiting for the clusters, and without the inode lock or >> + * ip_alloc_sem. >> + */ >> +static void ocfs2_dio_cleanup_pages(struct inode *inode, >> + struct list_head *head, bool write_zeroes) >> +{ >> + struct ocfs2_unwritten_extent *ue; >> + >> + list_for_each_entry(ue, head, ue_node) >> + ocfs2_dio_cleanup_clusters(inode, ue->ue_cpos, ue->ue_clusters, >> + write_zeroes); >> +} >> + > [ ... ] >> +static int ocfs2_dio_end_io(struct kiocb *iocb, ssize_t size, >> + int error, unsigned int flags, int level) >> +{ >> + struct inode *inode = file_inode(iocb->ki_filp); >> + loff_t offset = iocb->ki_pos; >> + bool recorded = error || (flags & IOMAP_DIO_UNWRITTEN); >> + LIST_HEAD(unwritten); >> + int ret = 0; >> + >> + if (recorded) >> + ocfs2_dio_collect_unwritten(inode, iocb, &unwritten); >> + >> + if (error) >> + mlog_ratelimited(ML_ERROR, "Direct IO failed, bytes = %lld errno:%d", >> + (long long)size, error); >> + >> + /* bounced back to buffered I/O, leave the rw_lock held */ >> + if (size == 0 && error == 0) { >> + ocfs2_free_unwritten_list(inode, &unwritten); >> + return 0; >> + } >> + >> + if (size && >> + (!list_empty(&unwritten) || >> + (offset + size > i_size_read(inode)))) { >> + /* >> + * A write that hit an error can still have a prefix on disk. >> + * Convert it and report the byte count, as the buffer_head >> + * path did, instead of leaving it to read back as zeroes. >> + */ >> + ret = ocfs2_dio_end_io_write(inode, offset, size, &unwritten); >> + if (ret < 0) { >> + mlog_errno(ret); >> + /* >> + * The conversion failed part way, some of the >> + * clusters may already be written. >> + */ >> + ocfs2_dio_cleanup_pages(inode, &unwritten, true); >> + } >> + } else if (error) { >> + /* Nothing is converted, the zeroes are not needed. */ >> + ocfs2_dio_cleanup_pages(inode, &unwritten, false); >> + } >> + >> + ocfs2_free_unwritten_list(inode, &unwritten); >> + >> + /* >> + * A failed extending write keeps its orphan entry, and the clusters it >> + * allocated beyond i_size, for the recovery in >> + * ocfs2_add_inode_to_orphan() (or the one at mount time), see >> + * ocfs2_file_write_iter(). Do not call ocfs2_dio_clear_orphan() here: >> + * a plain successful overwrite can be completed in interrupt context, >> + * where the inode lock must not be taken. >> + */ >> + ocfs2_rw_unlock(inode, level); >> + >> + /* >> + * Returning 0 makes iomap report dio->size, so a write that hit an >> + * error after completing a prefix still returns the bytes it wrote. >> + */ >> + return ret ? ret : (size ? 0 : error); >> +} > > [Severity: Critical] > Does ocfs2_dio_end_io() silently drop bio I/O errors and corrupt partial > unwritten clusters whenever size > 0 and error < 0? > > In iomap, iomap_dio_bio_iter() increments dio->size as each bio is > submitted, and iomap_dio_bio_end_io() records any block layer failure in > dio->error without reducing dio->size. When a write bio fails with -EIO, > iomap_dio_complete() calls ocfs2_dio_end_io() with size == dio->size and > error == -EIO: > > fs/iomap/direct-io.c:iomap_dio_complete() { > ... > if (dops && dops->end_io) > ret = dops->end_io(iocb, dio->size, ret, dio->flags); > ... > if (likely(!ret)) { > ret = dio->size; > ... > } > > Because ocfs2_dio_end_io() does not check for !error, it runs > ocfs2_dio_end_io_write() over the failed range—marking unwritten extents > written on disk (exposing uninitialized blocks), advancing i_size, and > clearing the orphan entry—and then returns 0, causing > iomap_dio_complete() to discard -EIO and report full success to > userspace. > > Even when size > 0 reflects a valid submitted prefix before a submission > error (such as -EFAULT on a later iovec inside an unwritten cluster), > converting [offset, offset + size) has several additional regressions: > > 1. ocfs2_grab_folios_direct() skipped zeroing folios across the full > mapped range [user_pos, user_pos + user_len), and iomap_dio_zero() > only pads up to i_blocksize(inode). ocfs2_dio_end_io_write() rounds > last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, offset + size) up to > the next cluster boundary and marks the whole cluster written, > exposing stale on-disk data in [offset + size, cluster_end). > 2. When ocfs2_dio_end_io_write() succeeds (ret == 0), any recorded > unwritten clusters at or beyond last_cpos are left unconverted, yet > ocfs2_dio_cleanup_pages() is skipped so their dirty zeroed folios are > never cleaned up before ocfs2_free_unwritten_list() drops the records. > 3. If a write spanning eof_blk is split mid-cluster and the second > mapping in that cluster fails after touched_end > pos, > ocfs2_dio_wr_map_blocks() runs ocfs2_dio_cleanup_clusters(..., false) > on that cluster, truncating the zeroed head folios prepared by the > first mapping before ocfs2_dio_end_io_write() converts the cluster. > Looks real, will fix in next version. >> diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c >> index 3d717200d53a4..2e4973db105f3 100644 >> --- a/fs/ocfs2/file.c >> +++ b/fs/ocfs2/file.c > [ ... ] >> @@ -2527,31 +2594,150 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, > [ ... ] >> + if ((iocb->ki_pos + count) > i_size_read(inode)) { >> + /* >> + * Complete before i_rwsem is dropped: i_size is only >> + * updated by ->end_io(), and the next writer would >> + * zero the gap between the size it still sees and its >> + * own write, over the data this one has just written. >> + */ >> + dio_flags |= IOMAP_DIO_FORCE_WAIT; >> + >> + if (ocfs2_clusters_for_bytes(inode->i_sb, >> + iocb->ki_pos + count) > >> + ocfs2_clusters_for_bytes(inode->i_sb, >> + i_size_read(inode))) { >> + /* >> + * when we are going to alloc extents beyond file >> + * size, add the inode to orphan dir, so we can >> + * recall those spaces when system crashed during >> + * write. >> + */ >> + ret = ocfs2_add_inode_to_orphan(osb, inode); >> + if (ret < 0) { >> + mlog_errno(ret); >> + goto out; >> + } >> + WRITE_ONCE(OCFS2_I(inode)->ip_dio_orphan_pid, >> + task_pid_nr(current)); >> + } >> + } >> + >> + ret = ocfs2_dio_zero_eof(inode, iocb->ki_pos, count); >> + if (ret < 0) >> + goto out; >> + >> + const struct iomap_dio_ops *dops = rw_level ? >> + &ocfs2_iomap_dio_ops_w_ex : &ocfs2_iomap_dio_ops_w_pr; >> + struct iomap_dio *dio; >> + >> + /* >> + * The kiocb is the owner of the unwritten clusters which >> + * ->iomap_begin() records for ->end_io() to convert, see >> + * ocfs2_iomap_next(). >> + */ >> + dio = __iomap_dio_rw(iocb, from, ocfs2_iomap_next, >> + dops, dio_flags, iocb, 0); >> + if (dio == NULL) { >> + /* No I/O issued; rw_lock still held. */ >> + written = 0; >> + } else if (IS_ERR(dio)) { >> + written = PTR_ERR(dio); >> + /* >> + * -EIOCBQUEUED: async I/O; end_io will drop the lock on >> + * completion. Any other error means no I/O started and >> + * end_io was not called, so the cleanup path below drops >> + * the lock. >> + */ >> + if (written == -EIOCBQUEUED) >> + rw_level = -1; >> + } else { >> + /* >> + * Synchronous completion. iomap_dio_complete() runs >> + * end_io, which drops the lock unless the mapping >> + * bounced us back to buffered I/O (returns 0, lock >> + * retained). >> + */ >> + written = iomap_dio_complete(dio); >> + if (written != 0) >> + rw_level = -1; >> + } > [ ... ] >> @@ -2571,11 +2757,39 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb, >> } >> >> out: >> + /* >> + * IOMAP_DIO_FORCE_WAIT made an extending dio complete synchronously, >> + * so its end_io has already updated i_size. >> + */ >> if (saved_ki_complete) >> xchg(&iocb->ki_complete, saved_ki_complete); >> >> - if (rw_level != -1) >> + if (rw_level != -1) { >> + /* >> + * The orphan entry this write added is still there when no io >> + * was issued at all, or when the write bailed out before >> + * submitting it. The conversion in ocfs2_dio_end_io_write() >> + * has removed it on the success path; a failed io is left to >> + * the recovery. Remove it here for the former, while the >> + * rw_lock is still held. >> + * >> + * Clearing it takes the inode cluster lock, which blocks, so >> + * IOCB_NOWAIT must not go there. The marker stays for a later >> + * write or for the recovery. >> + */ >> + if (!nowait) >> + ocfs2_dio_clear_orphan(inode); >> ocfs2_rw_unlock(inode, rw_level); >> + } > > [Severity: High] > Can a failed extending direct write leave OCFS2_DIO_ORPHANED_FL and > ip_dio_orphan_pid set, either leaking allocated clusters beyond i_size or > preventing the inode from being wiped on unlink? > > When an extending write fails inside __iomap_dio_rw() after > inode_dio_begin() with 0 bytes written (for example, an unmapped buffer at > iocb->ki_pos == i_size_read(inode) hitting -EFAULT after > ocfs2_dio_wr_map_blocks() allocated clusters, or hitting -ENOSPC), > __iomap_dio_rw() returns a valid dio pointer because IOMAP_DIO_FORCE_WAIT > is set. iomap_dio_complete() runs ocfs2_dio_end_io(), which unlocks > rw_lock without clearing the orphan entry and returns a negative error, so > ocfs2_file_write_iter() sets rw_level = -1 and skips > ocfs2_dio_clear_orphan() at out:. > > This leaves OCFS2_DIO_ORPHANED_FL on disk and ip_dio_orphan_pid set to > current's PID: > > 1. If the same task next does a non-extending write that reaches > ocfs2_dio_end_io_write() or out: with rw_level != -1, > ip_dio_orphan_pid matches current's PID and > ocfs2_dio_clear_orphan_locked() deletes the orphan entry without > truncating the excess clusters allocated beyond i_size. (The same > happens immediately at out: if ocfs2_dio_zero_eof() on a non-sparse > volume or direct_write_fallback() fails after allocating clusters.) Looks real, will fix in next version. > 2. If the file is instead unlinked without another write clearing > OCFS2_DIO_ORPHANED_FL, ocfs2_delete_inode() bails out when it sees > OCFS2_DIO_ORPHANED_FL and background orphan scans skip dio- orphan > entries, stranding the unlinked inode on disk until remount. > This is pre-existing design, not a regression. Thanks, Joseph