| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +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 |  | 
|  | 17 | #include "libappfuse/FuseBridgeLoop.h" | 
|  | 18 |  | 
|  | 19 | #include <android-base/logging.h> | 
|  | 20 | #include <android-base/unique_fd.h> | 
|  | 21 |  | 
|  | 22 | namespace android { | 
| Daichi Hirono | 691739b | 2016-11-15 15:42:05 +0900 | [diff] [blame] | 23 | namespace fuse { | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 24 |  | 
| Daichi Hirono | 691739b | 2016-11-15 15:42:05 +0900 | [diff] [blame] | 25 | bool StartFuseBridgeLoop( | 
|  | 26 | int raw_dev_fd, int raw_proxy_fd, FuseBridgeLoopCallback* callback) { | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 27 | base::unique_fd dev_fd(raw_dev_fd); | 
|  | 28 | base::unique_fd proxy_fd(raw_proxy_fd); | 
| Daichi Hirono | 691739b | 2016-11-15 15:42:05 +0900 | [diff] [blame] | 29 | FuseBuffer buffer; | 
| Daichi Hirono | 30e6808 | 2016-11-15 09:31:21 +0900 | [diff] [blame] | 30 | size_t open_count = 0; | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 31 |  | 
|  | 32 | LOG(DEBUG) << "Start fuse loop."; | 
|  | 33 | while (true) { | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 34 | if (!buffer.request.Read(dev_fd)) { | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 35 | return false; | 
|  | 36 | } | 
|  | 37 |  | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 38 | const uint32_t opcode = buffer.request.header.opcode; | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 39 | LOG(VERBOSE) << "Read a fuse packet, opcode=" << opcode; | 
|  | 40 | switch (opcode) { | 
|  | 41 | case FUSE_FORGET: | 
|  | 42 | // Do not reply to FUSE_FORGET. | 
|  | 43 | continue; | 
|  | 44 |  | 
|  | 45 | case FUSE_LOOKUP: | 
|  | 46 | case FUSE_GETATTR: | 
|  | 47 | case FUSE_OPEN: | 
|  | 48 | case FUSE_READ: | 
|  | 49 | case FUSE_WRITE: | 
|  | 50 | case FUSE_RELEASE: | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 51 | case FUSE_FSYNC: | 
|  | 52 | if (!buffer.request.Write(proxy_fd)) { | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 53 | LOG(ERROR) << "Failed to write a request to the proxy."; | 
|  | 54 | return false; | 
|  | 55 | } | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 56 | if (!buffer.response.Read(proxy_fd)) { | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 57 | LOG(ERROR) << "Failed to read a response from the proxy."; | 
|  | 58 | return false; | 
|  | 59 | } | 
|  | 60 | break; | 
|  | 61 |  | 
|  | 62 | case FUSE_INIT: | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 63 | buffer.HandleInit(); | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 64 | break; | 
|  | 65 |  | 
|  | 66 | default: | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 67 | buffer.HandleNotImpl(); | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 68 | break; | 
|  | 69 | } | 
|  | 70 |  | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 71 | if (!buffer.response.Write(dev_fd)) { | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 72 | LOG(ERROR) << "Failed to write a response to the device."; | 
|  | 73 | return false; | 
|  | 74 | } | 
|  | 75 |  | 
| Daichi Hirono | 30e6808 | 2016-11-15 09:31:21 +0900 | [diff] [blame] | 76 | switch (opcode) { | 
|  | 77 | case FUSE_INIT: | 
|  | 78 | callback->OnMount(); | 
|  | 79 | break; | 
|  | 80 | case FUSE_OPEN: | 
|  | 81 | if (buffer.response.header.error == fuse::kFuseSuccess) { | 
|  | 82 | open_count++; | 
|  | 83 | } | 
|  | 84 | break; | 
|  | 85 | case FUSE_RELEASE: | 
|  | 86 | if (open_count != 0) { | 
|  | 87 | open_count--; | 
|  | 88 | } else { | 
|  | 89 | LOG(WARNING) << "Unexpected FUSE_RELEASE before opening a file."; | 
|  | 90 | break; | 
|  | 91 | } | 
|  | 92 | if (open_count == 0) { | 
|  | 93 | return true; | 
|  | 94 | } | 
|  | 95 | break; | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 96 | } | 
|  | 97 | } | 
|  | 98 | } | 
|  | 99 |  | 
| Daichi Hirono | a0aecda | 2016-11-08 10:17:51 +0900 | [diff] [blame] | 100 | }  // namespace fuse | 
| Daichi Hirono | c613476 | 2016-10-27 14:57:55 +0900 | [diff] [blame] | 101 | }  // namespace android |