btrfs: tag as unlikely if statements that check for fs in error state

Having the filesystem in an error state, meaning we had a transaction
abort, is unexpected. Mark every check for the error state with the
unlikely annotation to convey that and to allow the compiler to generate
better code.

On x86_64, using gcc 14.2.0-19 from Debian, resulted in a slightly
reduced object size and better code.

Before:

  $ size fs/btrfs/btrfs.ko
     text	   data	    bss	    dec	    hex	filename
  2008598	 175912	  15592	2200102	 219226	fs/btrfs/btrfs.ko

After:

  $ size fs/btrfs/btrfs.ko
     text	   data	    bss	    dec	    hex	filename
  2008450	 175912	  15592	2199954	 219192	fs/btrfs/btrfs.ko

Reviewed-by: Anand Jain <asj@kernel.org>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Filipe Manana
2026-04-01 18:51:35 +01:00
committed by David Sterba
parent 3f487be812
commit 7801f3ea95
10 changed files with 16 additions and 16 deletions

View File

@@ -1556,7 +1556,7 @@ static int transaction_kthread(void *arg)
wake_up_process(fs_info->cleaner_kthread);
mutex_unlock(&fs_info->transaction_kthread_mutex);
if (BTRFS_FS_ERROR(fs_info))
if (unlikely(BTRFS_FS_ERROR(fs_info)))
btrfs_cleanup_transaction(fs_info);
if (!kthread_should_stop() &&
(!btrfs_transaction_blocked(fs_info) ||
@@ -4148,7 +4148,7 @@ void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
drop_ref = true;
spin_unlock(&fs_info->fs_roots_radix_lock);
if (BTRFS_FS_ERROR(fs_info)) {
if (unlikely(BTRFS_FS_ERROR(fs_info))) {
ASSERT(root->log_root == NULL);
if (root->reloc_root) {
btrfs_put_root(root->reloc_root);

View File

@@ -2400,7 +2400,7 @@ int btree_writepages(struct address_space *mapping, struct writeback_control *wb
* and it only returns 0 or errors.
*/
ASSERT(ret <= 0);
if (!ret && BTRFS_FS_ERROR(fs_info))
if (unlikely(!ret && BTRFS_FS_ERROR(fs_info)))
ret = -EROFS;
if (ctx.zoned_bg)

View File

@@ -1445,7 +1445,7 @@ ssize_t btrfs_do_write_iter(struct kiocb *iocb, struct iov_iter *from,
* have opened a file as writable, we have to stop this write operation
* to ensure consistency.
*/
if (BTRFS_FS_ERROR(inode->root->fs_info))
if (unlikely(BTRFS_FS_ERROR(inode->root->fs_info)))
return -EROFS;
if (encoded && (iocb->ki_flags & IOCB_NOWAIT))

View File

@@ -8972,7 +8972,7 @@ int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_conte
{
struct btrfs_fs_info *fs_info = root->fs_info;
if (BTRFS_FS_ERROR(fs_info))
if (unlikely(BTRFS_FS_ERROR(fs_info)))
return -EROFS;
return start_delalloc_inodes(root, NULL, true, in_reclaim_context);
}
@@ -8985,7 +8985,7 @@ int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
LIST_HEAD(splice);
int ret;
if (BTRFS_FS_ERROR(fs_info))
if (unlikely(BTRFS_FS_ERROR(fs_info)))
return -EROFS;
mutex_lock(&fs_info->delalloc_root_mutex);

View File

@@ -37,7 +37,7 @@ static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
curr += sizeof(STATE_STRING_PREFACE) - 1;
if (BTRFS_FS_ERROR(info)) {
if (unlikely(BTRFS_FS_ERROR(info))) {
*curr++ = 'E';
states_printed = true;
}

View File

@@ -2974,7 +2974,7 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
struct page *page;
struct btrfs_fs_info *fs_info = sctx->fs_info;
if (BTRFS_FS_ERROR(fs_info))
if (unlikely(BTRFS_FS_ERROR(fs_info)))
return -EROFS;
page = alloc_page(GFP_KERNEL);

View File

@@ -1299,7 +1299,7 @@ static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)
{
int ret;
if (BTRFS_FS_ERROR(fs_info)) {
if (unlikely(BTRFS_FS_ERROR(fs_info))) {
btrfs_err(fs_info,
"remounting read-write after error is not allowed");
return -EINVAL;

View File

@@ -275,7 +275,7 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
spin_lock(&fs_info->trans_lock);
loop:
/* The file system has been taken offline. No new transactions. */
if (BTRFS_FS_ERROR(fs_info)) {
if (unlikely(BTRFS_FS_ERROR(fs_info))) {
spin_unlock(&fs_info->trans_lock);
return -EROFS;
}
@@ -333,7 +333,7 @@ static noinline int join_transaction(struct btrfs_fs_info *fs_info,
btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
kfree(cur_trans);
goto loop;
} else if (BTRFS_FS_ERROR(fs_info)) {
} else if (unlikely(BTRFS_FS_ERROR(fs_info))) {
spin_unlock(&fs_info->trans_lock);
btrfs_lockdep_release(fs_info, btrfs_trans_num_extwriters);
btrfs_lockdep_release(fs_info, btrfs_trans_num_writers);
@@ -612,7 +612,7 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
bool do_chunk_alloc = false;
int ret;
if (BTRFS_FS_ERROR(fs_info))
if (unlikely(BTRFS_FS_ERROR(fs_info)))
return ERR_PTR(-EROFS);
if (current->journal_info) {
@@ -1120,7 +1120,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
if (throttle)
btrfs_run_delayed_iputs(info);
if (TRANS_ABORTED(trans) || BTRFS_FS_ERROR(info)) {
if (unlikely(TRANS_ABORTED(trans) || BTRFS_FS_ERROR(info))) {
wake_up_process(info->transaction_kthread);
if (TRANS_ABORTED(trans))
ret = trans->aborted;
@@ -2351,7 +2351,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
* abort to prevent writing a new superblock that reflects a
* corrupt state (pointing to trees with unwritten nodes/leafs).
*/
if (BTRFS_FS_ERROR(fs_info)) {
if (unlikely(BTRFS_FS_ERROR(fs_info))) {
spin_unlock(&fs_info->trans_lock);
ret = -EROFS;
goto lockdep_release;

View File

@@ -3566,7 +3566,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
* writing the super here would result in transid mismatches. If there
* is an error here just bail.
*/
if (BTRFS_FS_ERROR(fs_info)) {
if (unlikely(BTRFS_FS_ERROR(fs_info))) {
ret = -EIO;
btrfs_set_log_full_commit(trans);
btrfs_abort_transaction(trans, ret);

View File

@@ -3602,7 +3602,7 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, bool v
* If we had a transaction abort, stop all running scrubs.
* See transaction.c:cleanup_transaction() why we do it here.
*/
if (BTRFS_FS_ERROR(fs_info))
if (unlikely(BTRFS_FS_ERROR(fs_info)))
btrfs_scrub_cancel(fs_info);
return ret;
}