blob: 882d545525acec71be82a6ec72b3e2891f091b1e [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
26#include <android-base/logging.h>
27#include <android-base/macros.h>
28
29namespace android {
Daichi Hironoa0aecda2016-11-08 10:17:51 +090030namespace fuse {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090031
Daichi Hirono0d97be42016-11-10 09:17:17 +090032static_assert(
33 std::is_standard_layout<FuseBuffer>::value,
34 "FuseBuffer must be standard layout union.");
35
36template <typename T>
Daichi Hironoa6373ec2016-12-05 16:50:44 +090037bool FuseMessage<T>::CheckPacketSize(size_t size, const char* name) const {
Daichi Hirono0d97be42016-11-10 09:17:17 +090038 const auto& header = static_cast<const T*>(this)->header;
Daichi Hironoa6373ec2016-12-05 16:50:44 +090039 if (size >= sizeof(header) && size <= sizeof(T)) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090040 return true;
41 } else {
Daichi Hironoa6373ec2016-12-05 16:50:44 +090042 LOG(ERROR) << name << " is invalid=" << size;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090043 return false;
44 }
45}
46
Daichi Hirono0d97be42016-11-10 09:17:17 +090047template <typename T>
Daichi Hironoa6373ec2016-12-05 16:50:44 +090048bool FuseMessage<T>::CheckResult(int result, const char* operation_name) const {
49 if (result == 0) {
50 // Expected close of other endpoints.
51 return false;
52 }
53 if (result < 0) {
54 PLOG(ERROR) << "Failed to " << operation_name << " a packet";
55 return false;
56 }
57 return true;
58}
59
60template <typename T>
61bool FuseMessage<T>::CheckHeaderLength(int result, const char* operation_name) const {
Daichi Hirono0d97be42016-11-10 09:17:17 +090062 const auto& header = static_cast<const T*>(this)->header;
Daichi Hironoa6373ec2016-12-05 16:50:44 +090063 if (static_cast<uint32_t>(result) == header.len) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090064 return true;
65 } else {
Daichi Hironoa6373ec2016-12-05 16:50:44 +090066 LOG(ERROR) << "Invalid header length: operation_name=" << operation_name
67 << " result=" << result
68 << " header.len=" << header.len;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090069 return false;
70 }
71}
72
Daichi Hirono0d97be42016-11-10 09:17:17 +090073template <typename T>
74bool FuseMessage<T>::Read(int fd) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090075 const ssize_t result = TEMP_FAILURE_RETRY(::read(fd, this, sizeof(T)));
Daichi Hironoa6373ec2016-12-05 16:50:44 +090076 return CheckResult(result, "read") && CheckPacketSize(result, "read count") &&
77 CheckHeaderLength(result, "read");
Daichi Hirono7f8e8192016-10-27 10:37:05 +090078}
79
Daichi Hirono0d97be42016-11-10 09:17:17 +090080template <typename T>
81bool FuseMessage<T>::Write(int fd) const {
82 const auto& header = static_cast<const T*>(this)->header;
Daichi Hironoa6373ec2016-12-05 16:50:44 +090083 if (!CheckPacketSize(header.len, "header.len")) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090084 return false;
85 }
86 const ssize_t result = TEMP_FAILURE_RETRY(::write(fd, this, header.len));
Daichi Hironoa6373ec2016-12-05 16:50:44 +090087 return CheckResult(result, "write") && CheckHeaderLength(result, "write");
Daichi Hirono7f8e8192016-10-27 10:37:05 +090088}
89
Daichi Hirono0d97be42016-11-10 09:17:17 +090090template class FuseMessage<FuseRequest>;
91template class FuseMessage<FuseResponse>;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090092
Daichi Hironoa0aecda2016-11-08 10:17:51 +090093void FuseRequest::Reset(
94 uint32_t data_length, uint32_t opcode, uint64_t unique) {
95 memset(this, 0, sizeof(fuse_in_header) + data_length);
96 header.len = sizeof(fuse_in_header) + data_length;
97 header.opcode = opcode;
98 header.unique = unique;
99}
100
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900101void FuseResponse::ResetHeader(
102 uint32_t data_length, int32_t error, uint64_t unique) {
103 CHECK_LE(error, 0) << "error should be zero or negative.";
104 header.len = sizeof(fuse_out_header) + data_length;
105 header.error = error;
106 header.unique = unique;
107}
108
109void FuseResponse::Reset(uint32_t data_length, int32_t error, uint64_t unique) {
110 memset(this, 0, sizeof(fuse_out_header) + data_length);
111 ResetHeader(data_length, error, unique);
112}
113
114void FuseBuffer::HandleInit() {
115 const fuse_init_in* const in = &request.init_in;
116
117 // Before writing |out|, we need to copy data from |in|.
118 const uint64_t unique = request.header.unique;
119 const uint32_t minor = in->minor;
120 const uint32_t max_readahead = in->max_readahead;
121
122 // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
123 // defined (fuse version 7.6). The structure is the same from 7.6 through
124 // 7.22. Beginning with 7.23, the structure increased in size and added
125 // new parameters.
126 if (in->major != FUSE_KERNEL_VERSION || in->minor < 6) {
127 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " << in->major
128 << "." << in->minor << " Expected at least " << FUSE_KERNEL_VERSION
129 << ".6";
130 response.Reset(0, -EPERM, unique);
131 return;
132 }
133
Daichi Hirono471ad6a2016-11-10 09:41:54 +0900134 // We limit ourselves to minor=15 because we don't handle BATCH_FORGET yet.
135 // Thus we need to use FUSE_COMPAT_22_INIT_OUT_SIZE.
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900136#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
137 // FUSE_KERNEL_VERSION >= 23.
Daichi Hirono471ad6a2016-11-10 09:41:54 +0900138 const size_t response_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
139#else
140 const size_t response_size = sizeof(fuse_init_out);
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900141#endif
142
143 response.Reset(response_size, kFuseSuccess, unique);
144 fuse_init_out* const out = &response.init_out;
145 out->major = FUSE_KERNEL_VERSION;
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900146 out->minor = std::min(minor, 15u);
147 out->max_readahead = max_readahead;
148 out->flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
149 out->max_background = 32;
150 out->congestion_threshold = 32;
151 out->max_write = kFuseMaxWrite;
152}
153
154void FuseBuffer::HandleNotImpl() {
155 LOG(VERBOSE) << "NOTIMPL op=" << request.header.opcode << " uniq="
156 << request.header.unique << " nid=" << request.header.nodeid;
157 const uint64_t unique = request.header.unique;
158 response.Reset(0, -ENOSYS, unique);
159}
160
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900161} // namespace fuse
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900162} // namespace android