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