blob: 6101c680a325bb6635eb677e523c910e92cc3309 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Arnold11c066f2012-05-10 14:37:25 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/file_descriptor.h"
Gilad Arnold11c066f2012-05-10 14:37:25 -070018
19#include <fcntl.h>
Alex Deymo79715ad2015-10-02 14:27:53 -070020#include <linux/fs.h>
21#include <sys/ioctl.h>
Gilad Arnold11c066f2012-05-10 14:37:25 -070022#include <sys/stat.h>
23#include <sys/types.h>
Kelvin Zhang3dd83972020-10-15 14:02:00 -040024#include <unistd.h>
Gilad Arnold11c066f2012-05-10 14:37:25 -070025
Chris Sosafc661a12013-02-26 14:43:21 -080026#include <base/posix/eintr_wrapper.h>
Gilad Arnold11c066f2012-05-10 14:37:25 -070027
Alex Deymob86787c2016-05-12 18:46:25 -070028#include "update_engine/common/utils.h"
29
Gilad Arnold11c066f2012-05-10 14:37:25 -070030namespace chromeos_update_engine {
31
32bool EintrSafeFileDescriptor::Open(const char* path, int flags, mode_t mode) {
33 CHECK_EQ(fd_, -1);
34 return ((fd_ = HANDLE_EINTR(open(path, flags, mode))) >= 0);
35}
36
37bool EintrSafeFileDescriptor::Open(const char* path, int flags) {
38 CHECK_EQ(fd_, -1);
39 return ((fd_ = HANDLE_EINTR(open(path, flags))) >= 0);
40}
41
42ssize_t EintrSafeFileDescriptor::Read(void* buf, size_t count) {
43 CHECK_GE(fd_, 0);
44 return HANDLE_EINTR(read(fd_, buf, count));
45}
46
47ssize_t EintrSafeFileDescriptor::Write(const void* buf, size_t count) {
48 CHECK_GE(fd_, 0);
49
50 // Attempt repeated writes, as long as some progress is being made.
51 char* char_buf = const_cast<char*>(reinterpret_cast<const char*>(buf));
52 ssize_t written = 0;
53 while (count > 0) {
54 ssize_t ret = HANDLE_EINTR(write(fd_, char_buf, count));
55
56 // Fail on either an error or no progress.
57 if (ret <= 0)
58 return (written ? written : ret);
59 written += ret;
60 count -= ret;
61 char_buf += ret;
62 }
63 return written;
64}
65
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080066off64_t EintrSafeFileDescriptor::Seek(off64_t offset, int whence) {
67 CHECK_GE(fd_, 0);
68 return lseek64(fd_, offset, whence);
69}
70
Alex Deymob86787c2016-05-12 18:46:25 -070071uint64_t EintrSafeFileDescriptor::BlockDevSize() {
72 if (fd_ < 0)
73 return 0;
74 struct stat stbuf;
75 if (fstat(fd_, &stbuf) < 0) {
76 PLOG(ERROR) << "Error stat-ing fd " << fd_;
77 return 0;
78 }
79 if (!S_ISBLK(stbuf.st_mode))
80 return 0;
81 off_t block_size = utils::BlockDevSize(fd_);
82 return block_size < 0 ? 0 : block_size;
83}
84
Alex Deymo79715ad2015-10-02 14:27:53 -070085bool EintrSafeFileDescriptor::BlkIoctl(int request,
86 uint64_t start,
87 uint64_t length,
88 int* result) {
Alex Deymo05e0e382015-12-07 20:18:16 -080089 // If the ioctl BLKZEROOUT is not defined, just fail to perform any of these
90 // operations.
91#ifndef BLKZEROOUT
92 return false;
Amin Hassani008c4582019-01-13 16:22:47 -080093#else // defined(BLKZEROOUT)
Alex Deymo79715ad2015-10-02 14:27:53 -070094 DCHECK(request == BLKDISCARD || request == BLKZEROOUT ||
95 request == BLKSECDISCARD);
96 // On some devices, the BLKDISCARD will actually read back as zeros, instead
97 // of "undefined" data. The BLKDISCARDZEROES ioctl tells whether that's the
98 // case, so we issue a BLKDISCARD in those cases to speed up the writes.
99 unsigned int arg;
100 if (request == BLKZEROOUT && ioctl(fd_, BLKDISCARDZEROES, &arg) == 0 && arg)
101 request = BLKDISCARD;
102
103 // Ensure the |fd_| is in O_DIRECT mode during this operation, so the write
104 // cache for this region is invalidated. This is required since otherwise
105 // reading back this region could consume stale data from the cache.
106 int flags = fcntl(fd_, F_GETFL, 0);
107 if (flags == -1) {
108 PLOG(WARNING) << "Couldn't get flags on fd " << fd_;
109 return false;
110 }
111 if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags | O_DIRECT) == -1) {
112 PLOG(WARNING) << "Couldn't set O_DIRECT on fd " << fd_;
113 return false;
114 }
115
116 uint64_t range[2] = {start, length};
117 *result = ioctl(fd_, request, range);
118
119 if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags) == -1) {
120 PLOG(WARNING) << "Couldn't remove O_DIRECT on fd " << fd_;
121 return false;
122 }
123 return true;
Alex Deymo05e0e382015-12-07 20:18:16 -0800124#endif // defined(BLKZEROOUT)
Alex Deymo79715ad2015-10-02 14:27:53 -0700125}
126
Amin Hassani5192fe52017-08-28 10:28:46 -0700127bool EintrSafeFileDescriptor::Flush() {
128 CHECK_GE(fd_, 0);
Kelvin Zhang3dd83972020-10-15 14:02:00 -0400129 // Implemented as a No-Op, as delta_performer typically uses |O_DSYNC|, except
130 // in interactive settings.
Amin Hassani5192fe52017-08-28 10:28:46 -0700131 return true;
132}
133
Gilad Arnold11c066f2012-05-10 14:37:25 -0700134bool EintrSafeFileDescriptor::Close() {
135 CHECK_GE(fd_, 0);
Kelvin Zhang3dd83972020-10-15 14:02:00 -0400136 // https://stackoverflow.com/questions/705454/does-linux-guarantee-the-contents-of-a-file-is-flushed-to-disc-after-close
137 // |close()| doesn't imply |fsync()|, we need to do it manually.
138 fsync(fd_);
Mike Frysingerbcee2ca2014-05-14 16:28:23 -0400139 if (IGNORE_EINTR(close(fd_)))
Gilad Arnold6eccc532012-05-17 15:44:22 -0700140 return false;
Gilad Arnold6eccc532012-05-17 15:44:22 -0700141 fd_ = -1;
Alex Deymoa50011f2017-02-01 15:12:59 -0800142 return true;
Gilad Arnold6eccc532012-05-17 15:44:22 -0700143}
144
Gilad Arnold11c066f2012-05-10 14:37:25 -0700145} // namespace chromeos_update_engine