Discard the tail of the target partitions when updating.
The partition is normally way bigger than the data it holds and the
remainder of the partition is often ignored by the system. This patch
discards the tail of the partition, past the end of the updated data.
This would ensure that the unused blocks in these partitions can be
reallocated by the SSD controller to other blocks, and that data from
previous updates does not interfere with the current version.
Bug: 28744609
TEST=strace -e trace=file,ioctl shows the device discarding the end of the updated partitions.
Change-Id: Ie6135ee1aef2fa594c40d84af4d1d46a8f53cc3a
diff --git a/payload_consumer/file_descriptor.cc b/payload_consumer/file_descriptor.cc
index 309c60d..8a23dea 100644
--- a/payload_consumer/file_descriptor.cc
+++ b/payload_consumer/file_descriptor.cc
@@ -24,6 +24,8 @@
#include <base/posix/eintr_wrapper.h>
+#include "update_engine/common/utils.h"
+
namespace chromeos_update_engine {
bool EintrSafeFileDescriptor::Open(const char* path, int flags, mode_t mode) {
@@ -65,6 +67,20 @@
return lseek64(fd_, offset, whence);
}
+uint64_t EintrSafeFileDescriptor::BlockDevSize() {
+ if (fd_ < 0)
+ return 0;
+ struct stat stbuf;
+ if (fstat(fd_, &stbuf) < 0) {
+ PLOG(ERROR) << "Error stat-ing fd " << fd_;
+ return 0;
+ }
+ if (!S_ISBLK(stbuf.st_mode))
+ return 0;
+ off_t block_size = utils::BlockDevSize(fd_);
+ return block_size < 0 ? 0 : block_size;
+}
+
bool EintrSafeFileDescriptor::BlkIoctl(int request,
uint64_t start,
uint64_t length,