| 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 |  | 
| Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 26 | #include <android-base/file.h> | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 27 | #include <android-base/logging.h> | 
 | 28 | #include <android-base/macros.h> | 
 | 29 |  | 
 | 30 | namespace android { | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 31 | namespace fuse { | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 32 |  | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 33 | static_assert( | 
 | 34 |     std::is_standard_layout<FuseBuffer>::value, | 
 | 35 |     "FuseBuffer must be standard layout union."); | 
 | 36 |  | 
 | 37 | template <typename T> | 
| Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 38 | bool FuseMessage<T>::CheckHeaderLength(const char* name) const { | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 39 |   const auto& header = static_cast<const T*>(this)->header; | 
| Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 40 |   if (header.len >= sizeof(header) && header.len <= sizeof(T)) { | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 41 |     return true; | 
 | 42 |   } else { | 
| Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 43 |     LOG(ERROR) << "Invalid header length is found in " << name << ": " << | 
 | 44 |         header.len; | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 45 |     return false; | 
 | 46 |   } | 
 | 47 | } | 
 | 48 |  | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 49 | template <typename T> | 
 | 50 | bool FuseMessage<T>::Read(int fd) { | 
| Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 51 |   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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 81 | } | 
 | 82 |  | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 83 | template <typename T> | 
 | 84 | bool FuseMessage<T>::Write(int fd) const { | 
| Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 85 |   if (!CheckHeaderLength("Write")) { | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 86 |     return false; | 
 | 87 |   } | 
| Daichi Hirono | cb9153b | 2016-12-08 14:22:13 +0900 | [diff] [blame] | 88 |  | 
 | 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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 97 | } | 
 | 98 |  | 
| Daichi Hirono | 0d97be4 | 2016-11-10 09:17:17 +0900 | [diff] [blame] | 99 | template class FuseMessage<FuseRequest>; | 
 | 100 | template class FuseMessage<FuseResponse>; | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 101 |  | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 102 | void 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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 110 | void 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 |  | 
 | 118 | void 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 |  | 
 | 123 | void 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 Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame] | 143 |   // 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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 145 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) | 
 | 146 |   // FUSE_KERNEL_VERSION >= 23. | 
| Daichi Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame] | 147 |   const size_t response_size = FUSE_COMPAT_22_INIT_OUT_SIZE; | 
 | 148 | #else | 
 | 149 |   const size_t response_size = sizeof(fuse_init_out); | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 150 | #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 Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 155 |   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 |  | 
 | 163 | void 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 Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 170 | }  // namespace fuse | 
| Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 171 | }  // namespace android |