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