Skip to contentSkip to conversation

Race condition between btrfs and udev

DISCUSSION Wang Shilong 1 participant 1 message Jun 4, 2014 Btrfs file system

lore
Share & tools
lore
#0 lore raw

Originally this problem was reproduced by the following scripts:

# dd if=/dev/zero of=data bs=1M  count=50
# losetup /dev/loop1 data
# i=1
# while [ 1 ]
   do
           mkfs.btrfs -fK /dev/loop1 >& /dev/null || exit 1
           i++
           echo "loop $i"
   done

Further, a easy way to trigger this problem is by running the followng c codes repeatedly:

int main(int argc, char **argv)
{

        int fd = open(argv[1], O_RDWR | O_EXCL);
        if (fd < 0) {
                perror("fail to open");
                exit(1);
        }
        close(fd);
        return 0;
}

here @argv[1] needs a btrfs block device.

So the problem is RW opening would trigger udev event which will call btrfs_scan_one_device() In btrfs_scan_one_device(), it would open the block device with EXCL flag...meanwhile if another program try to open that device with O_EXCL, it would fail with EBUSY....

I don't know whether this is a serious problem, now there are two places in btrfs-progs that is trying to open device with O_EXCL:

1. in utils.c: test_dev_for_mkfs()
2. in disk-io.c: __open_ctree_fd()

Any ideas on this? maybe we can remove @EXCL flag from btrfs-progs?

Thanks,
Wang