blob: 13cfc88ec6b1de018205cacf544073db3d8a7e6f [file] [log] [blame]
Daichi Hirono7f8e8192016-10-27 10:37:05 +09001/*
2 * Copyright (C) 2016 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 specic language governing permissions and
14 * limitations under the License.
15 */
16
Daichi Hironoc6134762016-10-27 14:57:55 +090017#include "libappfuse/FuseBuffer.h"
Daichi Hirono7f8e8192016-10-27 10:37:05 +090018
19#include <inttypes.h>
20#include <string.h>
21#include <unistd.h>
22
23#include <algorithm>
Daichi Hirono0d97be42016-11-10 09:17:17 +090024#include <type_traits>
Daichi Hirono7f8e8192016-10-27 10:37:05 +090025
Daichi Hirono57b780f2017-03-06 14:02:42 +090026#include <sys/socket.h>
27
Daichi Hironocb9153b2016-12-08 14:22:13 +090028#include <android-base/file.h>
Daichi Hirono7f8e8192016-10-27 10:37:05 +090029#include <android-base/logging.h>
30#include <android-base/macros.h>
31
32namespace android {
Daichi Hironoa0aecda2016-11-08 10:17:51 +090033namespace fuse {
Daichi Hirono57b780f2017-03-06 14:02:42 +090034namespace {
Daichi Hirono0d97be42016-11-10 09:17:17 +090035
36template <typename T>
Daichi Hirono57b780f2017-03-06 14:02:42 +090037bool CheckHeaderLength(const FuseMessage<T>* self, const char* name) {
38 const auto& header = static_cast<const T*>(self)->header;
39 if (header.len >= sizeof(header) && header.len <= sizeof(T)) {
40 return true;
41 } else {
42 LOG(ERROR) << "Invalid header length is found in " << name << ": " << header.len;
43 return false;
44 }
45}
46
47template <typename T>
48ResultOrAgain ReadInternal(FuseMessage<T>* self, int fd, int sockflag) {
49 char* const buf = reinterpret_cast<char*>(self);
50 const ssize_t result = sockflag ? TEMP_FAILURE_RETRY(recv(fd, buf, sizeof(T), sockflag))
51 : TEMP_FAILURE_RETRY(read(fd, buf, sizeof(T)));
52
53 switch (result) {
54 case 0:
55 // Expected EOF.
56 return ResultOrAgain::kFailure;
57 case -1:
58 if (errno == EAGAIN) {
59 return ResultOrAgain::kAgain;
60 }
61 PLOG(ERROR) << "Failed to read a FUSE message";
62 return ResultOrAgain::kFailure;
63 }
64
65 const auto& header = static_cast<const T*>(self)->header;
66 if (result < static_cast<ssize_t>(sizeof(header))) {
67 LOG(ERROR) << "Read bytes " << result << " are shorter than header size " << sizeof(header);
68 return ResultOrAgain::kFailure;
69 }
70
71 if (!CheckHeaderLength<T>(self, "Read")) {
72 return ResultOrAgain::kFailure;
73 }
74
75 if (static_cast<uint32_t>(result) != header.len) {
76 LOG(ERROR) << "Read bytes " << result << " are different from header.len " << header.len;
77 return ResultOrAgain::kFailure;
78 }
79
80 return ResultOrAgain::kSuccess;
81}
82
83template <typename T>
84ResultOrAgain WriteInternal(const FuseMessage<T>* self, int fd, int sockflag) {
85 if (!CheckHeaderLength<T>(self, "Write")) {
86 return ResultOrAgain::kFailure;
87 }
88
89 const char* const buf = reinterpret_cast<const char*>(self);
90 const auto& header = static_cast<const T*>(self)->header;
91 const int result = sockflag ? TEMP_FAILURE_RETRY(send(fd, buf, header.len, sockflag))
92 : TEMP_FAILURE_RETRY(write(fd, buf, header.len));
93
94 if (result == -1) {
95 if (errno == EAGAIN) {
96 return ResultOrAgain::kAgain;
97 }
98 PLOG(ERROR) << "Failed to write a FUSE message";
99 return ResultOrAgain::kFailure;
100 }
101
102 CHECK(static_cast<uint32_t>(result) == header.len);
103 return ResultOrAgain::kSuccess;
104}
105}
106
107static_assert(std::is_standard_layout<FuseBuffer>::value,
108 "FuseBuffer must be standard layout union.");
109
110bool SetupMessageSockets(base::unique_fd (*result)[2]) {
111 base::unique_fd fds[2];
112 {
113 int raw_fds[2];
114 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, raw_fds) == -1) {
115 PLOG(ERROR) << "Failed to create sockets for proxy";
116 return false;
117 }
118 fds[0].reset(raw_fds[0]);
119 fds[1].reset(raw_fds[1]);
120 }
121
122 constexpr int kMaxMessageSize = sizeof(FuseBuffer);
123 if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &kMaxMessageSize, sizeof(int)) != 0 ||
124 setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, &kMaxMessageSize, sizeof(int)) != 0) {
125 PLOG(ERROR) << "Failed to update buffer size for socket";
126 return false;
127 }
128
129 (*result)[0] = std::move(fds[0]);
130 (*result)[1] = std::move(fds[1]);
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900131 return true;
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900132}
133
Daichi Hirono0d97be42016-11-10 09:17:17 +0900134template <typename T>
135bool FuseMessage<T>::Read(int fd) {
Daichi Hirono57b780f2017-03-06 14:02:42 +0900136 return ReadInternal(this, fd, 0) == ResultOrAgain::kSuccess;
137}
Daichi Hironocb9153b2016-12-08 14:22:13 +0900138
Daichi Hirono57b780f2017-03-06 14:02:42 +0900139template <typename T>
140ResultOrAgain FuseMessage<T>::ReadOrAgain(int fd) {
141 return ReadInternal(this, fd, MSG_DONTWAIT);
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900142}
143
Daichi Hirono0d97be42016-11-10 09:17:17 +0900144template <typename T>
145bool FuseMessage<T>::Write(int fd) const {
Daichi Hirono57b780f2017-03-06 14:02:42 +0900146 return WriteInternal(this, fd, 0) == ResultOrAgain::kSuccess;
147}
Daichi Hironocb9153b2016-12-08 14:22:13 +0900148
Daichi Hirono57b780f2017-03-06 14:02:42 +0900149template <typename T>
150ResultOrAgain FuseMessage<T>::WriteOrAgain(int fd) const {
151 return WriteInternal(this, fd, MSG_DONTWAIT);
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900152}
153
Daichi Hirono0d97be42016-11-10 09:17:17 +0900154template class FuseMessage<FuseRequest>;
155template class FuseMessage<FuseResponse>;
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900156
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900157void FuseRequest::Reset(
158 uint32_t data_length, uint32_t opcode, uint64_t unique) {
159 memset(this, 0, sizeof(fuse_in_header) + data_length);
160 header.len = sizeof(fuse_in_header) + data_length;
161 header.opcode = opcode;
162 header.unique = unique;
163}
164
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900165void FuseResponse::ResetHeader(
166 uint32_t data_length, int32_t error, uint64_t unique) {
167 CHECK_LE(error, 0) << "error should be zero or negative.";
168 header.len = sizeof(fuse_out_header) + data_length;
169 header.error = error;
170 header.unique = unique;
171}
172
173void FuseResponse::Reset(uint32_t data_length, int32_t error, uint64_t unique) {
174 memset(this, 0, sizeof(fuse_out_header) + data_length);
175 ResetHeader(data_length, error, unique);
176}
177
178void FuseBuffer::HandleInit() {
179 const fuse_init_in* const in = &request.init_in;
180
181 // Before writing |out|, we need to copy data from |in|.
182 const uint64_t unique = request.header.unique;
183 const uint32_t minor = in->minor;
184 const uint32_t max_readahead = in->max_readahead;
185
186 // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
187 // defined (fuse version 7.6). The structure is the same from 7.6 through
188 // 7.22. Beginning with 7.23, the structure increased in size and added
189 // new parameters.
190 if (in->major != FUSE_KERNEL_VERSION || in->minor < 6) {
191 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " << in->major
192 << "." << in->minor << " Expected at least " << FUSE_KERNEL_VERSION
193 << ".6";
194 response.Reset(0, -EPERM, unique);
195 return;
196 }
197
Daichi Hirono471ad6a2016-11-10 09:41:54 +0900198 // We limit ourselves to minor=15 because we don't handle BATCH_FORGET yet.
199 // Thus we need to use FUSE_COMPAT_22_INIT_OUT_SIZE.
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900200#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
201 // FUSE_KERNEL_VERSION >= 23.
Daichi Hirono471ad6a2016-11-10 09:41:54 +0900202 const size_t response_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
203#else
204 const size_t response_size = sizeof(fuse_init_out);
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900205#endif
206
207 response.Reset(response_size, kFuseSuccess, unique);
208 fuse_init_out* const out = &response.init_out;
209 out->major = FUSE_KERNEL_VERSION;
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900210 out->minor = std::min(minor, 15u);
211 out->max_readahead = max_readahead;
212 out->flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
213 out->max_background = 32;
214 out->congestion_threshold = 32;
215 out->max_write = kFuseMaxWrite;
216}
217
218void FuseBuffer::HandleNotImpl() {
219 LOG(VERBOSE) << "NOTIMPL op=" << request.header.opcode << " uniq="
220 << request.header.unique << " nid=" << request.header.nodeid;
221 const uint64_t unique = request.header.unique;
222 response.Reset(0, -ENOSYS, unique);
223}
224
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900225} // namespace fuse
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900226} // namespace android