Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 1 | /* |
| 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 Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 17 | #include "libappfuse/FuseBuffer.h" |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 18 | |
| 19 | #include <inttypes.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <algorithm> |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 24 | #include <type_traits> |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/macros.h> |
| 28 | |
| 29 | namespace android { |
Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 30 | namespace fuse { |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 31 | |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 32 | static_assert( |
| 33 | std::is_standard_layout<FuseBuffer>::value, |
| 34 | "FuseBuffer must be standard layout union."); |
| 35 | |
| 36 | template <typename T> |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 37 | bool FuseMessage<T>::CheckPacketSize(size_t size, const char* name) const { |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 38 | const auto& header = static_cast<const T*>(this)->header; |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 39 | if (size >= sizeof(header) && size <= sizeof(T)) { |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 40 | return true; |
| 41 | } else { |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 42 | LOG(ERROR) << name << " is invalid=" << size; |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 47 | template <typename T> |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 48 | bool 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 | |
| 60 | template <typename T> |
| 61 | bool FuseMessage<T>::CheckHeaderLength(int result, const char* operation_name) const { |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 62 | const auto& header = static_cast<const T*>(this)->header; |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 63 | if (static_cast<uint32_t>(result) == header.len) { |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 64 | return true; |
| 65 | } else { |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 66 | LOG(ERROR) << "Invalid header length: operation_name=" << operation_name |
| 67 | << " result=" << result |
| 68 | << " header.len=" << header.len; |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 73 | template <typename T> |
| 74 | bool FuseMessage<T>::Read(int fd) { |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 75 | const ssize_t result = TEMP_FAILURE_RETRY(::read(fd, this, sizeof(T))); |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 76 | return CheckResult(result, "read") && CheckPacketSize(result, "read count") && |
| 77 | CheckHeaderLength(result, "read"); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 78 | } |
| 79 | |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 80 | template <typename T> |
| 81 | bool FuseMessage<T>::Write(int fd) const { |
| 82 | const auto& header = static_cast<const T*>(this)->header; |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 83 | if (!CheckPacketSize(header.len, "header.len")) { |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | const ssize_t result = TEMP_FAILURE_RETRY(::write(fd, this, header.len)); |
Daichi Hirono | a6373ec | 2016-12-05 16:50:44 +0900 | [diff] [blame^] | 87 | return CheckResult(result, "write") && CheckHeaderLength(result, "write"); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 90 | template class FuseMessage<FuseRequest>; |
| 91 | template class FuseMessage<FuseResponse>; |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 92 | |
Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 93 | void 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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 101 | void 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 | |
| 109 | void 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 | |
| 114 | void 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 Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame] | 134 | // 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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 136 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
| 137 | // FUSE_KERNEL_VERSION >= 23. |
Daichi Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame] | 138 | const size_t response_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 139 | #else |
| 140 | const size_t response_size = sizeof(fuse_init_out); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 141 | #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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 146 | 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 | |
| 154 | void 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 Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 161 | } // namespace fuse |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 162 | } // namespace android |