| 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> | 
|  | 37 | bool 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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 40 | return true; | 
|  | 41 | } else { | 
|  | 42 | LOG(ERROR) << "Packet size is invalid=" << header.len; | 
|  | 43 | return false; | 
|  | 44 | } | 
|  | 45 | } | 
|  | 46 |  | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 47 | template <typename T> | 
|  | 48 | bool FuseMessage<T>::CheckResult( | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 49 | int result, const char* operation_name) const { | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 50 | const auto& header = static_cast<const T*>(this)->header; | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 51 | if (result >= 0 && static_cast<uint32_t>(result) == header.len) { | 
|  | 52 | return true; | 
|  | 53 | } else { | 
|  | 54 | PLOG(ERROR) << "Failed to " << operation_name | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 55 | << " a packet. result=" << result << " header.len=" | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 56 | << header.len; | 
|  | 57 | return false; | 
|  | 58 | } | 
|  | 59 | } | 
|  | 60 |  | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 61 | template <typename T> | 
|  | 62 | bool FuseMessage<T>::Read(int fd) { | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 63 | const ssize_t result = TEMP_FAILURE_RETRY(::read(fd, this, sizeof(T))); | 
|  | 64 | return CheckHeaderLength() && CheckResult(result, "read"); | 
|  | 65 | } | 
|  | 66 |  | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 67 | template <typename T> | 
|  | 68 | bool FuseMessage<T>::Write(int fd) const { | 
|  | 69 | const auto& header = static_cast<const T*>(this)->header; | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 70 | 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 Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 77 | template class FuseMessage<FuseRequest>; | 
|  | 78 | template class FuseMessage<FuseResponse>; | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 79 |  | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 80 | void 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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 88 | void 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 |  | 
|  | 96 | void 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 |  | 
|  | 101 | void 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 |  | 
|  | 146 | void 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 Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 153 | }  // namespace fuse | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 154 | }  // namespace android |