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