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