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