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