From: Josef Bacik <jbacik@fb.com>
To: Filipe Manana <fdmanana@suse.com>, <linux-btrfs@vger.kernel.org>
Subject: Re: [PATCH] Btrfs: fix race when cleaning unused block groups
Date: Wed, 5 Nov 2014 15:33:59 -0500 [thread overview]
Message-ID: <545A89B7.3000602@fb.com> (raw)
In-Reply-To: <1415217364-32108-1-git-send-email-fdmanana@suse.com>
On 11/05/2014 02:56 PM, Filipe Manana wrote:
> We have a race while deleting unused block groups that causes extents written
> by past generations/transactions to be rewritten by the current transaction
> before that transaction is committed. The steps that lead to this issue:
>
> 1) At transaction N one or more block groups became unused and we added them
> to the list fs_info->unused_bgs;
>
> 2) While still at transaction N we write btree extents to block group X and the
> transaction is committed;
>
> 3) The cleaner kthread is awaken and calls btrfs_delete_unused_bgs() to go through
> the list fs_info->unused_bgs and remove unused block groups;
>
> 4) Transaction N + 1 starts;
>
> 5) At transaction N + 1, block group X becomes unused and is added to the list
> fs_info->unused_bgs - this implies delayed refs were run, so we had the
> following function calls: btrfs_run_delayed_refs() -> __btrfs_free_extent()
> -> update_block_group(). The update_block_group() function grabs the lock
> fs_info->unused_bgs_lock, adds block group X to fs_info->unused_bgs and
> releases that lock;
>
> 6) The cleaner kthread, while at btrfs_delete_unused_bgs(), sees block group X
> added by transaction N + 1 because it's doing a loop that finishes only when
> the list fs_info->unused_bgs is empty and locks and unlocks the spinlock
> fs_info->unused_bgs_lock on each iteration. So it deletes the block group
> and its respective chunk is released. Even if it didn't do the lock/unlock
> per iteration, it could still see block group X in the list, because the
> cleaner kthread might call btrfs_delete_unused_bgs() multiple times (for
> example if there are several snapshots to delete);
>
> 7) A new block group X' is created for data, and it's associated to the same chunk
> that block group X was associated to;
>
Actually this can't happen, we search the commit root for a free dev
extent, so if block group X` get's mapped to a dev extent that was
deleted in the same transaction as it was free'd in then that is a
different problem.
> 8) Extents from block group X' are allocated for file data and for example an fsync
> makes the file data be effectively written to disk;
>
Also if a new block group is allocated fsync() will trigger a full
transaction commit. So thinking about this more I'm not entirely sure
there is actually a problem here. Did you observe this issue? Are you
sure it's because of this change and not just exacerbated by it? Thanks,
Josef
next prev parent reply other threads:[~2014-11-05 20:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-05 19:56 [PATCH] Btrfs: fix race when cleaning unused block groups Filipe Manana
2014-11-05 20:07 ` Josef Bacik
2014-11-05 20:33 ` Josef Bacik [this message]
2014-11-05 21:03 ` Filipe David Manana
2014-11-05 21:13 ` Josef Bacik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=545A89B7.3000602@fb.com \
--to=jbacik@fb.com \
--cc=fdmanana@suse.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox