blob: 74fe756a920c4df39033e49dc119a5c31ef3abcf [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>
37bool FuseMessage<T>::CheckHeaderLength() const {
38 const auto& header = static_cast<const T*>(this)->header;
39 if (sizeof(header) <= header.len && header.len <= sizeof(T)) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090040 return true;
41 } else {
42 LOG(ERROR) << "Packet size is invalid=" << header.len;
43 return false;
44 }
45}
46
Daichi Hirono0d97be42016-11-10 09:17:17 +090047template <typename T>
48bool FuseMessage<T>::CheckResult(
Daichi Hirono7f8e8192016-10-27 10:37:05 +090049 int result, const char* operation_name) const {
Daichi Hirono0d97be42016-11-10 09:17:17 +090050 const auto& header = static_cast<const T*>(this)->header;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090051 if (result >= 0 && static_cast<uint32_t>(result) == header.len) {
52 return true;
53 } else {
54 PLOG(ERROR) << "Failed to " << operation_name
Daichi Hironoa0aecda2016-11-08 10:17:51 +090055 << " a packet. result=" << result << " header.len="
Daichi Hirono7f8e8192016-10-27 10:37:05 +090056 << header.len;
57 return false;
58 }
59}
60
Daichi Hirono0d97be42016-11-10 09:17:17 +090061template <typename T>
62bool FuseMessage<T>::Read(int fd) {
Daichi Hirono7f8e8192016-10-27 10:37:05 +090063 const ssize_t result = TEMP_FAILURE_RETRY(::read(fd, this, sizeof(T)));
64 return CheckHeaderLength() && CheckResult(result, "read");
65}
66
Daichi Hirono0d97be42016-11-10 09:17:17 +090067template <typename T>
68bool FuseMessage<T>::Write(int fd) const {
69 const auto& header = static_cast<const T*>(this)->header;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090070 if (!CheckHeaderLength()) {
71 return false;
72 }
73 const ssize_t result = TEMP_FAILURE_RETRY(::write(fd, this, header.len));
74 return CheckResult(result, "write");
75}
76
Daichi Hirono0d97be42016-11-10 09:17:17 +090077template class FuseMessage<FuseRequest>;
78template class FuseMessage<FuseResponse>;
Daichi Hirono7f8e8192016-10-27 10:37:05 +090079
Daichi Hironoa0aecda2016-11-08 10:17:51 +090080void FuseRequest::Reset(
81 uint32_t data_length, uint32_t opcode, uint64_t unique) {
82 memset(this, 0, sizeof(fuse_in_header) + data_length);
83 header.len = sizeof(fuse_in_header) + data_length;
84 header.opcode = opcode;
85 header.unique = unique;
86}
87
Daichi Hirono7f8e8192016-10-27 10:37:05 +090088void FuseResponse::ResetHeader(
89 uint32_t data_length, int32_t error, uint64_t unique) {
90 CHECK_LE(error, 0) << "error should be zero or negative.";
91 header.len = sizeof(fuse_out_header) + data_length;
92 header.error = error;
93 header.unique = unique;
94}
95
96void FuseResponse::Reset(uint32_t data_length, int32_t error, uint64_t unique) {
97 memset(this, 0, sizeof(fuse_out_header) + data_length);
98 ResetHeader(data_length, error, unique);
99}
100
101void FuseBuffer::HandleInit() {
102 const fuse_init_in* const in = &request.init_in;
103
104 // Before writing |out|, we need to copy data from |in|.
105 const uint64_t unique = request.header.unique;
106 const uint32_t minor = in->minor;
107 const uint32_t max_readahead = in->max_readahead;
108
109 // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
110 // defined (fuse version 7.6). The structure is the same from 7.6 through
111 // 7.22. Beginning with 7.23, the structure increased in size and added
112 // new parameters.
113 if (in->major != FUSE_KERNEL_VERSION || in->minor < 6) {
114 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " << in->major
115 << "." << in->minor << " Expected at least " << FUSE_KERNEL_VERSION
116 << ".6";
117 response.Reset(0, -EPERM, unique);
118 return;
119 }
120
121 // We limit ourselves to 15 because we don't handle BATCH_FORGET yet
122 size_t response_size = sizeof(fuse_init_out);
123#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
124 // FUSE_KERNEL_VERSION >= 23.
125
126 // If the kernel only works on minor revs older than or equal to 22,
127 // then use the older structure size since this code only uses the 7.22
128 // version of the structure.
129 if (minor <= 22) {
130 response_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
131 }
132#endif
133
134 response.Reset(response_size, kFuseSuccess, unique);
135 fuse_init_out* const out = &response.init_out;
136 out->major = FUSE_KERNEL_VERSION;
137 // We limit ourselves to 15 because we don't handle BATCH_FORGET yet.
138 out->minor = std::min(minor, 15u);
139 out->max_readahead = max_readahead;
140 out->flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
141 out->max_background = 32;
142 out->congestion_threshold = 32;
143 out->max_write = kFuseMaxWrite;
144}
145
146void FuseBuffer::HandleNotImpl() {
147 LOG(VERBOSE) << "NOTIMPL op=" << request.header.opcode << " uniq="
148 << request.header.unique << " nid=" << request.header.nodeid;
149 const uint64_t unique = request.header.unique;
150 response.Reset(0, -ENOSYS, unique);
151}
152
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900153} // namespace fuse
Daichi Hirono7f8e8192016-10-27 10:37:05 +0900154} // namespace android