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 | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 22 | #include <android/log.h> |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 23 | #include <android/trace.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 24 | #include <ctype.h> |
| 25 | #include <dirent.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 28 | #include <fuse_i.h> |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 29 | #include <fuse_log.h> |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 30 | #include <fuse_lowlevel.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 31 | #include <inttypes.h> |
| 32 | #include <limits.h> |
| 33 | #include <linux/fuse.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 34 | #include <stdbool.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <sys/inotify.h> |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 39 | #include <sys/mman.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 40 | #include <sys/mount.h> |
| 41 | #include <sys/param.h> |
| 42 | #include <sys/resource.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/statfs.h> |
| 45 | #include <sys/statvfs.h> |
| 46 | #include <sys/time.h> |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/uio.h> |
| 49 | #include <unistd.h> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 50 | |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 51 | #include <iostream> |
Narayan Kamath | 8d76d0c | 2019-12-31 13:14:50 +0000 | [diff] [blame] | 52 | #include <list> |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 53 | #include <map> |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 54 | #include <mutex> |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 55 | #include <queue> |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 56 | #include <regex> |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 57 | #include <thread> |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 58 | #include <unordered_map> |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 59 | #include <unordered_set> |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 60 | #include <vector> |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 61 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 62 | #include "MediaProviderWrapper.h" |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 63 | #include "libfuse_jni/ReaddirHelper.h" |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 64 | #include "libfuse_jni/RedactionInfo.h" |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 65 | #include "node-inl.h" |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 66 | |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 67 | using mediaprovider::fuse::DirectoryEntry; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 68 | using mediaprovider::fuse::dirhandle; |
| 69 | using mediaprovider::fuse::handle; |
| 70 | using mediaprovider::fuse::node; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 71 | using mediaprovider::fuse::RedactionInfo; |
Narayan Kamath | 8d76d0c | 2019-12-31 13:14:50 +0000 | [diff] [blame] | 72 | using std::list; |
Jeff Sharkey | 7469b98 | 2019-08-28 16:51:02 -0600 | [diff] [blame] | 73 | using std::string; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 74 | using std::vector; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 75 | |
Narayan Kamath | 88203dc | 2019-08-30 17:19:38 +0100 | [diff] [blame] | 76 | // logging macros to avoid duplication. |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 77 | #define TRACE LOG(DEBUG) |
Zim | 357e307 | 2020-01-15 10:50:01 +0000 | [diff] [blame] | 78 | #define TRACE_VERBOSE LOG(VERBOSE) |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 79 | #define TRACE_FUSE(__fuse) TRACE << "[" << __fuse->path << "] " |
Zim | 357e307 | 2020-01-15 10:50:01 +0000 | [diff] [blame] | 80 | #define TRACE_FUSE_VERBOSE(__fuse) TRACE_VERBOSE << "[" << __fuse->path << "] " |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 81 | |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 82 | #define ATRACE_NAME(name) ScopedTrace ___tracer(name) |
| 83 | #define ATRACE_CALL() ATRACE_NAME(__FUNCTION__) |
| 84 | |
| 85 | class ScopedTrace { |
| 86 | public: |
Narayan Kamath | 64b4435 | 2019-12-27 17:07:50 +0000 | [diff] [blame] | 87 | explicit inline ScopedTrace(const char *name) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 88 | ATrace_beginSection(name); |
| 89 | } |
| 90 | |
| 91 | inline ~ScopedTrace() { |
| 92 | ATrace_endSection(); |
| 93 | } |
| 94 | }; |
| 95 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 96 | #define FUSE_UNKNOWN_INO 0xffffffff |
| 97 | |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 98 | constexpr size_t MAX_READ_SIZE = 128 * 1024; |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 99 | // Stolen from: UserHandle#getUserId |
| 100 | constexpr int PER_USER_RANGE = 100000; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 101 | |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 102 | /* |
| 103 | * In order to avoid double caching with fuse, call fadvise on the file handles |
| 104 | * in the underlying file system. However, if this is done on every read/write, |
| 105 | * the fadvises cause a very significant slowdown in tests (specifically fio |
| 106 | * seq_write). So call fadvise on the file handles with the most reads/writes |
| 107 | * only after a threshold is passed. |
| 108 | */ |
| 109 | class FAdviser { |
| 110 | public: |
| 111 | FAdviser() : thread_(MessageLoop, this), total_size_(0) {} |
| 112 | |
| 113 | ~FAdviser() { |
| 114 | SendMessage(Message::quit); |
| 115 | thread_.join(); |
| 116 | } |
| 117 | |
| 118 | void Record(int fd, size_t size) { SendMessage(Message::record, fd, size); } |
| 119 | |
| 120 | void Close(int fd) { SendMessage(Message::close, fd); } |
| 121 | |
| 122 | private: |
| 123 | std::thread thread_; |
| 124 | |
| 125 | struct Message { |
| 126 | enum Type { record, close, quit }; |
| 127 | Type type; |
| 128 | int fd; |
| 129 | size_t size; |
| 130 | }; |
| 131 | |
| 132 | void RecordImpl(int fd, size_t size) { |
| 133 | total_size_ += size; |
| 134 | |
| 135 | // Find or create record in files_ |
| 136 | // Remove record from sizes_ if it exists, adjusting size appropriately |
| 137 | auto file = files_.find(fd); |
| 138 | if (file != files_.end()) { |
| 139 | auto old_size = file->second; |
| 140 | size += old_size->first; |
| 141 | sizes_.erase(old_size); |
| 142 | } else { |
| 143 | file = files_.insert(Files::value_type(fd, sizes_.end())).first; |
| 144 | } |
| 145 | |
| 146 | // Now (re) insert record in sizes_ |
| 147 | auto new_size = sizes_.insert(Sizes::value_type(size, fd)); |
| 148 | file->second = new_size; |
| 149 | |
| 150 | if (total_size_ < threshold_) return; |
| 151 | |
| 152 | LOG(INFO) << "Threshold exceeded - fadvising " << total_size_; |
| 153 | while (!sizes_.empty() && total_size_ > target_) { |
| 154 | auto size = --sizes_.end(); |
| 155 | total_size_ -= size->first; |
| 156 | posix_fadvise(size->second, 0, 0, POSIX_FADV_DONTNEED); |
| 157 | files_.erase(size->second); |
| 158 | sizes_.erase(size); |
| 159 | } |
| 160 | LOG(INFO) << "Threshold now " << total_size_; |
| 161 | } |
| 162 | |
| 163 | void CloseImpl(int fd) { |
| 164 | auto file = files_.find(fd); |
| 165 | if (file == files_.end()) return; |
| 166 | |
| 167 | total_size_ -= file->second->first; |
| 168 | sizes_.erase(file->second); |
| 169 | files_.erase(file); |
| 170 | } |
| 171 | |
| 172 | void MessageLoopImpl() { |
| 173 | while (1) { |
| 174 | Message message; |
| 175 | |
| 176 | { |
| 177 | std::unique_lock<std::mutex> lock(mutex_); |
| 178 | cv_.wait(lock, [this] { return !queue_.empty(); }); |
| 179 | message = queue_.front(); |
| 180 | queue_.pop(); |
| 181 | } |
| 182 | |
| 183 | switch (message.type) { |
| 184 | case Message::record: |
| 185 | RecordImpl(message.fd, message.size); |
| 186 | break; |
| 187 | |
| 188 | case Message::close: |
| 189 | CloseImpl(message.fd); |
| 190 | break; |
| 191 | |
| 192 | case Message::quit: |
| 193 | return; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static int MessageLoop(FAdviser* ptr) { |
| 199 | ptr->MessageLoopImpl(); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | void SendMessage(Message::Type type, int fd = -1, size_t size = 0) { |
| 204 | { |
| 205 | std::unique_lock<std::mutex> lock(mutex_); |
| 206 | Message message = {type, fd, size}; |
| 207 | queue_.push(message); |
| 208 | } |
| 209 | cv_.notify_one(); |
| 210 | } |
| 211 | |
| 212 | std::queue<Message> queue_; |
| 213 | std::mutex mutex_; |
| 214 | std::condition_variable cv_; |
| 215 | |
| 216 | typedef std::multimap<size_t, int> Sizes; |
| 217 | typedef std::map<int, Sizes::iterator> Files; |
| 218 | |
| 219 | Files files_; |
| 220 | Sizes sizes_; |
| 221 | size_t total_size_; |
| 222 | |
| 223 | const size_t threshold_ = 64 * 1024 * 1024; |
| 224 | const size_t target_ = 32 * 1024 * 1024; |
| 225 | }; |
| 226 | |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 227 | // Whether inode tracking is enabled or not. When enabled, we maintain a |
| 228 | // separate mapping from inode numbers to "live" nodes so we can detect when |
| 229 | // we receive a request to a node that has been deleted. |
| 230 | static constexpr bool kEnableInodeTracking = true; |
| 231 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 232 | /* Single FUSE mount */ |
| 233 | struct fuse { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 234 | explicit fuse(const std::string& _path) |
| 235 | : path(_path), root(node::CreateRoot(_path, &lock)), mp(0), zero_addr(0) {} |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 236 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 237 | inline bool IsRoot(const node* node) const { return node == root; } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 238 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 239 | // Note that these two (FromInode / ToInode) conversion wrappers are required |
| 240 | // because fuse_lowlevel_ops documents that the root inode is always one |
| 241 | // (see FUSE_ROOT_ID in fuse_lowlevel.h). There are no particular requirements |
| 242 | // on any of the other inodes in the FS. |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 243 | inline node* FromInode(__u64 inode) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 244 | if (inode == FUSE_ROOT_ID) { |
| 245 | return root; |
| 246 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 247 | |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 248 | node* node = node::FromInode(inode); |
| 249 | |
| 250 | if (kEnableInodeTracking) { |
| 251 | std::lock_guard<std::recursive_mutex> guard(lock); |
| 252 | CHECK(inode_tracker_.find(node) != inode_tracker_.end()); |
| 253 | } |
| 254 | |
| 255 | return node; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | inline __u64 ToInode(node* node) const { |
| 259 | if (IsRoot(node)) { |
| 260 | return FUSE_ROOT_ID; |
| 261 | } |
| 262 | |
| 263 | return node::ToInode(node); |
| 264 | } |
| 265 | |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 266 | // Notify this FUSE instance that one of its nodes has been deleted. |
| 267 | void NodeDeleted(const node* node) { |
| 268 | if (kEnableInodeTracking) { |
| 269 | LOG(INFO) << "Node: " << node << " deleted."; |
| 270 | |
| 271 | std::lock_guard<std::recursive_mutex> guard(lock); |
| 272 | inode_tracker_.erase(node); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Notify this FUSE instance that a new nodes has been created. |
| 277 | void NodeCreated(const node* node) { |
| 278 | if (kEnableInodeTracking) { |
| 279 | LOG(INFO) << "Node: " << node << " created."; |
| 280 | |
| 281 | std::lock_guard<std::recursive_mutex> guard(lock); |
| 282 | inode_tracker_.insert(node); |
| 283 | } |
| 284 | } |
| 285 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 286 | std::recursive_mutex lock; |
| 287 | const string path; |
| 288 | node* const root; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Used to make JNI calls to MediaProvider. |
| 292 | * Responsibility of freeing this object falls on corresponding |
| 293 | * FuseDaemon object. |
| 294 | */ |
| 295 | mediaprovider::fuse::MediaProviderWrapper* mp; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * Points to a range of zeroized bytes, used by pf_read to represent redacted ranges. |
| 299 | * The memory is read only and should never be modified. |
| 300 | */ |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 301 | /* const */ char* zero_addr; |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 302 | |
| 303 | FAdviser fadviser; |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 304 | |
| 305 | std::unordered_set<const node*> inode_tracker_; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 306 | }; |
| 307 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 308 | static inline const char* safe_name(node* n) { |
| 309 | return n ? n->GetName().c_str() : "?"; |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 310 | } |
| 311 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 312 | static inline __u64 ptr_to_id(void* ptr) { |
| 313 | return (__u64)(uintptr_t) ptr; |
| 314 | } |
| 315 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 316 | /* |
| 317 | * Set an F_RDLCK or F_WRLCKK on fd with fcntl(2). |
| 318 | * |
| 319 | * This is called before the MediaProvider returns fd from the lower file |
| 320 | * system to an app over the ContentResolver interface. This allows us |
| 321 | * check with is_file_locked if any reference to that fd is still open. |
| 322 | */ |
| 323 | static int set_file_lock(int fd, bool for_read, const std::string& path) { |
| 324 | std::string lock_str = (for_read ? "read" : "write"); |
Zim | 5c57d83 | 2020-01-15 16:16:12 +0000 | [diff] [blame] | 325 | TRACE_VERBOSE << "Setting " << lock_str << " lock for path " << path; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 326 | |
| 327 | struct flock fl{}; |
| 328 | fl.l_type = for_read ? F_RDLCK : F_WRLCK; |
| 329 | fl.l_whence = SEEK_SET; |
| 330 | |
| 331 | int res = fcntl(fd, F_OFD_SETLK, &fl); |
| 332 | if (res) { |
| 333 | PLOG(ERROR) << "Failed to set " << lock_str << " lock on path " << path; |
| 334 | return res; |
| 335 | } |
Zim | 5c57d83 | 2020-01-15 16:16:12 +0000 | [diff] [blame] | 336 | TRACE_VERBOSE << "Successfully set " << lock_str << " lock on path " << path; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 337 | return res; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Check if an F_RDLCK or F_WRLCK is set on fd with fcntl(2). |
| 342 | * |
| 343 | * This is used to determine if the MediaProvider has given an fd to the lower fs to an app over |
| 344 | * the ContentResolver interface. Before that happens, we always call set_file_lock on the file |
| 345 | * allowing us to know if any reference to that fd is still open here. |
| 346 | * |
| 347 | * Returns true if fd may have a lock, false otherwise |
| 348 | */ |
| 349 | static bool is_file_locked(int fd, const std::string& path) { |
Zim | 5c57d83 | 2020-01-15 16:16:12 +0000 | [diff] [blame] | 350 | TRACE_VERBOSE << "Checking if file is locked " << path; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 351 | |
| 352 | struct flock fl{}; |
| 353 | fl.l_type = F_WRLCK; |
| 354 | fl.l_whence = SEEK_SET; |
| 355 | |
| 356 | int res = fcntl(fd, F_OFD_GETLK, &fl); |
| 357 | if (res) { |
| 358 | PLOG(ERROR) << "Failed to check lock for file " << path; |
| 359 | // Assume worst |
| 360 | return true; |
| 361 | } |
| 362 | bool locked = fl.l_type != F_UNLCK; |
Zim | 5c57d83 | 2020-01-15 16:16:12 +0000 | [diff] [blame] | 363 | TRACE_VERBOSE << "File " << path << " is " << (locked ? "locked" : "unlocked"); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 364 | return locked; |
| 365 | } |
| 366 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 367 | static struct fuse* get_fuse(fuse_req_t req) { |
| 368 | return reinterpret_cast<struct fuse*>(fuse_req_userdata(req)); |
| 369 | } |
| 370 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 371 | static node* make_node_entry(fuse_req_t req, node* parent, const string& name, const string& path, |
| 372 | struct fuse_entry_param* e, int* error_code) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 373 | struct fuse* fuse = get_fuse(req); |
Martijn Coenen | b8ff4eb | 2019-12-17 09:55:17 +0100 | [diff] [blame] | 374 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 375 | node* node; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 376 | |
Jeff Sharkey | 7469b98 | 2019-08-28 16:51:02 -0600 | [diff] [blame] | 377 | memset(e, 0, sizeof(*e)); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 378 | if (lstat(path.c_str(), &e->attr) < 0) { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 379 | *error_code = errno; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 380 | return NULL; |
| 381 | } |
| 382 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 383 | node = parent->LookupChildByName(name); |
| 384 | if (node) { |
| 385 | node->Acquire(); |
| 386 | } else { |
| 387 | node = ::node::Create(parent, name, &fuse->lock); |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 388 | fuse->NodeCreated(node); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 389 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 390 | |
| 391 | // Manipulate attr here if needed |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 392 | e->attr_timeout = 10; |
Martijn Coenen | b8ff4eb | 2019-12-17 09:55:17 +0100 | [diff] [blame] | 393 | int user_id = ctx->uid / PER_USER_RANGE; |
| 394 | const std::string android_path = fuse->path + "/" + std::to_string(user_id) + "/Android"; |
Zim | 6d0a3f1 | 2019-12-13 10:48:03 +0000 | [diff] [blame] | 395 | // Ensure the VFS does not cache dentries, if it caches, the following scenario could occur: |
| 396 | // 1. Process A has access to file A and does a lookup |
| 397 | // 2. Process B does not have access to file A and does a lookup |
| 398 | // (2) will succeed because the lookup request will not be sent from kernel to the FUSE daemon |
| 399 | // and the kernel will respond from cache. Even if this by itself is not a security risk |
| 400 | // because subsequent FUSE requests will fail if B does not have access to the resource. |
| 401 | // It does cause indeterministic behavior because whether (2) succeeds or not depends on if |
| 402 | // (1) occurred. |
Martijn Coenen | b8ff4eb | 2019-12-17 09:55:17 +0100 | [diff] [blame] | 403 | // We prevent this caching by setting the entry_timeout value to 0. |
| 404 | // The /0 and /0/Android paths are exempt, as they are visible to all apps. |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 405 | if (fuse->IsRoot(parent) || path.rfind(android_path, 0) == 0) { |
Martijn Coenen | b8ff4eb | 2019-12-17 09:55:17 +0100 | [diff] [blame] | 406 | e->entry_timeout = 10; |
| 407 | } else { |
| 408 | e->entry_timeout = 0; |
| 409 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 410 | e->ino = fuse->ToInode(node); |
Narayan Kamath | fc0aa95 | 2019-12-31 13:31:49 +0000 | [diff] [blame] | 411 | // This FS is not being exported via NFS so just a fixed generation number |
| 412 | // for now. If we do need this, we need to increment the generation ID each |
| 413 | // time the fuse daemon restarts because that's what it takes for us to |
| 414 | // reuse inode numbers. |
| 415 | e->generation = 0; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 416 | |
| 417 | return node; |
| 418 | } |
| 419 | |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 420 | static inline bool is_requesting_write(int flags) { |
| 421 | return flags & (O_WRONLY | O_RDWR); |
| 422 | } |
| 423 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 424 | namespace mediaprovider { |
| 425 | namespace fuse { |
| 426 | |
| 427 | /** |
| 428 | * Function implementations |
| 429 | * |
| 430 | * These implement the various functions in fuse_lowlevel_ops |
| 431 | * |
| 432 | */ |
| 433 | |
| 434 | static void pf_init(void* userdata, struct fuse_conn_info* conn) { |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 435 | // We don't want a getattr request with every read request |
| 436 | conn->want &= ~FUSE_CAP_AUTO_INVAL_DATA; |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 437 | unsigned mask = (FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE | FUSE_CAP_SPLICE_READ | |
| 438 | FUSE_CAP_ASYNC_READ | FUSE_CAP_ATOMIC_O_TRUNC | FUSE_CAP_WRITEBACK_CACHE | |
| 439 | FUSE_CAP_EXPORT_SUPPORT | FUSE_CAP_FLOCK_LOCKS); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 440 | conn->want |= conn->capable & mask; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 441 | conn->max_read = MAX_READ_SIZE; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Zim | 005cd81 | 2019-12-17 15:35:51 +0000 | [diff] [blame] | 444 | static void pf_destroy(void* userdata) { |
| 445 | struct fuse* fuse = reinterpret_cast<struct fuse*>(userdata); |
| 446 | LOG(INFO) << "DESTROY " << fuse->path; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 447 | |
| 448 | node::DeleteTree(fuse->root); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 449 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 450 | |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 451 | static std::regex storage_emulated_regex("^\\/storage\\/emulated\\/([0-9]+)"); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 452 | static node* do_lookup(fuse_req_t req, fuse_ino_t parent, const char* name, |
| 453 | struct fuse_entry_param* e, int* error_code) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 454 | struct fuse* fuse = get_fuse(req); |
| 455 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 456 | node* parent_node = fuse->FromInode(parent); |
| 457 | string parent_path = parent_node->BuildPath(); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 458 | |
Zim | 357e307 | 2020-01-15 10:50:01 +0000 | [diff] [blame] | 459 | TRACE_FUSE_VERBOSE(fuse) << "LOOKUP " << name << " @ " << parent << " (" |
| 460 | << safe_name(parent_node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 461 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 462 | string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 463 | |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 464 | std::smatch match; |
| 465 | std::regex_search(child_path, match, storage_emulated_regex); |
Zim | f0e3cd9 | 2020-01-03 00:57:43 +0000 | [diff] [blame] | 466 | if (match.size() == 2 && std::to_string(getuid() / PER_USER_RANGE) != match[1].str()) { |
| 467 | // Ensure the FuseDaemon user id matches the user id in requested path |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 468 | *error_code = EPERM; |
Zim | 859db93 | 2019-12-13 00:09:17 +0000 | [diff] [blame] | 469 | return nullptr; |
| 470 | } |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 471 | return make_node_entry(req, parent_node, name, child_path, e, error_code); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | 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] | 475 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 476 | struct fuse_entry_param e; |
| 477 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 478 | int error_code = 0; |
| 479 | if (do_lookup(req, parent, name, &e, &error_code)) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 480 | fuse_reply_entry(req, &e); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 481 | } else { |
| 482 | CHECK(error_code != 0); |
| 483 | fuse_reply_err(req, error_code); |
| 484 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 487 | static void do_forget(struct fuse* fuse, fuse_ino_t ino, uint64_t nlookup) { |
| 488 | node* node = fuse->FromInode(ino); |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 489 | TRACE_FUSE(fuse) << "FORGET #" << nlookup << " @ " << ino << " (" << safe_name(node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 490 | if (node) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 491 | // This is a narrowing conversion from an unsigned 64bit to a 32bit value. For |
| 492 | // some reason we only keep 32 bit refcounts but the kernel issues |
| 493 | // forget requests with a 64 bit counter. |
Narayan Kamath | 92bf67b | 2020-01-03 17:49:09 +0000 | [diff] [blame] | 494 | if (node->Release(static_cast<uint32_t>(nlookup))) { |
| 495 | fuse->NodeDeleted(node); |
| 496 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | static void pf_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 501 | ATRACE_CALL(); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 502 | node* node; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 503 | struct fuse* fuse = get_fuse(req); |
| 504 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 505 | do_forget(fuse, ino, nlookup); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 506 | fuse_reply_none(req); |
| 507 | } |
| 508 | |
| 509 | static void pf_forget_multi(fuse_req_t req, |
| 510 | size_t count, |
| 511 | struct fuse_forget_data* forgets) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 512 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 513 | struct fuse* fuse = get_fuse(req); |
| 514 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 515 | for (int i = 0; i < count; i++) { |
| 516 | do_forget(fuse, forgets[i].ino, forgets[i].nlookup); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 517 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 518 | fuse_reply_none(req); |
| 519 | } |
| 520 | |
| 521 | static void pf_getattr(fuse_req_t req, |
| 522 | fuse_ino_t ino, |
| 523 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 524 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 525 | struct fuse* fuse = get_fuse(req); |
| 526 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 527 | node* node = fuse->FromInode(ino); |
| 528 | string path = node->BuildPath(); |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 529 | TRACE_FUSE(fuse) << "GETATTR @ " << ino << " (" << safe_name(node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 530 | |
| 531 | if (!node) fuse_reply_err(req, ENOENT); |
| 532 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 533 | struct stat s; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 534 | memset(&s, 0, sizeof(s)); |
| 535 | if (lstat(path.c_str(), &s) < 0) { |
| 536 | fuse_reply_err(req, errno); |
| 537 | } else { |
| 538 | fuse_reply_attr(req, &s, 10); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | static void pf_setattr(fuse_req_t req, |
| 543 | fuse_ino_t ino, |
| 544 | struct stat* attr, |
| 545 | int to_set, |
| 546 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 547 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 548 | struct fuse* fuse = get_fuse(req); |
| 549 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 550 | node* node = fuse->FromInode(ino); |
| 551 | string path = node->BuildPath(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 552 | struct timespec times[2]; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 553 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 554 | TRACE_FUSE(fuse) << "SETATTR valid=" << to_set << " @ " << ino << "(" << safe_name(node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 555 | |
| 556 | if (!node) { |
| 557 | fuse_reply_err(req, ENOENT); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | /* XXX: incomplete implementation on purpose. |
| 562 | * chmod/chown should NEVER be implemented.*/ |
| 563 | |
| 564 | if ((to_set & FUSE_SET_ATTR_SIZE) |
| 565 | && truncate64(path.c_str(), attr->st_size) < 0) { |
| 566 | fuse_reply_err(req, errno); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 571 | * are both set, then set it to the current time. Else, set it to the |
| 572 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 573 | * as it allows ATIME and MTIME to be changed independently, and has |
| 574 | * nanosecond resolution which fuse also has. |
| 575 | */ |
| 576 | if (to_set & (FATTR_ATIME | FATTR_MTIME)) { |
| 577 | times[0].tv_nsec = UTIME_OMIT; |
| 578 | times[1].tv_nsec = UTIME_OMIT; |
| 579 | if (to_set & FATTR_ATIME) { |
| 580 | if (to_set & FATTR_ATIME_NOW) { |
| 581 | times[0].tv_nsec = UTIME_NOW; |
| 582 | } else { |
| 583 | times[0].tv_sec = attr->st_atime; |
| 584 | // times[0].tv_nsec = attr->st_atime.tv_nsec; |
| 585 | } |
| 586 | } |
| 587 | if (to_set & FATTR_MTIME) { |
| 588 | if (to_set & FATTR_MTIME_NOW) { |
| 589 | times[1].tv_nsec = UTIME_NOW; |
| 590 | } else { |
| 591 | times[1].tv_sec = attr->st_mtime; |
| 592 | // times[1].tv_nsec = attr->st_mtime.tv_nsec; |
| 593 | } |
| 594 | } |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 595 | TRACE_FUSE(fuse) << "Calling utimensat on " << path << " with atime " << times[0].tv_sec |
| 596 | << " mtime=" << times[1].tv_sec; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 597 | if (utimensat(-1, path.c_str(), times, 0) < 0) { |
| 598 | fuse_reply_err(req, errno); |
| 599 | return; |
| 600 | } |
| 601 | } |
| 602 | |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 603 | lstat(path.c_str(), attr); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 604 | fuse_reply_attr(req, attr, 10); |
| 605 | } |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 606 | |
| 607 | static void pf_canonical_path(fuse_req_t req, fuse_ino_t ino) |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 608 | { |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 609 | node* node = get_fuse(req)->FromInode(ino); |
| 610 | |
| 611 | if (node) { |
| 612 | // TODO(b/147482155): Check that uid has access to |path| and its contents |
| 613 | fuse_reply_canonical_path(req, node->BuildPath().c_str()); |
| 614 | return; |
| 615 | } |
| 616 | fuse_reply_err(req, ENOENT); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 617 | } |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 618 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 619 | static void pf_mknod(fuse_req_t req, |
| 620 | fuse_ino_t parent, |
| 621 | const char* name, |
| 622 | mode_t mode, |
| 623 | dev_t rdev) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 624 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 625 | struct fuse* fuse = get_fuse(req); |
| 626 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 627 | node* parent_node = fuse->FromInode(parent); |
| 628 | string parent_path = parent_node->BuildPath(); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 629 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 630 | TRACE_FUSE(fuse) << "MKNOD " << name << " 0" << std::oct << mode << " @ " << parent << " (" |
| 631 | << safe_name(parent_node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 632 | |
| 633 | if (!parent_node) { |
| 634 | fuse_reply_err(req, ENOENT); |
| 635 | return; |
| 636 | } |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 637 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 638 | |
| 639 | mode = (mode & (~0777)) | 0664; |
| 640 | if (mknod(child_path.c_str(), mode, rdev) < 0) { |
| 641 | fuse_reply_err(req, errno); |
| 642 | return; |
| 643 | } |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 644 | |
| 645 | int error_code = 0; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 646 | struct fuse_entry_param e; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 647 | if (make_node_entry(req, parent_node, name, child_path, &e, &error_code)) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 648 | fuse_reply_entry(req, &e); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 649 | } else { |
| 650 | CHECK(error_code != 0); |
| 651 | fuse_reply_err(req, error_code); |
| 652 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static void pf_mkdir(fuse_req_t req, |
| 656 | fuse_ino_t parent, |
| 657 | const char* name, |
| 658 | mode_t mode) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 659 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 660 | struct fuse* fuse = get_fuse(req); |
| 661 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 662 | node* parent_node = fuse->FromInode(parent); |
| 663 | const string parent_path = parent_node->BuildPath(); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 664 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 665 | TRACE_FUSE(fuse) << "MKDIR " << name << " 0" << std::oct << mode << " @ " << parent << " (" |
| 666 | << safe_name(parent_node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 667 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 668 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 669 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 670 | int status = -fuse->mp->IsCreatingDirAllowed(child_path, ctx->uid); |
| 671 | if (status) { |
| 672 | fuse_reply_err(req, status); |
| 673 | return; |
| 674 | } |
| 675 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 676 | mode = (mode & (~0777)) | 0775; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 677 | if (mkdir(child_path.c_str(), mode) < 0) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 678 | fuse_reply_err(req, errno); |
| 679 | return; |
| 680 | } |
| 681 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 682 | int error_code = 0; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 683 | struct fuse_entry_param e; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 684 | if (make_node_entry(req, parent_node, name, child_path, &e, &error_code)) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 685 | fuse_reply_entry(req, &e); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 686 | } else { |
| 687 | CHECK(error_code != 0); |
| 688 | fuse_reply_err(req, error_code); |
| 689 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | 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] | 693 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 694 | struct fuse* fuse = get_fuse(req); |
| 695 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 696 | node* parent_node = fuse->FromInode(parent); |
| 697 | const string parent_path = parent_node->BuildPath(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 698 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 699 | TRACE_FUSE(fuse) << "UNLINK " << name << " @ " << parent << "(" << safe_name(parent_node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 700 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 701 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 702 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 703 | int status = -fuse->mp->DeleteFile(child_path, ctx->uid); |
| 704 | if (status) { |
| 705 | fuse_reply_err(req, status); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 706 | return; |
| 707 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 708 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 709 | node* child_node = parent_node->LookupChildByName(name); |
| 710 | if (child_node) { |
| 711 | child_node->SetDeleted(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 712 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 713 | |
| 714 | fuse_reply_err(req, 0); |
| 715 | } |
| 716 | |
| 717 | 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] | 718 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 719 | struct fuse* fuse = get_fuse(req); |
| 720 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 721 | node* parent_node = fuse->FromInode(parent); |
| 722 | const string parent_path = parent_node->BuildPath(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 723 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 724 | TRACE_FUSE(fuse) << "RMDIR " << name << " @ " << parent << "(" << safe_name(parent_node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 725 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 726 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 727 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 728 | int status = -fuse->mp->IsDeletingDirAllowed(child_path, ctx->uid); |
| 729 | if (status) { |
| 730 | fuse_reply_err(req, status); |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | if (rmdir(child_path.c_str()) < 0) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 735 | fuse_reply_err(req, errno); |
| 736 | return; |
| 737 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 738 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 739 | node* child_node = parent_node->LookupChildByName(name); |
| 740 | if (child_node) { |
| 741 | child_node->SetDeleted(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 742 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 743 | |
| 744 | fuse_reply_err(req, 0); |
| 745 | } |
| 746 | /* |
| 747 | static void pf_symlink(fuse_req_t req, const char* link, fuse_ino_t parent, |
| 748 | const char* name) |
| 749 | { |
| 750 | cout << "TODO:" << __func__; |
| 751 | } |
| 752 | */ |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 753 | static int do_rename(fuse_req_t req, fuse_ino_t parent, const char* name, fuse_ino_t new_parent, |
| 754 | const char* new_name, unsigned int flags) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 755 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 756 | struct fuse* fuse = get_fuse(req); |
| 757 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 758 | |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 759 | if (flags != 0) { |
| 760 | LOG(ERROR) << "One or more rename flags not supported"; |
| 761 | return EINVAL; |
| 762 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 763 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 764 | node* old_parent_node = fuse->FromInode(parent); |
| 765 | const string old_parent_path = old_parent_node->BuildPath(); |
| 766 | node* new_parent_node = fuse->FromInode(new_parent); |
| 767 | const string new_parent_path = new_parent_node->BuildPath(); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 768 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 769 | if (!old_parent_node || !new_parent_node) { |
| 770 | return ENOENT; |
| 771 | } else if (parent == new_parent && name == new_name) { |
| 772 | // No rename required. |
| 773 | return 0; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 774 | } |
| 775 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 776 | TRACE_FUSE(fuse) << "RENAME " << name << " -> " << new_name << " @ " << parent << " (" |
| 777 | << safe_name(old_parent_node) << ") -> " << new_parent << " (" |
| 778 | << safe_name(new_parent_node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 779 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 780 | node* child_node = old_parent_node->LookupChildByName(name); |
| 781 | child_node->Acquire(); |
| 782 | |
| 783 | const string old_child_path = child_node->BuildPath(); |
| 784 | const string new_child_path = new_parent_path + "/" + new_name; |
| 785 | |
Sahana Rao | 182ec6b | 2020-01-03 15:00:46 +0000 | [diff] [blame] | 786 | // TODO(b/147408834): Check ENOTEMPTY & EEXIST error conditions before JNI call. |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 787 | 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] | 788 | // TODO(b/145663158): Lookups can go out of sync if file/directory is actually moved but |
| 789 | // EFAULT/EIO is reported due to JNI exception. |
| 790 | if (res == 0) { |
| 791 | child_node->Rename(new_name, new_parent_node); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 794 | child_node->Release(1); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 795 | return res; |
| 796 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 797 | |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 798 | static void pf_rename(fuse_req_t req, fuse_ino_t parent, const char* name, fuse_ino_t new_parent, |
| 799 | const char* new_name, unsigned int flags) { |
| 800 | int res = do_rename(req, parent, name, new_parent, new_name, flags); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 801 | fuse_reply_err(req, res); |
| 802 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 803 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 804 | /* |
Sahana Rao | 2c41603 | 2019-12-31 13:41:00 +0000 | [diff] [blame] | 805 | static void pf_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t new_parent, |
| 806 | const char* new_name) |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 807 | { |
| 808 | cout << "TODO:" << __func__; |
| 809 | } |
| 810 | */ |
| 811 | |
| 812 | 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] | 813 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 814 | struct fuse* fuse = get_fuse(req); |
| 815 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 816 | node* node = fuse->FromInode(ino); |
| 817 | const string path = node->BuildPath(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 818 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 819 | TRACE_FUSE(fuse) << "OPEN 0" << std::oct << fi->flags << " @ " << ino << " (" << safe_name(node) |
| 820 | << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 821 | |
| 822 | if (!node) { |
| 823 | fuse_reply_err(req, ENOENT); |
| 824 | return; |
| 825 | } |
| 826 | |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 827 | if (fi->flags & O_DIRECT) { |
| 828 | fi->flags &= ~O_DIRECT; |
| 829 | fi->direct_io = true; |
| 830 | } |
| 831 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 832 | TRACE_FUSE(fuse) << "OPEN " << path; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 833 | int status = -fuse->mp->IsOpenAllowed(path, ctx->uid, is_requesting_write(fi->flags)); |
| 834 | if (status) { |
| 835 | fuse_reply_err(req, status); |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | const int fd = open(path.c_str(), fi->flags); |
| 840 | if (fd < 0) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 841 | fuse_reply_err(req, errno); |
| 842 | return; |
| 843 | } |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 844 | |
shafik | c580b6d | 2019-12-10 18:45:17 +0000 | [diff] [blame] | 845 | // 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] | 846 | std::unique_ptr<RedactionInfo> ri; |
shafik | c580b6d | 2019-12-10 18:45:17 +0000 | [diff] [blame] | 847 | if (is_requesting_write(fi->flags)) { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 848 | ri = std::make_unique<RedactionInfo>(); |
shafik | c580b6d | 2019-12-10 18:45:17 +0000 | [diff] [blame] | 849 | } else { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 850 | ri = fuse->mp->GetRedactionInfo(path, req->ctx.uid); |
shafik | c580b6d | 2019-12-10 18:45:17 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 853 | if (!ri) { |
| 854 | close(fd); |
| 855 | fuse_reply_err(req, EFAULT); |
shafik | c580b6d | 2019-12-10 18:45:17 +0000 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 859 | if (ri->isRedactionNeeded() || is_file_locked(fd, path)) { |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 860 | // We don't want to use the FUSE VFS cache in two cases: |
| 861 | // 1. When redaction is needed because app A with EXIF access might access |
| 862 | // a region that should have been redacted for app B without EXIF access, but app B on |
| 863 | // a subsequent read, will be able to see the EXIF data because the read request for that |
| 864 | // region will be served from cache and not get to the FUSE daemon |
| 865 | // 2. When the file has a read or write lock on it. This means that the MediaProvider has |
| 866 | // given an fd to the lower file system to an app. There are two cases where using the cache |
| 867 | // in this case can be a problem: |
| 868 | // a. Writing to a FUSE fd with caching enabled will use the write-back cache and a |
| 869 | // subsequent read from the lower fs fd will not see the write. |
| 870 | // b. Reading from a FUSE fd with caching enabled may not see the latest writes using the |
| 871 | // lower fs fd because those writes did not go through the FUSE layer and reads from FUSE |
| 872 | // after that write may be served from cache |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 873 | if (ri->isRedactionNeeded()) { |
Martijn Coenen | d0f9589 | 2020-01-09 10:14:26 +0100 | [diff] [blame] | 874 | TRACE_FUSE(fuse) << "Using direct io for " << path << " because redaction is needed."; |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 875 | } else { |
Martijn Coenen | d0f9589 | 2020-01-09 10:14:26 +0100 | [diff] [blame] | 876 | TRACE_FUSE(fuse) << "Using direct io for " << path << " because the file is locked."; |
| 877 | } |
shafik | c580b6d | 2019-12-10 18:45:17 +0000 | [diff] [blame] | 878 | fi->direct_io = true; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 879 | } else { |
| 880 | TRACE_FUSE(fuse) << "Using cache for " << path; |
shafik | c580b6d | 2019-12-10 18:45:17 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 883 | handle* h = new handle(path, fd, ri.release(), !fi->direct_io); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 884 | node->AddHandle(h); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 885 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 886 | fi->fh = ptr_to_id(h); |
Zim | 61d1234 | 2019-10-01 15:47:45 +0100 | [diff] [blame] | 887 | fi->keep_cache = 1; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 888 | fuse_reply_open(req, fi); |
| 889 | } |
| 890 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 891 | 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] | 892 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 893 | struct fuse_bufvec buf = FUSE_BUFVEC_INIT(size); |
| 894 | |
| 895 | buf.buf[0].fd = h->fd; |
| 896 | buf.buf[0].pos = off; |
| 897 | buf.buf[0].flags = |
| 898 | (enum fuse_buf_flags) (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK); |
| 899 | |
| 900 | fuse_reply_data(req, &buf, (enum fuse_buf_copy_flags) 0); |
| 901 | } |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 902 | |
| 903 | static bool range_contains(const RedactionRange& rr, off_t off) { |
| 904 | return rr.first <= off && off <= rr.second; |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Sets the parameters for a fuse_buf that reads from memory, including flags. |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 909 | * Makes buf->mem point to an already mapped region of zeroized memory. |
| 910 | * This memory is read only. |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 911 | */ |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 912 | 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] | 913 | buf->size = size; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 914 | buf->mem = fuse->zero_addr; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 915 | buf->flags = static_cast<fuse_buf_flags>(0 /*read from fuse_buf.mem*/); |
| 916 | buf->pos = -1; |
| 917 | buf->fd = -1; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Sets the parameters for a fuse_buf that reads from file, including flags. |
| 922 | */ |
| 923 | static void create_file_fuse_buf(size_t size, off_t pos, int fd, fuse_buf* buf) { |
| 924 | buf->size = size; |
| 925 | buf->fd = fd; |
| 926 | buf->pos = pos; |
| 927 | buf->flags = static_cast<fuse_buf_flags>(FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK); |
| 928 | buf->mem = nullptr; |
| 929 | } |
| 930 | |
| 931 | 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] | 932 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 933 | auto overlapping_rr = h->ri->getOverlappingRedactionRanges(size, off); |
| 934 | |
| 935 | if (overlapping_rr->size() <= 0) { |
| 936 | // no relevant redaction ranges for this request |
| 937 | do_read(req, size, off, fi); |
| 938 | return; |
| 939 | } |
| 940 | // the number of buffers we need, if the read doesn't start or end with |
| 941 | // a redaction range. |
| 942 | int num_bufs = overlapping_rr->size() * 2 + 1; |
| 943 | if (overlapping_rr->front().first <= off) { |
| 944 | // the beginning of the read request is redacted |
| 945 | num_bufs--; |
| 946 | } |
| 947 | if (overlapping_rr->back().second >= off + size) { |
| 948 | // the end of the read request is redacted |
| 949 | num_bufs--; |
| 950 | } |
| 951 | auto bufvec_ptr = std::unique_ptr<fuse_bufvec, decltype(free)*>{ |
| 952 | reinterpret_cast<fuse_bufvec*>( |
| 953 | malloc(sizeof(fuse_bufvec) + (num_bufs - 1) * sizeof(fuse_buf))), |
| 954 | free}; |
| 955 | fuse_bufvec& bufvec = *bufvec_ptr; |
| 956 | |
| 957 | // initialize bufvec |
| 958 | bufvec.count = num_bufs; |
| 959 | bufvec.idx = 0; |
| 960 | bufvec.off = 0; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 961 | |
| 962 | int rr_idx = 0; |
| 963 | off_t start = off; |
| 964 | // Add a dummy redaction range to make sure we don't go out of vector |
| 965 | // limits when computing the end of the last non-redacted range. |
| 966 | // This ranges is invalid because its starting point is larger than it's ending point. |
| 967 | overlapping_rr->push_back(RedactionRange(LLONG_MAX, LLONG_MAX - 1)); |
| 968 | |
| 969 | for (int i = 0; i < num_bufs; ++i) { |
| 970 | off_t end; |
| 971 | if (range_contains(overlapping_rr->at(rr_idx), start)) { |
| 972 | // Handle a redacted range |
| 973 | // end should be the end of the redacted range, but can't be out of |
| 974 | // the read request bounds |
| 975 | end = std::min(static_cast<off_t>(off + size - 1), overlapping_rr->at(rr_idx).second); |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 976 | create_mem_fuse_buf(/*size*/ end - start + 1, &(bufvec.buf[i]), get_fuse(req)); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 977 | ++rr_idx; |
| 978 | } else { |
| 979 | // Handle a non-redacted range |
| 980 | // end should be right before the next redaction range starts or |
| 981 | // the end of the read request |
| 982 | end = std::min(static_cast<off_t>(off + size - 1), |
| 983 | overlapping_rr->at(rr_idx).first - 1); |
| 984 | create_file_fuse_buf(/*size*/ end - start + 1, start, h->fd, &(bufvec.buf[i])); |
| 985 | } |
| 986 | start = end + 1; |
| 987 | } |
| 988 | |
| 989 | fuse_reply_data(req, &bufvec, static_cast<fuse_buf_copy_flags>(0)); |
| 990 | } |
| 991 | |
| 992 | static void pf_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, |
| 993 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 994 | ATRACE_CALL(); |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 995 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 996 | struct fuse* fuse = get_fuse(req); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 997 | |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 998 | fuse->fadviser.Record(h->fd, size); |
| 999 | |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1000 | if (h->ri->isRedactionNeeded()) { |
| 1001 | do_read_with_redaction(req, size, off, fi); |
| 1002 | } else { |
| 1003 | do_read(req, size, off, fi); |
| 1004 | } |
| 1005 | } |
| 1006 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1007 | /* |
| 1008 | static void pf_write(fuse_req_t req, fuse_ino_t ino, const char* buf, |
| 1009 | size_t size, off_t off, struct fuse_file_info* fi) |
| 1010 | { |
| 1011 | cout << "TODO:" << __func__; |
| 1012 | } |
| 1013 | */ |
| 1014 | |
| 1015 | static void pf_write_buf(fuse_req_t req, |
| 1016 | fuse_ino_t ino, |
| 1017 | struct fuse_bufvec* bufv, |
| 1018 | off_t off, |
| 1019 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1020 | ATRACE_CALL(); |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1021 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1022 | struct fuse_bufvec buf = FUSE_BUFVEC_INIT(fuse_buf_size(bufv)); |
| 1023 | ssize_t size; |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 1024 | struct fuse* fuse = get_fuse(req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1025 | |
| 1026 | buf.buf[0].fd = h->fd; |
| 1027 | buf.buf[0].pos = off; |
| 1028 | buf.buf[0].flags = |
| 1029 | (enum fuse_buf_flags) (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK); |
| 1030 | size = fuse_buf_copy(&buf, bufv, (enum fuse_buf_copy_flags) 0); |
| 1031 | |
| 1032 | if (size < 0) |
| 1033 | fuse_reply_err(req, -size); |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 1034 | else { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1035 | fuse_reply_write(req, size); |
Paul Lawrence | 7e4a5a8 | 2019-09-27 21:39:49 +0200 | [diff] [blame] | 1036 | fuse->fadviser.Record(h->fd, size); |
| 1037 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1038 | } |
| 1039 | // Haven't tested this one. Not sure what calls it. |
| 1040 | #if 0 |
| 1041 | static void pf_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, |
| 1042 | off_t off_in, struct fuse_file_info* fi_in, |
| 1043 | fuse_ino_t ino_out, off_t off_out, |
| 1044 | struct fuse_file_info* fi_out, size_t len, |
| 1045 | int flags) |
| 1046 | { |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1047 | handle* h_in = reinterpret_cast<handle *>(fi_in->fh); |
| 1048 | handle* h_out = reinterpret_cast<handle *>(fi_out->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1049 | struct fuse_bufvec buf_in = FUSE_BUFVEC_INIT(len); |
| 1050 | struct fuse_bufvec buf_out = FUSE_BUFVEC_INIT(len); |
| 1051 | ssize_t size; |
| 1052 | |
| 1053 | buf_in.buf[0].fd = h_in->fd; |
| 1054 | buf_in.buf[0].pos = off_in; |
| 1055 | buf_in.buf[0].flags = (enum fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK); |
| 1056 | |
| 1057 | buf_out.buf[0].fd = h_out->fd; |
| 1058 | buf_out.buf[0].pos = off_out; |
| 1059 | buf_out.buf[0].flags = (enum fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK); |
| 1060 | size = fuse_buf_copy(&buf_out, &buf_in, (enum fuse_buf_copy_flags) 0); |
| 1061 | |
| 1062 | if (size < 0) { |
| 1063 | fuse_reply_err(req, -size); |
| 1064 | } |
| 1065 | |
| 1066 | fuse_reply_write(req, size); |
| 1067 | } |
| 1068 | #endif |
| 1069 | static void pf_flush(fuse_req_t req, |
| 1070 | fuse_ino_t ino, |
| 1071 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1072 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1073 | struct fuse* fuse = get_fuse(req); |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 1074 | TRACE_FUSE(fuse) << "FLUSH is a noop"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1075 | fuse_reply_err(req, 0); |
| 1076 | } |
| 1077 | |
| 1078 | static void pf_release(fuse_req_t req, |
| 1079 | fuse_ino_t ino, |
| 1080 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1081 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1082 | struct fuse* fuse = get_fuse(req); |
Kyle Tso | 613b7ab | 2019-12-24 06:30:00 +0000 | [diff] [blame] | 1083 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1084 | node* node = fuse->FromInode(ino); |
| 1085 | handle* h = reinterpret_cast<handle*>(fi->fh); |
| 1086 | TRACE_FUSE(fuse) << "RELEASE " |
| 1087 | << "0" << std::oct << fi->flags << " " << h << "(" << h->fd << ")"; |
shafik | 15e2d61 | 2019-10-31 20:10:25 +0000 | [diff] [blame] | 1088 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1089 | fuse->fadviser.Close(h->fd); |
| 1090 | // TODO(b/145737191): Figure out if we need to scan files on close, and how to do it properly |
| 1091 | if (node) { |
| 1092 | node->DestroyHandle(h); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1093 | } |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1094 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1095 | fuse_reply_err(req, 0); |
| 1096 | } |
| 1097 | |
| 1098 | static int do_sync_common(int fd, bool datasync) { |
| 1099 | int res = datasync ? fdatasync(fd) : fsync(fd); |
| 1100 | |
| 1101 | if (res == -1) return errno; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | static void pf_fsync(fuse_req_t req, |
| 1106 | fuse_ino_t ino, |
| 1107 | int datasync, |
| 1108 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1109 | ATRACE_CALL(); |
shafik | a2ae907 | 2019-10-28 12:16:00 +0000 | [diff] [blame] | 1110 | handle* h = reinterpret_cast<handle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1111 | int err = do_sync_common(h->fd, datasync); |
| 1112 | |
| 1113 | fuse_reply_err(req, err); |
| 1114 | } |
| 1115 | |
| 1116 | static void pf_fsyncdir(fuse_req_t req, |
| 1117 | fuse_ino_t ino, |
| 1118 | int datasync, |
| 1119 | struct fuse_file_info* fi) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1120 | dirhandle* h = reinterpret_cast<dirhandle*>(fi->fh); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1121 | int err = do_sync_common(dirfd(h->d), datasync); |
| 1122 | |
| 1123 | fuse_reply_err(req, err); |
| 1124 | } |
| 1125 | |
| 1126 | static void pf_opendir(fuse_req_t req, |
| 1127 | fuse_ino_t ino, |
| 1128 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1129 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1130 | struct fuse* fuse = get_fuse(req); |
| 1131 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1132 | node* node = fuse->FromInode(ino); |
| 1133 | const string path = node->BuildPath(); |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 1134 | |
Zim | db34d47 | 2019-09-30 12:29:24 +0100 | [diff] [blame] | 1135 | TRACE_FUSE(fuse) << "OPENDIR @ " << ino << " (" << safe_name(node) << ")" << path; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1136 | |
| 1137 | if (!node) { |
| 1138 | fuse_reply_err(req, ENOENT); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1142 | int status = -fuse->mp->IsOpendirAllowed(path, ctx->uid); |
| 1143 | if (status) { |
| 1144 | fuse_reply_err(req, status); |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | DIR* dir = opendir(path.c_str()); |
| 1149 | if (!dir) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1150 | fuse_reply_err(req, errno); |
| 1151 | return; |
| 1152 | } |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1153 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1154 | dirhandle* h = new dirhandle(dir); |
| 1155 | node->AddDirHandle(h); |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1156 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1157 | fi->fh = ptr_to_id(h); |
| 1158 | fuse_reply_open(req, fi); |
| 1159 | } |
| 1160 | |
| 1161 | #define READDIR_BUF 8192LU |
| 1162 | |
| 1163 | static void do_readdir_common(fuse_req_t req, |
| 1164 | fuse_ino_t ino, |
| 1165 | size_t size, |
| 1166 | off_t off, |
| 1167 | struct fuse_file_info* fi, |
| 1168 | bool plus) { |
| 1169 | struct fuse* fuse = get_fuse(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1170 | dirhandle* h = reinterpret_cast<dirhandle*>(fi->fh); |
Jeff Sharkey | 7469b98 | 2019-08-28 16:51:02 -0600 | [diff] [blame] | 1171 | size_t len = std::min<size_t>(size, READDIR_BUF); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1172 | char buf[READDIR_BUF]; |
| 1173 | size_t used = 0; |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1174 | std::shared_ptr<DirectoryEntry> de; |
| 1175 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1176 | struct fuse_entry_param e; |
Sahana Rao | 6f9ea98 | 2019-09-20 12:21:33 +0100 | [diff] [blame] | 1177 | size_t entry_size = 0; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1178 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1179 | node* node = fuse->FromInode(ino); |
| 1180 | const string path = node->BuildPath(); |
Narayan Kamath | 768bea3 | 2019-12-27 16:23:23 +0000 | [diff] [blame] | 1181 | |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1182 | TRACE_FUSE(fuse) << "READDIR @" << ino << " " << path << " at offset " << off; |
| 1183 | // Get all directory entries from MediaProvider on first readdir() call of |
| 1184 | // directory handle. h->next_off = 0 indicates that current readdir() call |
| 1185 | // is first readdir() call for the directory handle, Avoid multiple JNI calls |
| 1186 | // for single directory handle. |
| 1187 | if (h->next_off == 0) { |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 1188 | h->de = fuse->mp->GetDirectoryEntries(req->ctx.uid, path, h->d); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1189 | } |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1190 | // If the last entry in the previous readdir() call was rejected due to |
| 1191 | // buffer capacity constraints, update directory offset to start from |
| 1192 | // previously rejected entry. Directory offset can also change if there was |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 1193 | // a seekdir() on the given directory handle. |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1194 | if (off != h->next_off) { |
| 1195 | h->next_off = off; |
| 1196 | } |
| 1197 | const int num_directory_entries = h->de.size(); |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 1198 | // Check for errors. Any error/exception occurred while obtaining directory |
| 1199 | // entries will be indicated by marking first directory entry name as empty |
| 1200 | // string. In the erroneous case corresponding d_type will hold error number. |
Sahana Rao | 53bd1f6 | 2019-12-27 20:18:39 +0000 | [diff] [blame] | 1201 | if (num_directory_entries && h->de[0]->d_name.empty()) { |
| 1202 | fuse_reply_err(req, h->de[0]->d_type); |
| 1203 | return; |
| 1204 | } |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1205 | |
Sahana Rao | 53bd1f6 | 2019-12-27 20:18:39 +0000 | [diff] [blame] | 1206 | while (h->next_off < num_directory_entries) { |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1207 | de = h->de[h->next_off]; |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1208 | entry_size = 0; |
| 1209 | h->next_off++; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1210 | if (plus) { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1211 | int error_code = 0; |
| 1212 | 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] | 1213 | entry_size = fuse_add_direntry_plus(req, buf + used, len - used, de->d_name.c_str(), |
| 1214 | &e, h->next_off); |
Sahana Rao | 8a588e7 | 2019-12-06 11:32:56 +0000 | [diff] [blame] | 1215 | } else { |
Sahana Rao | 8d8dbb4 | 2019-12-18 12:57:02 +0000 | [diff] [blame] | 1216 | // Ignore lookup errors on |
| 1217 | // 1. non-existing files returned from MediaProvider database. |
| 1218 | // 2. path that doesn't match FuseDaemon UID and calling uid. |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1219 | if (error_code == ENOENT || error_code == EPERM) continue; |
| 1220 | fuse_reply_err(req, error_code); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1221 | return; |
| 1222 | } |
| 1223 | } else { |
| 1224 | e.attr.st_ino = FUSE_UNKNOWN_INO; |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1225 | e.attr.st_mode = de->d_type; |
| 1226 | entry_size = fuse_add_direntry(req, buf + used, len - used, de->d_name.c_str(), &e.attr, |
| 1227 | h->next_off); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1228 | } |
Sahana Rao | 6f9ea98 | 2019-09-20 12:21:33 +0100 | [diff] [blame] | 1229 | // If buffer in fuse_add_direntry[_plus] is not large enough then |
| 1230 | // the entry is not added to buffer but the size of the entry is still |
| 1231 | // returned. Check available buffer size + returned entry size is less |
| 1232 | // than actual buffer size to confirm entry is added to buffer. |
Sahana Rao | 43927f0 | 2019-12-10 22:59:01 +0000 | [diff] [blame] | 1233 | if (used + entry_size > len) { |
| 1234 | // When an entry is rejected, lookup called by readdir_plus will not be tracked by |
| 1235 | // kernel. Call forget on the rejected node to decrement the reference count. |
Narayan Kamath | d621984 | 2019-12-27 17:44:35 +0000 | [diff] [blame] | 1236 | if (plus) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1237 | do_forget(fuse, e.ino, 1); |
Narayan Kamath | d621984 | 2019-12-27 17:44:35 +0000 | [diff] [blame] | 1238 | } |
Sahana Rao | 43927f0 | 2019-12-10 22:59:01 +0000 | [diff] [blame] | 1239 | break; |
| 1240 | } |
Sahana Rao | 6f9ea98 | 2019-09-20 12:21:33 +0100 | [diff] [blame] | 1241 | used += entry_size; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1242 | } |
Sahana Rao | 53bd1f6 | 2019-12-27 20:18:39 +0000 | [diff] [blame] | 1243 | fuse_reply_buf(req, buf, used); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | static void pf_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, |
| 1247 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1248 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1249 | do_readdir_common(req, ino, size, off, fi, false); |
| 1250 | } |
| 1251 | |
| 1252 | static void pf_readdirplus(fuse_req_t req, |
| 1253 | fuse_ino_t ino, |
| 1254 | size_t size, |
| 1255 | off_t off, |
| 1256 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1257 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1258 | do_readdir_common(req, ino, size, off, fi, true); |
| 1259 | } |
| 1260 | |
| 1261 | static void pf_releasedir(fuse_req_t req, |
| 1262 | fuse_ino_t ino, |
| 1263 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1264 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1265 | struct fuse* fuse = get_fuse(req); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1266 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1267 | node* node = fuse->FromInode(ino); |
| 1268 | dirhandle* h = reinterpret_cast<dirhandle*>(fi->fh); |
| 1269 | TRACE_FUSE(fuse) << "RELEASEDIR " << h; |
| 1270 | if (node) { |
| 1271 | node->DestroyDirHandle(h); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1272 | } |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1273 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1274 | fuse_reply_err(req, 0); |
| 1275 | } |
| 1276 | |
| 1277 | static void pf_statfs(fuse_req_t req, fuse_ino_t ino) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1278 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1279 | struct statvfs st; |
| 1280 | struct fuse* fuse = get_fuse(req); |
| 1281 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1282 | if (statvfs(fuse->root->GetName().c_str(), &st)) |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1283 | fuse_reply_err(req, errno); |
| 1284 | else |
| 1285 | fuse_reply_statfs(req, &st); |
| 1286 | } |
| 1287 | /* |
| 1288 | static void pf_setxattr(fuse_req_t req, fuse_ino_t ino, const char* name, |
| 1289 | const char* value, size_t size, int flags) |
| 1290 | { |
| 1291 | cout << "TODO:" << __func__; |
| 1292 | } |
| 1293 | |
| 1294 | static void pf_getxattr(fuse_req_t req, fuse_ino_t ino, const char* name, |
| 1295 | size_t size) |
| 1296 | { |
| 1297 | cout << "TODO:" << __func__; |
| 1298 | } |
| 1299 | |
| 1300 | static void pf_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) |
| 1301 | { |
| 1302 | cout << "TODO:" << __func__; |
| 1303 | } |
| 1304 | |
| 1305 | static void pf_removexattr(fuse_req_t req, fuse_ino_t ino, const char* name) |
| 1306 | { |
| 1307 | cout << "TODO:" << __func__; |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1308 | }*/ |
| 1309 | |
| 1310 | 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] | 1311 | ATRACE_CALL(); |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1312 | struct fuse* fuse = get_fuse(req); |
| 1313 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
| 1314 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1315 | node* node = fuse->FromInode(ino); |
| 1316 | const string path = node->BuildPath(); |
Zim | 357e307 | 2020-01-15 10:50:01 +0000 | [diff] [blame] | 1317 | TRACE_FUSE_VERBOSE(fuse) << "ACCESS " << path; |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1318 | |
| 1319 | int res = access(path.c_str(), F_OK); |
| 1320 | fuse_reply_err(req, res ? errno : 0); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1321 | } |
Zim | a9fcd55 | 2019-08-29 15:17:04 +0100 | [diff] [blame] | 1322 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1323 | static void pf_create(fuse_req_t req, |
| 1324 | fuse_ino_t parent, |
| 1325 | const char* name, |
| 1326 | mode_t mode, |
| 1327 | struct fuse_file_info* fi) { |
Zim | ec8d572 | 2019-12-05 07:26:13 +0000 | [diff] [blame] | 1328 | ATRACE_CALL(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1329 | struct fuse* fuse = get_fuse(req); |
| 1330 | const struct fuse_ctx* ctx = fuse_req_ctx(req); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1331 | node* parent_node = fuse->FromInode(parent); |
| 1332 | const string parent_path = parent_node->BuildPath(); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1333 | |
shafik | 9dd60eb | 2019-11-12 20:28:53 +0000 | [diff] [blame] | 1334 | TRACE_FUSE(fuse) << "CREATE " << name << " 0" << std::oct << fi->flags << " @ " << parent |
| 1335 | << " (" << safe_name(parent_node) << ")"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1336 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1337 | const string child_path = parent_path + "/" + name; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1338 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1339 | int mp_return_code = -fuse->mp->InsertFile(child_path.c_str(), ctx->uid); |
| 1340 | if (mp_return_code) { |
shafik | a51f3ce | 2019-10-10 17:06:41 +0100 | [diff] [blame] | 1341 | PLOG(DEBUG) << "Could not create file: " << child_path; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1342 | fuse_reply_err(req, mp_return_code); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1343 | return; |
| 1344 | } |
| 1345 | |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1346 | mode = (mode & (~0777)) | 0664; |
| 1347 | int fd = open(child_path.c_str(), fi->flags, mode); |
| 1348 | if (fd < 0) { |
| 1349 | int error_code = errno; |
| 1350 | // We've already inserted the file into the MP database before the |
| 1351 | // failed open(), so that needs to be rolled back here. |
| 1352 | fuse->mp->DeleteFile(child_path.c_str(), ctx->uid); |
| 1353 | fuse_reply_err(req, error_code); |
| 1354 | return; |
| 1355 | } |
| 1356 | |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 1357 | // TODO(b/147274248): Assume there will be no EXIF to redact. |
Zim | 57a7535 | 2020-01-07 08:19:18 +0000 | [diff] [blame] | 1358 | // This prevents crashing during reads but can be a security hole if a malicious app opens an fd |
| 1359 | // to the file before all the EXIF content is written. We could special case reads before the |
| 1360 | // first close after a file has just been created. |
Narayan Kamath | bd22bb0 | 2020-01-08 16:02:50 +0000 | [diff] [blame] | 1361 | handle* h = new handle(child_path, fd, new RedactionInfo(), true /* cached */); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1362 | fi->fh = ptr_to_id(h); |
Zim | 61d1234 | 2019-10-01 15:47:45 +0100 | [diff] [blame] | 1363 | fi->keep_cache = 1; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1364 | |
| 1365 | int error_code = 0; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1366 | struct fuse_entry_param e; |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1367 | node* node = make_node_entry(req, parent_node, name, child_path, &e, &error_code); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1368 | if (node) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1369 | node->AddHandle(h); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1370 | fuse_reply_create(req, &e, fi); |
| 1371 | } else { |
Narayan Kamath | bbb779a | 2019-12-31 16:30:11 +0000 | [diff] [blame] | 1372 | CHECK(error_code != 0); |
| 1373 | fuse_reply_err(req, error_code); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | /* |
| 1377 | static void pf_getlk(fuse_req_t req, fuse_ino_t ino, |
| 1378 | struct fuse_file_info* fi, struct flock* lock) |
| 1379 | { |
| 1380 | cout << "TODO:" << __func__; |
| 1381 | } |
| 1382 | |
| 1383 | static void pf_setlk(fuse_req_t req, fuse_ino_t ino, |
| 1384 | struct fuse_file_info* fi, |
| 1385 | struct flock* lock, int sleep) |
| 1386 | { |
| 1387 | cout << "TODO:" << __func__; |
| 1388 | } |
| 1389 | |
| 1390 | static void pf_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, |
| 1391 | uint64_t idx) |
| 1392 | { |
| 1393 | cout << "TODO:" << __func__; |
| 1394 | } |
| 1395 | |
| 1396 | static void pf_ioctl(fuse_req_t req, fuse_ino_t ino, unsigned int cmd, |
| 1397 | void* arg, struct fuse_file_info* fi, unsigned flags, |
| 1398 | const void* in_buf, size_t in_bufsz, size_t out_bufsz) |
| 1399 | { |
| 1400 | cout << "TODO:" << __func__; |
| 1401 | } |
| 1402 | |
| 1403 | static void pf_poll(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi, |
| 1404 | struct fuse_pollhandle* ph) |
| 1405 | { |
| 1406 | cout << "TODO:" << __func__; |
| 1407 | } |
| 1408 | |
| 1409 | static void pf_retrieve_reply(fuse_req_t req, void* cookie, fuse_ino_t ino, |
| 1410 | off_t offset, struct fuse_bufvec* bufv) |
| 1411 | { |
| 1412 | cout << "TODO:" << __func__; |
| 1413 | } |
| 1414 | |
| 1415 | static void pf_flock(fuse_req_t req, fuse_ino_t ino, |
| 1416 | struct fuse_file_info* fi, int op) |
| 1417 | { |
| 1418 | cout << "TODO:" << __func__; |
| 1419 | } |
| 1420 | |
| 1421 | static void pf_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, |
| 1422 | off_t offset, off_t length, struct fuse_file_info* fi) |
| 1423 | { |
| 1424 | cout << "TODO:" << __func__; |
| 1425 | } |
| 1426 | */ |
| 1427 | |
| 1428 | static struct fuse_lowlevel_ops ops{ |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 1429 | .init = pf_init, |
Zim | 005cd81 | 2019-12-17 15:35:51 +0000 | [diff] [blame] | 1430 | .destroy = pf_destroy, |
Nick Desaulniers | 17714b8 | 2019-11-05 09:44:35 -0800 | [diff] [blame] | 1431 | .lookup = pf_lookup, .forget = pf_forget, .getattr = pf_getattr, |
| 1432 | .setattr = pf_setattr, |
Zim | b9730bf | 2019-11-30 15:10:04 +0000 | [diff] [blame] | 1433 | .canonical_path = pf_canonical_path, |
Nick Desaulniers | 17714b8 | 2019-11-05 09:44:35 -0800 | [diff] [blame] | 1434 | .mknod = pf_mknod, .mkdir = pf_mkdir, .unlink = pf_unlink, |
| 1435 | .rmdir = pf_rmdir, |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 1436 | /*.symlink = pf_symlink,*/ |
| 1437 | .rename = pf_rename, |
| 1438 | /*.link = pf_link,*/ |
| 1439 | .open = pf_open, .read = pf_read, |
| 1440 | /*.write = pf_write,*/ |
Nick Desaulniers | 17714b8 | 2019-11-05 09:44:35 -0800 | [diff] [blame] | 1441 | .flush = pf_flush, .release = pf_release, .fsync = pf_fsync, |
| 1442 | .opendir = pf_opendir, .readdir = pf_readdir, .releasedir = pf_releasedir, |
| 1443 | .fsyncdir = pf_fsyncdir, .statfs = pf_statfs, |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 1444 | /*.setxattr = pf_setxattr, |
| 1445 | .getxattr = pf_getxattr, |
| 1446 | .listxattr = pf_listxattr, |
| 1447 | .removexattr = pf_removexattr,*/ |
| 1448 | .access = pf_access, .create = pf_create, |
| 1449 | /*.getlk = pf_getlk, |
| 1450 | .setlk = pf_setlk, |
| 1451 | .bmap = pf_bmap, |
| 1452 | .ioctl = pf_ioctl, |
Nick Desaulniers | 17714b8 | 2019-11-05 09:44:35 -0800 | [diff] [blame] | 1453 | .poll = pf_poll,*/ |
| 1454 | .write_buf = pf_write_buf, |
| 1455 | /*.retrieve_reply = pf_retrieve_reply,*/ |
| 1456 | .forget_multi = pf_forget_multi, |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 1457 | /*.flock = pf_flock, |
| 1458 | .fallocate = pf_fallocate,*/ |
Nick Desaulniers | 17714b8 | 2019-11-05 09:44:35 -0800 | [diff] [blame] | 1459 | .readdirplus = pf_readdirplus, |
| 1460 | /*.copy_file_range = pf_copy_file_range,*/ |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1461 | }; |
| 1462 | |
| 1463 | static struct fuse_loop_config config = { |
| 1464 | .clone_fd = 0, |
| 1465 | .max_idle_threads = 10, |
| 1466 | }; |
| 1467 | |
Zim | b353eb4 | 2019-12-17 15:29:48 +0000 | [diff] [blame] | 1468 | static std::unordered_map<enum fuse_log_level, enum android_LogPriority> fuse_to_android_loglevel({ |
| 1469 | {FUSE_LOG_EMERG, ANDROID_LOG_FATAL}, |
| 1470 | {FUSE_LOG_ALERT, ANDROID_LOG_ERROR}, |
| 1471 | {FUSE_LOG_CRIT, ANDROID_LOG_ERROR}, |
| 1472 | {FUSE_LOG_ERR, ANDROID_LOG_ERROR}, |
| 1473 | {FUSE_LOG_WARNING, ANDROID_LOG_WARN}, |
| 1474 | {FUSE_LOG_NOTICE, ANDROID_LOG_INFO}, |
| 1475 | {FUSE_LOG_INFO, ANDROID_LOG_DEBUG}, |
| 1476 | {FUSE_LOG_DEBUG, ANDROID_LOG_VERBOSE}, |
| 1477 | }); |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 1478 | |
| 1479 | 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] | 1480 | __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] | 1481 | } |
| 1482 | |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1483 | bool FuseDaemon::ShouldOpenWithFuse(int fd, bool for_read, const std::string& path) { |
Zim | 5c57d83 | 2020-01-15 16:16:12 +0000 | [diff] [blame] | 1484 | TRACE_VERBOSE << "Checking if file should be opened with FUSE " << path; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1485 | bool use_fuse = false; |
| 1486 | |
| 1487 | if (active.load(std::memory_order_acquire)) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1488 | const node* node = node::LookupAbsolutePath(fuse->root, path); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1489 | if (node && node->HasCachedHandle()) { |
| 1490 | TRACE << "Should open " << path << " with FUSE. Reason: cache"; |
| 1491 | use_fuse = true; |
| 1492 | } else { |
| 1493 | // If we are unable to set a lock, we should use fuse since we can't track |
| 1494 | // when all fd references (including dups) are closed. This can happen when |
| 1495 | // we try to set a write lock twice on the same file |
| 1496 | use_fuse = set_file_lock(fd, for_read, path); |
| 1497 | TRACE << "Should open " << path << (use_fuse ? " with" : " without") |
| 1498 | << " FUSE. Reason: lock"; |
| 1499 | } |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1500 | } else { |
| 1501 | TRACE << "FUSE daemon is inactive. Should not open " << path << " with FUSE"; |
| 1502 | } |
| 1503 | |
| 1504 | return use_fuse; |
| 1505 | } |
| 1506 | |
| 1507 | FuseDaemon::FuseDaemon(JNIEnv* env, jobject mediaProvider) : mp(env, mediaProvider), |
| 1508 | active(false), fuse(nullptr) {} |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1509 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 1510 | void FuseDaemon::Start(const int fd, const std::string& path) { |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1511 | struct fuse_args args; |
| 1512 | struct fuse_cmdline_opts opts; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1513 | |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 1514 | SetMinimumLogSeverity(android::base::DEBUG); |
| 1515 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1516 | struct stat stat; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1517 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 1518 | if (lstat(path.c_str(), &stat)) { |
| 1519 | LOG(ERROR) << "ERROR: failed to stat source " << path; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 1520 | return; |
| 1521 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1522 | |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 1523 | if (!S_ISDIR(stat.st_mode)) { |
shafik | 458d110 | 2019-09-06 18:21:36 +0100 | [diff] [blame] | 1524 | LOG(ERROR) << "ERROR: source is not a directory"; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 1525 | return; |
| 1526 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1527 | |
| 1528 | args = FUSE_ARGS_INIT(0, nullptr); |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 1529 | if (fuse_opt_add_arg(&args, path.c_str()) || fuse_opt_add_arg(&args, "-odebug") || |
| 1530 | 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] | 1531 | LOG(ERROR) << "ERROR: failed to set options"; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1532 | return; |
| 1533 | } |
| 1534 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1535 | struct fuse fuse_default(path); |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1536 | fuse_default.mp = ∓ |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1537 | // fuse_default is stack allocated, but it's safe to save it as an instance variable because |
| 1538 | // this method blocks and FuseDaemon#active tells if we are currently blocking |
| 1539 | fuse = &fuse_default; |
shafik | c3f6267 | 2019-08-30 11:15:48 +0100 | [diff] [blame] | 1540 | |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 1541 | // Used by pf_read: redacted ranges are represented by zeroized ranges of bytes, |
| 1542 | // so we mmap the maximum length of redacted ranges in the beginning and save memory allocations |
| 1543 | // on each read. |
| 1544 | fuse_default.zero_addr = static_cast<char*>(mmap( |
| 1545 | NULL, MAX_READ_SIZE, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, /*fd*/ -1, /*off*/ 0)); |
| 1546 | if (fuse_default.zero_addr == MAP_FAILED) { |
| 1547 | LOG(FATAL) << "mmap failed - could not start fuse! errno = " << errno; |
| 1548 | } |
| 1549 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 1550 | umask(0); |
| 1551 | |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 1552 | // Custom logging for libfuse |
Zim | 724b8ca | 2019-11-09 09:37:24 +0000 | [diff] [blame] | 1553 | fuse_set_log_func(fuse_logger); |
| 1554 | |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1555 | struct fuse_session |
| 1556 | * se = fuse_session_new(&args, &ops, sizeof(ops), &fuse_default); |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 1557 | if (!se) { |
| 1558 | PLOG(ERROR) << "Failed to create session "; |
| 1559 | return; |
| 1560 | } |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1561 | se->fd = fd; |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 1562 | se->mountpoint = strdup(path.c_str()); |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 1563 | |
| 1564 | // Single thread. Useful for debugging |
| 1565 | // fuse_session_loop(se); |
| 1566 | // Multi-threaded |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 1567 | LOG(INFO) << "Starting fuse..."; |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1568 | active.store(true, std::memory_order_release); |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1569 | fuse_session_loop_mt(se, &config); |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 1570 | LOG(INFO) << "Ending fuse..."; |
Zim | 3e45d9b | 2019-08-19 21:14:14 +0100 | [diff] [blame] | 1571 | |
Zim | 7c8712d | 2019-10-03 21:01:26 +0100 | [diff] [blame] | 1572 | if (munmap(fuse_default.zero_addr, MAX_READ_SIZE)) { |
| 1573 | PLOG(ERROR) << "munmap failed!"; |
shafik | b334a61 | 2019-09-30 20:38:16 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 1576 | fuse_opt_free_args(&args); |
| 1577 | fuse_session_destroy(se); |
Zim | edbe69e | 2019-12-13 18:49:36 +0000 | [diff] [blame] | 1578 | active.store(false, std::memory_order_relaxed); |
Zim | 7e0d314 | 2019-10-17 21:06:12 +0100 | [diff] [blame] | 1579 | LOG(INFO) << "Ended fuse"; |
| 1580 | return; |
shafik | 1e3a267 | 2019-08-16 14:51:55 +0100 | [diff] [blame] | 1581 | } |
| 1582 | } //namespace fuse |
shafik | 8b57cd5 | 2019-09-06 10:51:29 +0100 | [diff] [blame] | 1583 | } // namespace mediaprovider |