shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 15 | #define ATRACE_TAG ATRACE_TAG_APP |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 16 | #define LOG_TAG "FuseDaemon" |
Zim | 357e307 | 2020-01-15 10:50:01 +0000 | [diff] [blame] | 17 | #define LIBFUSE_LOG_TAG "libfuse" |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 18 | |
| 19 | #include "FuseDaemon.h" |
Zim | 29f9ff5 | 2021-01-20 10:47:04 +0000 | [diff] [blame] | 20 | #include "android-base/strings.h" |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 21 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Zim | 32129b6 | 2020-03-11 11:37:16 +0000 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 24 | #include <android/log.h> |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 25 | #include <android/trace.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | #include <dirent.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 30 | #include <fuse_i.h> |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 31 | #include <fuse_log.h> |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 32 | #include <fuse_lowlevel.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 33 | #include <inttypes.h> |
| 34 | #include <limits.h> |
| 35 | #include <linux/fuse.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 36 | #include <stdbool.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <sys/inotify.h> |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 41 | #include <sys/mman.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 42 | #include <sys/mount.h> |
| 43 | #include <sys/param.h> |
| 44 | #include <sys/resource.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <sys/statfs.h> |
| 47 | #include <sys/statvfs.h> |
| 48 | #include <sys/time.h> |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/uio.h> |
| 51 | #include <unistd.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 52 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 53 | #include <iostream> |
Narayan Kamath | 8d76d0c | 2019-12-31 13:14:50 +0000 | [diff] [blame] | 54 | #include <list> |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 55 | #include <map> |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 56 | #include <mutex> |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 57 | #include <queue> |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 58 | #include <regex> |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 59 | #include <thread> |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 60 | #include <unordered_map> |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 61 | #include <unordered_set> |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 62 | #include <vector> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 63 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 64 | #include "MediaProviderWrapper.h" |
Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 65 | #include "libfuse_jni/FuseUtils.h" |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 66 | #include "libfuse_jni/ReaddirHelper.h" |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 67 | #include "libfuse_jni/RedactionInfo.h" |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 68 | #include "node-inl.h" |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 69 | |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 70 | using mediaprovider::fuse::DirectoryEntry; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 71 | using mediaprovider::fuse::dirhandle; |
| 72 | using mediaprovider::fuse::handle; |
| 73 | using mediaprovider::fuse::node; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 74 | using mediaprovider::fuse::RedactionInfo; |
Narayan Kamath | 8d76d0c | 2019-12-31 13:14:50 +0000 | [diff] [blame] | 75 | using std::list; |
Jeff Sharkey | 7469b98 | 2019-08-28 16:51:02 -0600 | [diff] [blame] | 76 | using std::string; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 77 | using std::vector; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 78 | |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 79 | // logging macros to avoid duplication. |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 80 | #define TRACE_NODE(__node, __req) \ |
| 81 | LOG(VERBOSE) << __FUNCTION__ << " : " << #__node << " = [" << get_name(__node) \ |
Alessio Balsini | 328abac | 2020-11-25 17:49:25 +0000 | [diff] [blame] | 82 | << "] (uid=" << (__req)->ctx.uid << ") " |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 83 | |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 84 | #define ATRACE_NAME(name) ScopedTrace ___tracer(name) |
| 85 | #define ATRACE_CALL() ATRACE_NAME(__FUNCTION__) |
| 86 | |
| 87 | class ScopedTrace { |
| 88 | public: |
Narayan Kamath | 64b4435 | 2019-12-27 17:07:50 +0000 | [diff] [blame] | 89 | explicit inline ScopedTrace(const char *name) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 90 | ATrace_beginSection(name); |
| 91 | } |
| 92 | |
| 93 | inline ~ScopedTrace() { |
| 94 | ATrace_endSection(); |
| 95 | } |
| 96 | }; |
| 97 | |
Zim | 32129b6 | 2020-03-11 11:37:16 +0000 | [diff] [blame] | 98 | const bool IS_OS_DEBUGABLE = android::base::GetIntProperty("ro.debuggable", 0); |
| 99 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 100 | #define FUSE_UNKNOWN_INO 0xffffffff |
| 101 | |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 102 | // Stolen from: android_filesystem_config.h |
| 103 | #define AID_APP_START 10000 |
| 104 | |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 105 | constexpr size_t MAX_READ_SIZE = 128 * 1024; |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 106 | // Stolen from: UserHandle#getUserId |
| 107 | constexpr int PER_USER_RANGE = 100000; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 108 | |
Martijn Coenen | 44ef630 | 2020-11-06 10:32:32 +0100 | [diff] [blame] | 109 | // Stolen from: UserManagerService |
| 110 | constexpr int MAX_USER_ID = UINT32_MAX / PER_USER_RANGE; |
| 111 | |
Zim | 3cde266 | 2021-02-22 21:20:33 +0000 | [diff] [blame] | 112 | const int MY_UID = getuid(); |
| 113 | const int MY_USER_ID = MY_UID / PER_USER_RANGE; |
| 114 | const std::string MY_USER_ID_STRING(std::to_string(MY_UID / PER_USER_RANGE)); |
| 115 | |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 116 | // Regex copied from FileUtils.java in MediaProvider, but without media directory. |
| 117 | const std::regex PATTERN_OWNED_PATH( |
Zim | 78ffeeb | 2020-09-22 20:15:25 +0100 | [diff] [blame] | 118 | "^/storage/[^/]+/(?:[0-9]+/)?Android/(?:data|obb)/([^/]+)(/?.*)?", |
| 119 | std::regex_constants::icase); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 120 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 121 | static constexpr char TRANSFORM_SYNTHETIC_DIR[] = "synthetic"; |
| 122 | static constexpr char TRANSFORM_TRANSCODE_DIR[] = "transcode"; |
| 123 | |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 124 | /* |
| 125 | * In order to avoid double caching with fuse, call fadvise on the file handles |
| 126 | * in the underlying file system. However, if this is done on every read/write, |
| 127 | * the fadvises cause a very significant slowdown in tests (specifically fio |
| 128 | * seq_write). So call fadvise on the file handles with the most reads/writes |
| 129 | * only after a threshold is passed. |
| 130 | */ |
| 131 | class FAdviser { |
| 132 | public: |
| 133 | FAdviser() : thread_(MessageLoop, this), total_size_(0) {} |
| 134 | |
| 135 | ~FAdviser() { |
| 136 | SendMessage(Message::quit); |
| 137 | thread_.join(); |
| 138 | } |
| 139 | |
| 140 | void Record(int fd, size_t size) { SendMessage(Message::record, fd, size); } |
| 141 | |
| 142 | void Close(int fd) { SendMessage(Message::close, fd); } |
| 143 | |
| 144 | private: |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 145 | struct Message { |
| 146 | enum Type { record, close, quit }; |
| 147 | Type type; |
| 148 | int fd; |
| 149 | size_t size; |
| 150 | }; |
| 151 | |
| 152 | void RecordImpl(int fd, size_t size) { |
| 153 | total_size_ += size; |
| 154 | |
| 155 | // Find or create record in files_ |
| 156 | // Remove record from sizes_ if it exists, adjusting size appropriately |
| 157 | auto file = files_.find(fd); |
| 158 | if (file != files_.end()) { |
| 159 | auto old_size = file->second; |
| 160 | size += old_size->first; |
| 161 | sizes_.erase(old_size); |
| 162 | } else { |
| 163 | file = files_.insert(Files::value_type(fd, sizes_.end())).first; |
| 164 | } |
| 165 | |
| 166 | // Now (re) insert record in sizes_ |
| 167 | auto new_size = sizes_.insert(Sizes::value_type(size, fd)); |
| 168 | file->second = new_size; |
| 169 | |
| 170 | if (total_size_ < threshold_) return; |
| 171 | |
| 172 | LOG(INFO) << "Threshold exceeded - fadvising " << total_size_; |
| 173 | while (!sizes_.empty() && total_size_ > target_) { |
| 174 | auto size = --sizes_.end(); |
| 175 | total_size_ -= size->first; |
| 176 | posix_fadvise(size->second, 0, 0, POSIX_FADV_DONTNEED); |
| 177 | files_.erase(size->second); |
| 178 | sizes_.erase(size); |
| 179 | } |
| 180 | LOG(INFO) << "Threshold now " << total_size_; |
| 181 | } |
| 182 | |
| 183 | void CloseImpl(int fd) { |
| 184 | auto file = files_.find(fd); |
| 185 | if (file == files_.end()) return; |
| 186 | |
| 187 | total_size_ -= file->second->first; |
| 188 | sizes_.erase(file->second); |
| 189 | files_.erase(file); |
| 190 | } |
| 191 | |
| 192 | void MessageLoopImpl() { |
| 193 | while (1) { |
| 194 | Message message; |
| 195 | |
| 196 | { |
| 197 | std::unique_lock<std::mutex> lock(mutex_); |
| 198 | cv_.wait(lock, [this] { return !queue_.empty(); }); |
| 199 | message = queue_.front(); |
| 200 | queue_.pop(); |
| 201 | } |
| 202 | |
| 203 | switch (message.type) { |
| 204 | case Message::record: |
| 205 | RecordImpl(message.fd, message.size); |
| 206 | break; |
| 207 | |
| 208 | case Message::close: |
| 209 | CloseImpl(message.fd); |
| 210 | break; |
| 211 | |
| 212 | case Message::quit: |
| 213 | return; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static int MessageLoop(FAdviser* ptr) { |
| 219 | ptr->MessageLoopImpl(); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | void SendMessage(Message::Type type, int fd = -1, size_t size = 0) { |
| 224 | { |
| 225 | std::unique_lock<std::mutex> lock(mutex_); |
| 226 | Message message = {type, fd, size}; |
| 227 | queue_.push(message); |
| 228 | } |
| 229 | cv_.notify_one(); |
| 230 | } |
| 231 | |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 232 | std::mutex mutex_; |
| 233 | std::condition_variable cv_; |
Zim | 695a127 | 2020-06-15 18:14:08 +0100 | [diff] [blame] | 234 | std::queue<Message> queue_; |
| 235 | std::thread thread_; |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 236 | |
| 237 | typedef std::multimap<size_t, int> Sizes; |
| 238 | typedef std::map<int, Sizes::iterator> Files; |
| 239 | |
| 240 | Files files_; |
| 241 | Sizes sizes_; |
| 242 | size_t total_size_; |
| 243 | |
| 244 | const size_t threshold_ = 64 * 1024 * 1024; |
| 245 | const size_t target_ = 32 * 1024 * 1024; |
| 246 | }; |
| 247 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 248 | /* Single FUSE mount */ |
| 249 | struct fuse { |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 250 | explicit fuse(const std::string& _path, ino_t _ino) |
Narayan Kamath | 568f17a | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 251 | : path(_path), |
| 252 | tracker(mediaprovider::fuse::NodeTracker(&lock)), |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 253 | root(node::CreateRoot(_path, &lock, _ino, &tracker)), |
Narayan Kamath | 568f17a | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 254 | mp(0), |
Martijn Coenen | 8713309 | 2020-10-14 17:00:28 +0200 | [diff] [blame] | 255 | zero_addr(0), |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 256 | disable_dentry_cache(false), |
| 257 | passthrough(false) {} |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 258 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 259 | inline bool IsRoot(const node* node) const { return node == root; } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 260 | |
Zim | b283401 | 2020-06-17 13:26:27 +0100 | [diff] [blame] | 261 | inline string GetEffectiveRootPath() { |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 262 | if (android::base::StartsWith(path, "/storage/emulated")) { |
Zim | 3cde266 | 2021-02-22 21:20:33 +0000 | [diff] [blame] | 263 | return path + "/" + MY_USER_ID_STRING; |
Zim | b283401 | 2020-06-17 13:26:27 +0100 | [diff] [blame] | 264 | } |
| 265 | return path; |
| 266 | } |
| 267 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 268 | inline string GetTransformsDir() { return GetEffectiveRootPath() + "/.transforms"; } |
| 269 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 270 | // Note that these two (FromInode / ToInode) conversion wrappers are required |
| 271 | // because fuse_lowlevel_ops documents that the root inode is always one |
| 272 | // (see FUSE_ROOT_ID in fuse_lowlevel.h). There are no particular requirements |
| 273 | // on any of the other inodes in the FS. |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 274 | inline node* FromInode(__u64 inode) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 275 | if (inode == FUSE_ROOT_ID) { |
| 276 | return root; |
| 277 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 278 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 279 | return node::FromInode(inode, &tracker); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | inline __u64 ToInode(node* node) const { |
| 283 | if (IsRoot(node)) { |
| 284 | return FUSE_ROOT_ID; |
| 285 | } |
| 286 | |
| 287 | return node::ToInode(node); |
| 288 | } |
| 289 | |
| 290 | std::recursive_mutex lock; |
| 291 | const string path; |
Narayan Kamath | 568f17a | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 292 | // The Inode tracker associated with this FUSE instance. |
| 293 | mediaprovider::fuse::NodeTracker tracker; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 294 | node* const root; |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 295 | struct fuse_session* se; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * Used to make JNI calls to MediaProvider. |
| 299 | * Responsibility of freeing this object falls on corresponding |
| 300 | * FuseDaemon object. |
| 301 | */ |
| 302 | mediaprovider::fuse::MediaProviderWrapper* mp; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * Points to a range of zeroized bytes, used by pf_read to represent redacted ranges. |
| 306 | * The memory is read only and should never be modified. |
| 307 | */ |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 308 | /* const */ char* zero_addr; |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 309 | |
| 310 | FAdviser fadviser; |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 311 | |
| 312 | std::atomic_bool* active; |
Martijn Coenen | 8713309 | 2020-10-14 17:00:28 +0200 | [diff] [blame] | 313 | std::atomic_bool disable_dentry_cache; |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 314 | std::atomic_bool passthrough; |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 315 | // FUSE device id. |
| 316 | std::atomic_uint dev; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 317 | }; |
| 318 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 319 | enum class FuseOp { lookup, readdir, mknod, mkdir, create }; |
| 320 | |
Zim | 32129b6 | 2020-03-11 11:37:16 +0000 | [diff] [blame] | 321 | static inline string get_name(node* n) { |
| 322 | if (n) { |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 323 | std::string name = IS_OS_DEBUGABLE ? "real_path: " + n->BuildPath() + " " : ""; |
| 324 | name += "node_path: " + n->BuildSafePath(); |
Zim | 32129b6 | 2020-03-11 11:37:16 +0000 | [diff] [blame] | 325 | return name; |
| 326 | } |
| 327 | return "?"; |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 328 | } |
| 329 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 330 | static inline __u64 ptr_to_id(void* ptr) { |
| 331 | return (__u64)(uintptr_t) ptr; |
| 332 | } |
| 333 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 334 | /* |
| 335 | * Set an F_RDLCK or F_WRLCKK on fd with fcntl(2). |
| 336 | * |
| 337 | * This is called before the MediaProvider returns fd from the lower file |
| 338 | * system to an app over the ContentResolver interface. This allows us |
| 339 | * check with is_file_locked if any reference to that fd is still open. |
| 340 | */ |
| 341 | static int set_file_lock(int fd, bool for_read, const std::string& path) { |
| 342 | std::string lock_str = (for_read ? "read" : "write"); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 343 | |
| 344 | struct flock fl{}; |
| 345 | fl.l_type = for_read ? F_RDLCK : F_WRLCK; |
| 346 | fl.l_whence = SEEK_SET; |
| 347 | |
| 348 | int res = fcntl(fd, F_OFD_SETLK, &fl); |
| 349 | if (res) { |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 350 | PLOG(WARNING) << "Failed to set lock: " << lock_str; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 351 | return res; |
| 352 | } |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 353 | return res; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Check if an F_RDLCK or F_WRLCK is set on fd with fcntl(2). |
| 358 | * |
| 359 | * This is used to determine if the MediaProvider has given an fd to the lower fs to an app over |
| 360 | * the ContentResolver interface. Before that happens, we always call set_file_lock on the file |
| 361 | * allowing us to know if any reference to that fd is still open here. |
| 362 | * |
| 363 | * Returns true if fd may have a lock, false otherwise |
| 364 | */ |
| 365 | static bool is_file_locked(int fd, const std::string& path) { |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 366 | struct flock fl{}; |
| 367 | fl.l_type = F_WRLCK; |
| 368 | fl.l_whence = SEEK_SET; |
| 369 | |
| 370 | int res = fcntl(fd, F_OFD_GETLK, &fl); |
| 371 | if (res) { |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 372 | PLOG(WARNING) << "Failed to check lock"; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 373 | // Assume worst |
| 374 | return true; |
| 375 | } |
| 376 | bool locked = fl.l_type != F_UNLCK; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 377 | return locked; |
| 378 | } |
| 379 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 380 | static struct fuse* get_fuse(fuse_req_t req) { |
| 381 | return reinterpret_cast<struct fuse*>(fuse_req_userdata(req)); |
| 382 | } |
| 383 | |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 384 | static bool is_package_owned_path(const string& path, const string& fuse_path) { |
| 385 | if (path.rfind(fuse_path, 0) != 0) { |
| 386 | return false; |
| 387 | } |
| 388 | return std::regex_match(path, PATTERN_OWNED_PATH); |
| 389 | } |
| 390 | |
Zim | 33aea10 | 2020-06-19 15:49:47 +0100 | [diff] [blame] | 391 | // See fuse_lowlevel.h fuse_lowlevel_notify_inval_entry for how to call this safetly without |
| 392 | // deadlocking the kernel |
| 393 | static void fuse_inval(fuse_session* se, fuse_ino_t parent_ino, fuse_ino_t child_ino, |
| 394 | const string& child_name, const string& path) { |
Zim | 3cde266 | 2021-02-22 21:20:33 +0000 | [diff] [blame] | 395 | if (mediaprovider::fuse::containsMount(path, MY_USER_ID_STRING)) { |
Zim | 33aea10 | 2020-06-19 15:49:47 +0100 | [diff] [blame] | 396 | LOG(WARNING) << "Ignoring attempt to invalidate dentry for FUSE mounts"; |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | if (fuse_lowlevel_notify_inval_entry(se, parent_ino, child_name.c_str(), child_name.size())) { |
| 401 | // Invalidating the dentry can fail if there's no dcache entry, however, there may still |
| 402 | // be cached attributes, so attempt to invalidate those by invalidating the inode |
| 403 | fuse_lowlevel_notify_inval_inode(se, child_ino, 0, 0); |
| 404 | } |
Zim | 8996231 | 2020-04-22 21:21:53 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 407 | static double get_entry_timeout(const string& path, node* node, struct fuse* fuse) { |
| 408 | string media_path = fuse->GetEffectiveRootPath() + "/Android/media"; |
Zim | ded1ab9 | 2021-01-15 10:36:17 +0000 | [diff] [blame] | 409 | if (fuse->disable_dentry_cache || node->ShouldInvalidate() || |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 410 | is_package_owned_path(path, fuse->path) || android::base::StartsWith(path, media_path)) { |
Zim | b283401 | 2020-06-17 13:26:27 +0100 | [diff] [blame] | 411 | // We set dentry timeout to 0 for the following reasons: |
Martijn Coenen | 8713309 | 2020-10-14 17:00:28 +0200 | [diff] [blame] | 412 | // 1. The dentry cache was completely disabled |
Zim | ded1ab9 | 2021-01-15 10:36:17 +0000 | [diff] [blame] | 413 | // 2.1 Case-insensitive lookups need to invalidate other case-insensitive dentry matches |
| 414 | // 2.2 Nodes supporting transforms need to be invalidated, so that subsequent lookups by a |
| 415 | // uid requiring a transform is guaranteed to come to the FUSE daemon. |
Martijn Coenen | 8713309 | 2020-10-14 17:00:28 +0200 | [diff] [blame] | 416 | // 3. With app data isolation enabled, app A should not guess existence of app B from the |
Zim | b283401 | 2020-06-17 13:26:27 +0100 | [diff] [blame] | 417 | // Android/{data,obb}/<package> paths, hence we prevent the kernel from caching that |
| 418 | // information. |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 419 | // 4. Installd might delete Android/media/<package> dirs when app data is cleared. |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 420 | // This can leave a stale entry in the kernel dcache, and break subsequent creation of the |
| 421 | // dir via FUSE. |
| 422 | return 0; |
| 423 | } |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 424 | return std::numeric_limits<double>::max(); |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static std::string get_path(node* node) { |
| 428 | const string& io_path = node->GetIoPath(); |
| 429 | return io_path.empty() ? node->BuildPath() : io_path; |
| 430 | } |
| 431 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 432 | // Returns true if the path resides under .transforms/synthetic. |
| 433 | // NOTE: currently only file paths corresponding to redacted URIs reside under this folder. The path |
| 434 | // itself never exists and just a link for transformation. |
| 435 | static inline bool is_synthetic_path(const string& path, struct fuse* fuse) { |
| 436 | return android::base::StartsWithIgnoreCase( |
| 437 | path, fuse->GetTransformsDir() + "/" + TRANSFORM_SYNTHETIC_DIR); |
| 438 | } |
| 439 | |
Zim | 549ee7d | 2021-03-19 17:26:20 +0000 | [diff] [blame] | 440 | static inline bool is_transcode_supported_path(const string& path, struct fuse* fuse) { |
| 441 | // Keep in sync with MediaProvider#supportsTranscode |
| 442 | return android::base::EndsWithIgnoreCase(path, ".mp4") && |
| 443 | android::base::StartsWithIgnoreCase(path, |
| 444 | fuse->GetEffectiveRootPath() + "/dcim/camera/"); |
| 445 | } |
| 446 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 447 | static inline bool is_transforms_dir_path(const string& path, struct fuse* fuse) { |
| 448 | return android::base::StartsWithIgnoreCase(path, fuse->GetTransformsDir()); |
| 449 | } |
| 450 | |
| 451 | static std::unique_ptr<mediaprovider::fuse::FileLookupResult> validate_node_path( |
| 452 | const std::string& path, const std::string& name, fuse_req_t req, int* error_code, |
| 453 | struct fuse_entry_param* e, const FuseOp op) { |
| 454 | struct fuse* fuse = get_fuse(req); |
| 455 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
| 456 | memset(e, 0, sizeof(*e)); |
| 457 | |
| 458 | const bool synthetic_path = is_synthetic_path(path, fuse); |
| 459 | if (lstat(path.c_str(), &e->attr) < 0 && !(op == FuseOp::lookup && synthetic_path)) { |
| 460 | *error_code = errno; |
| 461 | return nullptr; |
| 462 | } |
| 463 | |
| 464 | if (is_transforms_dir_path(path, fuse)) { |
| 465 | if (op == FuseOp::lookup) { |
| 466 | // Lookups are only allowed under .transforms/synthetic dir |
| 467 | if (!(android::base::EqualsIgnoreCase(path, fuse->GetTransformsDir()) || |
| 468 | android::base::StartsWithIgnoreCase( |
| 469 | path, fuse->GetTransformsDir() + "/" + TRANSFORM_SYNTHETIC_DIR))) { |
| 470 | *error_code = ENONET; |
| 471 | return nullptr; |
| 472 | } |
| 473 | } else { |
| 474 | // user-code is only allowed to make lookups under .transforms dir, and that too only |
| 475 | // under .transforms/synthetic dir |
| 476 | *error_code = ENOENT; |
| 477 | return nullptr; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (S_ISDIR(e->attr.st_mode)) { |
| 482 | // now that we have reached this point, ops on directories are safe and require no |
| 483 | // transformation. |
| 484 | return std::make_unique<mediaprovider::fuse::FileLookupResult>(0, 0, 0, true, false, ""); |
| 485 | } |
| 486 | |
Zim | 549ee7d | 2021-03-19 17:26:20 +0000 | [diff] [blame] | 487 | if (!synthetic_path && !is_transcode_supported_path(path, fuse)) { |
| 488 | // Transforms are only supported for synthetic or transcode-supported paths |
| 489 | return std::make_unique<mediaprovider::fuse::FileLookupResult>(0, 0, 0, true, false, ""); |
| 490 | } |
| 491 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 492 | // Handle potential file transforms |
| 493 | std::unique_ptr<mediaprovider::fuse::FileLookupResult> file_lookup_result = |
| 494 | fuse->mp->FileLookup(path, req->ctx.uid, req->ctx.pid); |
| 495 | |
| 496 | if (!file_lookup_result) { |
| 497 | // Fail lookup if we can't fetch FileLookupResult for path |
| 498 | LOG(WARNING) << "Failed to fetch FileLookupResult for " << path; |
| 499 | *error_code = ENOENT; |
| 500 | return nullptr; |
| 501 | } |
| 502 | |
| 503 | const string& io_path = file_lookup_result->io_path; |
| 504 | // Update size with io_path size if io_path is not same as path |
| 505 | if (!io_path.empty() && (io_path != path) && (lstat(io_path.c_str(), &e->attr) < 0)) { |
| 506 | *error_code = errno; |
| 507 | return nullptr; |
| 508 | } |
| 509 | |
| 510 | return file_lookup_result; |
| 511 | } |
| 512 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 513 | static node* make_node_entry(fuse_req_t req, node* parent, const string& name, const string& path, |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 514 | struct fuse_entry_param* e, int* error_code, const FuseOp op) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 515 | struct fuse* fuse = get_fuse(req); |
Martijn Coenen | b8ff4eb | 2019-12-17 09:55:17 +0100 | [diff] [blame] | 516 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 517 | node* node; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 518 | |
Jeff Sharkey | 7469b98 | 2019-08-28 16:51:02 -0600 | [diff] [blame] | 519 | memset(e, 0, sizeof(*e)); |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 520 | |
| 521 | std::unique_ptr<mediaprovider::fuse::FileLookupResult> file_lookup_result = |
| 522 | validate_node_path(path, name, req, error_code, e, op); |
| 523 | if (!file_lookup_result) { |
| 524 | // Fail lookup if we can't validate |path, |errno| would have already been set |
| 525 | return nullptr; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 526 | } |
| 527 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 528 | const bool should_invalidate = file_lookup_result->transforms_supported; |
| 529 | const bool transforms_complete = file_lookup_result->transforms_complete; |
| 530 | const int transforms = file_lookup_result->transforms; |
| 531 | const int transforms_reason = file_lookup_result->transforms_reason; |
| 532 | const string& io_path = file_lookup_result->io_path; |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 533 | |
Zim | ed257c1 | 2020-10-15 13:47:45 +0100 | [diff] [blame] | 534 | node = parent->LookupChildByName(name, true /* acquire */, transforms); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 535 | if (!node) { |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 536 | ino_t ino = e->attr.st_ino; |
Zim | ded1ab9 | 2021-01-15 10:36:17 +0000 | [diff] [blame] | 537 | node = ::node::Create(parent, name, io_path, should_invalidate, transforms_complete, |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 538 | transforms, transforms_reason, &fuse->lock, ino, &fuse->tracker); |
Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 539 | } else if (!mediaprovider::fuse::containsMount(path, std::to_string(getuid() / PER_USER_RANGE))) { |
Corina | 633099b | 2020-06-17 21:55:33 +0100 | [diff] [blame] | 540 | // Only invalidate a path if it does not contain mount. |
Zim | 0852ba7 | 2020-06-09 11:50:33 +0100 | [diff] [blame] | 541 | // Invalidate both names to ensure there's no dentry left in the kernel after the following |
| 542 | // operations: |
| 543 | // 1) touch foo, touch FOO, unlink *foo* |
| 544 | // 2) touch foo, touch FOO, unlink *FOO* |
| 545 | // Invalidating lookup_name fixes (1) and invalidating node_name fixes (2) |
Zim | ded1ab9 | 2021-01-15 10:36:17 +0000 | [diff] [blame] | 546 | // SetShouldInvalidate invalidates lookup_name by using 0 timeout below and we explicitly |
Zim | 33aea10 | 2020-06-19 15:49:47 +0100 | [diff] [blame] | 547 | // invalidate node_name if different case |
| 548 | // Note that we invalidate async otherwise we will deadlock the kernel |
| 549 | if (name != node->GetName()) { |
Zim | f2b47b4 | 2020-09-16 14:54:06 +0100 | [diff] [blame] | 550 | // Record that we have made a case insensitive lookup, this allows us invalidate nodes |
| 551 | // correctly on subsequent lookups for the case of |node| |
Zim | ded1ab9 | 2021-01-15 10:36:17 +0000 | [diff] [blame] | 552 | node->SetShouldInvalidate(); |
Zim | f2b47b4 | 2020-09-16 14:54:06 +0100 | [diff] [blame] | 553 | |
Narayan Kamath | 8b296f5 | 2020-07-29 13:10:17 +0100 | [diff] [blame] | 554 | // Make copies of the node name and path so we're not attempting to acquire |
| 555 | // any node locks from the invalidation thread. Depending on timing, we may end |
| 556 | // up invalidating the wrong inode but that shouldn't result in correctness issues. |
| 557 | const fuse_ino_t parent_ino = fuse->ToInode(parent); |
| 558 | const fuse_ino_t child_ino = fuse->ToInode(node); |
| 559 | const std::string& node_name = node->GetName(); |
Narayan Kamath | 8b296f5 | 2020-07-29 13:10:17 +0100 | [diff] [blame] | 560 | std::thread t([=]() { fuse_inval(fuse->se, parent_ino, child_ino, node_name, path); }); |
Zim | 33aea10 | 2020-06-19 15:49:47 +0100 | [diff] [blame] | 561 | t.detach(); |
| 562 | } |
Zim | e543368 | 2021-03-10 12:06:54 +0000 | [diff] [blame] | 563 | |
| 564 | // This updated value allows us correctly decide if to keep_cache and use direct_io during |
| 565 | // FUSE_OPEN. Between the last lookup and this lookup, we might have deleted a cached |
| 566 | // transcoded file on the lower fs. A subsequent transcode at FUSE_READ should ensure we |
| 567 | // don't reuse any stale transcode page cache content. |
| 568 | node->SetTransformsComplete(transforms_complete); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 569 | } |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 570 | TRACE_NODE(node, req); |
Zim | 8996231 | 2020-04-22 21:21:53 +0100 | [diff] [blame] | 571 | |
Narayan Kamath | fc0aa95 | 2019-12-31 13:31:49 +0000 | [diff] [blame] | 572 | // This FS is not being exported via NFS so just a fixed generation number |
| 573 | // for now. If we do need this, we need to increment the generation ID each |
| 574 | // time the fuse daemon restarts because that's what it takes for us to |
| 575 | // reuse inode numbers. |
| 576 | e->generation = 0; |
Zim | 8b2b6f2 | 2020-01-22 13:53:59 +0000 | [diff] [blame] | 577 | e->ino = fuse->ToInode(node); |
Zim | ded1ab9 | 2021-01-15 10:36:17 +0000 | [diff] [blame] | 578 | e->entry_timeout = get_entry_timeout(path, node, fuse); |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 579 | e->attr_timeout = std::numeric_limits<double>::max(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 580 | return node; |
| 581 | } |
| 582 | |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 583 | static inline bool is_requesting_write(int flags) { |
| 584 | return flags & (O_WRONLY | O_RDWR); |
| 585 | } |
| 586 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 587 | namespace mediaprovider { |
| 588 | namespace fuse { |
| 589 | |
| 590 | /** |
| 591 | * Function implementations |
| 592 | * |
| 593 | * These implement the various functions in fuse_lowlevel_ops |
| 594 | * |
| 595 | */ |
| 596 | |
| 597 | static void pf_init(void* userdata, struct fuse_conn_info* conn) { |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 598 | struct fuse* fuse = reinterpret_cast<struct fuse*>(userdata); |
| 599 | |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 600 | // We don't want a getattr request with every read request |
Zim | 1100f34 | 2020-05-15 16:13:00 +0100 | [diff] [blame] | 601 | conn->want &= ~FUSE_CAP_AUTO_INVAL_DATA & ~FUSE_CAP_READDIRPLUS_AUTO; |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 602 | unsigned mask = (FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE | FUSE_CAP_SPLICE_READ | |
| 603 | FUSE_CAP_ASYNC_READ | FUSE_CAP_ATOMIC_O_TRUNC | FUSE_CAP_WRITEBACK_CACHE | |
| 604 | FUSE_CAP_EXPORT_SUPPORT | FUSE_CAP_FLOCK_LOCKS); |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 605 | |
| 606 | if (fuse->passthrough) { |
| 607 | if (conn->capable & FUSE_CAP_PASSTHROUGH) { |
| 608 | mask |= FUSE_CAP_PASSTHROUGH; |
| 609 | } else { |
| 610 | LOG(WARNING) << "Passthrough feature not supported by the kernel"; |
| 611 | fuse->passthrough = false; |
| 612 | } |
| 613 | } |
| 614 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 615 | conn->want |= conn->capable & mask; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 616 | conn->max_read = MAX_READ_SIZE; |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 617 | |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 618 | fuse->active->store(true, std::memory_order_release); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Zim | 005cd81 | 2019-12-17 15:35:51 +0000 | [diff] [blame] | 621 | static void pf_destroy(void* userdata) { |
| 622 | struct fuse* fuse = reinterpret_cast<struct fuse*>(userdata); |
| 623 | LOG(INFO) << "DESTROY " << fuse->path; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 624 | |
| 625 | node::DeleteTree(fuse->root); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 626 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 627 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 628 | // Return true if the path is accessible for that uid. |
| 629 | static bool is_app_accessible_path(MediaProviderWrapper* mp, const string& path, uid_t uid) { |
Zim | 3cde266 | 2021-02-22 21:20:33 +0000 | [diff] [blame] | 630 | if (uid < AID_APP_START || uid == MY_UID) { |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 631 | return true; |
| 632 | } |
| 633 | |
Martijn Coenen | 71435d5 | 2020-06-15 20:23:40 +0200 | [diff] [blame] | 634 | if (path == "/storage/emulated") { |
| 635 | // Apps should never refer to /storage/emulated - they should be using the user-spcific |
| 636 | // subdirs, eg /storage/emulated/0 |
| 637 | return false; |
| 638 | } |
| 639 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 640 | std::smatch match; |
| 641 | if (std::regex_match(path, match, PATTERN_OWNED_PATH)) { |
| 642 | const std::string& pkg = match[1]; |
Abhijeet Kaur | 077fdd5 | 2020-05-28 13:59:31 +0100 | [diff] [blame] | 643 | // .nomedia is not a valid package. .nomedia always exists in /Android/data directory, |
| 644 | // and it's not an external file/directory of any package |
| 645 | if (pkg == ".nomedia") { |
| 646 | return true; |
| 647 | } |
Martijn Coenen | ba3e677 | 2021-04-28 09:27:54 +0200 | [diff] [blame] | 648 | if (android::base::StartsWith(path, "/storage/emulated")) { |
| 649 | // Emulated storage bind-mounts app-private data directories, and so these |
| 650 | // should not be accessible through FUSE anyway. |
| 651 | LOG(WARNING) << "Rejected access to app-private dir on FUSE: " << path |
| 652 | << " from uid: " << uid; |
| 653 | return false; |
| 654 | } |
Abhijeet Kaur | b3ac280 | 2020-10-07 16:42:42 +0100 | [diff] [blame] | 655 | if (!mp->isUidAllowedAccessToDataOrObbPath(uid, path)) { |
| 656 | PLOG(WARNING) << "Invalid other package file access from " << uid << "(: " << path; |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 663 | static std::regex storage_emulated_regex("^\\/storage\\/emulated\\/([0-9]+)"); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 664 | static node* do_lookup(fuse_req_t req, fuse_ino_t parent, const char* name, |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 665 | struct fuse_entry_param* e, int* error_code, const FuseOp op) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 666 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 667 | node* parent_node = fuse->FromInode(parent); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 668 | if (!parent_node) { |
| 669 | *error_code = ENOENT; |
| 670 | return nullptr; |
| 671 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 672 | string parent_path = parent_node->BuildPath(); |
Martijn Coenen | a5f19e8 | 2020-06-17 13:01:23 +0200 | [diff] [blame] | 673 | // We should always allow lookups on the root, because failing them could cause |
| 674 | // bind mounts to be invalidated. |
| 675 | if (!fuse->IsRoot(parent_node) && !is_app_accessible_path(fuse->mp, parent_path, req->ctx.uid)) { |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 676 | *error_code = ENOENT; |
| 677 | return nullptr; |
| 678 | } |
| 679 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 680 | TRACE_NODE(parent_node, req); |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 681 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 682 | const string child_path = parent_path + "/" + name; |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 683 | std::smatch match; |
| 684 | std::regex_search(child_path, match, storage_emulated_regex); |
Zim | 5077a89 | 2020-09-08 12:55:12 +0100 | [diff] [blame] | 685 | |
| 686 | // Ensure the FuseDaemon user id matches the user id or cross-user lookups are allowed in |
| 687 | // requested path |
Zim | 3cde266 | 2021-02-22 21:20:33 +0000 | [diff] [blame] | 688 | if (match.size() == 2 && MY_USER_ID_STRING != match[1].str()) { |
Zim | 5077a89 | 2020-09-08 12:55:12 +0100 | [diff] [blame] | 689 | // If user id mismatch, check cross-user lookups |
Martijn Coenen | 44ef630 | 2020-11-06 10:32:32 +0100 | [diff] [blame] | 690 | long userId = strtol(match[1].str().c_str(), nullptr, 10); |
| 691 | if (userId < 0 || userId > MAX_USER_ID || |
| 692 | !fuse->mp->ShouldAllowLookup(req->ctx.uid, userId)) { |
Zim | 5077a89 | 2020-09-08 12:55:12 +0100 | [diff] [blame] | 693 | *error_code = EACCES; |
| 694 | return nullptr; |
| 695 | } |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 696 | } |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 697 | |
| 698 | return make_node_entry(req, parent_node, name, child_path, e, error_code, op); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static void pf_lookup(fuse_req_t req, fuse_ino_t parent, const char* name) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 702 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 703 | struct fuse_entry_param e; |
| 704 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 705 | int error_code = 0; |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 706 | if (do_lookup(req, parent, name, &e, &error_code, FuseOp::lookup)) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 707 | fuse_reply_entry(req, &e); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 708 | } else { |
| 709 | CHECK(error_code != 0); |
| 710 | fuse_reply_err(req, error_code); |
| 711 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 714 | static void do_forget(fuse_req_t req, struct fuse* fuse, fuse_ino_t ino, uint64_t nlookup) { |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 715 | node* node = fuse->FromInode(ino); |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 716 | TRACE_NODE(node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 717 | if (node) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 718 | // This is a narrowing conversion from an unsigned 64bit to a 32bit value. For |
| 719 | // some reason we only keep 32 bit refcounts but the kernel issues |
| 720 | // forget requests with a 64 bit counter. |
Narayan Kamath | 568f17a | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 721 | node->Release(static_cast<uint32_t>(nlookup)); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
| 725 | static void pf_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) { |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 726 | // Always allow to forget so no need to check is_app_accessible_path() |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 727 | ATRACE_CALL(); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 728 | node* node; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 729 | struct fuse* fuse = get_fuse(req); |
| 730 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 731 | do_forget(req, fuse, ino, nlookup); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 732 | fuse_reply_none(req); |
| 733 | } |
| 734 | |
| 735 | static void pf_forget_multi(fuse_req_t req, |
| 736 | size_t count, |
| 737 | struct fuse_forget_data* forgets) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 738 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 739 | struct fuse* fuse = get_fuse(req); |
| 740 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 741 | for (int i = 0; i < count; i++) { |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 742 | do_forget(req, fuse, forgets[i].ino, forgets[i].nlookup); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 743 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 744 | fuse_reply_none(req); |
| 745 | } |
| 746 | |
Zim | 9e3c40b | 2021-04-09 15:51:15 +0100 | [diff] [blame] | 747 | static void pf_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset, off_t length, |
| 748 | fuse_file_info* fi) { |
| 749 | ATRACE_CALL(); |
| 750 | struct fuse* fuse = get_fuse(req); |
| 751 | |
Zim | 9e3c40b | 2021-04-09 15:51:15 +0100 | [diff] [blame] | 752 | handle* h = reinterpret_cast<handle*>(fi->fh); |
Zim | 9e3652b | 2021-05-05 11:35:00 +0100 | [diff] [blame^] | 753 | auto err = fallocate(h->fd, mode, offset, length); |
Zim | 9e3c40b | 2021-04-09 15:51:15 +0100 | [diff] [blame] | 754 | fuse_reply_err(req, err); |
| 755 | } |
| 756 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 757 | static void pf_getattr(fuse_req_t req, |
| 758 | fuse_ino_t ino, |
| 759 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 760 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 761 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 762 | node* node = fuse->FromInode(ino); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 763 | if (!node) { |
| 764 | fuse_reply_err(req, ENOENT); |
| 765 | return; |
| 766 | } |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 767 | const string& path = get_path(node); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 768 | if (!is_app_accessible_path(fuse->mp, path, req->ctx.uid)) { |
| 769 | fuse_reply_err(req, ENOENT); |
| 770 | return; |
| 771 | } |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 772 | TRACE_NODE(node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 773 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 774 | struct stat s; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 775 | memset(&s, 0, sizeof(s)); |
| 776 | if (lstat(path.c_str(), &s) < 0) { |
| 777 | fuse_reply_err(req, errno); |
| 778 | } else { |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 779 | fuse_reply_attr(req, &s, std::numeric_limits<double>::max()); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
| 783 | static void pf_setattr(fuse_req_t req, |
| 784 | fuse_ino_t ino, |
| 785 | struct stat* attr, |
| 786 | int to_set, |
| 787 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 788 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 789 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 790 | node* node = fuse->FromInode(ino); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 791 | if (!node) { |
| 792 | fuse_reply_err(req, ENOENT); |
| 793 | return; |
| 794 | } |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 795 | const string& path = get_path(node); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 796 | if (!is_app_accessible_path(fuse->mp, path, req->ctx.uid)) { |
| 797 | fuse_reply_err(req, ENOENT); |
| 798 | return; |
| 799 | } |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 800 | |
Zimuzo Ezeozue | 435bd25 | 2020-06-30 20:15:17 +0000 | [diff] [blame] | 801 | int fd = -1; |
| 802 | if (fi) { |
| 803 | // If we have a file_info, setattr was called with an fd so use the fd instead of path |
| 804 | handle* h = reinterpret_cast<handle*>(fi->fh); |
| 805 | fd = h->fd; |
| 806 | } else { |
| 807 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Zim | 73895ba | 2021-01-13 12:51:20 +0000 | [diff] [blame] | 808 | std::unique_ptr<FileOpenResult> result = fuse->mp->OnFileOpen( |
Zim | 801d985 | 2021-01-26 18:30:53 +0000 | [diff] [blame] | 809 | path, path, ctx->uid, ctx->pid, node->GetTransformsReason(), true /* for_write */, |
| 810 | false /* redact */, false /* log_transforms_metrics */); |
Zim | 73895ba | 2021-01-13 12:51:20 +0000 | [diff] [blame] | 811 | |
| 812 | if (!result) { |
| 813 | fuse_reply_err(req, EFAULT); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | if (result->status) { |
Zimuzo Ezeozue | 435bd25 | 2020-06-30 20:15:17 +0000 | [diff] [blame] | 818 | fuse_reply_err(req, EACCES); |
| 819 | return; |
| 820 | } |
| 821 | } |
| 822 | struct timespec times[2]; |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 823 | TRACE_NODE(node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 824 | |
| 825 | /* XXX: incomplete implementation on purpose. |
| 826 | * chmod/chown should NEVER be implemented.*/ |
| 827 | |
Zimuzo Ezeozue | 435bd25 | 2020-06-30 20:15:17 +0000 | [diff] [blame] | 828 | if ((to_set & FUSE_SET_ATTR_SIZE)) { |
| 829 | int res = 0; |
| 830 | if (fd == -1) { |
| 831 | res = truncate64(path.c_str(), attr->st_size); |
| 832 | } else { |
| 833 | res = ftruncate64(fd, attr->st_size); |
| 834 | } |
| 835 | |
| 836 | if (res < 0) { |
| 837 | fuse_reply_err(req, errno); |
| 838 | return; |
| 839 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 843 | * are both set, then set it to the current time. Else, set it to the |
| 844 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 845 | * as it allows ATIME and MTIME to be changed independently, and has |
| 846 | * nanosecond resolution which fuse also has. |
| 847 | */ |
| 848 | if (to_set & (FATTR_ATIME | FATTR_MTIME)) { |
| 849 | times[0].tv_nsec = UTIME_OMIT; |
| 850 | times[1].tv_nsec = UTIME_OMIT; |
| 851 | if (to_set & FATTR_ATIME) { |
| 852 | if (to_set & FATTR_ATIME_NOW) { |
| 853 | times[0].tv_nsec = UTIME_NOW; |
| 854 | } else { |
Zim | 0056c4b | 2020-04-27 18:56:51 +0100 | [diff] [blame] | 855 | times[0] = attr->st_atim; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 856 | } |
| 857 | } |
Zim | 0056c4b | 2020-04-27 18:56:51 +0100 | [diff] [blame] | 858 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 859 | if (to_set & FATTR_MTIME) { |
| 860 | if (to_set & FATTR_MTIME_NOW) { |
| 861 | times[1].tv_nsec = UTIME_NOW; |
| 862 | } else { |
Zim | 0056c4b | 2020-04-27 18:56:51 +0100 | [diff] [blame] | 863 | times[1] = attr->st_mtim; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 864 | } |
| 865 | } |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 866 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 867 | TRACE_NODE(node, req); |
Zimuzo Ezeozue | 435bd25 | 2020-06-30 20:15:17 +0000 | [diff] [blame] | 868 | int res = 0; |
| 869 | if (fd == -1) { |
| 870 | res = utimensat(-1, path.c_str(), times, 0); |
| 871 | } else { |
| 872 | res = futimens(fd, times); |
| 873 | } |
| 874 | |
| 875 | if (res < 0) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 876 | fuse_reply_err(req, errno); |
| 877 | return; |
| 878 | } |
| 879 | } |
| 880 | |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 881 | lstat(path.c_str(), attr); |
Zim | 3b6ff4d | 2021-03-03 11:14:59 +0000 | [diff] [blame] | 882 | fuse_reply_attr(req, attr, std::numeric_limits<double>::max()); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 883 | } |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 884 | |
| 885 | static void pf_canonical_path(fuse_req_t req, fuse_ino_t ino) |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 886 | { |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 887 | struct fuse* fuse = get_fuse(req); |
| 888 | node* node = fuse->FromInode(ino); |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 889 | const string& path = node ? get_path(node) : ""; |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 890 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 891 | if (node && is_app_accessible_path(fuse->mp, path, req->ctx.uid)) { |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 892 | // TODO(b/147482155): Check that uid has access to |path| and its contents |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 893 | fuse_reply_canonical_path(req, path.c_str()); |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 894 | return; |
| 895 | } |
| 896 | fuse_reply_err(req, ENOENT); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 897 | } |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 898 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 899 | static void pf_mknod(fuse_req_t req, |
| 900 | fuse_ino_t parent, |
| 901 | const char* name, |
| 902 | mode_t mode, |
| 903 | dev_t rdev) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 904 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 905 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 906 | node* parent_node = fuse->FromInode(parent); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 907 | if (!parent_node) { |
| 908 | fuse_reply_err(req, ENOENT); |
| 909 | return; |
| 910 | } |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 911 | string parent_path = parent_node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 912 | if (!is_app_accessible_path(fuse->mp, parent_path, req->ctx.uid)) { |
| 913 | fuse_reply_err(req, ENOENT); |
| 914 | return; |
| 915 | } |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 916 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 917 | TRACE_NODE(parent_node, req); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 918 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 919 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 920 | |
| 921 | mode = (mode & (~0777)) | 0664; |
| 922 | if (mknod(child_path.c_str(), mode, rdev) < 0) { |
| 923 | fuse_reply_err(req, errno); |
| 924 | return; |
| 925 | } |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 926 | |
| 927 | int error_code = 0; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 928 | struct fuse_entry_param e; |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 929 | if (make_node_entry(req, parent_node, name, child_path, &e, &error_code, FuseOp::mknod)) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 930 | fuse_reply_entry(req, &e); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 931 | } else { |
| 932 | CHECK(error_code != 0); |
| 933 | fuse_reply_err(req, error_code); |
| 934 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static void pf_mkdir(fuse_req_t req, |
| 938 | fuse_ino_t parent, |
| 939 | const char* name, |
| 940 | mode_t mode) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 941 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 942 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 943 | node* parent_node = fuse->FromInode(parent); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 944 | if (!parent_node) { |
| 945 | fuse_reply_err(req, ENOENT); |
| 946 | return; |
| 947 | } |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 948 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 949 | const string parent_path = parent_node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 950 | if (!is_app_accessible_path(fuse->mp, parent_path, ctx->uid)) { |
| 951 | fuse_reply_err(req, ENOENT); |
| 952 | return; |
| 953 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 954 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 955 | TRACE_NODE(parent_node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 956 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 957 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 958 | |
shafik | e4fb146 | 2020-01-29 16:25:23 +0000 | [diff] [blame] | 959 | int status = fuse->mp->IsCreatingDirAllowed(child_path, ctx->uid); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 960 | if (status) { |
| 961 | fuse_reply_err(req, status); |
| 962 | return; |
| 963 | } |
| 964 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 965 | mode = (mode & (~0777)) | 0775; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 966 | if (mkdir(child_path.c_str(), mode) < 0) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 967 | fuse_reply_err(req, errno); |
| 968 | return; |
| 969 | } |
| 970 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 971 | int error_code = 0; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 972 | struct fuse_entry_param e; |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 973 | if (make_node_entry(req, parent_node, name, child_path, &e, &error_code, FuseOp::mkdir)) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 974 | fuse_reply_entry(req, &e); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 975 | } else { |
| 976 | CHECK(error_code != 0); |
| 977 | fuse_reply_err(req, error_code); |
| 978 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static void pf_unlink(fuse_req_t req, fuse_ino_t parent, const char* name) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 982 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 983 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 984 | node* parent_node = fuse->FromInode(parent); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 985 | if (!parent_node) { |
| 986 | fuse_reply_err(req, ENOENT); |
| 987 | return; |
| 988 | } |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 989 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 990 | const string parent_path = parent_node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 991 | if (!is_app_accessible_path(fuse->mp, parent_path, ctx->uid)) { |
| 992 | fuse_reply_err(req, ENOENT); |
| 993 | return; |
| 994 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 995 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 996 | TRACE_NODE(parent_node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 997 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 998 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 999 | |
shafik | e4fb146 | 2020-01-29 16:25:23 +0000 | [diff] [blame] | 1000 | int status = fuse->mp->DeleteFile(child_path, ctx->uid); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1001 | if (status) { |
| 1002 | fuse_reply_err(req, status); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1003 | return; |
| 1004 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1005 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 1006 | // TODO(b/169306422): Log each deleted node |
| 1007 | parent_node->SetDeletedForChild(name); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1008 | fuse_reply_err(req, 0); |
| 1009 | } |
| 1010 | |
| 1011 | static void pf_rmdir(fuse_req_t req, fuse_ino_t parent, const char* name) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1012 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1013 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1014 | node* parent_node = fuse->FromInode(parent); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1015 | if (!parent_node) { |
| 1016 | fuse_reply_err(req, ENOENT); |
| 1017 | return; |
| 1018 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1019 | const string parent_path = parent_node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1020 | if (!is_app_accessible_path(fuse->mp, parent_path, req->ctx.uid)) { |
| 1021 | fuse_reply_err(req, ENOENT); |
| 1022 | return; |
| 1023 | } |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 1024 | |
| 1025 | if (is_transforms_dir_path(parent_path, fuse)) { |
| 1026 | // .transforms is a special daemon controlled dir so apps shouldn't be able to see it via |
| 1027 | // readdir, and any dir operations attempted on it should fail |
| 1028 | fuse_reply_err(req, ENOENT); |
| 1029 | return; |
| 1030 | } |
| 1031 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1032 | TRACE_NODE(parent_node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1033 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1034 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1035 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1036 | int status = fuse->mp->IsDeletingDirAllowed(child_path, req->ctx.uid); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1037 | if (status) { |
| 1038 | fuse_reply_err(req, status); |
| 1039 | return; |
| 1040 | } |
| 1041 | |
| 1042 | if (rmdir(child_path.c_str()) < 0) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1043 | fuse_reply_err(req, errno); |
| 1044 | return; |
| 1045 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1046 | |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 1047 | node* child_node = parent_node->LookupChildByName(name, false /* acquire */); |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1048 | TRACE_NODE(child_node, req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1049 | if (child_node) { |
| 1050 | child_node->SetDeleted(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1051 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1052 | |
| 1053 | fuse_reply_err(req, 0); |
| 1054 | } |
| 1055 | /* |
| 1056 | static void pf_symlink(fuse_req_t req, const char* link, fuse_ino_t parent, |
| 1057 | const char* name) |
| 1058 | { |
| 1059 | cout << "TODO:" << __func__; |
| 1060 | } |
| 1061 | */ |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 1062 | static int do_rename(fuse_req_t req, fuse_ino_t parent, const char* name, fuse_ino_t new_parent, |
| 1063 | const char* new_name, unsigned int flags) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1064 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1065 | struct fuse* fuse = get_fuse(req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1066 | |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 1067 | if (flags != 0) { |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 1068 | return EINVAL; |
| 1069 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1070 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1071 | node* old_parent_node = fuse->FromInode(parent); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1072 | if (!old_parent_node) return ENOENT; |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1073 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1074 | const string old_parent_path = old_parent_node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1075 | if (!is_app_accessible_path(fuse->mp, old_parent_path, ctx->uid)) { |
| 1076 | return ENOENT; |
| 1077 | } |
| 1078 | |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 1079 | if (is_transforms_dir_path(old_parent_path, fuse)) { |
| 1080 | // .transforms is a special daemon controlled dir so apps shouldn't be able to see it via |
| 1081 | // readdir, and any dir operations attempted on it should fail |
| 1082 | return ENOENT; |
| 1083 | } |
| 1084 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1085 | node* new_parent_node = fuse->FromInode(new_parent); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1086 | if (!new_parent_node) return ENOENT; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1087 | const string new_parent_path = new_parent_node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1088 | if (!is_app_accessible_path(fuse->mp, new_parent_path, ctx->uid)) { |
| 1089 | return ENOENT; |
| 1090 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1091 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1092 | if (!old_parent_node || !new_parent_node) { |
| 1093 | return ENOENT; |
| 1094 | } else if (parent == new_parent && name == new_name) { |
| 1095 | // No rename required. |
| 1096 | return 0; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1099 | TRACE_NODE(old_parent_node, req); |
| 1100 | TRACE_NODE(new_parent_node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1101 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 1102 | const string old_child_path = old_parent_path + "/" + name; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1103 | const string new_child_path = new_parent_path + "/" + new_name; |
| 1104 | |
Zim | bd89e7a | 2021-01-27 19:02:11 +0000 | [diff] [blame] | 1105 | if (android::base::EqualsIgnoreCase(fuse->GetEffectiveRootPath() + "/android", old_child_path)) { |
| 1106 | // Prevent renaming Android/ dir since it contains bind-mounts on the primary volume |
| 1107 | return EACCES; |
| 1108 | } |
| 1109 | |
Sahana Rao | 182ec6b | 2020-01-03 15:00:46 +0000 | [diff] [blame] | 1110 | // TODO(b/147408834): Check ENOTEMPTY & EEXIST error conditions before JNI call. |
shafik | e4fb146 | 2020-01-29 16:25:23 +0000 | [diff] [blame] | 1111 | const int res = fuse->mp->Rename(old_child_path, new_child_path, req->ctx.uid); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1112 | // TODO(b/145663158): Lookups can go out of sync if file/directory is actually moved but |
| 1113 | // EFAULT/EIO is reported due to JNI exception. |
| 1114 | if (res == 0) { |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 1115 | // TODO(b/169306422): Log each renamed node |
| 1116 | old_parent_node->RenameChild(name, new_name, new_parent_node); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1117 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1118 | return res; |
| 1119 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1120 | |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 1121 | static void pf_rename(fuse_req_t req, fuse_ino_t parent, const char* name, fuse_ino_t new_parent, |
| 1122 | const char* new_name, unsigned int flags) { |
| 1123 | int res = do_rename(req, parent, name, new_parent, new_name, flags); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1124 | fuse_reply_err(req, res); |
| 1125 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1126 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1127 | /* |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 1128 | static void pf_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t new_parent, |
| 1129 | const char* new_name) |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1130 | { |
| 1131 | cout << "TODO:" << __func__; |
| 1132 | } |
| 1133 | */ |
| 1134 | |
Zim | 9aa6f54 | 2020-10-19 15:39:33 +0100 | [diff] [blame] | 1135 | static handle* create_handle_for_node(struct fuse* fuse, const string& path, int fd, uid_t uid, |
Manish Singh | 9a6ccce | 2021-02-05 23:50:08 +0000 | [diff] [blame] | 1136 | uid_t transforms_uid, node* node, const RedactionInfo* ri, |
| 1137 | int* keep_cache) { |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1138 | std::lock_guard<std::recursive_mutex> guard(fuse->lock); |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1139 | |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1140 | bool redaction_needed = ri->isRedactionNeeded(); |
| 1141 | handle* handle = nullptr; |
Manish Singh | 9a6ccce | 2021-02-05 23:50:08 +0000 | [diff] [blame] | 1142 | int transforms = node->GetTransforms(); |
Zim | e543368 | 2021-03-10 12:06:54 +0000 | [diff] [blame] | 1143 | bool transforms_complete = node->IsTransformsComplete(); |
Manish Singh | 9a6ccce | 2021-02-05 23:50:08 +0000 | [diff] [blame] | 1144 | if (transforms_uid > 0) { |
| 1145 | CHECK(transforms); |
| 1146 | } |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (fuse->passthrough) { |
Zim | e543368 | 2021-03-10 12:06:54 +0000 | [diff] [blame] | 1149 | *keep_cache = transforms_complete; |
Zimuzo Ezeozue | 158b658 | 2021-03-04 12:50:54 +0000 | [diff] [blame] | 1150 | // We only enabled passthrough iff these 2 conditions hold |
| 1151 | // 1. Redaction is not needed |
| 1152 | // 2. Node transforms are completed, e.g transcoding. |
| 1153 | // (2) is important because we transcode lazily (on the first read) and with passthrough, |
| 1154 | // we will never get a read into the FUSE daemon, so passthrough would have returned |
| 1155 | // arbitrary bytes the first time around. However, if we ensure that transforms are |
| 1156 | // completed, then it's safe to use passthrough. Additionally, transcoded nodes never |
| 1157 | // require redaction so (2) implies (1) |
Zim | e543368 | 2021-03-10 12:06:54 +0000 | [diff] [blame] | 1158 | handle = new struct handle(fd, ri, true /* cached */, |
| 1159 | !redaction_needed && transforms_complete /* passthrough */, uid, |
| 1160 | transforms_uid); |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1161 | } else { |
| 1162 | // Without fuse->passthrough, we don't want to use the FUSE VFS cache in two cases: |
| 1163 | // 1. When redaction is needed because app A with EXIF access might access |
| 1164 | // a region that should have been redacted for app B without EXIF access, but app B on |
| 1165 | // a subsequent read, will be able to see the EXIF data because the read request for |
| 1166 | // that region will be served from cache and not get to the FUSE daemon |
| 1167 | // 2. When the file has a read or write lock on it. This means that the MediaProvider |
| 1168 | // has given an fd to the lower file system to an app. There are two cases where using |
| 1169 | // the cache in this case can be a problem: |
| 1170 | // a. Writing to a FUSE fd with caching enabled will use the write-back cache and a |
| 1171 | // subsequent read from the lower fs fd will not see the write. |
| 1172 | // b. Reading from a FUSE fd with caching enabled may not see the latest writes using |
| 1173 | // the lower fs fd because those writes did not go through the FUSE layer and reads from |
| 1174 | // FUSE after that write may be served from cache |
| 1175 | bool has_redacted = node->HasRedactedCache(); |
| 1176 | bool is_redaction_change = |
| 1177 | (redaction_needed && !has_redacted) || (!redaction_needed && has_redacted); |
| 1178 | bool is_cached_file_open = node->HasCachedHandle(); |
| 1179 | bool direct_io = (is_cached_file_open && is_redaction_change) || is_file_locked(fd, path); |
| 1180 | |
| 1181 | if (!is_cached_file_open && is_redaction_change) { |
| 1182 | node->SetRedactedCache(redaction_needed); |
| 1183 | // Purges stale page cache before open |
| 1184 | *keep_cache = 0; |
| 1185 | } else { |
Zim | e543368 | 2021-03-10 12:06:54 +0000 | [diff] [blame] | 1186 | *keep_cache = transforms_complete; |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1187 | } |
Manish Singh | 9a6ccce | 2021-02-05 23:50:08 +0000 | [diff] [blame] | 1188 | handle = new struct handle(fd, ri, !direct_io /* cached */, false /* passthrough */, uid, |
| 1189 | transforms_uid); |
Zim | 329ba2c | 2020-09-16 14:23:26 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1192 | node->AddHandle(handle); |
| 1193 | return handle; |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 1196 | bool do_passthrough_enable(fuse_req_t req, struct fuse_file_info* fi, unsigned int fd) { |
| 1197 | int passthrough_fh = fuse_passthrough_enable(req, fd); |
| 1198 | |
| 1199 | if (passthrough_fh <= 0) { |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
| 1203 | fi->passthrough_fh = passthrough_fh; |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1207 | static void pf_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1208 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1209 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1210 | node* node = fuse->FromInode(ino); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1211 | if (!node) { |
| 1212 | fuse_reply_err(req, ENOENT); |
| 1213 | return; |
| 1214 | } |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1215 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Zim | e9ae6ee | 2020-11-26 15:38:13 +0000 | [diff] [blame] | 1216 | const string& io_path = get_path(node); |
Zim | f7ec867 | 2020-10-28 18:25:45 +0000 | [diff] [blame] | 1217 | const string& build_path = node->BuildPath(); |
Zim | e9ae6ee | 2020-11-26 15:38:13 +0000 | [diff] [blame] | 1218 | if (!is_app_accessible_path(fuse->mp, io_path, ctx->uid)) { |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1219 | fuse_reply_err(req, ENOENT); |
| 1220 | return; |
| 1221 | } |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1222 | |
Manish Singh | 0f79b45 | 2021-03-04 00:20:38 +0000 | [diff] [blame] | 1223 | bool for_write = is_requesting_write(fi->flags); |
| 1224 | |
| 1225 | if (for_write && node->GetTransforms()) { |
| 1226 | TRACE_NODE(node, req) << "write with transforms"; |
| 1227 | } else { |
| 1228 | TRACE_NODE(node, req) << (for_write ? "write" : "read"); |
| 1229 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1230 | |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 1231 | if (fi->flags & O_DIRECT) { |
| 1232 | fi->flags &= ~O_DIRECT; |
| 1233 | fi->direct_io = true; |
| 1234 | } |
| 1235 | |
Zim | f7ec867 | 2020-10-28 18:25:45 +0000 | [diff] [blame] | 1236 | // Force permission check with the build path because the MediaProvider database might not be |
| 1237 | // aware of the io_path |
Zim | 73895ba | 2021-01-13 12:51:20 +0000 | [diff] [blame] | 1238 | // We don't redact if the caller was granted write permission for this file |
| 1239 | std::unique_ptr<FileOpenResult> result = fuse->mp->OnFileOpen( |
Zim | 801d985 | 2021-01-26 18:30:53 +0000 | [diff] [blame] | 1240 | build_path, io_path, ctx->uid, ctx->pid, node->GetTransformsReason(), for_write, |
| 1241 | !for_write /* redact */, true /* log_transforms_metrics */); |
Zim | 73895ba | 2021-01-13 12:51:20 +0000 | [diff] [blame] | 1242 | if (!result) { |
| 1243 | fuse_reply_err(req, EFAULT); |
| 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | if (result->status) { |
| 1248 | fuse_reply_err(req, result->status); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1249 | return; |
| 1250 | } |
| 1251 | |
Martijn Coenen | 2d7f1be | 2020-02-15 07:44:12 +0100 | [diff] [blame] | 1252 | // With the writeback cache enabled, FUSE may generate READ requests even for files that |
| 1253 | // were opened O_WRONLY; so make sure we open it O_RDWR instead. |
| 1254 | int open_flags = fi->flags; |
| 1255 | if (open_flags & O_WRONLY) { |
| 1256 | open_flags &= ~O_WRONLY; |
| 1257 | open_flags |= O_RDWR; |
| 1258 | } |
| 1259 | |
Zim | 1b14f2f | 2020-07-03 07:05:53 +0100 | [diff] [blame] | 1260 | if (open_flags & O_APPEND) { |
| 1261 | open_flags &= ~O_APPEND; |
| 1262 | } |
| 1263 | |
Zim | e9ae6ee | 2020-11-26 15:38:13 +0000 | [diff] [blame] | 1264 | const int fd = open(io_path.c_str(), open_flags); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1265 | if (fd < 0) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1266 | fuse_reply_err(req, errno); |
| 1267 | return; |
| 1268 | } |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 1269 | |
Zim | 329ba2c | 2020-09-16 14:23:26 +0100 | [diff] [blame] | 1270 | int keep_cache = 1; |
Manish Singh | 9a6ccce | 2021-02-05 23:50:08 +0000 | [diff] [blame] | 1271 | handle* h = create_handle_for_node(fuse, io_path, fd, result->uid, result->transforms_uid, node, |
Zim | 73895ba | 2021-01-13 12:51:20 +0000 | [diff] [blame] | 1272 | result->redaction_info.release(), &keep_cache); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1273 | fi->fh = ptr_to_id(h); |
Zim | 329ba2c | 2020-09-16 14:23:26 +0100 | [diff] [blame] | 1274 | fi->keep_cache = keep_cache; |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1275 | fi->direct_io = !h->cached; |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 1276 | |
| 1277 | // TODO(b/173190192) ensuring that h->cached must be enabled in order to |
| 1278 | // user FUSE passthrough is a conservative rule and might be dropped as |
| 1279 | // soon as demonstrated its correctness. |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1280 | if (h->passthrough) { |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 1281 | if (!do_passthrough_enable(req, fi, fd)) { |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1282 | // TODO: Should we crash here so we can find errors easily? |
Zim | e9ae6ee | 2020-11-26 15:38:13 +0000 | [diff] [blame] | 1283 | PLOG(ERROR) << "Passthrough OPEN failed for " << io_path; |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1284 | fuse_reply_err(req, EFAULT); |
| 1285 | return; |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1289 | fuse_reply_open(req, fi); |
| 1290 | } |
| 1291 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1292 | static void do_read(fuse_req_t req, size_t size, off_t off, struct fuse_file_info* fi) { |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1293 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1294 | struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size); |
| 1295 | |
| 1296 | buf.buf[0].fd = h->fd; |
| 1297 | buf.buf[0].pos = off; |
| 1298 | buf.buf[0].flags = |
| 1299 | (enum fuse_buf_flags) (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK); |
| 1300 | |
| 1301 | fuse_reply_data(req, &buf, (enum fuse_buf_copy_flags) 0); |
| 1302 | } |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1303 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1304 | /** |
| 1305 | * Sets the parameters for a fuse_buf that reads from memory, including flags. |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 1306 | * Makes buf->mem point to an already mapped region of zeroized memory. |
| 1307 | * This memory is read only. |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1308 | */ |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 1309 | static void create_mem_fuse_buf(size_t size, fuse_buf* buf, struct fuse* fuse) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1310 | buf->size = size; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 1311 | buf->mem = fuse->zero_addr; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1312 | buf->flags = static_cast<fuse_buf_flags>(0 /*read from fuse_buf.mem*/); |
| 1313 | buf->pos = -1; |
| 1314 | buf->fd = -1; |
| 1315 | } |
| 1316 | |
| 1317 | /** |
| 1318 | * Sets the parameters for a fuse_buf that reads from file, including flags. |
| 1319 | */ |
| 1320 | static void create_file_fuse_buf(size_t size, off_t pos, int fd, fuse_buf* buf) { |
| 1321 | buf->size = size; |
| 1322 | buf->fd = fd; |
| 1323 | buf->pos = pos; |
| 1324 | buf->flags = static_cast<fuse_buf_flags>(FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK); |
| 1325 | buf->mem = nullptr; |
| 1326 | } |
| 1327 | |
| 1328 | static void do_read_with_redaction(fuse_req_t req, size_t size, off_t off, fuse_file_info* fi) { |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1329 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1330 | |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 1331 | std::vector<ReadRange> ranges; |
| 1332 | h->ri->getReadRanges(off, size, &ranges); |
| 1333 | |
| 1334 | // As an optimization, return early if there are no ranges to redact. |
| 1335 | if (ranges.size() == 0) { |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1336 | do_read(req, size, off, fi); |
| 1337 | return; |
| 1338 | } |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 1339 | |
| 1340 | const size_t num_bufs = ranges.size(); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1341 | auto bufvec_ptr = std::unique_ptr<fuse_bufvec, decltype(free)*>{ |
| 1342 | reinterpret_cast<fuse_bufvec*>( |
| 1343 | malloc(sizeof(fuse_bufvec) + (num_bufs - 1) * sizeof(fuse_buf))), |
| 1344 | free}; |
| 1345 | fuse_bufvec& bufvec = *bufvec_ptr; |
| 1346 | |
| 1347 | // initialize bufvec |
| 1348 | bufvec.count = num_bufs; |
| 1349 | bufvec.idx = 0; |
| 1350 | bufvec.off = 0; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1351 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1352 | for (int i = 0; i < num_bufs; ++i) { |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 1353 | const ReadRange& range = ranges[i]; |
| 1354 | if (range.is_redaction) { |
| 1355 | create_mem_fuse_buf(range.size, &(bufvec.buf[i]), get_fuse(req)); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1356 | } else { |
Narayan Kamath | 11700c0 | 2020-10-06 09:15:34 +0100 | [diff] [blame] | 1357 | create_file_fuse_buf(range.size, range.start, h->fd, &(bufvec.buf[i])); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1358 | } |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | fuse_reply_data(req, &bufvec, static_cast<fuse_buf_copy_flags>(0)); |
| 1362 | } |
| 1363 | |
| 1364 | static void pf_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, |
| 1365 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1366 | ATRACE_CALL(); |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1367 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1368 | struct fuse* fuse = get_fuse(req); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1369 | |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 1370 | node* node = fuse->FromInode(ino); |
| 1371 | |
| 1372 | if (!node->IsTransformsComplete()) { |
| 1373 | if (!fuse->mp->Transform(node->BuildPath(), node->GetIoPath(), node->GetTransforms(), |
Manish Singh | 9a6ccce | 2021-02-05 23:50:08 +0000 | [diff] [blame] | 1374 | node->GetTransformsReason(), req->ctx.uid, h->uid, |
| 1375 | h->transforms_uid)) { |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 1376 | fuse_reply_err(req, EFAULT); |
| 1377 | return; |
| 1378 | } |
Zim | e543368 | 2021-03-10 12:06:54 +0000 | [diff] [blame] | 1379 | node->SetTransformsComplete(true); |
Zim | 867fece | 2020-09-23 15:23:19 +0100 | [diff] [blame] | 1380 | } |
| 1381 | |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 1382 | fuse->fadviser.Record(h->fd, size); |
| 1383 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1384 | if (h->ri->isRedactionNeeded()) { |
| 1385 | do_read_with_redaction(req, size, off, fi); |
| 1386 | } else { |
| 1387 | do_read(req, size, off, fi); |
| 1388 | } |
| 1389 | } |
| 1390 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1391 | /* |
| 1392 | static void pf_write(fuse_req_t req, fuse_ino_t ino, const char* buf, |
| 1393 | size_t size, off_t off, struct fuse_file_info* fi) |
| 1394 | { |
| 1395 | cout << "TODO:" << __func__; |
| 1396 | } |
| 1397 | */ |
| 1398 | |
| 1399 | static void pf_write_buf(fuse_req_t req, |
| 1400 | fuse_ino_t ino, |
| 1401 | struct fuse_bufvec* bufv, |
| 1402 | off_t off, |
| 1403 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1404 | ATRACE_CALL(); |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1405 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1406 | struct fuse_bufvec buf = FUSE_BUFVEC_INIT(fuse_buf_size(bufv)); |
| 1407 | ssize_t size; |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 1408 | struct fuse* fuse = get_fuse(req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1409 | |
| 1410 | buf.buf[0].fd = h->fd; |
| 1411 | buf.buf[0].pos = off; |
| 1412 | buf.buf[0].flags = |
| 1413 | (enum fuse_buf_flags) (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK); |
| 1414 | size = fuse_buf_copy(&buf, bufv, (enum fuse_buf_copy_flags) 0); |
| 1415 | |
| 1416 | if (size < 0) |
| 1417 | fuse_reply_err(req, -size); |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 1418 | else { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1419 | fuse_reply_write(req, size); |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 1420 | fuse->fadviser.Record(h->fd, size); |
| 1421 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1422 | } |
| 1423 | // Haven't tested this one. Not sure what calls it. |
| 1424 | #if 0 |
| 1425 | static void pf_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, |
| 1426 | off_t off_in, struct fuse_file_info* fi_in, |
| 1427 | fuse_ino_t ino_out, off_t off_out, |
| 1428 | struct fuse_file_info* fi_out, size_t len, |
| 1429 | int flags) |
| 1430 | { |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1431 | handle* h_in = reinterpret_cast<handle *>(fi_in->fh); |
| 1432 | handle* h_out = reinterpret_cast<handle *>(fi_out->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1433 | struct fuse_bufvec buf_in = FUSE_BUFVEC_INIT(len); |
| 1434 | struct fuse_bufvec buf_out = FUSE_BUFVEC_INIT(len); |
| 1435 | ssize_t size; |
| 1436 | |
| 1437 | buf_in.buf[0].fd = h_in->fd; |
| 1438 | buf_in.buf[0].pos = off_in; |
| 1439 | buf_in.buf[0].flags = (enum fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK); |
| 1440 | |
| 1441 | buf_out.buf[0].fd = h_out->fd; |
| 1442 | buf_out.buf[0].pos = off_out; |
| 1443 | buf_out.buf[0].flags = (enum fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK); |
| 1444 | size = fuse_buf_copy(&buf_out, &buf_in, (enum fuse_buf_copy_flags) 0); |
| 1445 | |
| 1446 | if (size < 0) { |
| 1447 | fuse_reply_err(req, -size); |
| 1448 | } |
| 1449 | |
| 1450 | fuse_reply_write(req, size); |
| 1451 | } |
| 1452 | #endif |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1453 | |
| 1454 | static void pf_release(fuse_req_t req, |
| 1455 | fuse_ino_t ino, |
| 1456 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1457 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1458 | struct fuse* fuse = get_fuse(req); |
Kyle Tso | 613b7ab | 2019-12-24 06:30:00 +0000 | [diff] [blame] | 1459 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1460 | node* node = fuse->FromInode(ino); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1461 | handle* h = reinterpret_cast<handle*>(fi->fh); |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1462 | TRACE_NODE(node, req); |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 1463 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1464 | fuse->fadviser.Close(h->fd); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1465 | if (node) { |
| 1466 | node->DestroyHandle(h); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1467 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1468 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1469 | fuse_reply_err(req, 0); |
| 1470 | } |
| 1471 | |
| 1472 | static int do_sync_common(int fd, bool datasync) { |
| 1473 | int res = datasync ? fdatasync(fd) : fsync(fd); |
| 1474 | |
| 1475 | if (res == -1) return errno; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | static void pf_fsync(fuse_req_t req, |
| 1480 | fuse_ino_t ino, |
| 1481 | int datasync, |
| 1482 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1483 | ATRACE_CALL(); |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1484 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1485 | int err = do_sync_common(h->fd, datasync); |
| 1486 | |
| 1487 | fuse_reply_err(req, err); |
| 1488 | } |
| 1489 | |
| 1490 | static void pf_fsyncdir(fuse_req_t req, |
| 1491 | fuse_ino_t ino, |
| 1492 | int datasync, |
| 1493 | struct fuse_file_info* fi) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1494 | dirhandle* h = reinterpret_cast<dirhandle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1495 | int err = do_sync_common(dirfd(h->d), datasync); |
| 1496 | |
| 1497 | fuse_reply_err(req, err); |
| 1498 | } |
| 1499 | |
| 1500 | static void pf_opendir(fuse_req_t req, |
| 1501 | fuse_ino_t ino, |
| 1502 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1503 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1504 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1505 | node* node = fuse->FromInode(ino); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1506 | if (!node) { |
| 1507 | fuse_reply_err(req, ENOENT); |
| 1508 | return; |
| 1509 | } |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1510 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1511 | const string path = node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1512 | if (!is_app_accessible_path(fuse->mp, path, ctx->uid)) { |
| 1513 | fuse_reply_err(req, ENOENT); |
| 1514 | return; |
| 1515 | } |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1516 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1517 | TRACE_NODE(node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1518 | |
Nandana Dutt | 5fc3201 | 2020-06-25 10:55:52 +0100 | [diff] [blame] | 1519 | int status = fuse->mp->IsOpendirAllowed(path, ctx->uid, /* forWrite */ false); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1520 | if (status) { |
| 1521 | fuse_reply_err(req, status); |
| 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | DIR* dir = opendir(path.c_str()); |
| 1526 | if (!dir) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1527 | fuse_reply_err(req, errno); |
| 1528 | return; |
| 1529 | } |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1530 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1531 | dirhandle* h = new dirhandle(dir); |
| 1532 | node->AddDirHandle(h); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1533 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1534 | fi->fh = ptr_to_id(h); |
| 1535 | fuse_reply_open(req, fi); |
| 1536 | } |
| 1537 | |
| 1538 | #define READDIR_BUF 8192LU |
| 1539 | |
| 1540 | static void do_readdir_common(fuse_req_t req, |
| 1541 | fuse_ino_t ino, |
| 1542 | size_t size, |
| 1543 | off_t off, |
| 1544 | struct fuse_file_info* fi, |
| 1545 | bool plus) { |
| 1546 | struct fuse* fuse = get_fuse(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1547 | dirhandle* h = reinterpret_cast<dirhandle*>(fi->fh); |
Jeff Sharkey | 7469b98 | 2019-08-28 16:51:02 -0600 | [diff] [blame] | 1548 | size_t len = std::min<size_t>(size, READDIR_BUF); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1549 | char buf[READDIR_BUF]; |
| 1550 | size_t used = 0; |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1551 | std::shared_ptr<DirectoryEntry> de; |
| 1552 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1553 | struct fuse_entry_param e; |
Sahana Rao | 6f9ea98 | 2019-09-20 12:21:33 +0100 | [diff] [blame] | 1554 | size_t entry_size = 0; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1555 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1556 | node* node = fuse->FromInode(ino); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1557 | if (!node) { |
| 1558 | fuse_reply_err(req, ENOENT); |
| 1559 | return; |
| 1560 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1561 | const string path = node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1562 | if (!is_app_accessible_path(fuse->mp, path, req->ctx.uid)) { |
| 1563 | fuse_reply_err(req, ENOENT); |
| 1564 | return; |
| 1565 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1566 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1567 | TRACE_NODE(node, req); |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1568 | // Get all directory entries from MediaProvider on first readdir() call of |
| 1569 | // directory handle. h->next_off = 0 indicates that current readdir() call |
| 1570 | // is first readdir() call for the directory handle, Avoid multiple JNI calls |
| 1571 | // for single directory handle. |
| 1572 | if (h->next_off == 0) { |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 1573 | h->de = fuse->mp->GetDirectoryEntries(req->ctx.uid, path, h->d); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1574 | } |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1575 | // If the last entry in the previous readdir() call was rejected due to |
| 1576 | // buffer capacity constraints, update directory offset to start from |
| 1577 | // previously rejected entry. Directory offset can also change if there was |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 1578 | // a seekdir() on the given directory handle. |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1579 | if (off != h->next_off) { |
| 1580 | h->next_off = off; |
| 1581 | } |
| 1582 | const int num_directory_entries = h->de.size(); |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 1583 | // Check for errors. Any error/exception occurred while obtaining directory |
| 1584 | // entries will be indicated by marking first directory entry name as empty |
| 1585 | // string. In the erroneous case corresponding d_type will hold error number. |
Sahana Rao | 53bd1f6 | 2019-12-27 20:18:39 +0000 | [diff] [blame] | 1586 | if (num_directory_entries && h->de[0]->d_name.empty()) { |
| 1587 | fuse_reply_err(req, h->de[0]->d_type); |
| 1588 | return; |
| 1589 | } |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1590 | |
Sahana Rao | 53bd1f6 | 2019-12-27 20:18:39 +0000 | [diff] [blame] | 1591 | while (h->next_off < num_directory_entries) { |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1592 | de = h->de[h->next_off]; |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1593 | entry_size = 0; |
| 1594 | h->next_off++; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1595 | if (plus) { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1596 | int error_code = 0; |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 1597 | if (do_lookup(req, ino, de->d_name.c_str(), &e, &error_code, FuseOp::readdir)) { |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1598 | entry_size = fuse_add_direntry_plus(req, buf + used, len - used, de->d_name.c_str(), |
| 1599 | &e, h->next_off); |
Sahana Rao | 8a588e7 | 2019-12-06 11:32:56 +0000 | [diff] [blame] | 1600 | } else { |
Sahana Rao | 8d8dbb4 | 2019-12-18 12:57:02 +0000 | [diff] [blame] | 1601 | // Ignore lookup errors on |
| 1602 | // 1. non-existing files returned from MediaProvider database. |
| 1603 | // 2. path that doesn't match FuseDaemon UID and calling uid. |
likai | d31e75b | 2020-12-09 16:14:28 +0800 | [diff] [blame] | 1604 | if (error_code == ENOENT || error_code == EPERM || error_code == EACCES |
| 1605 | || error_code == EIO) continue; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1606 | fuse_reply_err(req, error_code); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1607 | return; |
| 1608 | } |
| 1609 | } else { |
Zim | 1100f34 | 2020-05-15 16:13:00 +0100 | [diff] [blame] | 1610 | // This should never happen because we have readdir_plus enabled without adaptive |
| 1611 | // readdir_plus, FUSE_CAP_READDIRPLUS_AUTO |
| 1612 | LOG(WARNING) << "Handling plain readdir for " << de->d_name << ". Invalid d_ino"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1613 | e.attr.st_ino = FUSE_UNKNOWN_INO; |
Zim | 1100f34 | 2020-05-15 16:13:00 +0100 | [diff] [blame] | 1614 | e.attr.st_mode = de->d_type << 12; |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1615 | entry_size = fuse_add_direntry(req, buf + used, len - used, de->d_name.c_str(), &e.attr, |
| 1616 | h->next_off); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1617 | } |
Sahana Rao | 6f9ea98 | 2019-09-20 12:21:33 +0100 | [diff] [blame] | 1618 | // If buffer in fuse_add_direntry[_plus] is not large enough then |
| 1619 | // the entry is not added to buffer but the size of the entry is still |
| 1620 | // returned. Check available buffer size + returned entry size is less |
| 1621 | // than actual buffer size to confirm entry is added to buffer. |
Sahana Rao | 43927f0 | 2019-12-10 22:59:01 +0000 | [diff] [blame] | 1622 | if (used + entry_size > len) { |
| 1623 | // When an entry is rejected, lookup called by readdir_plus will not be tracked by |
| 1624 | // kernel. Call forget on the rejected node to decrement the reference count. |
Narayan Kamath | d621984 | 2019-12-27 17:44:35 +0000 | [diff] [blame] | 1625 | if (plus) { |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1626 | do_forget(req, fuse, e.ino, 1); |
Narayan Kamath | d621984 | 2019-12-27 17:44:35 +0000 | [diff] [blame] | 1627 | } |
Sahana Rao | 43927f0 | 2019-12-10 22:59:01 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
Sahana Rao | 6f9ea98 | 2019-09-20 12:21:33 +0100 | [diff] [blame] | 1630 | used += entry_size; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1631 | } |
Sahana Rao | 53bd1f6 | 2019-12-27 20:18:39 +0000 | [diff] [blame] | 1632 | fuse_reply_buf(req, buf, used); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static void pf_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, |
| 1636 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1637 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1638 | do_readdir_common(req, ino, size, off, fi, false); |
| 1639 | } |
| 1640 | |
| 1641 | static void pf_readdirplus(fuse_req_t req, |
| 1642 | fuse_ino_t ino, |
| 1643 | size_t size, |
| 1644 | off_t off, |
| 1645 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1646 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1647 | do_readdir_common(req, ino, size, off, fi, true); |
| 1648 | } |
| 1649 | |
| 1650 | static void pf_releasedir(fuse_req_t req, |
| 1651 | fuse_ino_t ino, |
| 1652 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1653 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1654 | struct fuse* fuse = get_fuse(req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1655 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1656 | node* node = fuse->FromInode(ino); |
| 1657 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1658 | dirhandle* h = reinterpret_cast<dirhandle*>(fi->fh); |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1659 | TRACE_NODE(node, req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1660 | if (node) { |
| 1661 | node->DestroyDirHandle(h); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1662 | } |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1663 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1664 | fuse_reply_err(req, 0); |
| 1665 | } |
| 1666 | |
| 1667 | static void pf_statfs(fuse_req_t req, fuse_ino_t ino) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1668 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1669 | struct statvfs st; |
| 1670 | struct fuse* fuse = get_fuse(req); |
| 1671 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1672 | if (statvfs(fuse->root->GetName().c_str(), &st)) |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1673 | fuse_reply_err(req, errno); |
| 1674 | else |
| 1675 | fuse_reply_statfs(req, &st); |
| 1676 | } |
| 1677 | /* |
| 1678 | static void pf_setxattr(fuse_req_t req, fuse_ino_t ino, const char* name, |
| 1679 | const char* value, size_t size, int flags) |
| 1680 | { |
| 1681 | cout << "TODO:" << __func__; |
| 1682 | } |
| 1683 | |
| 1684 | static void pf_getxattr(fuse_req_t req, fuse_ino_t ino, const char* name, |
| 1685 | size_t size) |
| 1686 | { |
| 1687 | cout << "TODO:" << __func__; |
| 1688 | } |
| 1689 | |
| 1690 | static void pf_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) |
| 1691 | { |
| 1692 | cout << "TODO:" << __func__; |
| 1693 | } |
| 1694 | |
| 1695 | static void pf_removexattr(fuse_req_t req, fuse_ino_t ino, const char* name) |
| 1696 | { |
| 1697 | cout << "TODO:" << __func__; |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1698 | }*/ |
| 1699 | |
| 1700 | static void pf_access(fuse_req_t req, fuse_ino_t ino, int mask) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1701 | ATRACE_CALL(); |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1702 | struct fuse* fuse = get_fuse(req); |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1703 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1704 | node* node = fuse->FromInode(ino); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1705 | if (!node) { |
| 1706 | fuse_reply_err(req, ENOENT); |
| 1707 | return; |
| 1708 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1709 | const string path = node->BuildPath(); |
Martijn Coenen | 19c1a87 | 2020-07-30 19:59:50 +0200 | [diff] [blame] | 1710 | if (path != "/storage/emulated" && !is_app_accessible_path(fuse->mp, path, req->ctx.uid)) { |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1711 | fuse_reply_err(req, ENOENT); |
| 1712 | return; |
| 1713 | } |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1714 | TRACE_NODE(node, req); |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1715 | |
Nandana Dutt | 1755505 | 2020-04-09 16:19:57 +0100 | [diff] [blame] | 1716 | // exists() checks are always allowed. |
| 1717 | if (mask == F_OK) { |
| 1718 | int res = access(path.c_str(), F_OK); |
| 1719 | fuse_reply_err(req, res ? errno : 0); |
| 1720 | return; |
| 1721 | } |
| 1722 | struct stat stat; |
| 1723 | if (lstat(path.c_str(), &stat)) { |
| 1724 | // File doesn't exist |
| 1725 | fuse_reply_err(req, ENOENT); |
| 1726 | return; |
| 1727 | } |
| 1728 | |
| 1729 | // For read and write permission checks we go to MediaProvider. |
| 1730 | int status = 0; |
Nandana Dutt | 5fc3201 | 2020-06-25 10:55:52 +0100 | [diff] [blame] | 1731 | bool for_write = mask & W_OK; |
Nandana Dutt | 1755505 | 2020-04-09 16:19:57 +0100 | [diff] [blame] | 1732 | bool is_directory = S_ISDIR(stat.st_mode); |
| 1733 | if (is_directory) { |
Martijn Coenen | 19c1a87 | 2020-07-30 19:59:50 +0200 | [diff] [blame] | 1734 | if (path == "/storage/emulated" && mask == X_OK) { |
| 1735 | // Special case for this path: apps should be allowed to enter it, |
| 1736 | // but not list directory contents (which would be user numbers). |
| 1737 | int res = access(path.c_str(), X_OK); |
| 1738 | fuse_reply_err(req, res ? errno : 0); |
| 1739 | return; |
| 1740 | } |
Nandana Dutt | 5fc3201 | 2020-06-25 10:55:52 +0100 | [diff] [blame] | 1741 | status = fuse->mp->IsOpendirAllowed(path, req->ctx.uid, for_write); |
Nandana Dutt | 1755505 | 2020-04-09 16:19:57 +0100 | [diff] [blame] | 1742 | } else { |
| 1743 | if (mask & X_OK) { |
| 1744 | // Fuse is mounted with MS_NOEXEC. |
| 1745 | fuse_reply_err(req, EACCES); |
| 1746 | return; |
| 1747 | } |
| 1748 | |
Zim | 73895ba | 2021-01-13 12:51:20 +0000 | [diff] [blame] | 1749 | std::unique_ptr<FileOpenResult> result = fuse->mp->OnFileOpen( |
Zim | 801d985 | 2021-01-26 18:30:53 +0000 | [diff] [blame] | 1750 | path, path, req->ctx.uid, req->ctx.pid, node->GetTransformsReason(), for_write, |
| 1751 | false /* redact */, false /* log_transforms_metrics */); |
Zim | 73895ba | 2021-01-13 12:51:20 +0000 | [diff] [blame] | 1752 | if (!result) { |
| 1753 | status = EFAULT; |
| 1754 | } |
| 1755 | |
| 1756 | if (result->status) { |
| 1757 | status = EACCES; |
| 1758 | } |
Nandana Dutt | 1755505 | 2020-04-09 16:19:57 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | fuse_reply_err(req, status); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1762 | } |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1763 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1764 | static void pf_create(fuse_req_t req, |
| 1765 | fuse_ino_t parent, |
| 1766 | const char* name, |
| 1767 | mode_t mode, |
| 1768 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1769 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1770 | struct fuse* fuse = get_fuse(req); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1771 | node* parent_node = fuse->FromInode(parent); |
Ricky Wai | f40c402 | 2020-04-15 19:00:06 +0100 | [diff] [blame] | 1772 | if (!parent_node) { |
| 1773 | fuse_reply_err(req, ENOENT); |
| 1774 | return; |
| 1775 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1776 | const string parent_path = parent_node->BuildPath(); |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1777 | if (!is_app_accessible_path(fuse->mp, parent_path, req->ctx.uid)) { |
| 1778 | fuse_reply_err(req, ENOENT); |
| 1779 | return; |
| 1780 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1781 | |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1782 | TRACE_NODE(parent_node, req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1783 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1784 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1785 | |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1786 | int mp_return_code = fuse->mp->InsertFile(child_path.c_str(), req->ctx.uid); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1787 | if (mp_return_code) { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1788 | fuse_reply_err(req, mp_return_code); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1789 | return; |
| 1790 | } |
| 1791 | |
Martijn Coenen | 2d7f1be | 2020-02-15 07:44:12 +0100 | [diff] [blame] | 1792 | // With the writeback cache enabled, FUSE may generate READ requests even for files that |
| 1793 | // were opened O_WRONLY; so make sure we open it O_RDWR instead. |
| 1794 | int open_flags = fi->flags; |
| 1795 | if (open_flags & O_WRONLY) { |
| 1796 | open_flags &= ~O_WRONLY; |
| 1797 | open_flags |= O_RDWR; |
| 1798 | } |
| 1799 | |
Zim | 1b14f2f | 2020-07-03 07:05:53 +0100 | [diff] [blame] | 1800 | if (open_flags & O_APPEND) { |
| 1801 | open_flags &= ~O_APPEND; |
| 1802 | } |
| 1803 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1804 | mode = (mode & (~0777)) | 0664; |
Martijn Coenen | 2d7f1be | 2020-02-15 07:44:12 +0100 | [diff] [blame] | 1805 | int fd = open(child_path.c_str(), open_flags, mode); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1806 | if (fd < 0) { |
| 1807 | int error_code = errno; |
| 1808 | // We've already inserted the file into the MP database before the |
| 1809 | // failed open(), so that needs to be rolled back here. |
Ricky Wai | 4467076 | 2020-05-01 11:25:28 +0100 | [diff] [blame] | 1810 | fuse->mp->DeleteFile(child_path.c_str(), req->ctx.uid); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1811 | fuse_reply_err(req, error_code); |
| 1812 | return; |
| 1813 | } |
| 1814 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1815 | int error_code = 0; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1816 | struct fuse_entry_param e; |
] | 3e9d56c | 2021-03-15 16:24:17 +0000 | [diff] [blame] | 1817 | node* node = |
| 1818 | make_node_entry(req, parent_node, name, child_path, &e, &error_code, FuseOp::create); |
Nandana Dutt | 154cb5d | 2020-06-04 11:53:31 +0100 | [diff] [blame] | 1819 | TRACE_NODE(node, req); |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1820 | if (!node) { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1821 | CHECK(error_code != 0); |
| 1822 | fuse_reply_err(req, error_code); |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1823 | return; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1824 | } |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1825 | |
Martijn Coenen | af2d34d | 2020-06-19 12:52:20 +0200 | [diff] [blame] | 1826 | // Let MediaProvider know we've created a new file |
| 1827 | fuse->mp->OnFileCreated(child_path); |
| 1828 | |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1829 | // TODO(b/147274248): Assume there will be no EXIF to redact. |
| 1830 | // This prevents crashing during reads but can be a security hole if a malicious app opens an fd |
| 1831 | // to the file before all the EXIF content is written. We could special case reads before the |
| 1832 | // first close after a file has just been created. |
Zim | 329ba2c | 2020-09-16 14:23:26 +0100 | [diff] [blame] | 1833 | int keep_cache = 1; |
Manish Singh | 9a6ccce | 2021-02-05 23:50:08 +0000 | [diff] [blame] | 1834 | handle* h = create_handle_for_node(fuse, child_path, fd, req->ctx.uid, 0 /* transforms_uid */, |
| 1835 | node, new RedactionInfo(), &keep_cache); |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1836 | fi->fh = ptr_to_id(h); |
Zim | 329ba2c | 2020-09-16 14:23:26 +0100 | [diff] [blame] | 1837 | fi->keep_cache = keep_cache; |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1838 | fi->direct_io = !h->cached; |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 1839 | |
| 1840 | // TODO(b/173190192) ensuring that h->cached must be enabled in order to |
| 1841 | // user FUSE passthrough is a conservative rule and might be dropped as |
| 1842 | // soon as demonstrated its correctness. |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1843 | if (h->passthrough) { |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 1844 | if (!do_passthrough_enable(req, fi, fd)) { |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1845 | PLOG(ERROR) << "Passthrough CREATE failed for " << child_path; |
| 1846 | fuse_reply_err(req, EFAULT); |
| 1847 | return; |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | |
Zim | c05c60f | 2020-03-05 13:02:26 +0000 | [diff] [blame] | 1851 | fuse_reply_create(req, &e, fi); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1852 | } |
| 1853 | /* |
| 1854 | static void pf_getlk(fuse_req_t req, fuse_ino_t ino, |
| 1855 | struct fuse_file_info* fi, struct flock* lock) |
| 1856 | { |
| 1857 | cout << "TODO:" << __func__; |
| 1858 | } |
| 1859 | |
| 1860 | static void pf_setlk(fuse_req_t req, fuse_ino_t ino, |
| 1861 | struct fuse_file_info* fi, |
| 1862 | struct flock* lock, int sleep) |
| 1863 | { |
| 1864 | cout << "TODO:" << __func__; |
| 1865 | } |
| 1866 | |
| 1867 | static void pf_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, |
| 1868 | uint64_t idx) |
| 1869 | { |
| 1870 | cout << "TODO:" << __func__; |
| 1871 | } |
| 1872 | |
| 1873 | static void pf_ioctl(fuse_req_t req, fuse_ino_t ino, unsigned int cmd, |
| 1874 | void* arg, struct fuse_file_info* fi, unsigned flags, |
| 1875 | const void* in_buf, size_t in_bufsz, size_t out_bufsz) |
| 1876 | { |
| 1877 | cout << "TODO:" << __func__; |
| 1878 | } |
| 1879 | |
| 1880 | static void pf_poll(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi, |
| 1881 | struct fuse_pollhandle* ph) |
| 1882 | { |
| 1883 | cout << "TODO:" << __func__; |
| 1884 | } |
| 1885 | |
| 1886 | static void pf_retrieve_reply(fuse_req_t req, void* cookie, fuse_ino_t ino, |
| 1887 | off_t offset, struct fuse_bufvec* bufv) |
| 1888 | { |
| 1889 | cout << "TODO:" << __func__; |
| 1890 | } |
| 1891 | |
| 1892 | static void pf_flock(fuse_req_t req, fuse_ino_t ino, |
| 1893 | struct fuse_file_info* fi, int op) |
| 1894 | { |
| 1895 | cout << "TODO:" << __func__; |
| 1896 | } |
| 1897 | |
| 1898 | static void pf_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, |
| 1899 | off_t offset, off_t length, struct fuse_file_info* fi) |
| 1900 | { |
| 1901 | cout << "TODO:" << __func__; |
| 1902 | } |
| 1903 | */ |
| 1904 | |
| 1905 | static struct fuse_lowlevel_ops ops{ |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 1906 | .init = pf_init, .destroy = pf_destroy, .lookup = pf_lookup, .forget = pf_forget, |
| 1907 | .getattr = pf_getattr, .setattr = pf_setattr, .canonical_path = pf_canonical_path, |
| 1908 | .mknod = pf_mknod, .mkdir = pf_mkdir, .unlink = pf_unlink, .rmdir = pf_rmdir, |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 1909 | /*.symlink = pf_symlink,*/ |
| 1910 | .rename = pf_rename, |
| 1911 | /*.link = pf_link,*/ |
| 1912 | .open = pf_open, .read = pf_read, |
| 1913 | /*.write = pf_write,*/ |
Alessio Balsini | 68c295d | 2020-07-23 14:32:01 +0100 | [diff] [blame] | 1914 | /*.flush = pf_flush,*/ |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 1915 | .release = pf_release, .fsync = pf_fsync, .opendir = pf_opendir, .readdir = pf_readdir, |
| 1916 | .releasedir = pf_releasedir, .fsyncdir = pf_fsyncdir, .statfs = pf_statfs, |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 1917 | /*.setxattr = pf_setxattr, |
| 1918 | .getxattr = pf_getxattr, |
| 1919 | .listxattr = pf_listxattr, |
| 1920 | .removexattr = pf_removexattr,*/ |
| 1921 | .access = pf_access, .create = pf_create, |
| 1922 | /*.getlk = pf_getlk, |
| 1923 | .setlk = pf_setlk, |
| 1924 | .bmap = pf_bmap, |
| 1925 | .ioctl = pf_ioctl, |
Nick Desaulniers | 17714b8 | 2019-11-05 09:44:35 -0800 | [diff] [blame] | 1926 | .poll = pf_poll,*/ |
| 1927 | .write_buf = pf_write_buf, |
| 1928 | /*.retrieve_reply = pf_retrieve_reply,*/ |
| 1929 | .forget_multi = pf_forget_multi, |
Zim | 9e3c40b | 2021-04-09 15:51:15 +0100 | [diff] [blame] | 1930 | /*.flock = pf_flock,*/ |
| 1931 | .fallocate = pf_fallocate, |
Nick Desaulniers | 17714b8 | 2019-11-05 09:44:35 -0800 | [diff] [blame] | 1932 | .readdirplus = pf_readdirplus, |
| 1933 | /*.copy_file_range = pf_copy_file_range,*/ |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1934 | }; |
| 1935 | |
| 1936 | static struct fuse_loop_config config = { |
Zim | 549f9da | 2020-02-09 13:54:22 +0000 | [diff] [blame] | 1937 | .clone_fd = 1, |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1938 | .max_idle_threads = 10, |
| 1939 | }; |
| 1940 | |
Zim | b353eb4 | 2019-12-17 15:29:48 +0000 | [diff] [blame] | 1941 | static std::unordered_map<enum fuse_log_level, enum android_LogPriority> fuse_to_android_loglevel({ |
| 1942 | {FUSE_LOG_EMERG, ANDROID_LOG_FATAL}, |
| 1943 | {FUSE_LOG_ALERT, ANDROID_LOG_ERROR}, |
| 1944 | {FUSE_LOG_CRIT, ANDROID_LOG_ERROR}, |
| 1945 | {FUSE_LOG_ERR, ANDROID_LOG_ERROR}, |
| 1946 | {FUSE_LOG_WARNING, ANDROID_LOG_WARN}, |
| 1947 | {FUSE_LOG_NOTICE, ANDROID_LOG_INFO}, |
| 1948 | {FUSE_LOG_INFO, ANDROID_LOG_DEBUG}, |
| 1949 | {FUSE_LOG_DEBUG, ANDROID_LOG_VERBOSE}, |
| 1950 | }); |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 1951 | |
| 1952 | static void fuse_logger(enum fuse_log_level level, const char* fmt, va_list ap) { |
Zim | 357e307 | 2020-01-15 10:50:01 +0000 | [diff] [blame] | 1953 | __android_log_vprint(fuse_to_android_loglevel.at(level), LIBFUSE_LOG_TAG, fmt, ap); |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1956 | bool FuseDaemon::ShouldOpenWithFuse(int fd, bool for_read, const std::string& path) { |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 1957 | if (fuse->passthrough) { |
| 1958 | // Always open with FUSE if passthrough is enabled. This avoids the delicate file lock |
| 1959 | // acquisition below to ensure VFS cache consistency and doesn't impact filesystem |
| 1960 | // performance since read(2)/write(2) happen in the kernel |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1964 | bool use_fuse = false; |
| 1965 | |
| 1966 | if (active.load(std::memory_order_acquire)) { |
Zim | dc78f53 | 2020-03-04 18:46:06 +0000 | [diff] [blame] | 1967 | std::lock_guard<std::recursive_mutex> guard(fuse->lock); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1968 | const node* node = node::LookupAbsolutePath(fuse->root, path); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1969 | if (node && node->HasCachedHandle()) { |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1970 | use_fuse = true; |
| 1971 | } else { |
| 1972 | // If we are unable to set a lock, we should use fuse since we can't track |
| 1973 | // when all fd references (including dups) are closed. This can happen when |
| 1974 | // we try to set a write lock twice on the same file |
| 1975 | use_fuse = set_file_lock(fd, for_read, path); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1976 | } |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1977 | } else { |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 1978 | LOG(WARNING) << "FUSE daemon is inactive. Cannot open file with FUSE"; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | return use_fuse; |
| 1982 | } |
| 1983 | |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 1984 | void FuseDaemon::InvalidateFuseDentryCache(const std::string& path) { |
Zim | 4d3fa27 | 2020-03-06 13:45:19 +0000 | [diff] [blame] | 1985 | LOG(VERBOSE) << "Invalidating FUSE dentry cache"; |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 1986 | if (active.load(std::memory_order_acquire)) { |
Zim | a55568e | 2020-04-24 22:40:34 +0100 | [diff] [blame] | 1987 | string name; |
| 1988 | fuse_ino_t parent; |
Zim | 33aea10 | 2020-06-19 15:49:47 +0100 | [diff] [blame] | 1989 | fuse_ino_t child; |
Zim | a55568e | 2020-04-24 22:40:34 +0100 | [diff] [blame] | 1990 | { |
| 1991 | std::lock_guard<std::recursive_mutex> guard(fuse->lock); |
| 1992 | const node* node = node::LookupAbsolutePath(fuse->root, path); |
| 1993 | if (node) { |
| 1994 | name = node->GetName(); |
Zim | 33aea10 | 2020-06-19 15:49:47 +0100 | [diff] [blame] | 1995 | child = fuse->ToInode(const_cast<class node*>(node)); |
Zim | a55568e | 2020-04-24 22:40:34 +0100 | [diff] [blame] | 1996 | parent = fuse->ToInode(node->GetParent()); |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 1997 | } |
| 1998 | } |
Zim | a55568e | 2020-04-24 22:40:34 +0100 | [diff] [blame] | 1999 | |
Zim | 33aea10 | 2020-06-19 15:49:47 +0100 | [diff] [blame] | 2000 | if (!name.empty()) { |
| 2001 | fuse_inval(fuse->se, parent, child, name, path); |
Zim | a55568e | 2020-04-24 22:40:34 +0100 | [diff] [blame] | 2002 | } |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 2003 | } else { |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 2004 | LOG(WARNING) << "FUSE daemon is inactive. Cannot invalidate dentry"; |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 2008 | FuseDaemon::FuseDaemon(JNIEnv* env, jobject mediaProvider) : mp(env, mediaProvider), |
| 2009 | active(false), fuse(nullptr) {} |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2010 | |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 2011 | bool FuseDaemon::IsStarted() const { |
| 2012 | return active.load(std::memory_order_acquire); |
| 2013 | } |
| 2014 | |
Martijn Coenen | 083eb69 | 2020-04-24 09:39:58 +0200 | [diff] [blame] | 2015 | void FuseDaemon::Start(android::base::unique_fd fd, const std::string& path) { |
Nikita Ioffe | c33ee1a | 2020-06-23 21:31:20 +0100 | [diff] [blame] | 2016 | android::base::SetDefaultTag(LOG_TAG); |
| 2017 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2018 | struct fuse_args args; |
| 2019 | struct fuse_cmdline_opts opts; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2020 | |
| 2021 | struct stat stat; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2022 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 2023 | if (lstat(path.c_str(), &stat)) { |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 2024 | PLOG(ERROR) << "ERROR: failed to stat source " << path; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 2025 | return; |
| 2026 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2027 | |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 2028 | if (!S_ISDIR(stat.st_mode)) { |
Zimuzo Ezeozue | 67db40c | 2020-02-21 19:41:33 +0000 | [diff] [blame] | 2029 | PLOG(ERROR) << "ERROR: source is not a directory"; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 2030 | return; |
| 2031 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2032 | |
| 2033 | args = FUSE_ARGS_INIT(0, nullptr); |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 2034 | if (fuse_opt_add_arg(&args, path.c_str()) || fuse_opt_add_arg(&args, "-odebug") || |
| 2035 | fuse_opt_add_arg(&args, ("-omax_read=" + std::to_string(MAX_READ_SIZE)).c_str())) { |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 2036 | LOG(ERROR) << "ERROR: failed to set options"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2037 | return; |
| 2038 | } |
| 2039 | |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 2040 | struct fuse fuse_default(path, stat.st_ino); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 2041 | fuse_default.mp = ∓ |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 2042 | // fuse_default is stack allocated, but it's safe to save it as an instance variable because |
| 2043 | // this method blocks and FuseDaemon#active tells if we are currently blocking |
| 2044 | fuse = &fuse_default; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 2045 | |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 2046 | // Used by pf_read: redacted ranges are represented by zeroized ranges of bytes, |
| 2047 | // so we mmap the maximum length of redacted ranges in the beginning and save memory allocations |
| 2048 | // on each read. |
| 2049 | fuse_default.zero_addr = static_cast<char*>(mmap( |
| 2050 | NULL, MAX_READ_SIZE, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, /*fd*/ -1, /*off*/ 0)); |
| 2051 | if (fuse_default.zero_addr == MAP_FAILED) { |
| 2052 | LOG(FATAL) << "mmap failed - could not start fuse! errno = " << errno; |
| 2053 | } |
| 2054 | |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 2055 | // Custom logging for libfuse |
Zim | 0594b03 | 2020-04-29 11:43:43 +0100 | [diff] [blame] | 2056 | if (android::base::GetBoolProperty("persist.sys.fuse.log", false)) { |
| 2057 | fuse_set_log_func(fuse_logger); |
| 2058 | } |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 2059 | |
Zim | 3cde266 | 2021-02-22 21:20:33 +0000 | [diff] [blame] | 2060 | if (MY_USER_ID != 0 && mp.IsAppCloneUser(MY_USER_ID)) { |
Martijn Coenen | 8713309 | 2020-10-14 17:00:28 +0200 | [diff] [blame] | 2061 | // Disable dentry caching for the app clone user |
| 2062 | fuse->disable_dentry_cache = true; |
| 2063 | } |
| 2064 | |
Alessio Balsini | b1c366b | 2020-11-26 15:15:14 +0000 | [diff] [blame] | 2065 | fuse->passthrough = android::base::GetBoolProperty("persist.sys.fuse.passthrough.enable", false); |
| 2066 | if (fuse->passthrough) { |
| 2067 | LOG(INFO) << "Using FUSE passthrough"; |
| 2068 | } |
Alessio Balsini | f220a96 | 2020-07-13 12:01:13 +0100 | [diff] [blame] | 2069 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2070 | struct fuse_session |
| 2071 | * se = fuse_session_new(&args, &ops, sizeof(ops), &fuse_default); |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 2072 | if (!se) { |
| 2073 | PLOG(ERROR) << "Failed to create session "; |
| 2074 | return; |
| 2075 | } |
Zim | a76c349 | 2020-02-19 01:23:26 +0000 | [diff] [blame] | 2076 | fuse_default.se = se; |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 2077 | fuse_default.active = &active; |
Martijn Coenen | 3ec6c06 | 2020-04-28 19:09:53 +0200 | [diff] [blame] | 2078 | se->fd = fd.release(); // libfuse owns the FD now |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 2079 | se->mountpoint = strdup(path.c_str()); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 2080 | |
| 2081 | // Single thread. Useful for debugging |
| 2082 | // fuse_session_loop(se); |
| 2083 | // Multi-threaded |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 2084 | LOG(INFO) << "Starting fuse..."; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2085 | fuse_session_loop_mt(se, &config); |
Zim | d0435b2 | 2020-03-05 13:52:51 +0000 | [diff] [blame] | 2086 | fuse->active->store(false, std::memory_order_release); |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 2087 | LOG(INFO) << "Ending fuse..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 2088 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 2089 | if (munmap(fuse_default.zero_addr, MAX_READ_SIZE)) { |
| 2090 | PLOG(ERROR) << "munmap failed!"; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 2091 | } |
| 2092 | |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 2093 | fuse_opt_free_args(&args); |
| 2094 | fuse_session_destroy(se); |
| 2095 | LOG(INFO) << "Ended fuse"; |
| 2096 | return; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2097 | } |
Biswarup Pal | 93f4ec1 | 2021-02-15 13:39:36 +0000 | [diff] [blame] | 2098 | |
| 2099 | const string FuseDaemon::GetOriginalMediaFormatFilePath(int fd) const { |
| 2100 | struct stat s; |
| 2101 | memset(&s, 0, sizeof(s)); |
| 2102 | if (fstat(fd, &s) < 0) { |
| 2103 | PLOG(DEBUG) << "GetOriginalMediaFormatFilePath fstat failed."; |
| 2104 | return string(); |
| 2105 | } |
| 2106 | |
| 2107 | ino_t ino = s.st_ino; |
| 2108 | dev_t dev = s.st_dev; |
| 2109 | |
| 2110 | dev_t fuse_dev = fuse->dev.load(std::memory_order_acquire); |
| 2111 | if (dev != fuse_dev) { |
| 2112 | PLOG(DEBUG) << "GetOriginalMediaFormatFilePath FUSE device id does not match."; |
| 2113 | return string(); |
| 2114 | } |
| 2115 | |
| 2116 | const node* node = node::LookupInode(fuse->root, ino); |
| 2117 | if (!node) { |
| 2118 | PLOG(DEBUG) << "GetOriginalMediaFormatFilePath no node found with given ino"; |
| 2119 | return string(); |
| 2120 | } |
| 2121 | |
| 2122 | return node->BuildPath(); |
| 2123 | } |
| 2124 | |
| 2125 | void FuseDaemon::InitializeDeviceId(const std::string& path) { |
| 2126 | struct stat stat; |
| 2127 | |
| 2128 | if (lstat(path.c_str(), &stat)) { |
| 2129 | PLOG(ERROR) << "InitializeDeviceId failed to stat given path " << path; |
| 2130 | return; |
| 2131 | } |
| 2132 | |
| 2133 | fuse->dev.store(stat.st_dev, std::memory_order_release); |
| 2134 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 2135 | } //namespace fuse |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 2136 | } // namespace mediaprovider |