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