blob: 4eabb8f93b505501d10ef816e21e53ec642cb598 [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>
24
Chris Sosafc661a12013-02-26 14:43:21 -080025#include <base/posix/eintr_wrapper.h>
Gilad Arnold11c066f2012-05-10 14:37:25 -070026
Alex Deymob86787c2016-05-12 18:46:25 -070027#include "update_engine/common/utils.h"
28
Gilad Arnold11c066f2012-05-10 14:37:25 -070029namespace chromeos_update_engine {
30
31bool EintrSafeFileDescriptor::Open(const char* path, int flags, mode_t mode) {
32 CHECK_EQ(fd_, -1);
33 return ((fd_ = HANDLE_EINTR(open(path, flags, mode))) >= 0);
34}
35
36bool EintrSafeFileDescriptor::Open(const char* path, int flags) {
37 CHECK_EQ(fd_, -1);
38 return ((fd_ = HANDLE_EINTR(open(path, flags))) >= 0);
39}
40
41ssize_t EintrSafeFileDescriptor::Read(void* buf, size_t count) {
42 CHECK_GE(fd_, 0);
43 return HANDLE_EINTR(read(fd_, buf, count));
44}
45
46ssize_t EintrSafeFileDescriptor::Write(const void* buf, size_t count) {
47 CHECK_GE(fd_, 0);
48
49 // Attempt repeated writes, as long as some progress is being made.
50 char* char_buf = const_cast<char*>(reinterpret_cast<const char*>(buf));
51 ssize_t written = 0;
52 while (count > 0) {
53 ssize_t ret = HANDLE_EINTR(write(fd_, char_buf, count));
54
55 // Fail on either an error or no progress.
56 if (ret <= 0)
57 return (written ? written : ret);
58 written += ret;
59 count -= ret;
60 char_buf += ret;
61 }
62 return written;
63}
64
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080065off64_t EintrSafeFileDescriptor::Seek(off64_t offset, int whence) {
66 CHECK_GE(fd_, 0);
67 return lseek64(fd_, offset, whence);
68}
69
Alex Deymob86787c2016-05-12 18:46:25 -070070uint64_t EintrSafeFileDescriptor::BlockDevSize() {
71 if (fd_ < 0)
72 return 0;
73 struct stat stbuf;
74 if (fstat(fd_, &stbuf) < 0) {
75 PLOG(ERROR) << "Error stat-ing fd " << fd_;
76 return 0;
77 }
78 if (!S_ISBLK(stbuf.st_mode))
79 return 0;
80 off_t block_size = utils::BlockDevSize(fd_);
81 return block_size < 0 ? 0 : block_size;
82}
83
Alex Deymo79715ad2015-10-02 14:27:53 -070084bool EintrSafeFileDescriptor::BlkIoctl(int request,
85 uint64_t start,
86 uint64_t length,
87 int* result) {
Alex Deymo05e0e382015-12-07 20:18:16 -080088 // If the ioctl BLKZEROOUT is not defined, just fail to perform any of these
89 // operations.
90#ifndef BLKZEROOUT
91 return false;
92#else // defined(BLKZEROOUT)
Alex Deymo79715ad2015-10-02 14:27:53 -070093 DCHECK(request == BLKDISCARD || request == BLKZEROOUT ||
94 request == BLKSECDISCARD);
95 // On some devices, the BLKDISCARD will actually read back as zeros, instead
96 // of "undefined" data. The BLKDISCARDZEROES ioctl tells whether that's the
97 // case, so we issue a BLKDISCARD in those cases to speed up the writes.
98 unsigned int arg;
99 if (request == BLKZEROOUT && ioctl(fd_, BLKDISCARDZEROES, &arg) == 0 && arg)
100 request = BLKDISCARD;
101
102 // Ensure the |fd_| is in O_DIRECT mode during this operation, so the write
103 // cache for this region is invalidated. This is required since otherwise
104 // reading back this region could consume stale data from the cache.
105 int flags = fcntl(fd_, F_GETFL, 0);
106 if (flags == -1) {
107 PLOG(WARNING) << "Couldn't get flags on fd " << fd_;
108 return false;
109 }
110 if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags | O_DIRECT) == -1) {
111 PLOG(WARNING) << "Couldn't set O_DIRECT on fd " << fd_;
112 return false;
113 }
114
115 uint64_t range[2] = {start, length};
116 *result = ioctl(fd_, request, range);
117
118 if ((flags & O_DIRECT) == 0 && fcntl(fd_, F_SETFL, flags) == -1) {
119 PLOG(WARNING) << "Couldn't remove O_DIRECT on fd " << fd_;
120 return false;
121 }
122 return true;
Alex Deymo05e0e382015-12-07 20:18:16 -0800123#endif // defined(BLKZEROOUT)
Alex Deymo79715ad2015-10-02 14:27:53 -0700124}
125
Amin Hassani5192fe52017-08-28 10:28:46 -0700126bool EintrSafeFileDescriptor::Flush() {
127 CHECK_GE(fd_, 0);
128 return true;
129}
130
Gilad Arnold11c066f2012-05-10 14:37:25 -0700131bool EintrSafeFileDescriptor::Close() {
132 CHECK_GE(fd_, 0);
Mike Frysingerbcee2ca2014-05-14 16:28:23 -0400133 if (IGNORE_EINTR(close(fd_)))
Gilad Arnold6eccc532012-05-17 15:44:22 -0700134 return false;
Gilad Arnold6eccc532012-05-17 15:44:22 -0700135 fd_ = -1;
Alex Deymoa50011f2017-02-01 15:12:59 -0800136 return true;
Gilad Arnold6eccc532012-05-17 15:44:22 -0700137}
138
Gilad Arnold11c066f2012-05-10 14:37:25 -0700139} // namespace chromeos_update_engine