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 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 54 | /* TODO: remove debugging - poison memory */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 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 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 339 | node = static_cast<struct 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 | } |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 343 | node->name = static_cast<char*>(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)) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 350 | node->actual_name = static_cast<char*>(malloc(namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 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) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 380 | char* new_name = static_cast<char*>(realloc(node->name, namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 381 | if (!new_name) { |
| 382 | return -ENOMEM; |
| 383 | } |
| 384 | node->name = new_name; |
| 385 | if (need_actual_name && node->actual_name) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 386 | char* new_actual_name = static_cast<char*>(realloc(node->actual_name, namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 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) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 397 | node->actual_name = static_cast<char*>(malloc(namelen + 1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 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 { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 417 | return static_cast<struct node*>(id_to_ptr(nid)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 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)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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 */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 805 | TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n", |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 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)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [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 */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 853 | TRACE("[%d] fuse_notify_delete parent=%" PRIx64 ", child=%" PRIx64 ", name=%s\n", |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 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; |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 880 | int search; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 881 | int res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 882 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 883 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 884 | old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 885 | old_parent_path, sizeof(old_parent_path)); |
| 886 | new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir, |
| 887 | new_parent_path, sizeof(new_parent_path)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 888 | TRACE("[%d] RENAME %s->%s @ %" PRIx64 " (%s) -> %" PRIx64 " (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 889 | old_name, new_name, |
| 890 | hdr->nodeid, old_parent_node ? old_parent_node->name : "?", |
| 891 | req->newdir, new_parent_node ? new_parent_node->name : "?"); |
| 892 | if (!old_parent_node || !new_parent_node) { |
| 893 | res = -ENOENT; |
| 894 | goto lookup_error; |
| 895 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 896 | 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] | 897 | res = -EACCES; |
| 898 | goto lookup_error; |
| 899 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 900 | 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] | 901 | res = -EACCES; |
| 902 | goto lookup_error; |
| 903 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 904 | child_node = lookup_child_by_name_locked(old_parent_node, old_name); |
| 905 | if (!child_node || get_node_path_locked(child_node, |
| 906 | old_child_path, sizeof(old_child_path)) < 0) { |
| 907 | res = -ENOENT; |
| 908 | goto lookup_error; |
| 909 | } |
| 910 | acquire_node_locked(child_node); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 911 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 912 | |
| 913 | /* Special case for renaming a file where destination is same path |
| 914 | * differing only by case. In this case we don't want to look for a case |
| 915 | * insensitive match. This allows commands like "mv foo FOO" to work as expected. |
| 916 | */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 917 | search = old_parent_node != new_parent_node |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 918 | || strcasecmp(old_name, new_name); |
| 919 | if (!(new_actual_name = find_file_within(new_parent_path, new_name, |
| 920 | new_child_path, sizeof(new_child_path), search))) { |
| 921 | res = -ENOENT; |
| 922 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 925 | TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path); |
| 926 | res = rename(old_child_path, new_child_path); |
| 927 | if (res < 0) { |
| 928 | res = -errno; |
| 929 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 932 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 933 | res = rename_node_locked(child_node, new_name, new_actual_name); |
| 934 | if (!res) { |
| 935 | remove_node_from_parent_locked(child_node); |
Jeff Sharkey | 22b9126 | 2015-12-14 11:02:01 -0700 | [diff] [blame] | 936 | derive_permissions_locked(fuse, new_parent_node, child_node); |
| 937 | derive_permissions_recursive_locked(fuse, child_node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 938 | add_node_to_parent_locked(child_node, new_parent_node); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 939 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 940 | goto done; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 941 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 942 | io_error: |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 943 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 944 | done: |
| 945 | release_node_locked(child_node); |
| 946 | lookup_error: |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 947 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 948 | return res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 951 | static int open_flags_to_access_mode(int open_flags) { |
| 952 | if ((open_flags & O_ACCMODE) == O_RDONLY) { |
| 953 | return R_OK; |
| 954 | } else if ((open_flags & O_ACCMODE) == O_WRONLY) { |
| 955 | return W_OK; |
| 956 | } else { |
| 957 | /* Probably O_RDRW, but treat as default to be safe */ |
| 958 | return R_OK | W_OK; |
| 959 | } |
| 960 | } |
| 961 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 962 | static int handle_open(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 963 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 964 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 965 | struct node* node; |
| 966 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 967 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 968 | struct handle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 969 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 970 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 971 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 972 | TRACE("[%d] OPEN 0%o @ %" PRIx64 " (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 973 | req->flags, hdr->nodeid, node ? node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 974 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 975 | |
| 976 | if (!node) { |
| 977 | return -ENOENT; |
| 978 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 979 | if (!check_caller_access_to_node(fuse, hdr, node, |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 980 | open_flags_to_access_mode(req->flags))) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 981 | return -EACCES; |
| 982 | } |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 983 | h = static_cast<struct handle*>(malloc(sizeof(*h))); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 984 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 985 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 986 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 987 | TRACE("[%d] OPEN %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 988 | h->fd = open(path, req->flags); |
| 989 | if (h->fd < 0) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 990 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 991 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 992 | } |
| 993 | out.fh = ptr_to_id(h); |
| 994 | out.open_flags = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 995 | |
| 996 | #ifdef FUSE_STACKED_IO |
| 997 | out.lower_fd = h->fd; |
| 998 | #else |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 999 | out.padding = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1000 | #endif |
| 1001 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1002 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1003 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1006 | static int handle_read(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1007 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1008 | { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1009 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1010 | __u64 unique = hdr->unique; |
| 1011 | __u32 size = req->size; |
| 1012 | __u64 offset = req->offset; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1013 | int res; |
Elliott Hughes | e24e9a5 | 2015-07-28 16:36:47 -0700 | [diff] [blame] | 1014 | __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] | 1015 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1016 | /* Don't access any other fields of hdr or req beyond this point, the read buffer |
| 1017 | * overlaps the request buffer and will clobber data in the request. This |
| 1018 | * 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] | 1019 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1020 | TRACE("[%d] READ %p(%d) %u@%" PRIu64 "\n", handler->token, |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 1021 | h, h->fd, size, (uint64_t) offset); |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1022 | if (size > MAX_READ) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1023 | return -EINVAL; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1024 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1025 | res = pread64(h->fd, read_buffer, size, offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1026 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1027 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1028 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1029 | fuse_reply(fuse, unique, read_buffer, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1030 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1033 | static int handle_write(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1034 | const struct fuse_in_header* hdr, const struct fuse_write_in* req, |
| 1035 | const void* buffer) |
| 1036 | { |
| 1037 | struct fuse_write_out out; |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1038 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1039 | int res; |
Elliott Hughes | e24e9a5 | 2015-07-28 16:36:47 -0700 | [diff] [blame] | 1040 | __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE))); |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 1041 | |
Arpad Horvath | 49e9344 | 2014-02-18 10:18:25 +0100 | [diff] [blame] | 1042 | if (req->flags & O_DIRECT) { |
| 1043 | memcpy(aligned_buffer, buffer, req->size); |
| 1044 | buffer = (const __u8*) aligned_buffer; |
| 1045 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1046 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1047 | TRACE("[%d] WRITE %p(%d) %u@%" PRIu64 "\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1048 | h, h->fd, req->size, req->offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1049 | res = pwrite64(h->fd, buffer, req->size, req->offset); |
| 1050 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1051 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1052 | } |
| 1053 | out.size = res; |
Daisuke Okitsu | 19ec886 | 2013-08-05 12:18:15 +0900 | [diff] [blame] | 1054 | out.padding = 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1055 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1056 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1059 | static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1060 | const struct fuse_in_header* hdr) |
| 1061 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1062 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1063 | struct statfs stat; |
| 1064 | struct fuse_statfs_out out; |
| 1065 | int res; |
| 1066 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1067 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1068 | TRACE("[%d] STATFS\n", handler->token); |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 1069 | res = get_node_path_locked(&fuse->global->root, path, sizeof(path)); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1070 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1071 | if (res < 0) { |
| 1072 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1073 | } |
Jeff Sharkey | ed2fe57 | 2015-07-16 09:13:52 -0700 | [diff] [blame] | 1074 | if (statfs(fuse->global->root.name, &stat) < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1075 | return -errno; |
| 1076 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1077 | memset(&out, 0, sizeof(out)); |
| 1078 | out.st.blocks = stat.f_blocks; |
| 1079 | out.st.bfree = stat.f_bfree; |
| 1080 | out.st.bavail = stat.f_bavail; |
| 1081 | out.st.files = stat.f_files; |
| 1082 | out.st.ffree = stat.f_ffree; |
| 1083 | out.st.bsize = stat.f_bsize; |
| 1084 | out.st.namelen = stat.f_namelen; |
| 1085 | out.st.frsize = stat.f_frsize; |
| 1086 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1087 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1090 | static int handle_release(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1091 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1092 | { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1093 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1094 | |
| 1095 | TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1096 | close(h->fd); |
| 1097 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1098 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1101 | static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1102 | const struct fuse_in_header* hdr, const struct fuse_fsync_in* req) |
| 1103 | { |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1104 | bool is_dir = (hdr->opcode == FUSE_FSYNCDIR); |
| 1105 | bool is_data_sync = req->fsync_flags & 1; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1106 | |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1107 | int fd = -1; |
| 1108 | if (is_dir) { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1109 | struct dirhandle *dh = static_cast<struct dirhandle*>(id_to_ptr(req->fh)); |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1110 | fd = dirfd(dh->d); |
| 1111 | } else { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1112 | struct handle *h = static_cast<struct handle*>(id_to_ptr(req->fh)); |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1113 | fd = h->fd; |
| 1114 | } |
| 1115 | |
| 1116 | TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token, |
| 1117 | is_dir ? "FSYNCDIR" : "FSYNC", |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1118 | static_cast<struct node*>(id_to_ptr(req->fh)), fd, is_data_sync); |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1119 | int res = is_data_sync ? fdatasync(fd) : fsync(fd); |
| 1120 | if (res == -1) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1121 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1122 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1123 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1126 | static int handle_flush(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1127 | const struct fuse_in_header* hdr) |
| 1128 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1129 | TRACE("[%d] FLUSH\n", handler->token); |
| 1130 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1133 | static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1134 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 1135 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1136 | struct node* node; |
| 1137 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1138 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1139 | struct dirhandle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1140 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1141 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1142 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1143 | TRACE("[%d] OPENDIR @ %" PRIx64 " (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1144 | hdr->nodeid, node ? node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1145 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1146 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1147 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1148 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1149 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1150 | if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1151 | return -EACCES; |
| 1152 | } |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1153 | h = static_cast<struct dirhandle*>(malloc(sizeof(*h))); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1154 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1155 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1156 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1157 | TRACE("[%d] OPENDIR %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1158 | h->d = opendir(path); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1159 | if (!h->d) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1160 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1161 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1162 | } |
| 1163 | out.fh = ptr_to_id(h); |
Ken Sumrall | 3a87688 | 2013-08-14 20:02:13 -0700 | [diff] [blame] | 1164 | out.open_flags = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1165 | |
| 1166 | #ifdef FUSE_STACKED_IO |
| 1167 | out.lower_fd = -1; |
| 1168 | #else |
Ken Sumrall | 3a87688 | 2013-08-14 20:02:13 -0700 | [diff] [blame] | 1169 | out.padding = 0; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1170 | #endif |
| 1171 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1172 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1173 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1176 | static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1177 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1178 | { |
| 1179 | char buffer[8192]; |
| 1180 | struct fuse_dirent *fde = (struct fuse_dirent*) buffer; |
| 1181 | struct dirent *de; |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1182 | struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1183 | |
| 1184 | TRACE("[%d] READDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1185 | if (req->offset == 0) { |
| 1186 | /* rewinddir() might have been called above us, so rewind here too */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1187 | TRACE("[%d] calling rewinddir()\n", handler->token); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1188 | rewinddir(h->d); |
| 1189 | } |
| 1190 | de = readdir(h->d); |
| 1191 | if (!de) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1192 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1193 | } |
| 1194 | fde->ino = FUSE_UNKNOWN_INO; |
| 1195 | /* increment the offset so we can detect when rewinddir() seeks back to the beginning */ |
| 1196 | fde->off = req->offset + 1; |
| 1197 | fde->type = de->d_type; |
| 1198 | fde->namelen = strlen(de->d_name); |
| 1199 | memcpy(fde->name, de->d_name, fde->namelen + 1); |
| 1200 | fuse_reply(fuse, hdr->unique, fde, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1201 | FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); |
| 1202 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1205 | static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1206 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1207 | { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1208 | struct dirhandle *h = static_cast<struct dirhandle*>(id_to_ptr(req->fh)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1209 | |
| 1210 | TRACE("[%d] RELEASEDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1211 | closedir(h->d); |
| 1212 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1213 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1216 | static int handle_init(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1217 | const struct fuse_in_header* hdr, const struct fuse_init_in* req) |
| 1218 | { |
| 1219 | struct fuse_init_out out; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1220 | size_t fuse_struct_size; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1221 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1222 | TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n", |
| 1223 | handler->token, req->major, req->minor, req->max_readahead, req->flags); |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1224 | |
| 1225 | /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out |
| 1226 | * defined (fuse version 7.6). The structure is the same from 7.6 through |
| 1227 | * 7.22. Beginning with 7.23, the structure increased in size and added |
| 1228 | * new parameters. |
| 1229 | */ |
| 1230 | if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) { |
| 1231 | ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6", |
| 1232 | req->major, req->minor, FUSE_KERNEL_VERSION); |
| 1233 | return -1; |
| 1234 | } |
| 1235 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1236 | /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */ |
| 1237 | out.minor = MIN(req->minor, 15); |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1238 | fuse_struct_size = sizeof(out); |
| 1239 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
| 1240 | /* FUSE_KERNEL_VERSION >= 23. */ |
| 1241 | |
| 1242 | /* If the kernel only works on minor revs older than or equal to 22, |
| 1243 | * then use the older structure size since this code only uses the 7.22 |
| 1244 | * version of the structure. */ |
| 1245 | if (req->minor <= 22) { |
| 1246 | fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 1247 | } |
| 1248 | #endif |
| 1249 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1250 | out.major = FUSE_KERNEL_VERSION; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1251 | out.max_readahead = req->max_readahead; |
| 1252 | out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
Thierry Strudel | ac5175f | 2016-01-13 15:11:35 -0800 | [diff] [blame] | 1253 | |
| 1254 | #ifdef FUSE_STACKED_IO |
| 1255 | out.flags |= FUSE_STACKED_IO; |
| 1256 | #endif |
| 1257 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1258 | out.max_background = 32; |
| 1259 | out.congestion_threshold = 32; |
| 1260 | out.max_write = MAX_WRITE; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1261 | fuse_reply(fuse, hdr->unique, &out, fuse_struct_size); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1262 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1265 | static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1266 | const struct fuse_in_header *hdr, const void *data, size_t data_len) |
| 1267 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1268 | switch (hdr->opcode) { |
| 1269 | case FUSE_LOOKUP: { /* bytez[] -> entry_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1270 | const char *name = static_cast<const char*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1271 | return handle_lookup(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1272 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1273 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1274 | case FUSE_FORGET: { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1275 | const struct fuse_forget_in *req = static_cast<const struct fuse_forget_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1276 | return handle_forget(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1277 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1278 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1279 | case FUSE_GETATTR: { /* getattr_in -> attr_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1280 | const struct fuse_getattr_in *req = static_cast<const struct fuse_getattr_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1281 | return handle_getattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1282 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1283 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1284 | case FUSE_SETATTR: { /* setattr_in -> attr_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1285 | const struct fuse_setattr_in *req = static_cast<const struct fuse_setattr_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1286 | return handle_setattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1287 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1288 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1289 | // case FUSE_READLINK: |
| 1290 | // case FUSE_SYMLINK: |
| 1291 | case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1292 | const struct fuse_mknod_in *req = static_cast<const struct fuse_mknod_in*>(data); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1293 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1294 | return handle_mknod(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1295 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1296 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1297 | case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1298 | const struct fuse_mkdir_in *req = static_cast<const struct fuse_mkdir_in*>(data); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1299 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1300 | return handle_mkdir(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1301 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1302 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1303 | case FUSE_UNLINK: { /* bytez[] -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1304 | const char *name = static_cast<const char*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1305 | return handle_unlink(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1306 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1307 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1308 | case FUSE_RMDIR: { /* bytez[] -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1309 | const char *name = static_cast<const char*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1310 | return handle_rmdir(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1311 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1312 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1313 | case FUSE_RENAME: { /* rename_in, oldname, newname -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1314 | const struct fuse_rename_in *req = static_cast<const struct fuse_rename_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1315 | const char *old_name = ((const char*) data) + sizeof(*req); |
| 1316 | const char *new_name = old_name + strlen(old_name) + 1; |
| 1317 | return handle_rename(fuse, handler, hdr, req, old_name, new_name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1318 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1319 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1320 | // case FUSE_LINK: |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1321 | case FUSE_OPEN: { /* open_in -> open_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1322 | const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1323 | return handle_open(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1324 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1325 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1326 | case FUSE_READ: { /* read_in -> byte[] */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1327 | const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1328 | return handle_read(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1329 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1330 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1331 | case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1332 | const struct fuse_write_in *req = static_cast<const struct fuse_write_in*>(data); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1333 | const void* buffer = (const __u8*)data + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1334 | return handle_write(fuse, handler, hdr, req, buffer); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1335 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1336 | |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1337 | case FUSE_STATFS: { /* getattr_in -> attr_out */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1338 | return handle_statfs(fuse, handler, hdr); |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1339 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1340 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1341 | case FUSE_RELEASE: { /* release_in -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1342 | const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1343 | return handle_release(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1344 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1345 | |
Daisuke Okitsu | b2831a2 | 2014-02-17 10:33:11 +0100 | [diff] [blame] | 1346 | case FUSE_FSYNC: |
| 1347 | case FUSE_FSYNCDIR: { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1348 | const struct fuse_fsync_in *req = static_cast<const struct fuse_fsync_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1349 | return handle_fsync(fuse, handler, hdr, req); |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1352 | // case FUSE_SETXATTR: |
| 1353 | // case FUSE_GETXATTR: |
| 1354 | // case FUSE_LISTXATTR: |
| 1355 | // case FUSE_REMOVEXATTR: |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1356 | case FUSE_FLUSH: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1357 | return handle_flush(fuse, handler, hdr); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1360 | case FUSE_OPENDIR: { /* open_in -> open_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1361 | const struct fuse_open_in *req = static_cast<const struct fuse_open_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1362 | return handle_opendir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1363 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1364 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1365 | case FUSE_READDIR: { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1366 | const struct fuse_read_in *req = static_cast<const struct fuse_read_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1367 | return handle_readdir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1368 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1369 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1370 | case FUSE_RELEASEDIR: { /* release_in -> */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1371 | const struct fuse_release_in *req = static_cast<const struct fuse_release_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1372 | return handle_releasedir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1373 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1374 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1375 | case FUSE_INIT: { /* init_in -> init_out */ |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1376 | const struct fuse_init_in *req = static_cast<const struct fuse_init_in*>(data); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1377 | return handle_init(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1378 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1379 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1380 | default: { |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1381 | TRACE("[%d] NOTIMPL op=%d uniq=%" PRIx64 " nid=%" PRIx64 "\n", |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1382 | handler->token, hdr->opcode, hdr->unique, hdr->nodeid); |
| 1383 | return -ENOSYS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1384 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1385 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
Jorge Lucangeli Obes | c255f25 | 2016-07-12 15:13:05 -0400 | [diff] [blame] | 1388 | void handle_fuse_requests(struct fuse_handler* handler) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1389 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1390 | struct fuse* fuse = handler->fuse; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1391 | for (;;) { |
Mark Salyzyn | 6b6c1bd | 2015-07-06 10:00:36 -0700 | [diff] [blame] | 1392 | ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd, |
| 1393 | handler->request_buffer, sizeof(handler->request_buffer))); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1394 | if (len < 0) { |
Jeff Sharkey | 4a48581 | 2015-06-30 16:02:40 -0700 | [diff] [blame] | 1395 | if (errno == ENODEV) { |
| 1396 | ERROR("[%d] someone stole our marbles!\n", handler->token); |
| 1397 | exit(2); |
| 1398 | } |
Mark Salyzyn | 6b6c1bd | 2015-07-06 10:00:36 -0700 | [diff] [blame] | 1399 | ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1400 | continue; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1401 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1402 | |
| 1403 | if ((size_t)len < sizeof(struct fuse_in_header)) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1404 | ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len); |
| 1405 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Jorge Lucangeli Obes | f08ba05 | 2016-07-13 16:52:24 -0400 | [diff] [blame^] | 1408 | const struct fuse_in_header* hdr = |
| 1409 | reinterpret_cast<const struct fuse_in_header*>(handler->request_buffer); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1410 | if (hdr->len != (size_t)len) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1411 | ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n", |
| 1412 | handler->token, (size_t)len, hdr->len); |
| 1413 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1416 | const void *data = handler->request_buffer + sizeof(struct fuse_in_header); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1417 | size_t data_len = len - sizeof(struct fuse_in_header); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1418 | __u64 unique = hdr->unique; |
| 1419 | int res = handle_fuse_request(fuse, handler, hdr, data, data_len); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1420 | |
| 1421 | /* We do not access the request again after this point because the underlying |
| 1422 | * buffer storage may have been reused while processing the request. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1423 | |
| 1424 | if (res != NO_STATUS) { |
| 1425 | if (res) { |
| 1426 | TRACE("[%d] ERROR %d\n", handler->token, res); |
| 1427 | } |
| 1428 | fuse_status(fuse, unique, res); |
| 1429 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1430 | } |
| 1431 | } |