Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 300d564 | 2014-07-08 13:53:26 -0700 | [diff] [blame] | 17 | #define LOG_TAG "sdcard" |
| 18 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 19 | #include "fuse.h" |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 20 | |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 23 | #define FUSE_UNKNOWN_INO 0xffffffff |
| 24 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 25 | /* Pseudo-error constant used to indicate that no fuse status is needed |
| 26 | * or that a reply has already been written. */ |
| 27 | #define NO_STATUS 1 |
| 28 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 29 | static inline void *id_to_ptr(__u64 nid) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 30 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 31 | return (void *) (uintptr_t) nid; |
| 32 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 33 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 34 | static inline __u64 ptr_to_id(void *ptr) |
| 35 | { |
| 36 | return (__u64) (uintptr_t) ptr; |
| 37 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 38 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 39 | static void acquire_node_locked(struct node* node) |
| 40 | { |
| 41 | node->refcount++; |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 42 | DLOG(INFO) << "ACQUIRE " << std::hex << node << std::dec |
| 43 | << " (" << node->name << ") rc=" << node->refcount; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static void remove_node_from_parent_locked(struct node* node); |
| 47 | |
| 48 | static void release_node_locked(struct node* node) |
| 49 | { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 50 | DLOG(INFO) << "RELEASE " << std::hex << node << std::dec |
| 51 | << " (" << node->name << ") rc=" << node->refcount; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 52 | if (node->refcount > 0) { |
| 53 | node->refcount--; |
| 54 | if (!node->refcount) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 55 | DLOG(INFO) << "DESTROY " << std::hex << node << std::dec << " (" << node->name << ")"; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 56 | remove_node_from_parent_locked(node); |
| 57 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 58 | /* TODO: remove debugging - poison memory */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 59 | memset(node->name, 0xef, node->namelen); |
| 60 | free(node->name); |
| 61 | free(node->actual_name); |
| 62 | memset(node, 0xfc, sizeof(*node)); |
| 63 | free(node); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 64 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 65 | } else { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 66 | LOG(ERROR) << std::hex << node << std::dec << " refcount=0"; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | static void add_node_to_parent_locked(struct node *node, struct node *parent) { |
| 71 | node->parent = parent; |
| 72 | node->next = parent->child; |
| 73 | parent->child = node; |
| 74 | acquire_node_locked(parent); |
| 75 | } |
| 76 | |
| 77 | static void remove_node_from_parent_locked(struct node* node) |
| 78 | { |
| 79 | if (node->parent) { |
| 80 | if (node->parent->child == node) { |
| 81 | node->parent->child = node->parent->child->next; |
| 82 | } else { |
| 83 | struct node *node2; |
| 84 | node2 = node->parent->child; |
| 85 | while (node2->next != node) |
| 86 | node2 = node2->next; |
| 87 | node2->next = node->next; |
| 88 | } |
| 89 | release_node_locked(node->parent); |
| 90 | node->parent = NULL; |
| 91 | node->next = NULL; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* Gets the absolute path to a node into the provided buffer. |
| 96 | * |
| 97 | * Populates 'buf' with the path and returns the length of the path on success, |
| 98 | * or returns -1 if the path is too long for the provided buffer. |
| 99 | */ |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 100 | static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) { |
| 101 | const char* name; |
| 102 | size_t namelen; |
| 103 | if (node->graft_path) { |
| 104 | name = node->graft_path; |
| 105 | namelen = node->graft_pathlen; |
| 106 | } else if (node->actual_name) { |
| 107 | name = node->actual_name; |
| 108 | namelen = node->namelen; |
| 109 | } else { |
| 110 | name = node->name; |
| 111 | namelen = node->namelen; |
| 112 | } |
| 113 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 114 | if (bufsize < namelen + 1) { |
| 115 | return -1; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 118 | ssize_t pathlen = 0; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 119 | if (node->parent && node->graft_path == NULL) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 120 | pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2); |
| 121 | if (pathlen < 0) { |
| 122 | return -1; |
| 123 | } |
| 124 | buf[pathlen++] = '/'; |
| 125 | } |
| 126 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 127 | memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */ |
| 128 | return pathlen + namelen; |
| 129 | } |
| 130 | |
| 131 | /* Finds the absolute path of a file within a given directory. |
| 132 | * Performs a case-insensitive search for the file and sets the buffer to the path |
| 133 | * of the first matching file. If 'search' is zero or if no match is found, sets |
| 134 | * the buffer to the path that the file would have, assuming the name were case-sensitive. |
| 135 | * |
| 136 | * Populates 'buf' with the path and returns the actual name (within 'buf') on success, |
| 137 | * or returns NULL if the path is too long for the provided buffer. |
| 138 | */ |
| 139 | static char* find_file_within(const char* path, const char* name, |
| 140 | char* buf, size_t bufsize, int search) |
| 141 | { |
| 142 | size_t pathlen = strlen(path); |
| 143 | size_t namelen = strlen(name); |
| 144 | size_t childlen = pathlen + namelen + 1; |
| 145 | char* actual; |
| 146 | |
| 147 | if (bufsize <= childlen) { |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | memcpy(buf, path, pathlen); |
| 152 | buf[pathlen] = '/'; |
| 153 | actual = buf + pathlen + 1; |
| 154 | memcpy(actual, name, namelen + 1); |
| 155 | |
| 156 | if (search && access(buf, F_OK)) { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 157 | struct dirent* entry; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 158 | DIR* dir = opendir(path); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 159 | if (!dir) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 160 | PLOG(ERROR) << "opendir(" << path << ") failed"; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 161 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 162 | } |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 163 | while ((entry = readdir(dir))) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 164 | if (!strcasecmp(entry->d_name, name)) { |
| 165 | /* we have a match - replace the name, don't need to copy the null again */ |
| 166 | memcpy(actual, entry->d_name, namelen); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | } |
| 170 | closedir(dir); |
| 171 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 172 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 175 | static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr, |
| 176 | const struct stat *s, const struct node* node) { |
Narayan Kamath | faa0935 | 2015-01-13 18:21:10 +0000 | [diff] [blame] | 177 | attr->ino = node->ino; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 178 | attr->size = s->st_size; |
| 179 | attr->blocks = s->st_blocks; |
Elliott Hughes | f1df854 | 2014-11-10 11:03:38 -0800 | [diff] [blame] | 180 | attr->atime = s->st_atim.tv_sec; |
| 181 | attr->mtime = s->st_mtim.tv_sec; |
| 182 | attr->ctime = s->st_ctim.tv_sec; |
| 183 | attr->atimensec = s->st_atim.tv_nsec; |
| 184 | attr->mtimensec = s->st_mtim.tv_nsec; |
| 185 | attr->ctimensec = s->st_ctim.tv_nsec; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 186 | attr->mode = s->st_mode; |
| 187 | attr->nlink = s->st_nlink; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 188 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 189 | attr->uid = node->uid; |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 190 | |
| 191 | if (fuse->gid == AID_SDCARD_RW) { |
| 192 | /* As an optimization, certain trusted system components only run |
| 193 | * as owner but operate across all users. Since we're now handing |
| 194 | * out the sdcard_rw GID only to trusted apps, we're okay relaxing |
| 195 | * the user boundary enforcement for the default view. The UIDs |
| 196 | * assigned to app directories are still multiuser aware. */ |
| 197 | attr->gid = AID_SDCARD_RW; |
| 198 | } else { |
| 199 | attr->gid = multiuser_get_uid(node->userid, fuse->gid); |
| 200 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 201 | |
Jeff Sharkey | 10a239b | 2015-07-28 10:49:41 -0700 | [diff] [blame] | 202 | int visible_mode = 0775 & ~fuse->mask; |
| 203 | if (node->perm == PERM_PRE_ROOT) { |
| 204 | /* Top of multi-user view should always be visible to ensure |
| 205 | * secondary users can traverse inside. */ |
| 206 | visible_mode = 0711; |
| 207 | } else if (node->under_android) { |
| 208 | /* Block "other" access to Android directories, since only apps |
| 209 | * belonging to a specific user should be in there; we still |
| 210 | * leave +x open for the default view. */ |
| 211 | if (fuse->gid == AID_SDCARD_RW) { |
| 212 | visible_mode = visible_mode & ~0006; |
| 213 | } else { |
| 214 | visible_mode = visible_mode & ~0007; |
| 215 | } |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 216 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 217 | int owner_mode = s->st_mode & 0700; |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 218 | int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 219 | attr->mode = (attr->mode & S_IFMT) | filtered_mode; |
| 220 | } |
| 221 | |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 222 | static int touch(char* path, mode_t mode) { |
| 223 | int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode); |
| 224 | if (fd == -1) { |
| 225 | if (errno == EEXIST) { |
| 226 | return 0; |
| 227 | } else { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 228 | PLOG(ERROR) << "open(" << path << ") failed"; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 229 | return -1; |
| 230 | } |
| 231 | } |
| 232 | close(fd); |
| 233 | return 0; |
| 234 | } |
| 235 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 236 | static void derive_permissions_locked(struct fuse* fuse, struct node *parent, |
| 237 | struct node *node) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 238 | appid_t appid; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 239 | |
| 240 | /* By default, each node inherits from its parent */ |
| 241 | node->perm = PERM_INHERIT; |
| 242 | node->userid = parent->userid; |
| 243 | node->uid = parent->uid; |
Jeff Sharkey | 10a239b | 2015-07-28 10:49:41 -0700 | [diff] [blame] | 244 | node->under_android = parent->under_android; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 245 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 246 | /* Derive custom permissions based on parent and current node */ |
| 247 | switch (parent->perm) { |
| 248 | case PERM_INHERIT: |
| 249 | /* Already inherited above */ |
| 250 | break; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 251 | case PERM_PRE_ROOT: |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 252 | /* Legacy internal layout places users at top level */ |
| 253 | node->perm = PERM_ROOT; |
| 254 | node->userid = strtoul(node->name, NULL, 10); |
| 255 | break; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 256 | case PERM_ROOT: |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 257 | /* Assume masked off by default. */ |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 258 | if (!strcasecmp(node->name, "Android")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 259 | /* App-specific directories inside; let anyone traverse */ |
| 260 | node->perm = PERM_ANDROID; |
Jeff Sharkey | 10a239b | 2015-07-28 10:49:41 -0700 | [diff] [blame] | 261 | node->under_android = true; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 262 | } |
| 263 | break; |
| 264 | case PERM_ANDROID: |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 265 | if (!strcasecmp(node->name, "data")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 266 | /* App-specific directories inside; let anyone traverse */ |
| 267 | node->perm = PERM_ANDROID_DATA; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 268 | } else if (!strcasecmp(node->name, "obb")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 269 | /* App-specific directories inside; let anyone traverse */ |
| 270 | node->perm = PERM_ANDROID_OBB; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 271 | /* Single OBB directory is always shared */ |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 272 | node->graft_path = fuse->global->obb_path; |
| 273 | node->graft_pathlen = strlen(fuse->global->obb_path); |
Jeff Sharkey | 2e7d80d | 2014-05-30 15:38:31 -0700 | [diff] [blame] | 274 | } else if (!strcasecmp(node->name, "media")) { |
| 275 | /* App-specific directories inside; let anyone traverse */ |
| 276 | node->perm = PERM_ANDROID_MEDIA; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 277 | } |
| 278 | break; |
| 279 | case PERM_ANDROID_DATA: |
| 280 | case PERM_ANDROID_OBB: |
Jeff Sharkey | 2e7d80d | 2014-05-30 15:38:31 -0700 | [diff] [blame] | 281 | case PERM_ANDROID_MEDIA: |
Jorge Lucangeli Obes | d6d8faa | 2016-07-19 12:10:26 -0400 | [diff] [blame] | 282 | const auto& iter = fuse->global->package_to_appid->find(node->name); |
| 283 | if (iter != fuse->global->package_to_appid->end()) { |
| 284 | appid = iter->second; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 285 | node->uid = multiuser_get_uid(parent->userid, appid); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 286 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 287 | break; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 288 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 291 | void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) { |
Jeff Sharkey | 22b9126 | 2015-12-14 11:02:01 -0700 | [diff] [blame] | 292 | struct node *node; |
| 293 | for (node = parent->child; node; node = node->next) { |
| 294 | derive_permissions_locked(fuse, parent, node); |
| 295 | if (node->child) { |
| 296 | derive_permissions_recursive_locked(fuse, node); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 301 | /* Kernel has already enforced everything we returned through |
| 302 | * derive_permissions_locked(), so this is used to lock down access |
| 303 | * even further, such as enforcing that apps hold sdcard_rw. */ |
| 304 | static bool check_caller_access_to_name(struct fuse* fuse, |
| 305 | const struct fuse_in_header *hdr, const struct node* parent_node, |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 306 | const char* name, int mode) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 307 | /* Always block security-sensitive files at root */ |
| 308 | if (parent_node && parent_node->perm == PERM_ROOT) { |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 309 | if (!strcasecmp(name, "autorun.inf") |
| 310 | || !strcasecmp(name, ".android_secure") |
| 311 | || !strcasecmp(name, "android_secure")) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 316 | /* Root always has access; access for any other UIDs should always |
| 317 | * be controlled through packages.list. */ |
| 318 | if (hdr->uid == 0) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 322 | /* No extra permissions to enforce */ |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | static bool check_caller_access_to_node(struct fuse* fuse, |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 327 | const struct fuse_in_header *hdr, const struct node* node, int mode) { |
| 328 | return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 331 | struct node *create_node_locked(struct fuse* fuse, |
| 332 | struct node *parent, const char *name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 333 | { |
| 334 | struct node *node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 335 | size_t namelen = strlen(name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 336 | |
Narayan Kamath | faa0935 | 2015-01-13 18:21:10 +0000 | [diff] [blame] | 337 | // Detect overflows in the inode counter. "4 billion nodes should be enough |
| 338 | // for everybody". |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 339 | if (fuse->global->inode_ctr == 0) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 340 | LOG(ERROR) << "No more inode numbers available"; |
Narayan Kamath | faa0935 | 2015-01-13 18:21:10 +0000 | [diff] [blame] | 341 | return NULL; |
| 342 | } |
| 343 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 344 | node = static_cast<struct node*>(calloc(1, sizeof(struct node))); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 345 | if (!node) { |
| 346 | return NULL; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 347 | } |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 348 | node->name = static_cast<char*>(malloc(namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 349 | if (!node->name) { |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 350 | free(node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 351 | return NULL; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 352 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 353 | memcpy(node->name, name, namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 354 | if (strcmp(name, actual_name)) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 355 | node->actual_name = static_cast<char*>(malloc(namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 356 | if (!node->actual_name) { |
| 357 | free(node->name); |
| 358 | free(node); |
| 359 | return NULL; |
| 360 | } |
| 361 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 362 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 363 | node->namelen = namelen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 364 | node->nid = ptr_to_id(node); |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 365 | node->ino = fuse->global->inode_ctr++; |
| 366 | node->gen = fuse->global->next_generation++; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 367 | |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 368 | node->deleted = false; |
| 369 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 370 | derive_permissions_locked(fuse, parent, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 371 | acquire_node_locked(node); |
| 372 | add_node_to_parent_locked(node, parent); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 373 | return node; |
| 374 | } |
| 375 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 376 | static int rename_node_locked(struct node *node, const char *name, |
| 377 | const char* actual_name) |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 378 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 379 | size_t namelen = strlen(name); |
| 380 | int need_actual_name = strcmp(name, actual_name); |
| 381 | |
| 382 | /* make the storage bigger without actually changing the name |
| 383 | * in case an error occurs part way */ |
| 384 | if (namelen > node->namelen) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 385 | char* new_name = static_cast<char*>(realloc(node->name, namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 386 | if (!new_name) { |
| 387 | return -ENOMEM; |
| 388 | } |
| 389 | node->name = new_name; |
| 390 | if (need_actual_name && node->actual_name) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 391 | char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 392 | if (!new_actual_name) { |
| 393 | return -ENOMEM; |
| 394 | } |
| 395 | node->actual_name = new_actual_name; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* update the name, taking care to allocate storage before overwriting the old name */ |
| 400 | if (need_actual_name) { |
| 401 | if (!node->actual_name) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 402 | node->actual_name = static_cast<char*>(malloc(namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 403 | if (!node->actual_name) { |
| 404 | return -ENOMEM; |
| 405 | } |
| 406 | } |
| 407 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 408 | } else { |
| 409 | free(node->actual_name); |
| 410 | node->actual_name = NULL; |
| 411 | } |
| 412 | memcpy(node->name, name, namelen + 1); |
| 413 | node->namelen = namelen; |
| 414 | return 0; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 417 | static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 418 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 419 | if (nid == FUSE_ROOT_ID) { |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 420 | return &fuse->global->root; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 421 | } else { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 422 | return static_cast<struct node*>(id_to_ptr(nid)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 426 | static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid, |
| 427 | char* buf, size_t bufsize) |
| 428 | { |
| 429 | struct node* node = lookup_node_by_id_locked(fuse, nid); |
| 430 | if (node && get_node_path_locked(node, buf, bufsize) < 0) { |
| 431 | node = NULL; |
| 432 | } |
| 433 | return node; |
| 434 | } |
| 435 | |
| 436 | static struct node *lookup_child_by_name_locked(struct node *node, const char *name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 437 | { |
| 438 | for (node = node->child; node; node = node->next) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 439 | /* use exact string comparison, nodes that differ by case |
| 440 | * must be considered distinct even if they refer to the same |
| 441 | * underlying file as otherwise operations such as "mv x x" |
| 442 | * will not work because the source and target nodes are the same. */ |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 443 | if (!strcmp(name, node->name) && !node->deleted) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 444 | return node; |
| 445 | } |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 450 | static struct node* acquire_or_create_child_locked( |
| 451 | struct fuse* fuse, struct node* parent, |
| 452 | const char* name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 453 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 454 | struct node* child = lookup_child_by_name_locked(parent, name); |
| 455 | if (child) { |
| 456 | acquire_node_locked(child); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 457 | } else { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 458 | child = create_node_locked(fuse, parent, name, actual_name); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 459 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 460 | return child; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 463 | static void fuse_status(struct fuse *fuse, __u64 unique, int err) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 464 | { |
| 465 | struct fuse_out_header hdr; |
| 466 | hdr.len = sizeof(hdr); |
| 467 | hdr.error = err; |
| 468 | hdr.unique = unique; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 469 | write(fuse->fd, &hdr, sizeof(hdr)); |
| 470 | } |
| 471 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 472 | static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 473 | { |
| 474 | struct fuse_out_header hdr; |
| 475 | struct iovec vec[2]; |
| 476 | int res; |
| 477 | |
| 478 | hdr.len = len + sizeof(hdr); |
| 479 | hdr.error = 0; |
| 480 | hdr.unique = unique; |
| 481 | |
| 482 | vec[0].iov_base = &hdr; |
| 483 | vec[0].iov_len = sizeof(hdr); |
| 484 | vec[1].iov_base = data; |
| 485 | vec[1].iov_len = len; |
| 486 | |
| 487 | res = writev(fuse->fd, vec, 2); |
| 488 | if (res < 0) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 489 | PLOG(ERROR) << "*** REPLY FAILED ***"; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 493 | static int fuse_reply_entry(struct fuse* fuse, __u64 unique, |
| 494 | struct node* parent, const char* name, const char* actual_name, |
| 495 | const char* path) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 496 | { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 497 | struct node* node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 498 | struct fuse_entry_out out; |
| 499 | struct stat s; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 500 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 501 | if (lstat(path, &s) < 0) { |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 502 | return -errno; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 503 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 504 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 505 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 506 | node = acquire_or_create_child_locked(fuse, parent, name, actual_name); |
| 507 | if (!node) { |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 508 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 509 | return -ENOMEM; |
| 510 | } |
| 511 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 512 | attr_from_stat(fuse, &out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 513 | out.attr_valid = 10; |
| 514 | out.entry_valid = 10; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 515 | out.nodeid = node->nid; |
| 516 | out.generation = node->gen; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 517 | pthread_mutex_unlock(&fuse->global->lock); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 518 | fuse_reply(fuse, unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 519 | return NO_STATUS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 522 | static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 523 | const char* path) |
| 524 | { |
| 525 | struct fuse_attr_out out; |
| 526 | struct stat s; |
| 527 | |
| 528 | if (lstat(path, &s) < 0) { |
| 529 | return -errno; |
| 530 | } |
| 531 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 532 | attr_from_stat(fuse, &out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 533 | out.attr_valid = 10; |
| 534 | fuse_reply(fuse, unique, &out, sizeof(out)); |
| 535 | return NO_STATUS; |
| 536 | } |
| 537 | |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 538 | static void fuse_notify_delete(struct fuse* fuse, const __u64 parent, |
| 539 | const __u64 child, const char* name) { |
| 540 | struct fuse_out_header hdr; |
| 541 | struct fuse_notify_delete_out data; |
| 542 | struct iovec vec[3]; |
| 543 | size_t namelen = strlen(name); |
| 544 | int res; |
| 545 | |
| 546 | hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1; |
| 547 | hdr.error = FUSE_NOTIFY_DELETE; |
| 548 | hdr.unique = 0; |
| 549 | |
| 550 | data.parent = parent; |
| 551 | data.child = child; |
| 552 | data.namelen = namelen; |
| 553 | data.padding = 0; |
| 554 | |
| 555 | vec[0].iov_base = &hdr; |
| 556 | vec[0].iov_len = sizeof(hdr); |
| 557 | vec[1].iov_base = &data; |
| 558 | vec[1].iov_len = sizeof(data); |
| 559 | vec[2].iov_base = (void*) name; |
| 560 | vec[2].iov_len = namelen + 1; |
| 561 | |
| 562 | res = writev(fuse->fd, vec, 3); |
| 563 | /* Ignore ENOENT, since other views may not have seen the entry */ |
| 564 | if (res < 0 && errno != ENOENT) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 565 | PLOG(ERROR) << "*** NOTIFY FAILED ***"; |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 569 | static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 570 | const struct fuse_in_header *hdr, const char* name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 571 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 572 | struct node* parent_node; |
| 573 | char parent_path[PATH_MAX]; |
| 574 | char child_path[PATH_MAX]; |
| 575 | const char* actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 576 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 577 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 578 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 579 | parent_path, sizeof(parent_path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 580 | DLOG(INFO) << "[" << handler->token << "] LOOKUP " << name << " @ " << hdr->nodeid |
| 581 | << " (" << (parent_node ? parent_node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 582 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 583 | |
| 584 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 585 | child_path, sizeof(child_path), 1))) { |
| 586 | return -ENOENT; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 587 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 588 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 589 | return -EACCES; |
| 590 | } |
| 591 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 592 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 595 | static int handle_forget(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 596 | const struct fuse_in_header *hdr, const struct fuse_forget_in *req) |
| 597 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 598 | struct node* node; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 599 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 600 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 601 | node = lookup_node_by_id_locked(fuse, hdr->nodeid); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 602 | DLOG(INFO) << "[" << handler->token << "] FORGET #" << req->nlookup |
| 603 | << " @ " << std::hex << hdr->nodeid |
| 604 | << " (" << (node ? node->name : "?") << ")"; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 605 | if (node) { |
| 606 | __u64 n = req->nlookup; |
Daniel Micay | df9c4a0 | 2016-04-26 11:42:08 -0400 | [diff] [blame] | 607 | while (n) { |
| 608 | n--; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 609 | release_node_locked(node); |
| 610 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 611 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 612 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 613 | return NO_STATUS; /* no reply */ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 616 | static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 617 | const struct fuse_in_header *hdr, const struct fuse_getattr_in *req) |
| 618 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 619 | struct node* node; |
| 620 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 621 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 622 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 623 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 624 | DLOG(INFO) << "[" << handler->token << "] GETATTR flags=" << req->getattr_flags |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 625 | << " fh=" << std::hex << req->fh << " @ " << hdr->nodeid << std::dec |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 626 | << " (" << (node ? node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 627 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 628 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 629 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 630 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 631 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 632 | if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 633 | return -EACCES; |
| 634 | } |
| 635 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 636 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 639 | static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 640 | const struct fuse_in_header *hdr, const struct fuse_setattr_in *req) |
| 641 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 642 | struct node* node; |
| 643 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 644 | struct timespec times[2]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 645 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 646 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 647 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 648 | DLOG(INFO) << "[" << handler->token << "] SETATTR fh=" << std::hex << req->fh |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 649 | << " valid=" << std::hex << req->valid << " @ " << hdr->nodeid << std::dec |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 650 | << " (" << (node ? node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 651 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 652 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 653 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 654 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 655 | } |
Marco Nelissen | a80f098 | 2014-12-10 10:44:20 -0800 | [diff] [blame] | 656 | |
| 657 | if (!(req->valid & FATTR_FH) && |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 658 | !check_caller_access_to_node(fuse, hdr, node, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 659 | return -EACCES; |
| 660 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 661 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 662 | /* XXX: incomplete implementation on purpose. |
| 663 | * chmod/chown should NEVER be implemented.*/ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 664 | |
Elliott Hughes | 853574d | 2014-07-31 12:03:03 -0700 | [diff] [blame] | 665 | if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 666 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 670 | * are both set, then set it to the current time. Else, set it to the |
| 671 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 672 | * as it allows ATIME and MTIME to be changed independently, and has |
| 673 | * nanosecond resolution which fuse also has. |
| 674 | */ |
| 675 | if (req->valid & (FATTR_ATIME | FATTR_MTIME)) { |
| 676 | times[0].tv_nsec = UTIME_OMIT; |
| 677 | times[1].tv_nsec = UTIME_OMIT; |
| 678 | if (req->valid & FATTR_ATIME) { |
| 679 | if (req->valid & FATTR_ATIME_NOW) { |
| 680 | times[0].tv_nsec = UTIME_NOW; |
| 681 | } else { |
| 682 | times[0].tv_sec = req->atime; |
| 683 | times[0].tv_nsec = req->atimensec; |
| 684 | } |
| 685 | } |
| 686 | if (req->valid & FATTR_MTIME) { |
| 687 | if (req->valid & FATTR_MTIME_NOW) { |
| 688 | times[1].tv_nsec = UTIME_NOW; |
| 689 | } else { |
| 690 | times[1].tv_sec = req->mtime; |
| 691 | times[1].tv_nsec = req->mtimensec; |
| 692 | } |
| 693 | } |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 694 | DLOG(INFO) << "[" << handler->token << "] Calling utimensat on " << path |
| 695 | << " with atime " << times[0].tv_sec << ", mtime=" << times[1].tv_sec; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 696 | if (utimensat(-1, path, times, 0) < 0) { |
| 697 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 698 | } |
| 699 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 700 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 703 | static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 704 | const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name) |
| 705 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 706 | struct node* parent_node; |
| 707 | char parent_path[PATH_MAX]; |
| 708 | char child_path[PATH_MAX]; |
| 709 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 710 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 711 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 712 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 713 | parent_path, sizeof(parent_path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 714 | DLOG(INFO) << "[" << handler->token << "] MKNOD " << name << " 0" << std::oct << req->mode |
| 715 | << " @ " << std::hex << hdr->nodeid |
| 716 | << " (" << (parent_node ? parent_node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 717 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 718 | |
| 719 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 720 | child_path, sizeof(child_path), 1))) { |
| 721 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 722 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 723 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 724 | return -EACCES; |
| 725 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 726 | __u32 mode = (req->mode & (~0777)) | 0664; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 727 | if (mknod(child_path, mode, req->rdev) < 0) { |
| 728 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 729 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 730 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 733 | static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 734 | const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name) |
| 735 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 736 | struct node* parent_node; |
| 737 | char parent_path[PATH_MAX]; |
| 738 | char child_path[PATH_MAX]; |
| 739 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 740 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 741 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 742 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 743 | parent_path, sizeof(parent_path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 744 | DLOG(INFO) << "[" << handler->token << "] MKDIR " << name << " 0" << std::oct << req->mode |
| 745 | << " @ " << std::hex << hdr->nodeid |
| 746 | << " (" << (parent_node ? parent_node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 747 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 748 | |
| 749 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 750 | child_path, sizeof(child_path), 1))) { |
| 751 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 752 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 753 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 754 | return -EACCES; |
| 755 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 756 | __u32 mode = (req->mode & (~0777)) | 0775; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 757 | if (mkdir(child_path, mode) < 0) { |
| 758 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 759 | } |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 760 | |
| 761 | /* When creating /Android/data and /Android/obb, mark them as .nomedia */ |
| 762 | if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) { |
| 763 | char nomedia[PATH_MAX]; |
| 764 | snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path); |
| 765 | if (touch(nomedia, 0664) != 0) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 766 | PLOG(ERROR) << "touch(" << nomedia << ") failed"; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 767 | return -ENOENT; |
| 768 | } |
| 769 | } |
| 770 | if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) { |
| 771 | char nomedia[PATH_MAX]; |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 772 | snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path); |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 773 | if (touch(nomedia, 0664) != 0) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 774 | PLOG(ERROR) << "touch(" << nomedia << ") failed"; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 775 | return -ENOENT; |
| 776 | } |
| 777 | } |
| 778 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 779 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 782 | static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 783 | const struct fuse_in_header* hdr, const char* name) |
| 784 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 785 | struct node* parent_node; |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 786 | struct node* child_node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 787 | char parent_path[PATH_MAX]; |
| 788 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 789 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 790 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 791 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 792 | parent_path, sizeof(parent_path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 793 | DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid |
| 794 | << " (" << (parent_node ? parent_node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 795 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 796 | |
| 797 | if (!parent_node || !find_file_within(parent_path, name, |
| 798 | child_path, sizeof(child_path), 1)) { |
| 799 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 800 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 801 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 802 | return -EACCES; |
| 803 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 804 | if (unlink(child_path) < 0) { |
| 805 | return -errno; |
| 806 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 807 | pthread_mutex_lock(&fuse->global->lock); |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 808 | child_node = lookup_child_by_name_locked(parent_node, name); |
| 809 | if (child_node) { |
| 810 | child_node->deleted = true; |
| 811 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 812 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 813 | if (parent_node && child_node) { |
| 814 | /* Tell all other views that node is gone */ |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 815 | DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete" |
| 816 | << " parent=" << std::hex << parent_node->nid |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 817 | << ", child=" << std::hex << child_node->nid << std::dec |
| 818 | << ", name=" << name; |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 819 | if (fuse != fuse->global->fuse_default) { |
| 820 | fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name); |
| 821 | } |
| 822 | if (fuse != fuse->global->fuse_read) { |
| 823 | fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name); |
| 824 | } |
| 825 | if (fuse != fuse->global->fuse_write) { |
| 826 | fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name); |
| 827 | } |
| 828 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 829 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 832 | static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 833 | const struct fuse_in_header* hdr, const char* name) |
| 834 | { |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 835 | struct node* child_node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 836 | struct node* parent_node; |
| 837 | char parent_path[PATH_MAX]; |
| 838 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 839 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 840 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 841 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 842 | parent_path, sizeof(parent_path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 843 | DLOG(INFO) << "[" << handler->token << "] UNLINK " << name << " @ " << std::hex << hdr->nodeid |
| 844 | << " (" << (parent_node ? parent_node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 845 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 846 | |
| 847 | if (!parent_node || !find_file_within(parent_path, name, |
| 848 | child_path, sizeof(child_path), 1)) { |
| 849 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 850 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 851 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 852 | return -EACCES; |
| 853 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 854 | if (rmdir(child_path) < 0) { |
| 855 | return -errno; |
| 856 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 857 | pthread_mutex_lock(&fuse->global->lock); |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 858 | child_node = lookup_child_by_name_locked(parent_node, name); |
| 859 | if (child_node) { |
| 860 | child_node->deleted = true; |
| 861 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 862 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 863 | if (parent_node && child_node) { |
| 864 | /* Tell all other views that node is gone */ |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 865 | DLOG(INFO) << "[" << handler->token << "] fuse_notify_delete" |
| 866 | << " parent=" << std::hex << parent_node->nid |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 867 | << ", child=" << std::hex << child_node->nid << std::dec |
| 868 | << ", name=" << name; |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 869 | if (fuse != fuse->global->fuse_default) { |
| 870 | fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name); |
| 871 | } |
| 872 | if (fuse != fuse->global->fuse_read) { |
| 873 | fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name); |
| 874 | } |
| 875 | if (fuse != fuse->global->fuse_write) { |
| 876 | fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name); |
| 877 | } |
| 878 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 879 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 882 | static int handle_rename(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 883 | const struct fuse_in_header* hdr, const struct fuse_rename_in* req, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 884 | const char* old_name, const char* new_name) |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 885 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 886 | struct node* old_parent_node; |
| 887 | struct node* new_parent_node; |
| 888 | struct node* child_node; |
| 889 | char old_parent_path[PATH_MAX]; |
| 890 | char new_parent_path[PATH_MAX]; |
| 891 | char old_child_path[PATH_MAX]; |
| 892 | char new_child_path[PATH_MAX]; |
| 893 | const char* new_actual_name; |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 894 | int search; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 895 | int res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 896 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 897 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 898 | old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 899 | old_parent_path, sizeof(old_parent_path)); |
| 900 | new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir, |
| 901 | new_parent_path, sizeof(new_parent_path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 902 | DLOG(INFO) << "[" << handler->token << "] RENAME " << old_name << "->" << new_name |
| 903 | << " @ " << std::hex << hdr->nodeid |
| 904 | << " (" << (old_parent_node ? old_parent_node->name : "?") << ") -> " |
| 905 | << std::hex << req->newdir |
| 906 | << " (" << (new_parent_node ? new_parent_node->name : "?") << ")"; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 907 | if (!old_parent_node || !new_parent_node) { |
| 908 | res = -ENOENT; |
| 909 | goto lookup_error; |
| 910 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 911 | if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 912 | res = -EACCES; |
| 913 | goto lookup_error; |
| 914 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 915 | if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 916 | res = -EACCES; |
| 917 | goto lookup_error; |
| 918 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 919 | child_node = lookup_child_by_name_locked(old_parent_node, old_name); |
| 920 | if (!child_node || get_node_path_locked(child_node, |
| 921 | old_child_path, sizeof(old_child_path)) < 0) { |
| 922 | res = -ENOENT; |
| 923 | goto lookup_error; |
| 924 | } |
| 925 | acquire_node_locked(child_node); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 926 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 927 | |
| 928 | /* Special case for renaming a file where destination is same path |
| 929 | * differing only by case. In this case we don't want to look for a case |
| 930 | * insensitive match. This allows commands like "mv foo FOO" to work as expected. |
| 931 | */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 932 | search = old_parent_node != new_parent_node |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 933 | || strcasecmp(old_name, new_name); |
| 934 | if (!(new_actual_name = find_file_within(new_parent_path, new_name, |
| 935 | new_child_path, sizeof(new_child_path), search))) { |
| 936 | res = -ENOENT; |
| 937 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 940 | DLOG(INFO) << "[" << handler->token << "] RENAME " << old_child_path << "->" << new_child_path; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 941 | res = rename(old_child_path, new_child_path); |
| 942 | if (res < 0) { |
| 943 | res = -errno; |
| 944 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 947 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 948 | res = rename_node_locked(child_node, new_name, new_actual_name); |
| 949 | if (!res) { |
| 950 | remove_node_from_parent_locked(child_node); |
Jeff Sharkey | 22b9126 | 2015-12-14 11:02:01 -0700 | [diff] [blame] | 951 | derive_permissions_locked(fuse, new_parent_node, child_node); |
| 952 | derive_permissions_recursive_locked(fuse, child_node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 953 | add_node_to_parent_locked(child_node, new_parent_node); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 954 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 955 | goto done; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 956 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 957 | io_error: |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 958 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 959 | done: |
| 960 | release_node_locked(child_node); |
| 961 | lookup_error: |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 962 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 963 | return res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 966 | static int open_flags_to_access_mode(int open_flags) { |
| 967 | if ((open_flags & O_ACCMODE) == O_RDONLY) { |
| 968 | return R_OK; |
| 969 | } else if ((open_flags & O_ACCMODE) == O_WRONLY) { |
| 970 | return W_OK; |
| 971 | } else { |
| 972 | /* Probably O_RDRW, but treat as default to be safe */ |
| 973 | return R_OK | W_OK; |
| 974 | } |
| 975 | } |
| 976 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 977 | static int handle_open(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 978 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 979 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 980 | struct node* node; |
| 981 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 982 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 983 | struct handle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 984 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 985 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 986 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 987 | DLOG(INFO) << "[" << handler->token << "] OPEN 0" << std::oct << req->flags |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 988 | << " @ " << std::hex << hdr->nodeid << std::dec |
| 989 | << " (" << (node ? node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 990 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 991 | |
| 992 | if (!node) { |
| 993 | return -ENOENT; |
| 994 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 995 | if (!check_caller_access_to_node(fuse, hdr, node, |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 996 | open_flags_to_access_mode(req->flags))) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 997 | return -EACCES; |
| 998 | } |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 999 | h = static_cast<struct handle*>(malloc(sizeof(*h))); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1000 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1001 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1002 | } |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1003 | DLOG(INFO) << "[" << handler->token << "] OPEN " << path; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1004 | h->fd = open(path, req->flags); |
| 1005 | if (h->fd < 0) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1006 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1007 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1008 | } |
| 1009 | out.fh = ptr_to_id(h); |
| 1010 | out.open_flags = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1011 | |
| 1012 | #ifdef FUSE_STACKED_IO |
| 1013 | out.lower_fd = h->fd; |
| 1014 | #else |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1015 | out.padding = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1016 | #endif |
| 1017 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1018 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1019 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1022 | static int handle_read(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1023 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1024 | { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1025 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1026 | __u64 unique = hdr->unique; |
| 1027 | __u32 size = req->size; |
| 1028 | __u64 offset = req->offset; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1029 | int res; |
Elliott Hughes | e24e9a5 | 2015-07-28 16:36:47 -0700 | [diff] [blame] | 1030 | __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1031 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1032 | /* Don't access any other fields of hdr or req beyond this point, the read buffer |
| 1033 | * overlaps the request buffer and will clobber data in the request. This |
| 1034 | * saves us 128KB per request handler thread at the cost of this scary comment. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1035 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1036 | DLOG(INFO) << "[" << handler->token << "] READ " << std::hex << h << std::dec |
| 1037 | << "(" << h->fd << ") " << size << "@" << offset; |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1038 | if (size > MAX_READ) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1039 | return -EINVAL; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1040 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1041 | res = pread64(h->fd, read_buffer, size, offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1042 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1043 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1044 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1045 | fuse_reply(fuse, unique, read_buffer, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1046 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1049 | static int handle_write(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1050 | const struct fuse_in_header* hdr, const struct fuse_write_in* req, |
| 1051 | const void* buffer) |
| 1052 | { |
| 1053 | struct fuse_write_out out; |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1054 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1055 | int res; |
Elliott Hughes | e24e9a5 | 2015-07-28 16:36:47 -0700 | [diff] [blame] | 1056 | __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE))); |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 1057 | |
Arpad Horvath | 49e9344 | 2014-02-18 10:18:25 +0100 | [diff] [blame] | 1058 | if (req->flags & O_DIRECT) { |
| 1059 | memcpy(aligned_buffer, buffer, req->size); |
| 1060 | buffer = (const __u8*) aligned_buffer; |
| 1061 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1062 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1063 | DLOG(INFO) << "[" << handler->token << "] WRITE " << std::hex << h << std::dec |
| 1064 | << "(" << h->fd << ") " << req->size << "@" << req->offset; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1065 | res = pwrite64(h->fd, buffer, req->size, req->offset); |
| 1066 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1067 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1068 | } |
| 1069 | out.size = res; |
Daisuke Okitsu | 19ec886 | 2013-08-05 12:18:15 +0900 | [diff] [blame] | 1070 | out.padding = 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1071 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1072 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1075 | static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1076 | const struct fuse_in_header* hdr) |
| 1077 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1078 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1079 | struct statfs stat; |
| 1080 | struct fuse_statfs_out out; |
| 1081 | int res; |
| 1082 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1083 | pthread_mutex_lock(&fuse->global->lock); |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1084 | DLOG(INFO) << "[" << handler->token << "] STATFS"; |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 1085 | res = get_node_path_locked(&fuse->global->root, path, sizeof(path)); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1086 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1087 | if (res < 0) { |
| 1088 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1089 | } |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 1090 | if (statfs(fuse->global->root.name, &stat) < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1091 | return -errno; |
| 1092 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1093 | memset(&out, 0, sizeof(out)); |
| 1094 | out.st.blocks = stat.f_blocks; |
| 1095 | out.st.bfree = stat.f_bfree; |
| 1096 | out.st.bavail = stat.f_bavail; |
| 1097 | out.st.files = stat.f_files; |
| 1098 | out.st.ffree = stat.f_ffree; |
| 1099 | out.st.bsize = stat.f_bsize; |
| 1100 | out.st.namelen = stat.f_namelen; |
| 1101 | out.st.frsize = stat.f_frsize; |
| 1102 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1103 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1106 | static int handle_release(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1107 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1108 | { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1109 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1110 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1111 | DLOG(INFO) << "[" << handler->token << "] RELEASE " << std::hex << h << std::dec |
| 1112 | << "(" << h->fd << ")"; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1113 | close(h->fd); |
| 1114 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1115 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1118 | static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1119 | const struct fuse_in_header* hdr, const struct fuse_fsync_in* req) |
| 1120 | { |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1121 | bool is_dir = (hdr->opcode == FUSE_FSYNCDIR); |
| 1122 | bool is_data_sync = req->fsync_flags & 1; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1123 | |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1124 | int fd = -1; |
| 1125 | if (is_dir) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1126 | struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh)); |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1127 | fd = dirfd(dh->d); |
| 1128 | } else { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1129 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1130 | fd = h->fd; |
| 1131 | } |
| 1132 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1133 | DLOG(INFO) << "[" << handler->token << "] " << (is_dir ? "FSYNCDIR" : "FSYNC") << " " |
| 1134 | << std::hex << req->fh << std::dec << "(" << fd << ") is_data_sync=" << is_data_sync; |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1135 | int res = is_data_sync ? fdatasync(fd) : fsync(fd); |
| 1136 | if (res == -1) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1137 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1138 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1139 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1142 | static int handle_flush(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1143 | const struct fuse_in_header* hdr) |
| 1144 | { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1145 | DLOG(INFO) << "[" << handler->token << "] FLUSH"; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1146 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1149 | static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1150 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 1151 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1152 | struct node* node; |
| 1153 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1154 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1155 | struct dirhandle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1156 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1157 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1158 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 1159 | DLOG(INFO) << "[" << handler->token << "] OPENDIR @ " << std::hex << hdr->nodeid |
| 1160 | << " (" << (node ? node->name : "?") << ")"; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1161 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1162 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1163 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1164 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1165 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1166 | if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1167 | return -EACCES; |
| 1168 | } |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1169 | h = static_cast<struct dirhandle*>(malloc(sizeof(*h))); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1170 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1171 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1172 | } |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1173 | DLOG(INFO) << "[" << handler->token << "] OPENDIR " << path; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1174 | h->d = opendir(path); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1175 | if (!h->d) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1176 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1177 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1178 | } |
| 1179 | out.fh = ptr_to_id(h); |
Ken Sumrall | 3a87688 | 2013-08-14 20:02:13 -0700 | [diff] [blame] | 1180 | out.open_flags = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1181 | |
| 1182 | #ifdef FUSE_STACKED_IO |
| 1183 | out.lower_fd = -1; |
| 1184 | #else |
Ken Sumrall | 3a87688 | 2013-08-14 20:02:13 -0700 | [diff] [blame] | 1185 | out.padding = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1186 | #endif |
| 1187 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1188 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1189 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1192 | static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1193 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1194 | { |
| 1195 | char buffer[8192]; |
| 1196 | struct fuse_dirent *fde = (struct fuse_dirent*) buffer; |
| 1197 | struct dirent *de; |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1198 | struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1199 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1200 | DLOG(INFO) << "[" << handler->token << "] READDIR " << h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1201 | if (req->offset == 0) { |
| 1202 | /* rewinddir() might have been called above us, so rewind here too */ |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1203 | DLOG(INFO) << "[" << handler->token << "] calling rewinddir()"; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1204 | rewinddir(h->d); |
| 1205 | } |
| 1206 | de = readdir(h->d); |
| 1207 | if (!de) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1208 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1209 | } |
| 1210 | fde->ino = FUSE_UNKNOWN_INO; |
| 1211 | /* increment the offset so we can detect when rewinddir() seeks back to the beginning */ |
| 1212 | fde->off = req->offset + 1; |
| 1213 | fde->type = de->d_type; |
| 1214 | fde->namelen = strlen(de->d_name); |
| 1215 | memcpy(fde->name, de->d_name, fde->namelen + 1); |
| 1216 | fuse_reply(fuse, hdr->unique, fde, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1217 | FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); |
| 1218 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1221 | static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1222 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1223 | { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1224 | struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1225 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1226 | DLOG(INFO) << "[" << handler->token << "] RELEASEDIR " << h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1227 | closedir(h->d); |
| 1228 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1229 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1232 | static int handle_init(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1233 | const struct fuse_in_header* hdr, const struct fuse_init_in* req) |
| 1234 | { |
| 1235 | struct fuse_init_out out; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1236 | size_t fuse_struct_size; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1237 | |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1238 | DLOG(INFO) << "[" << handler->token << "] INIT ver=" << req->major << "." << req->minor |
| 1239 | << " maxread=" << req->max_readahead << " flags=" << std::hex << req->flags; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1240 | |
| 1241 | /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out |
| 1242 | * defined (fuse version 7.6). The structure is the same from 7.6 through |
| 1243 | * 7.22. Beginning with 7.23, the structure increased in size and added |
| 1244 | * new parameters. |
| 1245 | */ |
| 1246 | if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1247 | LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " |
| 1248 | << req->major << "." << req->minor |
| 1249 | << ", Expected at least " << FUSE_KERNEL_VERSION << ".6"; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1250 | return -1; |
| 1251 | } |
| 1252 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1253 | /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */ |
| 1254 | out.minor = MIN(req->minor, 15); |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1255 | fuse_struct_size = sizeof(out); |
| 1256 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
| 1257 | /* FUSE_KERNEL_VERSION >= 23. */ |
| 1258 | |
| 1259 | /* If the kernel only works on minor revs older than or equal to 22, |
| 1260 | * then use the older structure size since this code only uses the 7.22 |
| 1261 | * version of the structure. */ |
| 1262 | if (req->minor <= 22) { |
| 1263 | fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 1264 | } |
| 1265 | #endif |
| 1266 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1267 | out.major = FUSE_KERNEL_VERSION; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1268 | out.max_readahead = req->max_readahead; |
| 1269 | out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1270 | |
| 1271 | #ifdef FUSE_STACKED_IO |
| 1272 | out.flags |= FUSE_STACKED_IO; |
| 1273 | #endif |
| 1274 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1275 | out.max_background = 32; |
| 1276 | out.congestion_threshold = 32; |
| 1277 | out.max_write = MAX_WRITE; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1278 | fuse_reply(fuse, hdr->unique, &out, fuse_struct_size); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1279 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1282 | static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1283 | const struct fuse_in_header *hdr, const void *data, size_t data_len) |
| 1284 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1285 | switch (hdr->opcode) { |
| 1286 | case FUSE_LOOKUP: { /* bytez[] -> entry_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1287 | const char *name = static_cast<const char*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1288 | return handle_lookup(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1289 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1290 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1291 | case FUSE_FORGET: { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1292 | const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1293 | return handle_forget(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1294 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1295 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1296 | case FUSE_GETATTR: { /* getattr_in -> attr_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1297 | const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1298 | return handle_getattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1299 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1300 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1301 | case FUSE_SETATTR: { /* setattr_in -> attr_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1302 | const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1303 | return handle_setattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1304 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1305 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1306 | // case FUSE_READLINK: |
| 1307 | // case FUSE_SYMLINK: |
| 1308 | case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1309 | const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1310 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1311 | return handle_mknod(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1312 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1313 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1314 | case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1315 | const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1316 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1317 | return handle_mkdir(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1318 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1319 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1320 | case FUSE_UNLINK: { /* bytez[] -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1321 | const char *name = static_cast<const char*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1322 | return handle_unlink(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1323 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1324 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1325 | case FUSE_RMDIR: { /* bytez[] -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1326 | const char *name = static_cast<const char*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1327 | return handle_rmdir(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1328 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1329 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1330 | case FUSE_RENAME: { /* rename_in, oldname, newname -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1331 | const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1332 | const char *old_name = ((const char*) data) + sizeof(*req); |
| 1333 | const char *new_name = old_name + strlen(old_name) + 1; |
| 1334 | return handle_rename(fuse, handler, hdr, req, old_name, new_name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1335 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1336 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1337 | // case FUSE_LINK: |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1338 | case FUSE_OPEN: { /* open_in -> open_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1339 | const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1340 | return handle_open(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1341 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1342 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1343 | case FUSE_READ: { /* read_in -> byte[] */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1344 | const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1345 | return handle_read(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1346 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1347 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1348 | case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1349 | const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1350 | const void* buffer = (const __u8*)data + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1351 | return handle_write(fuse, handler, hdr, req, buffer); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1352 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1353 | |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1354 | case FUSE_STATFS: { /* getattr_in -> attr_out */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1355 | return handle_statfs(fuse, handler, hdr); |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1356 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1357 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1358 | case FUSE_RELEASE: { /* release_in -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1359 | const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1360 | return handle_release(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1361 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1362 | |
Daisuke Okitsu | b2831a2 | 2014-02-17 10:33:11 +0100 | [diff] [blame] | 1363 | case FUSE_FSYNC: |
| 1364 | case FUSE_FSYNCDIR: { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1365 | const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1366 | return handle_fsync(fuse, handler, hdr, req); |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1369 | // case FUSE_SETXATTR: |
| 1370 | // case FUSE_GETXATTR: |
| 1371 | // case FUSE_LISTXATTR: |
| 1372 | // case FUSE_REMOVEXATTR: |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1373 | case FUSE_FLUSH: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1374 | return handle_flush(fuse, handler, hdr); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1377 | case FUSE_OPENDIR: { /* open_in -> open_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1378 | const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1379 | return handle_opendir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1380 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1381 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1382 | case FUSE_READDIR: { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1383 | const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1384 | return handle_readdir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1385 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1386 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1387 | case FUSE_RELEASEDIR: { /* release_in -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1388 | const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1389 | return handle_releasedir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1390 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1391 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1392 | case FUSE_INIT: { /* init_in -> init_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1393 | const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1394 | return handle_init(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1395 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1396 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1397 | default: { |
Jorge Lucangeli Obes | 714ec9d | 2016-07-20 15:19:44 -0400 | [diff] [blame] | 1398 | DLOG(INFO) << "[" << handler->token << "] NOTIMPL op=" << hdr->opcode |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1399 | << "uniq=" << std::hex << hdr->unique << "nid=" << hdr->nodeid << std::dec; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1400 | return -ENOSYS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1401 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1402 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 1405 | void handle_fuse_requests(struct fuse_handler* handler) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1406 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1407 | struct fuse* fuse = handler->fuse; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1408 | for (;;) { |
Mark Salyzyn | 6b6c1bd | 2015-07-06 10:00:36 -0700 | [diff] [blame] | 1409 | ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd, |
| 1410 | handler->request_buffer, sizeof(handler->request_buffer))); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1411 | if (len < 0) { |
Jeff Sharkey | 4a48581 | 2015-06-30 16:02:40 -0700 | [diff] [blame] | 1412 | if (errno == ENODEV) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1413 | LOG(ERROR) << "[" << handler->token << "] someone stole our marbles!"; |
Jeff Sharkey | 4a48581 | 2015-06-30 16:02:40 -0700 | [diff] [blame] | 1414 | exit(2); |
| 1415 | } |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1416 | PLOG(ERROR) << "[" << handler->token << "] handle_fuse_requests"; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1417 | continue; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1418 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1419 | |
| 1420 | if ((size_t)len < sizeof(struct fuse_in_header)) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1421 | LOG(ERROR) << "[" << handler->token << "] request too short: len=" << len; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1422 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame] | 1425 | const struct fuse_in_header* hdr = |
| 1426 | reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1427 | if (hdr->len != (size_t)len) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1428 | LOG(ERROR) << "[" << handler->token << "] malformed header: len=" << len |
| 1429 | << ", hdr->len=" << hdr->len; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1430 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1433 | const void *data = handler->request_buffer + sizeof(struct fuse_in_header); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1434 | size_t data_len = len - sizeof(struct fuse_in_header); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1435 | __u64 unique = hdr->unique; |
| 1436 | int res = handle_fuse_request(fuse, handler, hdr, data, data_len); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1437 | |
| 1438 | /* We do not access the request again after this point because the underlying |
| 1439 | * buffer storage may have been reused while processing the request. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1440 | |
| 1441 | if (res != NO_STATUS) { |
| 1442 | if (res) { |
Jorge Lucangeli Obes | e157b25 | 2016-07-26 15:22:31 -0400 | [diff] [blame^] | 1443 | DLOG(INFO) << "[" << handler->token << "] ERROR " << res; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1444 | } |
| 1445 | fuse_status(fuse, unique, res); |
| 1446 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1447 | } |
| 1448 | } |