Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/file_descriptor.h" |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 20 | #include <linux/fs.h> |
| 21 | #include <sys/ioctl.h> |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
Kelvin Zhang | 3dd8397 | 2020-10-15 14:02:00 -0400 | [diff] [blame] | 24 | #include <unistd.h> |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 25 | |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 26 | #include <base/posix/eintr_wrapper.h> |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 27 | |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 28 | #include "update_engine/common/utils.h" |
| 29 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 30 | namespace chromeos_update_engine { |
| 31 | |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 32 | EintrSafeFileDescriptor::~EintrSafeFileDescriptor() { |
| 33 | if (IsOpen()) { |
| 34 | Close(); |
| 35 | } |
| 36 | } |
| 37 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 38 | bool EintrSafeFileDescriptor::Open(const char* path, int flags, mode_t mode) { |
| 39 | CHECK_EQ(fd_, -1); |
| 40 | return ((fd_ = HANDLE_EINTR(open(path, flags, mode))) >= 0); |
| 41 | } |
| 42 | |
| 43 | bool EintrSafeFileDescriptor::Open(const char* path, int flags) { |
| 44 | CHECK_EQ(fd_, -1); |
| 45 | return ((fd_ = HANDLE_EINTR(open(path, flags))) >= 0); |
| 46 | } |
| 47 | |
| 48 | ssize_t EintrSafeFileDescriptor::Read(void* buf, size_t count) { |
| 49 | CHECK_GE(fd_, 0); |
| 50 | return HANDLE_EINTR(read(fd_, buf, count)); |
| 51 | } |
| 52 | |
| 53 | ssize_t EintrSafeFileDescriptor::Write(const void* buf, size_t count) { |
| 54 | CHECK_GE(fd_, 0); |
| 55 | |
| 56 | // Attempt repeated writes, as long as some progress is being made. |
| 57 | char* char_buf = const_cast<char*>(reinterpret_cast<const char*>(buf)); |
| 58 | ssize_t written = 0; |
| 59 | while (count > 0) { |
| 60 | ssize_t ret = HANDLE_EINTR(write(fd_, char_buf, count)); |
| 61 | |
| 62 | // Fail on either an error or no progress. |
| 63 | if (ret <= 0) |
| 64 | return (written ? written : ret); |
| 65 | written += ret; |
| 66 | count -= ret; |
| 67 | char_buf += ret; |
| 68 | } |
| 69 | return written; |
| 70 | } |
| 71 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 72 | off64_t EintrSafeFileDescriptor::Seek(off64_t offset, int whence) { |
| 73 | CHECK_GE(fd_, 0); |
| 74 | return lseek64(fd_, offset, whence); |
| 75 | } |
| 76 | |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 77 | uint64_t EintrSafeFileDescriptor::BlockDevSize() { |
| 78 | if (fd_ < 0) |
| 79 | return 0; |
Kelvin Zhang | 8fffea9 | 2023-07-25 20:34:26 -0700 | [diff] [blame^] | 80 | struct stat stbuf {}; |
Alex Deymo | b86787c | 2016-05-12 18:46:25 -0700 | [diff] [blame] | 81 | if (fstat(fd_, &stbuf) < 0) { |
| 82 | PLOG(ERROR) << "Error stat-ing fd " << fd_; |
| 83 | return 0; |
| 84 | } |
| 85 | if (!S_ISBLK(stbuf.st_mode)) |
| 86 | return 0; |
| 87 | off_t block_size = utils::BlockDevSize(fd_); |
| 88 | return block_size < 0 ? 0 : block_size; |
| 89 | } |
| 90 | |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 91 | bool EintrSafeFileDescriptor::BlkIoctl(int request, |
| 92 | uint64_t start, |
| 93 | uint64_t length, |
| 94 | int* result) { |
Alex Deymo | 05e0e38 | 2015-12-07 20:18:16 -0800 | [diff] [blame] | 95 | // If the ioctl BLKZEROOUT is not defined, just fail to perform any of these |
| 96 | // operations. |
| 97 | #ifndef BLKZEROOUT |
| 98 | return false; |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 99 | #else // defined(BLKZEROOUT) |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 100 | DCHECK(request == BLKDISCARD || request == BLKZEROOUT || |
| 101 | request == BLKSECDISCARD); |
| 102 | // On some devices, the BLKDISCARD will actually read back as zeros, instead |
| 103 | // of "undefined" data. The BLKDISCARDZEROES ioctl tells whether that's the |
| 104 | // case, so we issue a BLKDISCARD in those cases to speed up the writes. |
Kelvin Zhang | 8fffea9 | 2023-07-25 20:34:26 -0700 | [diff] [blame^] | 105 | unsigned int arg{}; |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 106 | if (request == BLKZEROOUT && ioctl(fd_, BLKDISCARDZEROES, &arg) == 0 && arg) |
| 107 | request = BLKDISCARD; |
| 108 | |
| 109 | // Ensure the |fd_| is in O_DIRECT mode during this operation, so the write |
| 110 | // cache for this region is invalidated. This is required since otherwise |
| 111 | // reading back this region could consume stale data from the cache. |
| 112 | int flags = fcntl(fd_, F_GETFL, 0); |
| 113 | if (flags == -1) { |
| 114 | PLOG(WARNING) << "Couldn't get flags on fd " << fd_; |
| 115 | return false; |
| 116 | } |
| 117 | if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags | O_DIRECT) == -1) { |
| 118 | PLOG(WARNING) << "Couldn't set O_DIRECT on fd " << fd_; |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | uint64_t range[2] = {start, length}; |
| 123 | *result = ioctl(fd_, request, range); |
| 124 | |
| 125 | if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags) == -1) { |
| 126 | PLOG(WARNING) << "Couldn't remove O_DIRECT on fd " << fd_; |
| 127 | return false; |
| 128 | } |
| 129 | return true; |
Alex Deymo | 05e0e38 | 2015-12-07 20:18:16 -0800 | [diff] [blame] | 130 | #endif // defined(BLKZEROOUT) |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Amin Hassani | 5192fe5 | 2017-08-28 10:28:46 -0700 | [diff] [blame] | 133 | bool EintrSafeFileDescriptor::Flush() { |
| 134 | CHECK_GE(fd_, 0); |
Kelvin Zhang | 3dd8397 | 2020-10-15 14:02:00 -0400 | [diff] [blame] | 135 | // Implemented as a No-Op, as delta_performer typically uses |O_DSYNC|, except |
| 136 | // in interactive settings. |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 137 | fsync(fd_); |
Amin Hassani | 5192fe5 | 2017-08-28 10:28:46 -0700 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 141 | bool EintrSafeFileDescriptor::Close() { |
Kelvin Zhang | 8704c83 | 2021-05-10 17:53:14 -0400 | [diff] [blame] | 142 | if (fd_ < 0) { |
| 143 | return false; |
| 144 | } |
Kelvin Zhang | 3dd8397 | 2020-10-15 14:02:00 -0400 | [diff] [blame] | 145 | // https://stackoverflow.com/questions/705454/does-linux-guarantee-the-contents-of-a-file-is-flushed-to-disc-after-close |
| 146 | // |close()| doesn't imply |fsync()|, we need to do it manually. |
| 147 | fsync(fd_); |
Mike Frysinger | bcee2ca | 2014-05-14 16:28:23 -0400 | [diff] [blame] | 148 | if (IGNORE_EINTR(close(fd_))) |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 149 | return false; |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 150 | fd_ = -1; |
Alex Deymo | a50011f | 2017-02-01 15:12:59 -0800 | [diff] [blame] | 151 | return true; |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Gilad Arnold | 11c066f | 2012-05-10 14:37:25 -0700 | [diff] [blame] | 154 | } // namespace chromeos_update_engine |