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> |
| 24 | |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/macros.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | template <typename T, typename Header> |
| 31 | bool FuseMessage<T, Header>::CheckHeaderLength() const { |
| 32 | if (sizeof(Header) <= header.len && header.len <= sizeof(T)) { |
| 33 | return true; |
| 34 | } else { |
| 35 | LOG(ERROR) << "Packet size is invalid=" << header.len; |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | template <typename T, typename Header> |
| 41 | bool FuseMessage<T, Header>::CheckResult( |
| 42 | int result, const char* operation_name) const { |
| 43 | if (result >= 0 && static_cast<uint32_t>(result) == header.len) { |
| 44 | return true; |
| 45 | } else { |
| 46 | PLOG(ERROR) << "Failed to " << operation_name |
| 47 | << " a packet from FD. result=" << result << " header.len=" |
| 48 | << header.len; |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | template <typename T, typename Header> |
| 54 | bool FuseMessage<T, Header>::Read(int fd) { |
| 55 | const ssize_t result = TEMP_FAILURE_RETRY(::read(fd, this, sizeof(T))); |
| 56 | return CheckHeaderLength() && CheckResult(result, "read"); |
| 57 | } |
| 58 | |
| 59 | template <typename T, typename Header> |
| 60 | bool FuseMessage<T, Header>::Write(int fd) const { |
| 61 | if (!CheckHeaderLength()) { |
| 62 | return false; |
| 63 | } |
| 64 | const ssize_t result = TEMP_FAILURE_RETRY(::write(fd, this, header.len)); |
| 65 | return CheckResult(result, "write"); |
| 66 | } |
| 67 | |
| 68 | template struct FuseMessage<FuseRequest, fuse_in_header>; |
| 69 | template struct FuseMessage<FuseResponse, fuse_out_header>; |
| 70 | |
| 71 | void FuseResponse::ResetHeader( |
| 72 | uint32_t data_length, int32_t error, uint64_t unique) { |
| 73 | CHECK_LE(error, 0) << "error should be zero or negative."; |
| 74 | header.len = sizeof(fuse_out_header) + data_length; |
| 75 | header.error = error; |
| 76 | header.unique = unique; |
| 77 | } |
| 78 | |
| 79 | void FuseResponse::Reset(uint32_t data_length, int32_t error, uint64_t unique) { |
| 80 | memset(this, 0, sizeof(fuse_out_header) + data_length); |
| 81 | ResetHeader(data_length, error, unique); |
| 82 | } |
| 83 | |
| 84 | void FuseBuffer::HandleInit() { |
| 85 | const fuse_init_in* const in = &request.init_in; |
| 86 | |
| 87 | // Before writing |out|, we need to copy data from |in|. |
| 88 | const uint64_t unique = request.header.unique; |
| 89 | const uint32_t minor = in->minor; |
| 90 | const uint32_t max_readahead = in->max_readahead; |
| 91 | |
| 92 | // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out |
| 93 | // defined (fuse version 7.6). The structure is the same from 7.6 through |
| 94 | // 7.22. Beginning with 7.23, the structure increased in size and added |
| 95 | // new parameters. |
| 96 | if (in->major != FUSE_KERNEL_VERSION || in->minor < 6) { |
| 97 | LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " << in->major |
| 98 | << "." << in->minor << " Expected at least " << FUSE_KERNEL_VERSION |
| 99 | << ".6"; |
| 100 | response.Reset(0, -EPERM, unique); |
| 101 | return; |
| 102 | } |
| 103 | |
Daichi Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame^] | 104 | // We limit ourselves to minor=15 because we don't handle BATCH_FORGET yet. |
| 105 | // Thus we need to use FUSE_COMPAT_22_INIT_OUT_SIZE. |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 106 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
| 107 | // FUSE_KERNEL_VERSION >= 23. |
Daichi Hirono | 471ad6a | 2016-11-10 09:41:54 +0900 | [diff] [blame^] | 108 | const size_t response_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 109 | #else |
| 110 | const size_t response_size = sizeof(fuse_init_out); |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 111 | #endif |
| 112 | |
| 113 | response.Reset(response_size, kFuseSuccess, unique); |
| 114 | fuse_init_out* const out = &response.init_out; |
| 115 | out->major = FUSE_KERNEL_VERSION; |
Daichi Hirono | 7f8e819 | 2016-10-27 10:37:05 +0900 | [diff] [blame] | 116 | out->minor = std::min(minor, 15u); |
| 117 | out->max_readahead = max_readahead; |
| 118 | out->flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
| 119 | out->max_background = 32; |
| 120 | out->congestion_threshold = 32; |
| 121 | out->max_write = kFuseMaxWrite; |
| 122 | } |
| 123 | |
| 124 | void FuseBuffer::HandleNotImpl() { |
| 125 | LOG(VERBOSE) << "NOTIMPL op=" << request.header.opcode << " uniq=" |
| 126 | << request.header.unique << " nid=" << request.header.nodeid; |
| 127 | const uint64_t unique = request.header.unique; |
| 128 | response.Reset(0, -ENOSYS, unique); |
| 129 | } |
| 130 | |
| 131 | } // namespace android |