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