blob: b6bc191b789d8330f6c7a1fb15242f129a36f83a [file] [log] [blame]
Daichi Hironoa0aecda2016-11-08 10:17:51 +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
17#include "libappfuse/FuseAppLoop.h"
18
Daichi Hirono8e16cee2017-03-06 15:23:16 +090019#include <sys/eventfd.h>
Daichi Hironoa0aecda2016-11-08 10:17:51 +090020#include <sys/stat.h>
21
22#include <android-base/logging.h>
23#include <android-base/unique_fd.h>
24
Daichi Hirono8e16cee2017-03-06 15:23:16 +090025#include "libappfuse/EpollController.h"
26
Daichi Hironoa0aecda2016-11-08 10:17:51 +090027namespace android {
28namespace fuse {
29
30namespace {
31
Daichi Hirono8e16cee2017-03-06 15:23:16 +090032bool HandleLookUp(FuseAppLoop* loop, FuseBuffer* buffer, FuseAppLoopCallback* callback) {
33 // AppFuse does not support directory structure now.
34 // It can lookup only files under the mount point.
35 if (buffer->request.header.nodeid != FUSE_ROOT_ID) {
36 LOG(ERROR) << "Nodeid is not FUSE_ROOT_ID.";
37 return loop->ReplySimple(buffer->request.header.unique, -ENOENT);
Daichi Hironoa0aecda2016-11-08 10:17:51 +090038 }
Daichi Hironoa0aecda2016-11-08 10:17:51 +090039
Daichi Hirono8e16cee2017-03-06 15:23:16 +090040 // Ensure that the filename ends with 0.
41 const size_t filename_length = buffer->request.header.len - sizeof(fuse_in_header);
42 if (buffer->request.lookup_name[filename_length - 1] != 0) {
43 LOG(ERROR) << "File name does not end with 0.";
44 return loop->ReplySimple(buffer->request.header.unique, -ENOENT);
45 }
46
47 const uint64_t inode = static_cast<uint64_t>(atol(buffer->request.lookup_name));
48 if (inode == 0 || inode == LONG_MAX) {
49 LOG(ERROR) << "Invalid filename";
50 return loop->ReplySimple(buffer->request.header.unique, -ENOENT);
51 }
52
53 callback->OnLookup(buffer->request.header.unique, inode);
54 return true;
Daichi Hironoa0aecda2016-11-08 10:17:51 +090055}
56
Daichi Hirono8e16cee2017-03-06 15:23:16 +090057bool HandleGetAttr(FuseAppLoop* loop, FuseBuffer* buffer, FuseAppLoopCallback* callback) {
58 if (buffer->request.header.nodeid == FUSE_ROOT_ID) {
59 return loop->ReplyGetAttr(buffer->request.header.unique, buffer->request.header.nodeid, 0,
60 S_IFDIR | 0777);
61 } else {
62 callback->OnGetAttr(buffer->request.header.unique, buffer->request.header.nodeid);
63 return true;
64 }
Daichi Hironoa0aecda2016-11-08 10:17:51 +090065}
66
Daichi Hirono8e16cee2017-03-06 15:23:16 +090067bool HandleRead(FuseAppLoop* loop, FuseBuffer* buffer, FuseAppLoopCallback* callback) {
68 if (buffer->request.read_in.size > kFuseMaxRead) {
69 return loop->ReplySimple(buffer->request.header.unique, -EINVAL);
70 }
71
72 callback->OnRead(buffer->request.header.unique, buffer->request.header.nodeid,
73 buffer->request.read_in.offset, buffer->request.read_in.size);
74 return true;
Daichi Hironoa0aecda2016-11-08 10:17:51 +090075}
76
Daichi Hirono8e16cee2017-03-06 15:23:16 +090077bool HandleWrite(FuseAppLoop* loop, FuseBuffer* buffer, FuseAppLoopCallback* callback) {
78 if (buffer->request.write_in.size > kFuseMaxWrite) {
79 return loop->ReplySimple(buffer->request.header.unique, -EINVAL);
80 }
81
82 callback->OnWrite(buffer->request.header.unique, buffer->request.header.nodeid,
83 buffer->request.write_in.offset, buffer->request.write_in.size,
84 buffer->request.write_data);
85 return true;
Daichi Hironoa0aecda2016-11-08 10:17:51 +090086}
87
Daichi Hirono8e16cee2017-03-06 15:23:16 +090088bool HandleMessage(FuseAppLoop* loop, FuseBuffer* buffer, int fd, FuseAppLoopCallback* callback) {
89 if (!buffer->request.Read(fd)) {
90 return false;
91 }
Daichi Hironoa0aecda2016-11-08 10:17:51 +090092
Daichi Hirono8e16cee2017-03-06 15:23:16 +090093 const uint32_t opcode = buffer->request.header.opcode;
94 LOG(VERBOSE) << "Read a fuse packet, opcode=" << opcode;
95 switch (opcode) {
96 case FUSE_FORGET:
97 // Do not reply to FUSE_FORGET.
98 return true;
Daichi Hironoa0aecda2016-11-08 10:17:51 +090099
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900100 case FUSE_LOOKUP:
101 return HandleLookUp(loop, buffer, callback);
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900102
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900103 case FUSE_GETATTR:
104 return HandleGetAttr(loop, buffer, callback);
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900105
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900106 case FUSE_OPEN:
107 callback->OnOpen(buffer->request.header.unique, buffer->request.header.nodeid);
108 return true;
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900109
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900110 case FUSE_READ:
111 return HandleRead(loop, buffer, callback);
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900112
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900113 case FUSE_WRITE:
114 return HandleWrite(loop, buffer, callback);
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900115
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900116 case FUSE_RELEASE:
117 callback->OnRelease(buffer->request.header.unique, buffer->request.header.nodeid);
118 return true;
119
120 case FUSE_FSYNC:
121 callback->OnFsync(buffer->request.header.unique, buffer->request.header.nodeid);
122 return true;
123
124 default:
125 buffer->HandleNotImpl();
126 return buffer->response.Write(fd);
127 }
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900128}
129
130} // namespace
131
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900132FuseAppLoopCallback::~FuseAppLoopCallback() = default;
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900133
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900134FuseAppLoop::FuseAppLoop(base::unique_fd&& fd) : fd_(std::move(fd)) {}
135
136void FuseAppLoop::Break() {
137 const int64_t value = 1;
138 if (write(break_fd_, &value, sizeof(value)) == -1) {
139 PLOG(ERROR) << "Failed to send a break event";
140 }
141}
142
143bool FuseAppLoop::ReplySimple(uint64_t unique, int32_t result) {
144 if (result == -ENOSYS) {
145 // We should not return -ENOSYS because the kernel stops delivering FUSE
146 // command after receiving -ENOSYS as a result for the command.
147 result = -EBADF;
148 }
149 FuseSimpleResponse response;
150 response.Reset(0, result, unique);
151 return response.Write(fd_);
152}
153
154bool FuseAppLoop::ReplyLookup(uint64_t unique, uint64_t inode, int64_t size) {
155 FuseSimpleResponse response;
156 response.Reset(sizeof(fuse_entry_out), 0, unique);
157 response.entry_out.nodeid = inode;
158 response.entry_out.attr_valid = 10;
159 response.entry_out.entry_valid = 10;
160 response.entry_out.attr.ino = inode;
161 response.entry_out.attr.mode = S_IFREG | 0777;
162 response.entry_out.attr.size = size;
163 return response.Write(fd_);
164}
165
166bool FuseAppLoop::ReplyGetAttr(uint64_t unique, uint64_t inode, int64_t size, int mode) {
167 CHECK(mode == (S_IFREG | 0777) || mode == (S_IFDIR | 0777));
168 FuseSimpleResponse response;
169 response.Reset(sizeof(fuse_attr_out), 0, unique);
170 response.attr_out.attr_valid = 10;
171 response.attr_out.attr.ino = inode;
172 response.attr_out.attr.mode = mode;
173 response.attr_out.attr.size = size;
174 return response.Write(fd_);
175}
176
177bool FuseAppLoop::ReplyOpen(uint64_t unique, uint64_t fh) {
178 FuseSimpleResponse response;
179 response.Reset(sizeof(fuse_open_out), kFuseSuccess, unique);
180 response.open_out.fh = fh;
181 return response.Write(fd_);
182}
183
184bool FuseAppLoop::ReplyWrite(uint64_t unique, uint32_t size) {
185 CHECK(size <= kFuseMaxWrite);
186 FuseSimpleResponse response;
187 response.Reset(sizeof(fuse_write_out), kFuseSuccess, unique);
188 response.write_out.size = size;
189 return response.Write(fd_);
190}
191
192bool FuseAppLoop::ReplyRead(uint64_t unique, uint32_t size, const void* data) {
193 CHECK(size <= kFuseMaxRead);
194 FuseSimpleResponse response;
195 response.ResetHeader(size, kFuseSuccess, unique);
196 return response.WriteWithBody(fd_, sizeof(FuseResponse), data);
197}
198
199void FuseAppLoop::Start(FuseAppLoopCallback* callback) {
200 break_fd_.reset(eventfd(/* initval */ 0, EFD_CLOEXEC));
201 if (break_fd_.get() == -1) {
202 PLOG(ERROR) << "Failed to open FD for break event";
203 return;
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900204 }
205
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900206 base::unique_fd epoll_fd(epoll_create1(EPOLL_CLOEXEC));
207 if (epoll_fd.get() == -1) {
208 PLOG(ERROR) << "Failed to open FD for epoll";
209 return;
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900210 }
211
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900212 int last_event;
213 int break_event;
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900214
Daichi Hirono8e16cee2017-03-06 15:23:16 +0900215 std::unique_ptr<EpollController> epoll_controller(new EpollController(std::move(epoll_fd)));
216 if (!epoll_controller->AddFd(fd_, EPOLLIN, &last_event)) {
217 return;
218 }
219 if (!epoll_controller->AddFd(break_fd_, EPOLLIN, &break_event)) {
220 return;
221 }
222
223 last_event = 0;
224 break_event = 0;
225
226 FuseBuffer buffer;
227 while (true) {
228 if (!epoll_controller->Wait(1)) {
229 break;
230 }
231 last_event = 0;
232 *reinterpret_cast<int*>(epoll_controller->events()[0].data.ptr) =
233 epoll_controller->events()[0].events;
234
235 if (break_event != 0 || (last_event & ~EPOLLIN) != 0) {
236 break;
237 }
238
239 if (!HandleMessage(this, &buffer, fd_, callback)) {
240 break;
241 }
242 }
243
244 LOG(VERBOSE) << "FuseAppLoop exit";
Daichi Hironoa0aecda2016-11-08 10:17:51 +0900245}
246
247} // namespace fuse
248} // namespace android