From cedb3f17d257fdcd2079a3cad45f5850991c1ef6 Mon Sep 17 00:00:00 2001 From: Michel Machado Date: Fri, 13 Nov 2015 20:18:12 -0500 Subject: [PATCH] f3probe: fix a (supposed) race condition Although I (Michel) am not 100% sure, the kernel seemed to be allowing reads to get data from an internal cache in the kernel; the condition is tricky to reproduce, and trickier to be sure since fake drives are not reliable or preditable. The results seems more stable with this patch, though. --- libdevs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libdevs.c b/libdevs.c index a38ed07..c53c3de 100644 --- a/libdevs.c +++ b/libdevs.c @@ -444,15 +444,22 @@ static int bdev_write_blocks(struct device *dev, const char *buf, size_t length = (last_pos - first_pos + 1) << block_order; off_t offset = first_pos << block_order; off_t off_ret = lseek(bdev->fd, offset, SEEK_SET); + int rc; if (off_ret < 0) return - errno; assert(off_ret == offset); - return write_all(bdev->fd, buf, length); + rc = write_all(bdev->fd, buf, length); + if (rc) + return rc; + rc = fsync(bdev->fd); + if (rc) + return rc; + return posix_fadvise(bdev->fd, 0, 0, POSIX_FADV_DONTNEED); } static inline int bdev_open(const char *filename) { - return open(filename, O_RDWR | O_DIRECT | O_SYNC); + return open(filename, O_RDWR | O_DIRECT); } static struct udev_device *map_dev_to_usb_dev(struct udev_device *dev)