blob: beee4a6f54cb6ad0007b45ea064ed7199da7221a [file] [log] [blame]
Daichi Hironoc6134762016-10-27 14:57:55 +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/FuseBridgeLoop.h"
18
19#include <android-base/logging.h>
20#include <android-base/unique_fd.h>
21
22namespace android {
23
24bool FuseBridgeLoop::Start(
25 int raw_dev_fd, int raw_proxy_fd, FuseBridgeLoop::Callback* callback) {
26 base::unique_fd dev_fd(raw_dev_fd);
27 base::unique_fd proxy_fd(raw_proxy_fd);
Daichi Hironoa0aecda2016-11-08 10:17:51 +090028 fuse::FuseBuffer buffer;
Daichi Hirono30e68082016-11-15 09:31:21 +090029 size_t open_count = 0;
Daichi Hironoc6134762016-10-27 14:57:55 +090030
31 LOG(DEBUG) << "Start fuse loop.";
32 while (true) {
Daichi Hironoa0aecda2016-11-08 10:17:51 +090033 if (!buffer.request.Read(dev_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090034 return false;
35 }
36
Daichi Hironoa0aecda2016-11-08 10:17:51 +090037 const uint32_t opcode = buffer.request.header.opcode;
Daichi Hironoc6134762016-10-27 14:57:55 +090038 LOG(VERBOSE) << "Read a fuse packet, opcode=" << opcode;
39 switch (opcode) {
40 case FUSE_FORGET:
41 // Do not reply to FUSE_FORGET.
42 continue;
43
44 case FUSE_LOOKUP:
45 case FUSE_GETATTR:
46 case FUSE_OPEN:
47 case FUSE_READ:
48 case FUSE_WRITE:
49 case FUSE_RELEASE:
Daichi Hironoa0aecda2016-11-08 10:17:51 +090050 case FUSE_FSYNC:
51 if (!buffer.request.Write(proxy_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090052 LOG(ERROR) << "Failed to write a request to the proxy.";
53 return false;
54 }
Daichi Hironoa0aecda2016-11-08 10:17:51 +090055 if (!buffer.response.Read(proxy_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090056 LOG(ERROR) << "Failed to read a response from the proxy.";
57 return false;
58 }
59 break;
60
61 case FUSE_INIT:
Daichi Hironoa0aecda2016-11-08 10:17:51 +090062 buffer.HandleInit();
Daichi Hironoc6134762016-10-27 14:57:55 +090063 break;
64
65 default:
Daichi Hironoa0aecda2016-11-08 10:17:51 +090066 buffer.HandleNotImpl();
Daichi Hironoc6134762016-10-27 14:57:55 +090067 break;
68 }
69
Daichi Hironoa0aecda2016-11-08 10:17:51 +090070 if (!buffer.response.Write(dev_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090071 LOG(ERROR) << "Failed to write a response to the device.";
72 return false;
73 }
74
Daichi Hirono30e68082016-11-15 09:31:21 +090075 switch (opcode) {
76 case FUSE_INIT:
77 callback->OnMount();
78 break;
79 case FUSE_OPEN:
80 if (buffer.response.header.error == fuse::kFuseSuccess) {
81 open_count++;
82 }
83 break;
84 case FUSE_RELEASE:
85 if (open_count != 0) {
86 open_count--;
87 } else {
88 LOG(WARNING) << "Unexpected FUSE_RELEASE before opening a file.";
89 break;
90 }
91 if (open_count == 0) {
92 return true;
93 }
94 break;
Daichi Hironoc6134762016-10-27 14:57:55 +090095 }
96 }
97}
98
Daichi Hironoa0aecda2016-11-08 10:17:51 +090099namespace fuse {
100
101bool StartFuseBridgeLoop(
102 int raw_dev_fd, int raw_proxy_fd, FuseBridgeLoopCallback* callback) {
103 return FuseBridgeLoop().Start(raw_dev_fd, raw_proxy_fd, callback);
104}
105
106} // namespace fuse
Daichi Hironoc6134762016-10-27 14:57:55 +0900107} // namespace android