blob: 8fb2dbcc562ad665b39c69b0a32c098dc072bfa4 [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 Hironocb9153b2016-12-08 14:22:13 +090026#include <android-base/file.h>
Daichi Hirono7f8e8192016-10-27 10:37:05 +090027#include <android-base/logging.h>
28#include <android-base/macros.h>
29
30namespace android {
Daichi Hironoa0aecda2016-11-08 10:17:51 +090031namespace fuse {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090032
Daichi Hirono0d97be42016-11-10 09:17:17 +090033static_assert(
34 std::is_standard_layout<FuseBuffer>::value,
35 "FuseBuffer must be standard layout union.");
36
37template <typename T>
Daichi Hironocb9153b2016-12-08 14:22:13 +090038bool FuseMessage<T>::CheckHeaderLength(const char* name) const {
Daichi Hirono0d97be42016-11-10 09:17:17 +090039 const auto& header = static_cast<const T*>(this)->header;
Daichi Hironocb9153b2016-12-08 14:22:13 +090040 if (header.len >= sizeof(header) && header.len <= sizeof(T)) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090041 return true;
42 } else {
Daichi Hironocb9153b2016-12-08 14:22:13 +090043 LOG(ERROR) << "Invalid header length is found in " << name << ": " <<
44 header.len;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090045 return false;
46 }
47}
48
Daichi Hirono0d97be42016-11-10 09:17:17 +090049template <typename T>
50bool FuseMessage<T>::Read(int fd) {
Daichi Hironocb9153b2016-12-08 14:22:13 +090051 char* const buf = reinterpret_cast<char*>(this);
52 const ssize_t result = TEMP_FAILURE_RETRY(::read(fd, buf, sizeof(T)));
53 if (result < 0) {
54 PLOG(ERROR) << "Failed to read a FUSE message";
55 return false;
56 }
57
58 const auto& header = static_cast<const T*>(this)->header;
59 if (result < static_cast<ssize_t>(sizeof(header))) {
60 LOG(ERROR) << "Read bytes " << result << " are shorter than header size " <<
61 sizeof(header);
62 return false;
63 }
64
65 if (!CheckHeaderLength("Read")) {
66 return false;
67 }
68
69 if (static_cast<uint32_t>(result) > header.len) {
70 LOG(ERROR) << "Read bytes " << result << " are longer than header.len " <<
71 header.len;
72 return false;
73 }
74
75 if (!base::ReadFully(fd, buf + result, header.len - result)) {
76 PLOG(ERROR) << "ReadFully failed";
77 return false;
78 }
79
80 return true;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090081}
82
Daichi Hirono0d97be42016-11-10 09:17:17 +090083template <typename T>
84bool FuseMessage<T>::Write(int fd) const {
Daichi Hironocb9153b2016-12-08 14:22:13 +090085 if (!CheckHeaderLength("Write")) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090086 return false;
87 }
Daichi Hironocb9153b2016-12-08 14:22:13 +090088
89 const char* const buf = reinterpret_cast<const char*>(this);
90 const auto& header = static_cast<const T*>(this)->header;
91 if (!base::WriteFully(fd, buf, header.len)) {
92 PLOG(ERROR) << "WriteFully failed";
93 return false;
94 }
95
96 return true;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090097}
98
Daichi Hirono0d97be42016-11-10 09:17:17 +090099template class FuseMessage<FuseRequest>;
100template class FuseMessage<FuseResponse>;
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900101
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900102void FuseRequest::Reset(
103 uint32_t data_length, uint32_t opcode, uint64_t unique) {
104 memset(this, 0, sizeof(fuse_in_header) + data_length);
105 header.len = sizeof(fuse_in_header) + data_length;
106 header.opcode = opcode;
107 header.unique = unique;
108}
109
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900110void FuseResponse::ResetHeader(
111 uint32_t data_length, int32_t error, uint64_t unique) {
112 CHECK_LE(error, 0) << "error should be zero or negative.";
113 header.len = sizeof(fuse_out_header) + data_length;
114 header.error = error;
115 header.unique = unique;
116}
117
118void FuseResponse::Reset(uint32_t data_length, int32_t error, uint64_t unique) {
119 memset(this, 0, sizeof(fuse_out_header) + data_length);
120 ResetHeader(data_length, error, unique);
121}
122
123void FuseBuffer::HandleInit() {
124 const fuse_init_in* const in = &request.init_in;
125
126 // Before writing |out|, we need to copy data from |in|.
127 const uint64_t unique = request.header.unique;
128 const uint32_t minor = in->minor;
129 const uint32_t max_readahead = in->max_readahead;
130
131 // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
132 // defined (fuse version 7.6). The structure is the same from 7.6 through
133 // 7.22. Beginning with 7.23, the structure increased in size and added
134 // new parameters.
135 if (in->major != FUSE_KERNEL_VERSION || in->minor < 6) {
136 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " << in->major
137 << "." << in->minor << " Expected at least " << FUSE_KERNEL_VERSION
138 << ".6";
139 response.Reset(0, -EPERM, unique);
140 return;
141 }
142
Daichi Hirono471ad6a2016-11-10 09:41:54 +0900143 // We limit ourselves to minor=15 because we don't handle BATCH_FORGET yet.
144 // Thus we need to use FUSE_COMPAT_22_INIT_OUT_SIZE.
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900145#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
146 // FUSE_KERNEL_VERSION >= 23.
Daichi Hirono471ad6a2016-11-10 09:41:54 +0900147 const size_t response_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
148#else
149 const size_t response_size = sizeof(fuse_init_out);
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900150#endif
151
152 response.Reset(response_size, kFuseSuccess, unique);
153 fuse_init_out* const out = &response.init_out;
154 out->major = FUSE_KERNEL_VERSION;
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900155 out->minor = std::min(minor, 15u);
156 out->max_readahead = max_readahead;
157 out->flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
158 out->max_background = 32;
159 out->congestion_threshold = 32;
160 out->max_write = kFuseMaxWrite;
161}
162
163void FuseBuffer::HandleNotImpl() {
164 LOG(VERBOSE) << "NOTIMPL op=" << request.header.opcode << " uniq="
165 << request.header.unique << " nid=" << request.header.nodeid;
166 const uint64_t unique = request.header.unique;
167 response.Reset(0, -ENOSYS, unique);
168}
169
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900170} // namespace fuse
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900171} // namespace android