md: avoid crash when stopping md array races with closing other open fds.
authorNeilBrown <neilb@suse.de>
Thu, 19 Jul 2012 05:59:18 +0000 (15:59 +1000)
committerBen Hutchings <ben@decadent.org.uk>
Wed, 25 Jul 2012 03:11:27 +0000 (04:11 +0100)
commit068bd5de42ebc3a2aa2ef876614d7c7d11c79f6d
tree224b250ad29d1df8011bb3e04761d9ff306c349a
parente23f455279bab00dcd73e8dd26c5f262dd1e3cc2
md: avoid crash when stopping md array races with closing other open fds.

commit a05b7ea03d72f36edb0cec05e8893803335c61a0 upstream.

md will refuse to stop an array if any other fd (or mounted fs) is
using it.
When any fs is unmounted of when the last open fd is closed all
pending IO will be flushed (e.g. sync_blockdev call in __blkdev_put)
so there will be no pending IO to worry about when the array is
stopped.

However in order to send the STOP_ARRAY ioctl to stop the array one
must first get and open fd on the block device.
If some fd is being used to write to the block device and it is closed
after mdadm open the block device, but before mdadm issues the
STOP_ARRAY ioctl, then there will be no last-close on the md device so
__blkdev_put will not call sync_blockdev.

If this happens, then IO can still be in-flight while md tears down
the array and bad things can happen (use-after-free and subsequent
havoc).

So in the case where do_md_stop is being called from an open file
descriptor, call sync_block after taking the mutex to ensure there
will be no new openers.

This is needed when setting a read-write device to read-only too.

Reported-by: majianpeng <majianpeng@gmail.com>
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/md/md.c