From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20222382F0E for ; Fri, 20 Mar 2026 13:43:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774014186; cv=none; b=Vhm0ZpvZCGIO9KfFj9uycktxVWV8eCSB/XhTfoRd1EKlZQRCHc8R/+QVrxhSij1vW6fGxb0QVcPPmuYT1JiEIwiqXTH2Q2xBRMoJjetCnVW5euIyJzO1YJ9vq6bnrrphjEo6bFixFIgWqPmYFxP8QxmQKLnqFWh5McxQBIoONzs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774014186; c=relaxed/simple; bh=rLj2k8FC5Tey+MdE0VfUZj3+Y5rWoTAvVRz1Fch+n/c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a5ChXENGA77TbIAJyvVEFcsiv7k95SjUYCypGlZ5e4BOyFkLSr/4jSHQOwPAfM3hNIYRy9a9JSEHRJMJXTpSd36wgme3VySACMerBJq9yeXcM9HcG5AoX2IxNGNY8IvTfwBOnn+Galk0bsFIvmRqXG1xxoKxKki/Eh3nbz1Emkw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=D7FnOwOI; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=uf4ahYfU; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=D7FnOwOI; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=uf4ahYfU; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="D7FnOwOI"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="uf4ahYfU"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="D7FnOwOI"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="uf4ahYfU" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 821B44D422; Fri, 20 Mar 2026 13:41:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1774014105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oX/JsvXwPm/CAxn2sGZiEEbiTcloxq4ZGOeTqxutKB4=; b=D7FnOwOIvl44uigTgjxjb7eeoZKN2Xf1sdrO6INVERm5jsCooPW0DIyZQlPGrOk/jOFH42 mSuqmZ+EKg+3R7TjkaD5Y5PqUUNiSdOkDm+KrP7RHN1roEixKt8wYqSl2avEIexfL2K1Zf GS5R2It3/gxvNY81tfEagMLOw4rt0f8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1774014105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oX/JsvXwPm/CAxn2sGZiEEbiTcloxq4ZGOeTqxutKB4=; b=uf4ahYfU4yJZTPRzSJ/nsD0y0+cs//RFp48RznNG1HNwkDtjucyxEtUrNeezV20Q95/noe tnyuuLAkOa+c5EBg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1774014105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oX/JsvXwPm/CAxn2sGZiEEbiTcloxq4ZGOeTqxutKB4=; b=D7FnOwOIvl44uigTgjxjb7eeoZKN2Xf1sdrO6INVERm5jsCooPW0DIyZQlPGrOk/jOFH42 mSuqmZ+EKg+3R7TjkaD5Y5PqUUNiSdOkDm+KrP7RHN1roEixKt8wYqSl2avEIexfL2K1Zf GS5R2It3/gxvNY81tfEagMLOw4rt0f8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1774014105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oX/JsvXwPm/CAxn2sGZiEEbiTcloxq4ZGOeTqxutKB4=; b=uf4ahYfU4yJZTPRzSJ/nsD0y0+cs//RFp48RznNG1HNwkDtjucyxEtUrNeezV20Q95/noe tnyuuLAkOa+c5EBg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 740C34281E; Fri, 20 Mar 2026 13:41:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id SPZUHJlOvWmDCQAAD6G6ig (envelope-from ); Fri, 20 Mar 2026 13:41:45 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 3E69AA0B2E; Fri, 20 Mar 2026 14:41:45 +0100 (CET) From: Jan Kara To: Cc: , Christian Brauner , Al Viro , , Ted Tso , "Tigran A. Aivazian" , David Sterba , OGAWA Hirofumi , Muchun Song , Oscar Salvador , David Hildenbrand , linux-mm@kvack.org, linux-aio@kvack.org, Benjamin LaHaise , Jan Kara Subject: [PATCH 27/41] fs: Fold fsync_buffers_list() into sync_mapping_buffers() Date: Fri, 20 Mar 2026 14:41:22 +0100 Message-ID: <20260320134100.20731-68-jack@suse.cz> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260320131728.6449-1-jack@suse.cz> References: <20260320131728.6449-1-jack@suse.cz> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7522; i=jack@suse.cz; h=from:subject; bh=rLj2k8FC5Tey+MdE0VfUZj3+Y5rWoTAvVRz1Fch+n/c=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBpvU6DlCp9UTpHdt1UkLQXTnr+fUisFYJHI+1Da skt7bJqQneJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCab1OgwAKCRCcnaoHP2RA 2SjECADZ/AZUhmJibhL/fxZh7Xl+8rRaEw7FOUlq47Zky0nr29CR2tbaJ7BDYJLQ4JPzM5qLiwv iktX6OPkxdT8MbOT9W4BjSaB72eVrBQufgQFhkmrp3xcksckct9uUc8ll36o+fMqlM5k62hgheu 5ElO/bc6TVvvG/Iu6jYesvvAcZl78ziVXI7NPzIlS6FwGPCAztC7bVpWpj0LGnROceh2b15c/Da yZFlkUfX67vBsOTG/FZjUUQLIDM+H9YOhaNwKP08RBb3/nuG6R+WYph/8+FbaIWHv0XxmdWqb/J X5jLst0caltmwm0AIi04oqX2rqSL/VeKD32XpO42ZbAgD+3B X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Content-Transfer-Encoding: 8bit X-Spam-Score: -5.30 X-Spam-Level: X-Spamd-Result: default: False [-5.30 / 50.00]; REPLY(-4.00)[]; BAYES_HAM(-3.00)[100.00%]; SUSPICIOUS_RECIPS(1.50)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.998]; MIME_GOOD(-0.10)[text/plain]; TAGGED_RCPT(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_TWELVE(0.00)[16]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[vger.kernel.org,kernel.org,ZenIV.linux.org.uk,mit.edu,gmail.com,suse.com,mail.parknet.co.jp,linux.dev,suse.de,kvack.org,suse.cz]; R_RATELIMIT(0.00)[to_ip_from(RLck8brw5hxmszoarioc7838it)]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.cz:mid,suse.cz:email]; FREEMAIL_ENVRCPT(0.00)[gmail.com] X-Spam-Flag: NO There's only single caller of fsync_buffers_list() so untangle the code a bit by folding fsync_buffers_list() into sync_mapping_buffers(). Also merge the comments and update them to reflect current state of code. Signed-off-by: Jan Kara --- fs/buffer.c | 180 +++++++++++++++++++++++----------------------------- 1 file changed, 80 insertions(+), 100 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index 1c0e7c81a38b..fa3d84084adf 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -54,7 +54,6 @@ #include "internal.h" -static int fsync_buffers_list(spinlock_t *lock, struct list_head *list); static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, enum rw_hint hint, struct writeback_control *wbc); @@ -531,22 +530,96 @@ EXPORT_SYMBOL_GPL(inode_has_buffers); * @mapping: the mapping which wants those buffers written * * Starts I/O against the buffers at mapping->i_private_list, and waits upon - * that I/O. + * that I/O. Basically, this is a convenience function for fsync(). @mapping + * is a file or directory which needs those buffers to be written for a + * successful fsync(). * - * Basically, this is a convenience function for fsync(). - * @mapping is a file or directory which needs those buffers to be written for - * a successful fsync(). + * We have conflicting pressures: we want to make sure that all + * initially dirty buffers get waited on, but that any subsequently + * dirtied buffers don't. After all, we don't want fsync to last + * forever if somebody is actively writing to the file. + * + * Do this in two main stages: first we copy dirty buffers to a + * temporary inode list, queueing the writes as we go. Then we clean + * up, waiting for those writes to complete. mark_buffer_dirty_inode() + * doesn't touch b_assoc_buffers list if b_assoc_map is not NULL so we + * are sure the buffer stays on our list until IO completes (at which point + * it can be reaped). */ int sync_mapping_buffers(struct address_space *mapping) { struct address_space *buffer_mapping = mapping->host->i_sb->s_bdev->bd_mapping; + struct buffer_head *bh; + int err = 0; + struct blk_plug plug; + LIST_HEAD(tmp); if (list_empty(&mapping->i_private_list)) return 0; - return fsync_buffers_list(&buffer_mapping->i_private_lock, - &mapping->i_private_list); + blk_start_plug(&plug); + + spin_lock(&buffer_mapping->i_private_lock); + while (!list_empty(&mapping->i_private_list)) { + bh = BH_ENTRY(mapping->i_private_list.next); + WARN_ON_ONCE(bh->b_assoc_map != mapping); + __remove_assoc_queue(bh); + /* Avoid race with mark_buffer_dirty_inode() which does + * a lockless check and we rely on seeing the dirty bit */ + smp_mb(); + if (buffer_dirty(bh) || buffer_locked(bh)) { + list_add(&bh->b_assoc_buffers, &tmp); + bh->b_assoc_map = mapping; + if (buffer_dirty(bh)) { + get_bh(bh); + spin_unlock(&buffer_mapping->i_private_lock); + /* + * Ensure any pending I/O completes so that + * write_dirty_buffer() actually writes the + * current contents - it is a noop if I/O is + * still in flight on potentially older + * contents. + */ + write_dirty_buffer(bh, REQ_SYNC); + + /* + * Kick off IO for the previous mapping. Note + * that we will not run the very last mapping, + * wait_on_buffer() will do that for us + * through sync_buffer(). + */ + brelse(bh); + spin_lock(&buffer_mapping->i_private_lock); + } + } + } + + spin_unlock(&buffer_mapping->i_private_lock); + blk_finish_plug(&plug); + spin_lock(&buffer_mapping->i_private_lock); + + while (!list_empty(&tmp)) { + bh = BH_ENTRY(tmp.prev); + get_bh(bh); + __remove_assoc_queue(bh); + /* Avoid race with mark_buffer_dirty_inode() which does + * a lockless check and we rely on seeing the dirty bit */ + smp_mb(); + if (buffer_dirty(bh)) { + list_add(&bh->b_assoc_buffers, + &mapping->i_private_list); + bh->b_assoc_map = mapping; + } + spin_unlock(&buffer_mapping->i_private_lock); + wait_on_buffer(bh); + if (!buffer_uptodate(bh)) + err = -EIO; + brelse(bh); + spin_lock(&buffer_mapping->i_private_lock); + } + spin_unlock(&buffer_mapping->i_private_lock); + return err; } EXPORT_SYMBOL(sync_mapping_buffers); @@ -719,99 +792,6 @@ bool block_dirty_folio(struct address_space *mapping, struct folio *folio) } EXPORT_SYMBOL(block_dirty_folio); -/* - * Write out and wait upon a list of buffers. - * - * We have conflicting pressures: we want to make sure that all - * initially dirty buffers get waited on, but that any subsequently - * dirtied buffers don't. After all, we don't want fsync to last - * forever if somebody is actively writing to the file. - * - * Do this in two main stages: first we copy dirty buffers to a - * temporary inode list, queueing the writes as we go. Then we clean - * up, waiting for those writes to complete. - * - * During this second stage, any subsequent updates to the file may end - * up refiling the buffer on the original inode's dirty list again, so - * there is a chance we will end up with a buffer queued for write but - * not yet completed on that list. So, as a final cleanup we go through - * the osync code to catch these locked, dirty buffers without requeuing - * any newly dirty buffers for write. - */ -static int fsync_buffers_list(spinlock_t *lock, struct list_head *list) -{ - struct buffer_head *bh; - struct address_space *mapping; - int err = 0; - struct blk_plug plug; - LIST_HEAD(tmp); - - blk_start_plug(&plug); - - spin_lock(lock); - while (!list_empty(list)) { - bh = BH_ENTRY(list->next); - mapping = bh->b_assoc_map; - __remove_assoc_queue(bh); - /* Avoid race with mark_buffer_dirty_inode() which does - * a lockless check and we rely on seeing the dirty bit */ - smp_mb(); - if (buffer_dirty(bh) || buffer_locked(bh)) { - list_add(&bh->b_assoc_buffers, &tmp); - bh->b_assoc_map = mapping; - if (buffer_dirty(bh)) { - get_bh(bh); - spin_unlock(lock); - /* - * Ensure any pending I/O completes so that - * write_dirty_buffer() actually writes the - * current contents - it is a noop if I/O is - * still in flight on potentially older - * contents. - */ - write_dirty_buffer(bh, REQ_SYNC); - - /* - * Kick off IO for the previous mapping. Note - * that we will not run the very last mapping, - * wait_on_buffer() will do that for us - * through sync_buffer(). - */ - brelse(bh); - spin_lock(lock); - } - } - } - - spin_unlock(lock); - blk_finish_plug(&plug); - spin_lock(lock); - - while (!list_empty(&tmp)) { - bh = BH_ENTRY(tmp.prev); - get_bh(bh); - mapping = bh->b_assoc_map; - __remove_assoc_queue(bh); - /* Avoid race with mark_buffer_dirty_inode() which does - * a lockless check and we rely on seeing the dirty bit */ - smp_mb(); - if (buffer_dirty(bh)) { - list_add(&bh->b_assoc_buffers, - &mapping->i_private_list); - bh->b_assoc_map = mapping; - } - spin_unlock(lock); - wait_on_buffer(bh); - if (!buffer_uptodate(bh)) - err = -EIO; - brelse(bh); - spin_lock(lock); - } - - spin_unlock(lock); - return err; -} - /* * Invalidate any and all dirty buffers on a given inode. We are * probably unmounting the fs, but that doesn't mean we have already -- 2.51.0