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