From 363d624d5379ec735b38de22add32022e206ab37 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Fri, 4 Jan 2019 11:14:19 -0800 Subject: updater: erase ignores EOPNOTSUPP for BLKDISCARD Test: sideload on cuttlefish Bug: 111801737 Change-Id: I784a2142049054f38d6b70c7af7e88a451996d83 --- updater/blockimg.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index c4c09098e..9d5b01734 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp @@ -178,14 +178,18 @@ static bool SetPartitionUpdatedMarker(const std::string& marker) { return true; } -static bool discard_blocks(int fd, off64_t offset, uint64_t size) { - // Don't discard blocks unless the update is a retry run. - if (!is_retry) { +static bool discard_blocks(int fd, off64_t offset, uint64_t size, bool force = false) { + // Don't discard blocks unless the update is a retry run or force == true + if (!is_retry && !force) { return true; } uint64_t args[2] = { static_cast(offset), size }; if (ioctl(fd, BLKDISCARD, &args) == -1) { + // On devices that does not support BLKDISCARD, ignore the error. + if (errno == EOPNOTSUPP) { + return true; + } PLOG(ERROR) << "BLKDISCARD ioctl failed"; return false; } @@ -1448,14 +1452,9 @@ static int PerformCommandErase(CommandParameters& params) { LOG(INFO) << " erasing " << tgt.blocks() << " blocks"; for (const auto& [begin, end] : tgt) { - uint64_t blocks[2]; - // offset in bytes - blocks[0] = begin * static_cast(BLOCKSIZE); - // length in bytes - blocks[1] = (end - begin) * static_cast(BLOCKSIZE); - - if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) { - PLOG(ERROR) << "BLKDISCARD ioctl failed"; + off64_t offset = static_cast(begin) * BLOCKSIZE; + size_t size = (end - begin) * BLOCKSIZE; + if (!discard_blocks(params.fd, offset, size, true /* force */)) { return -1; } } -- cgit v1.2.3