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 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/stat.h> |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 25 | #include <sys/statfs.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 26 | #include <sys/uio.h> |
| 27 | #include <dirent.h> |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 28 | #include <limits.h> |
Mike Lockwood | 1bedb73 | 2011-01-13 13:38:42 -0500 | [diff] [blame] | 29 | #include <ctype.h> |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 30 | #include <pthread.h> |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 31 | #include <sys/time.h> |
| 32 | #include <sys/resource.h> |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 33 | #include <sys/inotify.h> |
| 34 | |
| 35 | #include <cutils/hashmap.h> |
| 36 | #include <cutils/multiuser.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 37 | |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
| 39 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 40 | #include "fuse.h" |
| 41 | |
| 42 | /* README |
| 43 | * |
| 44 | * What is this? |
| 45 | * |
| 46 | * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style |
| 47 | * directory permissions (all files are given fixed owner, group, and |
| 48 | * permissions at creation, owner, group, and permissions are not |
| 49 | * changeable, symlinks and hardlinks are not createable, etc. |
| 50 | * |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 51 | * See usage() for command line options. |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 52 | * |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 53 | * It must be run as root, but will drop to requested UID/GID as soon as it |
| 54 | * mounts a filesystem. It will refuse to run if requested UID/GID are zero. |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 55 | * |
| 56 | * Things I believe to be true: |
| 57 | * |
| 58 | * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK, |
| 59 | * CREAT) must bump that node's refcount |
| 60 | * - don't forget that FORGET can forget multiple references (req->nlookup) |
| 61 | * - if an op that returns a fuse_entry fails writing the reply to the |
| 62 | * kernel, you must rollback the refcount to reflect the reference the |
| 63 | * kernel did not actually acquire |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 64 | * |
| 65 | * This daemon can also derive custom filesystem permissions based on directory |
| 66 | * structure when requested. These custom permissions support several features: |
| 67 | * |
| 68 | * - Apps can access their own files in /Android/data/com.example/ without |
| 69 | * requiring any additional GIDs. |
| 70 | * - Separate permissions for protecting directories like Pictures and Music. |
| 71 | * - Multi-user separation on the same physical device. |
| 72 | * |
| 73 | * The derived permissions look like this: |
| 74 | * |
| 75 | * rwxrwx--x root:sdcard_rw / |
| 76 | * rwxrwx--- root:sdcard_pics /Pictures |
| 77 | * rwxrwx--- root:sdcard_av /Music |
| 78 | * |
| 79 | * rwxrwx--x root:sdcard_rw /Android |
| 80 | * rwxrwx--x root:sdcard_rw /Android/data |
| 81 | * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example |
| 82 | * rwxrwx--x root:sdcard_rw /Android/obb/ |
| 83 | * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example |
| 84 | * |
| 85 | * rwxrwx--- root:sdcard_all /Android/user |
| 86 | * rwxrwx--x root:sdcard_rw /Android/user/10 |
| 87 | * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 88 | */ |
| 89 | |
| 90 | #define FUSE_TRACE 0 |
| 91 | |
| 92 | #if FUSE_TRACE |
| 93 | #define TRACE(x...) fprintf(stderr,x) |
| 94 | #else |
| 95 | #define TRACE(x...) do {} while (0) |
| 96 | #endif |
| 97 | |
| 98 | #define ERROR(x...) fprintf(stderr,x) |
| 99 | |
| 100 | #define FUSE_UNKNOWN_INO 0xffffffff |
| 101 | |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 102 | /* Maximum number of bytes to write in one request. */ |
| 103 | #define MAX_WRITE (256 * 1024) |
| 104 | |
| 105 | /* Maximum number of bytes to read in one request. */ |
| 106 | #define MAX_READ (128 * 1024) |
| 107 | |
| 108 | /* Largest possible request. |
| 109 | * The request size is bounded by the maximum size of a FUSE_WRITE request because it has |
| 110 | * the largest possible data payload. */ |
| 111 | #define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE) |
| 112 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 113 | /* Default number of threads. */ |
| 114 | #define DEFAULT_NUM_THREADS 2 |
| 115 | |
| 116 | /* Pseudo-error constant used to indicate that no fuse status is needed |
| 117 | * or that a reply has already been written. */ |
| 118 | #define NO_STATUS 1 |
| 119 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 120 | /* Path to system-provided mapping of package name to appIds */ |
| 121 | static const char* const kPackagesListFile = "/data/system/packages.list"; |
| 122 | |
| 123 | /* Supplementary groups to execute with */ |
| 124 | static const gid_t kGroups[1] = { AID_PACKAGE_INFO }; |
| 125 | |
| 126 | /* Permission mode for a specific node. Controls how file permissions |
| 127 | * are derived for children nodes. */ |
| 128 | typedef enum { |
| 129 | PERM_INHERIT, |
| 130 | PERM_ROOT, |
| 131 | PERM_ANDROID, |
| 132 | PERM_ANDROID_DATA, |
| 133 | PERM_ANDROID_OBB, |
| 134 | PERM_ANDROID_USER, |
| 135 | } perm_t; |
| 136 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 137 | struct handle { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 138 | int fd; |
| 139 | }; |
| 140 | |
| 141 | struct dirhandle { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 142 | DIR *d; |
| 143 | }; |
| 144 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 145 | struct package { |
| 146 | appid_t appid; |
| 147 | }; |
| 148 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 149 | struct node { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 150 | __u32 refcount; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 151 | __u64 nid; |
| 152 | __u64 gen; |
| 153 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 154 | /* State derived based on current position in hierarchy. */ |
| 155 | perm_t perm; |
| 156 | userid_t userid; |
| 157 | uid_t uid; |
| 158 | gid_t gid; |
| 159 | mode_t mode; |
| 160 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 161 | struct node *next; /* per-dir sibling list */ |
| 162 | struct node *child; /* first contained file by this dir */ |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 163 | struct node *parent; /* containing directory */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 164 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 165 | size_t namelen; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 166 | char *name; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 167 | /* If non-null, this is the real name of the file in the underlying storage. |
| 168 | * This may differ from the field "name" only by case. |
| 169 | * strlen(actual_name) will always equal strlen(name), so it is safe to use |
| 170 | * namelen for both fields. |
| 171 | */ |
| 172 | char *actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 175 | static int str_hash(void *key) { |
| 176 | return hashmapHash(key, strlen(key)); |
| 177 | } |
| 178 | |
| 179 | static bool str_equals(void *keyA, void *keyB) { |
| 180 | return strcmp(keyA, keyB) == 0; |
| 181 | } |
| 182 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 183 | /* Global data structure shared by all fuse handlers. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 184 | struct fuse { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 185 | pthread_mutex_t lock; |
| 186 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 187 | __u64 next_generation; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 188 | int fd; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 189 | bool derive_perms; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 190 | struct node root; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 191 | char rootpath[PATH_MAX]; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 192 | |
| 193 | Hashmap *package_to_appid; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 196 | /* Private data used by a single fuse handler. */ |
| 197 | struct fuse_handler { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 198 | struct fuse* fuse; |
| 199 | int token; |
| 200 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 201 | /* To save memory, we never use the contents of the request buffer and the read |
| 202 | * buffer at the same time. This allows us to share the underlying storage. */ |
| 203 | union { |
| 204 | __u8 request_buffer[MAX_REQUEST_SIZE]; |
| 205 | __u8 read_buffer[MAX_READ]; |
| 206 | }; |
| 207 | }; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 208 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 209 | static inline void *id_to_ptr(__u64 nid) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 210 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 211 | return (void *) (uintptr_t) nid; |
| 212 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 213 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 214 | static inline __u64 ptr_to_id(void *ptr) |
| 215 | { |
| 216 | return (__u64) (uintptr_t) ptr; |
| 217 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 218 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 219 | static void acquire_node_locked(struct node* node) |
| 220 | { |
| 221 | node->refcount++; |
| 222 | TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount); |
| 223 | } |
| 224 | |
| 225 | static void remove_node_from_parent_locked(struct node* node); |
| 226 | |
| 227 | static void release_node_locked(struct node* node) |
| 228 | { |
| 229 | TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount); |
| 230 | if (node->refcount > 0) { |
| 231 | node->refcount--; |
| 232 | if (!node->refcount) { |
| 233 | TRACE("DESTROY %p (%s)\n", node, node->name); |
| 234 | remove_node_from_parent_locked(node); |
| 235 | |
| 236 | /* TODO: remove debugging - poison memory */ |
| 237 | memset(node->name, 0xef, node->namelen); |
| 238 | free(node->name); |
| 239 | free(node->actual_name); |
| 240 | memset(node, 0xfc, sizeof(*node)); |
| 241 | free(node); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 242 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 243 | } else { |
| 244 | ERROR("Zero refcnt %p\n", node); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static void add_node_to_parent_locked(struct node *node, struct node *parent) { |
| 249 | node->parent = parent; |
| 250 | node->next = parent->child; |
| 251 | parent->child = node; |
| 252 | acquire_node_locked(parent); |
| 253 | } |
| 254 | |
| 255 | static void remove_node_from_parent_locked(struct node* node) |
| 256 | { |
| 257 | if (node->parent) { |
| 258 | if (node->parent->child == node) { |
| 259 | node->parent->child = node->parent->child->next; |
| 260 | } else { |
| 261 | struct node *node2; |
| 262 | node2 = node->parent->child; |
| 263 | while (node2->next != node) |
| 264 | node2 = node2->next; |
| 265 | node2->next = node->next; |
| 266 | } |
| 267 | release_node_locked(node->parent); |
| 268 | node->parent = NULL; |
| 269 | node->next = NULL; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* Gets the absolute path to a node into the provided buffer. |
| 274 | * |
| 275 | * Populates 'buf' with the path and returns the length of the path on success, |
| 276 | * or returns -1 if the path is too long for the provided buffer. |
| 277 | */ |
| 278 | static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) |
| 279 | { |
| 280 | size_t namelen = node->namelen; |
| 281 | if (bufsize < namelen + 1) { |
| 282 | return -1; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 285 | ssize_t pathlen = 0; |
| 286 | if (node->parent) { |
| 287 | pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2); |
| 288 | if (pathlen < 0) { |
| 289 | return -1; |
| 290 | } |
| 291 | buf[pathlen++] = '/'; |
| 292 | } |
| 293 | |
| 294 | const char* name = node->actual_name ? node->actual_name : node->name; |
| 295 | memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */ |
| 296 | return pathlen + namelen; |
| 297 | } |
| 298 | |
| 299 | /* Finds the absolute path of a file within a given directory. |
| 300 | * Performs a case-insensitive search for the file and sets the buffer to the path |
| 301 | * of the first matching file. If 'search' is zero or if no match is found, sets |
| 302 | * the buffer to the path that the file would have, assuming the name were case-sensitive. |
| 303 | * |
| 304 | * Populates 'buf' with the path and returns the actual name (within 'buf') on success, |
| 305 | * or returns NULL if the path is too long for the provided buffer. |
| 306 | */ |
| 307 | static char* find_file_within(const char* path, const char* name, |
| 308 | char* buf, size_t bufsize, int search) |
| 309 | { |
| 310 | size_t pathlen = strlen(path); |
| 311 | size_t namelen = strlen(name); |
| 312 | size_t childlen = pathlen + namelen + 1; |
| 313 | char* actual; |
| 314 | |
| 315 | if (bufsize <= childlen) { |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | memcpy(buf, path, pathlen); |
| 320 | buf[pathlen] = '/'; |
| 321 | actual = buf + pathlen + 1; |
| 322 | memcpy(actual, name, namelen + 1); |
| 323 | |
| 324 | if (search && access(buf, F_OK)) { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 325 | struct dirent* entry; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 326 | DIR* dir = opendir(path); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 327 | if (!dir) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 328 | ERROR("opendir %s failed: %s\n", path, strerror(errno)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 329 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 330 | } |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 331 | while ((entry = readdir(dir))) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 332 | if (!strcasecmp(entry->d_name, name)) { |
| 333 | /* we have a match - replace the name, don't need to copy the null again */ |
| 334 | memcpy(actual, entry->d_name, namelen); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } |
| 338 | closedir(dir); |
| 339 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 340 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 343 | static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 344 | { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 345 | attr->ino = node->nid; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 346 | attr->size = s->st_size; |
| 347 | attr->blocks = s->st_blocks; |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 348 | attr->atime = s->st_atime; |
| 349 | attr->mtime = s->st_mtime; |
| 350 | attr->ctime = s->st_ctime; |
| 351 | attr->atimensec = s->st_atime_nsec; |
| 352 | attr->mtimensec = s->st_mtime_nsec; |
| 353 | attr->ctimensec = s->st_ctime_nsec; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 354 | attr->mode = s->st_mode; |
| 355 | attr->nlink = s->st_nlink; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 356 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 357 | attr->uid = node->uid; |
| 358 | attr->gid = node->gid; |
| 359 | |
| 360 | /* Filter requested mode based on underlying file, and |
| 361 | * pass through file type. */ |
| 362 | int owner_mode = s->st_mode & 0700; |
| 363 | int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6)); |
| 364 | attr->mode = (attr->mode & S_IFMT) | filtered_mode; |
| 365 | } |
| 366 | |
| 367 | static void derive_permissions_locked(struct fuse* fuse, struct node *parent, |
| 368 | struct node *node) { |
| 369 | struct package* package; |
| 370 | |
| 371 | /* By default, each node inherits from its parent */ |
| 372 | node->perm = PERM_INHERIT; |
| 373 | node->userid = parent->userid; |
| 374 | node->uid = parent->uid; |
| 375 | node->gid = parent->gid; |
| 376 | node->mode = parent->mode; |
| 377 | |
| 378 | if (!fuse->derive_perms) { |
| 379 | return; |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 382 | /* Derive custom permissions based on parent and current node */ |
| 383 | switch (parent->perm) { |
| 384 | case PERM_INHERIT: |
| 385 | /* Already inherited above */ |
| 386 | break; |
| 387 | case PERM_ROOT: |
| 388 | if (!strcmp(node->name, "Android")) { |
| 389 | /* App-specific directories inside; let anyone traverse */ |
| 390 | node->perm = PERM_ANDROID; |
| 391 | node->mode = 0771; |
| 392 | } else if (!strcmp(node->name, "DCIM") |
| 393 | || !strcmp(node->name, "Pictures")) { |
| 394 | node->gid = AID_SDCARD_PICS; |
| 395 | node->mode = 0770; |
| 396 | } else if (!strcmp(node->name, "Alarms") |
| 397 | || !strcmp(node->name, "Movies") |
| 398 | || !strcmp(node->name, "Music") |
| 399 | || !strcmp(node->name, "Notifications") |
| 400 | || !strcmp(node->name, "Podcasts") |
| 401 | || !strcmp(node->name, "Ringtones")) { |
| 402 | node->gid = AID_SDCARD_AV; |
| 403 | node->mode = 0770; |
| 404 | } |
| 405 | break; |
| 406 | case PERM_ANDROID: |
| 407 | if (!strcmp(node->name, "data")) { |
| 408 | /* App-specific directories inside; let anyone traverse */ |
| 409 | node->perm = PERM_ANDROID_DATA; |
| 410 | node->mode = 0771; |
| 411 | } else if (!strcmp(node->name, "obb")) { |
| 412 | /* App-specific directories inside; let anyone traverse */ |
| 413 | node->perm = PERM_ANDROID_OBB; |
| 414 | node->mode = 0771; |
| 415 | } else if (!strcmp(node->name, "user")) { |
| 416 | /* User directories must only be accessible to system, protected |
| 417 | * by sdcard_all. Zygote will bind mount the appropriate user- |
| 418 | * specific path. */ |
| 419 | node->perm = PERM_ANDROID_USER; |
| 420 | node->gid = AID_SDCARD_ALL; |
| 421 | node->mode = 0770; |
| 422 | } |
| 423 | break; |
| 424 | case PERM_ANDROID_DATA: |
| 425 | case PERM_ANDROID_OBB: |
| 426 | package = hashmapGet(fuse->package_to_appid, node->name); |
| 427 | if (package != NULL) { |
| 428 | node->uid = multiuser_get_uid(parent->userid, package->appid); |
| 429 | } |
| 430 | node->mode = 0770; |
| 431 | break; |
| 432 | case PERM_ANDROID_USER: |
| 433 | /* Root of a secondary user */ |
| 434 | node->perm = PERM_ROOT; |
| 435 | node->userid = atoi(node->name); |
| 436 | node->gid = AID_SDCARD_RW; |
| 437 | node->mode = 0771; |
| 438 | break; |
| 439 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 442 | struct node *create_node_locked(struct fuse* fuse, |
| 443 | struct node *parent, const char *name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 444 | { |
| 445 | struct node *node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 446 | size_t namelen = strlen(name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 447 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 448 | node = calloc(1, sizeof(struct node)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 449 | if (!node) { |
| 450 | return NULL; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 451 | } |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 452 | node->name = malloc(namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 453 | if (!node->name) { |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 454 | free(node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 455 | return NULL; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 456 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 457 | memcpy(node->name, name, namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 458 | if (strcmp(name, actual_name)) { |
| 459 | node->actual_name = malloc(namelen + 1); |
| 460 | if (!node->actual_name) { |
| 461 | free(node->name); |
| 462 | free(node); |
| 463 | return NULL; |
| 464 | } |
| 465 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 466 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 467 | node->namelen = namelen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 468 | node->nid = ptr_to_id(node); |
| 469 | node->gen = fuse->next_generation++; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 470 | |
| 471 | derive_permissions_locked(fuse, parent, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 472 | acquire_node_locked(node); |
| 473 | add_node_to_parent_locked(node, parent); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 474 | return node; |
| 475 | } |
| 476 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 477 | static int rename_node_locked(struct node *node, const char *name, |
| 478 | const char* actual_name) |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 479 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 480 | size_t namelen = strlen(name); |
| 481 | int need_actual_name = strcmp(name, actual_name); |
| 482 | |
| 483 | /* make the storage bigger without actually changing the name |
| 484 | * in case an error occurs part way */ |
| 485 | if (namelen > node->namelen) { |
| 486 | char* new_name = realloc(node->name, namelen + 1); |
| 487 | if (!new_name) { |
| 488 | return -ENOMEM; |
| 489 | } |
| 490 | node->name = new_name; |
| 491 | if (need_actual_name && node->actual_name) { |
| 492 | char* new_actual_name = realloc(node->actual_name, namelen + 1); |
| 493 | if (!new_actual_name) { |
| 494 | return -ENOMEM; |
| 495 | } |
| 496 | node->actual_name = new_actual_name; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /* update the name, taking care to allocate storage before overwriting the old name */ |
| 501 | if (need_actual_name) { |
| 502 | if (!node->actual_name) { |
| 503 | node->actual_name = malloc(namelen + 1); |
| 504 | if (!node->actual_name) { |
| 505 | return -ENOMEM; |
| 506 | } |
| 507 | } |
| 508 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 509 | } else { |
| 510 | free(node->actual_name); |
| 511 | node->actual_name = NULL; |
| 512 | } |
| 513 | memcpy(node->name, name, namelen + 1); |
| 514 | node->namelen = namelen; |
| 515 | return 0; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 518 | 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] | 519 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 520 | if (nid == FUSE_ROOT_ID) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 521 | return &fuse->root; |
| 522 | } else { |
| 523 | return id_to_ptr(nid); |
| 524 | } |
| 525 | } |
| 526 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 527 | static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid, |
| 528 | char* buf, size_t bufsize) |
| 529 | { |
| 530 | struct node* node = lookup_node_by_id_locked(fuse, nid); |
| 531 | if (node && get_node_path_locked(node, buf, bufsize) < 0) { |
| 532 | node = NULL; |
| 533 | } |
| 534 | return node; |
| 535 | } |
| 536 | |
| 537 | 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] | 538 | { |
| 539 | for (node = node->child; node; node = node->next) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 540 | /* use exact string comparison, nodes that differ by case |
| 541 | * must be considered distinct even if they refer to the same |
| 542 | * underlying file as otherwise operations such as "mv x x" |
| 543 | * will not work because the source and target nodes are the same. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 544 | if (!strcmp(name, node->name)) { |
| 545 | return node; |
| 546 | } |
| 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 551 | static struct node* acquire_or_create_child_locked( |
| 552 | struct fuse* fuse, struct node* parent, |
| 553 | const char* name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 554 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 555 | struct node* child = lookup_child_by_name_locked(parent, name); |
| 556 | if (child) { |
| 557 | acquire_node_locked(child); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 558 | } else { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 559 | child = create_node_locked(fuse, parent, name, actual_name); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 560 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 561 | return child; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 564 | static void fuse_init(struct fuse *fuse, int fd, const char *source_path, bool derive_perms) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 565 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 566 | pthread_mutex_init(&fuse->lock, NULL); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 567 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 568 | fuse->fd = fd; |
| 569 | fuse->next_generation = 0; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 570 | fuse->derive_perms = derive_perms; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 571 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 572 | memset(&fuse->root, 0, sizeof(fuse->root)); |
| 573 | fuse->root.nid = FUSE_ROOT_ID; /* 1 */ |
| 574 | fuse->root.refcount = 2; |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 575 | fuse->root.namelen = strlen(source_path); |
| 576 | fuse->root.name = strdup(source_path); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 577 | |
| 578 | fuse->root.perm = PERM_ROOT; |
| 579 | fuse->root.userid = 0; |
| 580 | fuse->root.uid = AID_ROOT; |
| 581 | fuse->root.gid = AID_SDCARD_RW; |
| 582 | if (derive_perms) { |
| 583 | fuse->root.mode = 0771; |
| 584 | } else { |
| 585 | fuse->root.mode = 0775; |
| 586 | } |
| 587 | |
| 588 | if (derive_perms) { |
| 589 | fuse->package_to_appid = hashmapCreate(256, str_hash, str_equals); |
| 590 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 593 | static void fuse_status(struct fuse *fuse, __u64 unique, int err) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 594 | { |
| 595 | struct fuse_out_header hdr; |
| 596 | hdr.len = sizeof(hdr); |
| 597 | hdr.error = err; |
| 598 | hdr.unique = unique; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 599 | write(fuse->fd, &hdr, sizeof(hdr)); |
| 600 | } |
| 601 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 602 | 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] | 603 | { |
| 604 | struct fuse_out_header hdr; |
| 605 | struct iovec vec[2]; |
| 606 | int res; |
| 607 | |
| 608 | hdr.len = len + sizeof(hdr); |
| 609 | hdr.error = 0; |
| 610 | hdr.unique = unique; |
| 611 | |
| 612 | vec[0].iov_base = &hdr; |
| 613 | vec[0].iov_len = sizeof(hdr); |
| 614 | vec[1].iov_base = data; |
| 615 | vec[1].iov_len = len; |
| 616 | |
| 617 | res = writev(fuse->fd, vec, 2); |
| 618 | if (res < 0) { |
| 619 | ERROR("*** REPLY FAILED *** %d\n", errno); |
| 620 | } |
| 621 | } |
| 622 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 623 | static int fuse_reply_entry(struct fuse* fuse, __u64 unique, |
| 624 | struct node* parent, const char* name, const char* actual_name, |
| 625 | const char* path) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 626 | { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 627 | struct node* node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 628 | struct fuse_entry_out out; |
| 629 | struct stat s; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 630 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 631 | if (lstat(path, &s) < 0) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 632 | /* But wait! We'll automatically create a directory if its |
| 633 | * a valid package name under data or obb, since apps may not |
| 634 | * have enough permissions to create for themselves. */ |
| 635 | if (errno == ENOENT && (parent->perm == PERM_ANDROID_DATA |
| 636 | || parent->perm == PERM_ANDROID_OBB)) { |
| 637 | TRACE("automatically creating %s\n", path); |
| 638 | |
| 639 | pthread_mutex_lock(&fuse->lock); |
| 640 | bool validPackage = hashmapContainsKey(fuse->package_to_appid, name); |
| 641 | pthread_mutex_unlock(&fuse->lock); |
| 642 | |
| 643 | if (!validPackage) { |
| 644 | return -ENOENT; |
| 645 | } |
| 646 | if (mkdir(path, 0775) == -1) { |
| 647 | /* We might have raced with ourselves and already created */ |
| 648 | if (errno != EEXIST) { |
| 649 | ERROR("failed to mkdir(%s): %s\n", name, strerror(errno)); |
| 650 | return -ENOENT; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* It should exist this time around! */ |
| 655 | if (lstat(path, &s) < 0) { |
| 656 | ERROR("failed to lstat(%s): %s\n", name, strerror(errno)); |
| 657 | return -errno; |
| 658 | } |
| 659 | } else { |
| 660 | return -errno; |
| 661 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 662 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 663 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 664 | pthread_mutex_lock(&fuse->lock); |
| 665 | node = acquire_or_create_child_locked(fuse, parent, name, actual_name); |
| 666 | if (!node) { |
| 667 | pthread_mutex_unlock(&fuse->lock); |
| 668 | return -ENOMEM; |
| 669 | } |
| 670 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 671 | attr_from_stat(&out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 672 | out.attr_valid = 10; |
| 673 | out.entry_valid = 10; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 674 | out.nodeid = node->nid; |
| 675 | out.generation = node->gen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 676 | pthread_mutex_unlock(&fuse->lock); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 677 | fuse_reply(fuse, unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 678 | return NO_STATUS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 681 | 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] | 682 | const char* path) |
| 683 | { |
| 684 | struct fuse_attr_out out; |
| 685 | struct stat s; |
| 686 | |
| 687 | if (lstat(path, &s) < 0) { |
| 688 | return -errno; |
| 689 | } |
| 690 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 691 | attr_from_stat(&out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 692 | out.attr_valid = 10; |
| 693 | fuse_reply(fuse, unique, &out, sizeof(out)); |
| 694 | return NO_STATUS; |
| 695 | } |
| 696 | |
| 697 | static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 698 | const struct fuse_in_header *hdr, const char* name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 699 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 700 | struct node* parent_node; |
| 701 | char parent_path[PATH_MAX]; |
| 702 | char child_path[PATH_MAX]; |
| 703 | const char* actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 704 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 705 | pthread_mutex_lock(&fuse->lock); |
| 706 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 707 | parent_path, sizeof(parent_path)); |
| 708 | TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid, |
| 709 | parent_node ? parent_node->name : "?"); |
| 710 | pthread_mutex_unlock(&fuse->lock); |
| 711 | |
| 712 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 713 | child_path, sizeof(child_path), 1))) { |
| 714 | return -ENOENT; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 715 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 716 | 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] | 717 | } |
| 718 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 719 | static int handle_forget(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 720 | const struct fuse_in_header *hdr, const struct fuse_forget_in *req) |
| 721 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 722 | struct node* node; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 723 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 724 | pthread_mutex_lock(&fuse->lock); |
| 725 | node = lookup_node_by_id_locked(fuse, hdr->nodeid); |
| 726 | TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup, |
| 727 | hdr->nodeid, node ? node->name : "?"); |
| 728 | if (node) { |
| 729 | __u64 n = req->nlookup; |
| 730 | while (n--) { |
| 731 | release_node_locked(node); |
| 732 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 733 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 734 | pthread_mutex_unlock(&fuse->lock); |
| 735 | return NO_STATUS; /* no reply */ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 738 | static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 739 | const struct fuse_in_header *hdr, const struct fuse_getattr_in *req) |
| 740 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 741 | struct node* node; |
| 742 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 743 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 744 | pthread_mutex_lock(&fuse->lock); |
| 745 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 746 | TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token, |
| 747 | req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?"); |
| 748 | pthread_mutex_unlock(&fuse->lock); |
| 749 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 750 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 751 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 752 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 753 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 756 | static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 757 | const struct fuse_in_header *hdr, const struct fuse_setattr_in *req) |
| 758 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 759 | struct node* node; |
| 760 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 761 | struct timespec times[2]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 762 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 763 | pthread_mutex_lock(&fuse->lock); |
| 764 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 765 | TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token, |
| 766 | req->fh, req->valid, hdr->nodeid, node ? node->name : "?"); |
| 767 | pthread_mutex_unlock(&fuse->lock); |
| 768 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 769 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 770 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 773 | /* XXX: incomplete implementation on purpose. |
| 774 | * chmod/chown should NEVER be implemented.*/ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 775 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 776 | if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) { |
| 777 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 781 | * are both set, then set it to the current time. Else, set it to the |
| 782 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 783 | * as it allows ATIME and MTIME to be changed independently, and has |
| 784 | * nanosecond resolution which fuse also has. |
| 785 | */ |
| 786 | if (req->valid & (FATTR_ATIME | FATTR_MTIME)) { |
| 787 | times[0].tv_nsec = UTIME_OMIT; |
| 788 | times[1].tv_nsec = UTIME_OMIT; |
| 789 | if (req->valid & FATTR_ATIME) { |
| 790 | if (req->valid & FATTR_ATIME_NOW) { |
| 791 | times[0].tv_nsec = UTIME_NOW; |
| 792 | } else { |
| 793 | times[0].tv_sec = req->atime; |
| 794 | times[0].tv_nsec = req->atimensec; |
| 795 | } |
| 796 | } |
| 797 | if (req->valid & FATTR_MTIME) { |
| 798 | if (req->valid & FATTR_MTIME_NOW) { |
| 799 | times[1].tv_nsec = UTIME_NOW; |
| 800 | } else { |
| 801 | times[1].tv_sec = req->mtime; |
| 802 | times[1].tv_nsec = req->mtimensec; |
| 803 | } |
| 804 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 805 | TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n", |
| 806 | handler->token, path, times[0].tv_sec, times[1].tv_sec); |
| 807 | if (utimensat(-1, path, times, 0) < 0) { |
| 808 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 811 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 814 | static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 815 | const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name) |
| 816 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 817 | struct node* parent_node; |
| 818 | char parent_path[PATH_MAX]; |
| 819 | char child_path[PATH_MAX]; |
| 820 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 821 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 822 | pthread_mutex_lock(&fuse->lock); |
| 823 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 824 | parent_path, sizeof(parent_path)); |
| 825 | TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token, |
| 826 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 827 | pthread_mutex_unlock(&fuse->lock); |
| 828 | |
| 829 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 830 | child_path, sizeof(child_path), 1))) { |
| 831 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 832 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 833 | __u32 mode = (req->mode & (~0777)) | 0664; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 834 | if (mknod(child_path, mode, req->rdev) < 0) { |
| 835 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 836 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 837 | 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] | 838 | } |
| 839 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 840 | static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 841 | const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name) |
| 842 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 843 | struct node* parent_node; |
| 844 | char parent_path[PATH_MAX]; |
| 845 | char child_path[PATH_MAX]; |
| 846 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 847 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 848 | pthread_mutex_lock(&fuse->lock); |
| 849 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 850 | parent_path, sizeof(parent_path)); |
| 851 | TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token, |
| 852 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 853 | pthread_mutex_unlock(&fuse->lock); |
| 854 | |
| 855 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 856 | child_path, sizeof(child_path), 1))) { |
| 857 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 858 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 859 | __u32 mode = (req->mode & (~0777)) | 0775; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 860 | if (mkdir(child_path, mode) < 0) { |
| 861 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 862 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 863 | 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] | 864 | } |
| 865 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 866 | static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 867 | const struct fuse_in_header* hdr, const char* name) |
| 868 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 869 | struct node* parent_node; |
| 870 | char parent_path[PATH_MAX]; |
| 871 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 872 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 873 | pthread_mutex_lock(&fuse->lock); |
| 874 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 875 | parent_path, sizeof(parent_path)); |
| 876 | TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token, |
| 877 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 878 | pthread_mutex_unlock(&fuse->lock); |
| 879 | |
| 880 | if (!parent_node || !find_file_within(parent_path, name, |
| 881 | child_path, sizeof(child_path), 1)) { |
| 882 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 883 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 884 | if (unlink(child_path) < 0) { |
| 885 | return -errno; |
| 886 | } |
| 887 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 890 | static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 891 | const struct fuse_in_header* hdr, const char* name) |
| 892 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 893 | struct node* parent_node; |
| 894 | char parent_path[PATH_MAX]; |
| 895 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 896 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 897 | pthread_mutex_lock(&fuse->lock); |
| 898 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 899 | parent_path, sizeof(parent_path)); |
| 900 | TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token, |
| 901 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 902 | pthread_mutex_unlock(&fuse->lock); |
| 903 | |
| 904 | if (!parent_node || !find_file_within(parent_path, name, |
| 905 | child_path, sizeof(child_path), 1)) { |
| 906 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 907 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 908 | if (rmdir(child_path) < 0) { |
| 909 | return -errno; |
| 910 | } |
| 911 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 914 | static int handle_rename(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 915 | const struct fuse_in_header* hdr, const struct fuse_rename_in* req, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 916 | const char* old_name, const char* new_name) |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 917 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 918 | struct node* old_parent_node; |
| 919 | struct node* new_parent_node; |
| 920 | struct node* child_node; |
| 921 | char old_parent_path[PATH_MAX]; |
| 922 | char new_parent_path[PATH_MAX]; |
| 923 | char old_child_path[PATH_MAX]; |
| 924 | char new_child_path[PATH_MAX]; |
| 925 | const char* new_actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 926 | int res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 927 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 928 | pthread_mutex_lock(&fuse->lock); |
| 929 | old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 930 | old_parent_path, sizeof(old_parent_path)); |
| 931 | new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir, |
| 932 | new_parent_path, sizeof(new_parent_path)); |
| 933 | TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token, |
| 934 | old_name, new_name, |
| 935 | hdr->nodeid, old_parent_node ? old_parent_node->name : "?", |
| 936 | req->newdir, new_parent_node ? new_parent_node->name : "?"); |
| 937 | if (!old_parent_node || !new_parent_node) { |
| 938 | res = -ENOENT; |
| 939 | goto lookup_error; |
| 940 | } |
| 941 | child_node = lookup_child_by_name_locked(old_parent_node, old_name); |
| 942 | if (!child_node || get_node_path_locked(child_node, |
| 943 | old_child_path, sizeof(old_child_path)) < 0) { |
| 944 | res = -ENOENT; |
| 945 | goto lookup_error; |
| 946 | } |
| 947 | acquire_node_locked(child_node); |
| 948 | pthread_mutex_unlock(&fuse->lock); |
| 949 | |
| 950 | /* Special case for renaming a file where destination is same path |
| 951 | * differing only by case. In this case we don't want to look for a case |
| 952 | * insensitive match. This allows commands like "mv foo FOO" to work as expected. |
| 953 | */ |
| 954 | int search = old_parent_node != new_parent_node |
| 955 | || strcasecmp(old_name, new_name); |
| 956 | if (!(new_actual_name = find_file_within(new_parent_path, new_name, |
| 957 | new_child_path, sizeof(new_child_path), search))) { |
| 958 | res = -ENOENT; |
| 959 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 962 | TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path); |
| 963 | res = rename(old_child_path, new_child_path); |
| 964 | if (res < 0) { |
| 965 | res = -errno; |
| 966 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 969 | pthread_mutex_lock(&fuse->lock); |
| 970 | res = rename_node_locked(child_node, new_name, new_actual_name); |
| 971 | if (!res) { |
| 972 | remove_node_from_parent_locked(child_node); |
| 973 | add_node_to_parent_locked(child_node, new_parent_node); |
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 | goto done; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 976 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 977 | io_error: |
| 978 | pthread_mutex_lock(&fuse->lock); |
| 979 | done: |
| 980 | release_node_locked(child_node); |
| 981 | lookup_error: |
| 982 | pthread_mutex_unlock(&fuse->lock); |
| 983 | return res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 986 | static int handle_open(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 987 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 988 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 989 | struct node* node; |
| 990 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 991 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 992 | struct handle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 993 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 994 | pthread_mutex_lock(&fuse->lock); |
| 995 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 996 | TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token, |
| 997 | req->flags, hdr->nodeid, node ? node->name : "?"); |
| 998 | pthread_mutex_unlock(&fuse->lock); |
| 999 | |
| 1000 | if (!node) { |
| 1001 | return -ENOENT; |
| 1002 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1003 | h = malloc(sizeof(*h)); |
| 1004 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1005 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1006 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1007 | TRACE("[%d] OPEN %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1008 | h->fd = open(path, req->flags); |
| 1009 | if (h->fd < 0) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1010 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1011 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1012 | } |
| 1013 | out.fh = ptr_to_id(h); |
| 1014 | out.open_flags = 0; |
| 1015 | out.padding = 0; |
| 1016 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1017 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1020 | static int handle_read(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1021 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1022 | { |
| 1023 | struct handle *h = id_to_ptr(req->fh); |
| 1024 | __u64 unique = hdr->unique; |
| 1025 | __u32 size = req->size; |
| 1026 | __u64 offset = req->offset; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1027 | int res; |
| 1028 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1029 | /* Don't access any other fields of hdr or req beyond this point, the read buffer |
| 1030 | * overlaps the request buffer and will clobber data in the request. This |
| 1031 | * 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] | 1032 | |
| 1033 | TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token, |
| 1034 | h, h->fd, size, offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1035 | if (size > sizeof(handler->read_buffer)) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1036 | return -EINVAL; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1037 | } |
| 1038 | res = pread64(h->fd, handler->read_buffer, size, offset); |
| 1039 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1040 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1041 | } |
| 1042 | fuse_reply(fuse, unique, handler->read_buffer, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1043 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1046 | static int handle_write(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1047 | const struct fuse_in_header* hdr, const struct fuse_write_in* req, |
| 1048 | const void* buffer) |
| 1049 | { |
| 1050 | struct fuse_write_out out; |
| 1051 | struct handle *h = id_to_ptr(req->fh); |
| 1052 | int res; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1053 | |
| 1054 | TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token, |
| 1055 | h, h->fd, req->size, req->offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1056 | res = pwrite64(h->fd, buffer, req->size, req->offset); |
| 1057 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1058 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1059 | } |
| 1060 | out.size = res; |
| 1061 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1062 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1065 | static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1066 | const struct fuse_in_header* hdr) |
| 1067 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1068 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1069 | struct statfs stat; |
| 1070 | struct fuse_statfs_out out; |
| 1071 | int res; |
| 1072 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1073 | pthread_mutex_lock(&fuse->lock); |
| 1074 | TRACE("[%d] STATFS\n", handler->token); |
| 1075 | res = get_node_path_locked(&fuse->root, path, sizeof(path)); |
| 1076 | pthread_mutex_unlock(&fuse->lock); |
| 1077 | if (res < 0) { |
| 1078 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1079 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1080 | if (statfs(fuse->root.name, &stat) < 0) { |
| 1081 | return -errno; |
| 1082 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1083 | memset(&out, 0, sizeof(out)); |
| 1084 | out.st.blocks = stat.f_blocks; |
| 1085 | out.st.bfree = stat.f_bfree; |
| 1086 | out.st.bavail = stat.f_bavail; |
| 1087 | out.st.files = stat.f_files; |
| 1088 | out.st.ffree = stat.f_ffree; |
| 1089 | out.st.bsize = stat.f_bsize; |
| 1090 | out.st.namelen = stat.f_namelen; |
| 1091 | out.st.frsize = stat.f_frsize; |
| 1092 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1093 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1096 | static int handle_release(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1097 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1098 | { |
| 1099 | struct handle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1100 | |
| 1101 | TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1102 | close(h->fd); |
| 1103 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1104 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1107 | static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1108 | const struct fuse_in_header* hdr, const struct fuse_fsync_in* req) |
| 1109 | { |
| 1110 | int is_data_sync = req->fsync_flags & 1; |
| 1111 | struct handle *h = id_to_ptr(req->fh); |
| 1112 | int res; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1113 | |
| 1114 | TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token, |
| 1115 | h, h->fd, is_data_sync); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1116 | res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd); |
| 1117 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1118 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1119 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1120 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1123 | static int handle_flush(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1124 | const struct fuse_in_header* hdr) |
| 1125 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1126 | TRACE("[%d] FLUSH\n", handler->token); |
| 1127 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1130 | static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1131 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 1132 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1133 | struct node* node; |
| 1134 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1135 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1136 | struct dirhandle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1137 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1138 | pthread_mutex_lock(&fuse->lock); |
| 1139 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 1140 | TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token, |
| 1141 | hdr->nodeid, node ? node->name : "?"); |
| 1142 | pthread_mutex_unlock(&fuse->lock); |
| 1143 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1144 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1145 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1146 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1147 | h = malloc(sizeof(*h)); |
| 1148 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1149 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1150 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1151 | TRACE("[%d] OPENDIR %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1152 | h->d = opendir(path); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1153 | if (!h->d) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1154 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1155 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1156 | } |
| 1157 | out.fh = ptr_to_id(h); |
| 1158 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1159 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1162 | static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1163 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1164 | { |
| 1165 | char buffer[8192]; |
| 1166 | struct fuse_dirent *fde = (struct fuse_dirent*) buffer; |
| 1167 | struct dirent *de; |
| 1168 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1169 | |
| 1170 | TRACE("[%d] READDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1171 | if (req->offset == 0) { |
| 1172 | /* rewinddir() might have been called above us, so rewind here too */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1173 | TRACE("[%d] calling rewinddir()\n", handler->token); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1174 | rewinddir(h->d); |
| 1175 | } |
| 1176 | de = readdir(h->d); |
| 1177 | if (!de) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1178 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1179 | } |
| 1180 | fde->ino = FUSE_UNKNOWN_INO; |
| 1181 | /* increment the offset so we can detect when rewinddir() seeks back to the beginning */ |
| 1182 | fde->off = req->offset + 1; |
| 1183 | fde->type = de->d_type; |
| 1184 | fde->namelen = strlen(de->d_name); |
| 1185 | memcpy(fde->name, de->d_name, fde->namelen + 1); |
| 1186 | fuse_reply(fuse, hdr->unique, fde, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1187 | FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); |
| 1188 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1191 | static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1192 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1193 | { |
| 1194 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1195 | |
| 1196 | TRACE("[%d] RELEASEDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1197 | closedir(h->d); |
| 1198 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1199 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1202 | static int handle_init(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1203 | const struct fuse_in_header* hdr, const struct fuse_init_in* req) |
| 1204 | { |
| 1205 | struct fuse_init_out out; |
| 1206 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1207 | TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n", |
| 1208 | handler->token, req->major, req->minor, req->max_readahead, req->flags); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1209 | out.major = FUSE_KERNEL_VERSION; |
| 1210 | out.minor = FUSE_KERNEL_MINOR_VERSION; |
| 1211 | out.max_readahead = req->max_readahead; |
| 1212 | out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
| 1213 | out.max_background = 32; |
| 1214 | out.congestion_threshold = 32; |
| 1215 | out.max_write = MAX_WRITE; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1216 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1217 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1220 | static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1221 | const struct fuse_in_header *hdr, const void *data, size_t data_len) |
| 1222 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1223 | switch (hdr->opcode) { |
| 1224 | case FUSE_LOOKUP: { /* bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1225 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1226 | return handle_lookup(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1227 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1228 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1229 | case FUSE_FORGET: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1230 | const struct fuse_forget_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1231 | return handle_forget(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1232 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1233 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1234 | case FUSE_GETATTR: { /* getattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1235 | const struct fuse_getattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1236 | return handle_getattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1237 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1238 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1239 | case FUSE_SETATTR: { /* setattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1240 | const struct fuse_setattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1241 | return handle_setattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1242 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1243 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1244 | // case FUSE_READLINK: |
| 1245 | // case FUSE_SYMLINK: |
| 1246 | case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1247 | const struct fuse_mknod_in *req = data; |
| 1248 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1249 | return handle_mknod(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1250 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1251 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1252 | case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1253 | const struct fuse_mkdir_in *req = data; |
| 1254 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1255 | return handle_mkdir(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1256 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1257 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1258 | case FUSE_UNLINK: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1259 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1260 | return handle_unlink(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1261 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1262 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1263 | case FUSE_RMDIR: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1264 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1265 | return handle_rmdir(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1266 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1267 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1268 | case FUSE_RENAME: { /* rename_in, oldname, newname -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1269 | const struct fuse_rename_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1270 | const char *old_name = ((const char*) data) + sizeof(*req); |
| 1271 | const char *new_name = old_name + strlen(old_name) + 1; |
| 1272 | return handle_rename(fuse, handler, hdr, req, old_name, new_name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1273 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1274 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1275 | // case FUSE_LINK: |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1276 | case FUSE_OPEN: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1277 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1278 | return handle_open(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1279 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1280 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1281 | case FUSE_READ: { /* read_in -> byte[] */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1282 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1283 | return handle_read(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1284 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1285 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1286 | case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1287 | const struct fuse_write_in *req = data; |
| 1288 | const void* buffer = (const __u8*)data + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1289 | return handle_write(fuse, handler, hdr, req, buffer); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1290 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1291 | |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1292 | case FUSE_STATFS: { /* getattr_in -> attr_out */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1293 | return handle_statfs(fuse, handler, hdr); |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1294 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1295 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1296 | case FUSE_RELEASE: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1297 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1298 | return handle_release(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1299 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1300 | |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1301 | case FUSE_FSYNC: { |
| 1302 | const struct fuse_fsync_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1303 | return handle_fsync(fuse, handler, hdr, req); |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1306 | // case FUSE_SETXATTR: |
| 1307 | // case FUSE_GETXATTR: |
| 1308 | // case FUSE_LISTXATTR: |
| 1309 | // case FUSE_REMOVEXATTR: |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1310 | case FUSE_FLUSH: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1311 | return handle_flush(fuse, handler, hdr); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1314 | case FUSE_OPENDIR: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1315 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1316 | return handle_opendir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1317 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1318 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1319 | case FUSE_READDIR: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1320 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1321 | return handle_readdir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1322 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1323 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1324 | case FUSE_RELEASEDIR: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1325 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1326 | return handle_releasedir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1327 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1328 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1329 | // case FUSE_FSYNCDIR: |
| 1330 | case FUSE_INIT: { /* init_in -> init_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1331 | const struct fuse_init_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1332 | return handle_init(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1333 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1334 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1335 | default: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1336 | TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n", |
| 1337 | handler->token, hdr->opcode, hdr->unique, hdr->nodeid); |
| 1338 | return -ENOSYS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1339 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1340 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1343 | static void handle_fuse_requests(struct fuse_handler* handler) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1344 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1345 | struct fuse* fuse = handler->fuse; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1346 | for (;;) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1347 | ssize_t len = read(fuse->fd, |
| 1348 | handler->request_buffer, sizeof(handler->request_buffer)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1349 | if (len < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1350 | if (errno != EINTR) { |
| 1351 | ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno); |
| 1352 | } |
| 1353 | continue; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1354 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1355 | |
| 1356 | if ((size_t)len < sizeof(struct fuse_in_header)) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1357 | ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len); |
| 1358 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1361 | const struct fuse_in_header *hdr = (void*)handler->request_buffer; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1362 | if (hdr->len != (size_t)len) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1363 | ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n", |
| 1364 | handler->token, (size_t)len, hdr->len); |
| 1365 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1368 | const void *data = handler->request_buffer + sizeof(struct fuse_in_header); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1369 | size_t data_len = len - sizeof(struct fuse_in_header); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1370 | __u64 unique = hdr->unique; |
| 1371 | int res = handle_fuse_request(fuse, handler, hdr, data, data_len); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1372 | |
| 1373 | /* We do not access the request again after this point because the underlying |
| 1374 | * buffer storage may have been reused while processing the request. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1375 | |
| 1376 | if (res != NO_STATUS) { |
| 1377 | if (res) { |
| 1378 | TRACE("[%d] ERROR %d\n", handler->token, res); |
| 1379 | } |
| 1380 | fuse_status(fuse, unique, res); |
| 1381 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1385 | static void* start_handler(void* data) |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1386 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1387 | struct fuse_handler* handler = data; |
| 1388 | handle_fuse_requests(handler); |
| 1389 | return NULL; |
| 1390 | } |
| 1391 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1392 | static bool hashmap_remove(void *key, void *value, void *context) { |
| 1393 | Hashmap* map = context; |
| 1394 | hashmapRemove(map, key); |
| 1395 | free(key); |
| 1396 | free(value); |
| 1397 | return true; |
| 1398 | } |
| 1399 | |
| 1400 | static int read_package_list(struct fuse *fuse) { |
| 1401 | pthread_mutex_lock(&fuse->lock); |
| 1402 | |
| 1403 | hashmapForEach(fuse->package_to_appid, hashmap_remove, fuse->package_to_appid); |
| 1404 | |
| 1405 | FILE* file = fopen(kPackagesListFile, "r"); |
| 1406 | if (!file) { |
| 1407 | ERROR("failed to open package list: %s\n", strerror(errno)); |
| 1408 | pthread_mutex_unlock(&fuse->lock); |
| 1409 | return -1; |
| 1410 | } |
| 1411 | |
| 1412 | char buf[512]; |
| 1413 | while (fgets(buf, sizeof(buf), file) != NULL) { |
| 1414 | char package_name[512]; |
| 1415 | int appid; |
| 1416 | if (sscanf(buf, "%s %d", package_name, &appid) == 2) { |
| 1417 | char* package_name_dup = strdup(package_name); |
| 1418 | struct package* package = malloc(sizeof(struct package)); |
| 1419 | if (!package_name_dup || !package) { |
| 1420 | ERROR("cannot allocate package details\n"); |
| 1421 | return -ENOMEM; |
| 1422 | } |
| 1423 | |
| 1424 | package->appid = appid; |
| 1425 | hashmapPut(fuse->package_to_appid, package_name_dup, package); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | TRACE("read_package_list: found %d packages\n", hashmapSize(fuse->package_to_appid)); |
| 1430 | fclose(file); |
| 1431 | pthread_mutex_unlock(&fuse->lock); |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | static void watch_package_list(struct fuse* fuse) { |
| 1436 | struct inotify_event *event; |
| 1437 | char event_buf[512]; |
| 1438 | |
| 1439 | int nfd = inotify_init(); |
| 1440 | if (nfd < 0) { |
| 1441 | ERROR("inotify_init failed: %s\n", strerror(errno)); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | bool active = false; |
| 1446 | while (1) { |
| 1447 | if (!active) { |
| 1448 | int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF); |
| 1449 | if (res == -1) { |
| 1450 | if (errno == ENOENT || errno == EACCES) { |
| 1451 | /* Framework may not have created yet, sleep and retry */ |
| 1452 | ERROR("missing packages.list; retrying\n"); |
| 1453 | sleep(3); |
| 1454 | continue; |
| 1455 | } else { |
| 1456 | ERROR("inotify_add_watch failed: %s\n", strerror(errno)); |
| 1457 | return; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /* Watch above will tell us about any future changes, so |
| 1462 | * read the current state. */ |
| 1463 | if (read_package_list(fuse) == -1) { |
| 1464 | ERROR("read_package_list failed: %s\n", strerror(errno)); |
| 1465 | return; |
| 1466 | } |
| 1467 | active = true; |
| 1468 | } |
| 1469 | |
| 1470 | int event_pos = 0; |
| 1471 | int res = read(nfd, event_buf, sizeof(event_buf)); |
| 1472 | if (res < (int) sizeof(*event)) { |
| 1473 | if (errno == EINTR) |
| 1474 | continue; |
| 1475 | ERROR("failed to read inotify event: %s\n", strerror(errno)); |
| 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | while (res >= (int) sizeof(*event)) { |
| 1480 | int event_size; |
| 1481 | event = (struct inotify_event *) (event_buf + event_pos); |
| 1482 | |
| 1483 | TRACE("inotify event: %08x\n", event->mask); |
| 1484 | if ((event->mask & IN_IGNORED) == IN_IGNORED) { |
| 1485 | /* Previously watched file was deleted, probably due to move |
| 1486 | * that swapped in new data; re-arm the watch and read. */ |
| 1487 | active = false; |
| 1488 | } |
| 1489 | |
| 1490 | event_size = sizeof(*event) + event->len; |
| 1491 | res -= event_size; |
| 1492 | event_pos += event_size; |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1497 | static int ignite_fuse(struct fuse* fuse, int num_threads) |
| 1498 | { |
| 1499 | struct fuse_handler* handlers; |
| 1500 | int i; |
| 1501 | |
| 1502 | handlers = malloc(num_threads * sizeof(struct fuse_handler)); |
| 1503 | if (!handlers) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1504 | ERROR("cannot allocate storage for threads\n"); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1505 | return -ENOMEM; |
| 1506 | } |
| 1507 | |
| 1508 | for (i = 0; i < num_threads; i++) { |
| 1509 | handlers[i].fuse = fuse; |
| 1510 | handlers[i].token = i; |
| 1511 | } |
| 1512 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1513 | /* When deriving permissions, this thread is used to process inotify events, |
| 1514 | * otherwise it becomes one of the FUSE handlers. */ |
| 1515 | i = fuse->derive_perms ? 0 : 1; |
| 1516 | for (; i < num_threads; i++) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1517 | pthread_t thread; |
| 1518 | int res = pthread_create(&thread, NULL, start_handler, &handlers[i]); |
| 1519 | if (res) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1520 | ERROR("failed to start thread #%d, error=%d\n", i, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1521 | goto quit; |
| 1522 | } |
| 1523 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1524 | |
| 1525 | if (fuse->derive_perms) { |
| 1526 | watch_package_list(fuse); |
| 1527 | } else { |
| 1528 | handle_fuse_requests(&handlers[0]); |
| 1529 | } |
| 1530 | |
| 1531 | ERROR("terminated prematurely\n"); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1532 | |
| 1533 | /* don't bother killing all of the other threads or freeing anything, |
| 1534 | * should never get here anyhow */ |
| 1535 | quit: |
| 1536 | exit(1); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1539 | static int usage() |
| 1540 | { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1541 | ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n" |
| 1542 | " -u: specify UID to run as\n" |
| 1543 | " -g: specify GID to run as\n" |
| 1544 | " -t: specify number of threads to use (default %d)\n" |
| 1545 | " -d: derive file permissions based on path\n" |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1546 | "\n", DEFAULT_NUM_THREADS); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1547 | return 1; |
| 1548 | } |
| 1549 | |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1550 | static int run(const char* source_path, const char* dest_path, uid_t uid, gid_t gid, |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1551 | int num_threads, bool derive_perms) { |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1552 | int fd; |
| 1553 | char opts[256]; |
| 1554 | int res; |
| 1555 | struct fuse fuse; |
| 1556 | |
| 1557 | /* cleanup from previous instance, if necessary */ |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1558 | umount2(dest_path, 2); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1559 | |
| 1560 | fd = open("/dev/fuse", O_RDWR); |
| 1561 | if (fd < 0){ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1562 | ERROR("cannot open fuse device: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1563 | return -1; |
| 1564 | } |
| 1565 | |
| 1566 | snprintf(opts, sizeof(opts), |
| 1567 | "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d", |
| 1568 | fd, uid, gid); |
| 1569 | |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1570 | res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1571 | if (res < 0) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1572 | ERROR("cannot mount fuse filesystem: %s\n", strerror(errno)); |
| 1573 | goto error; |
| 1574 | } |
| 1575 | |
| 1576 | res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups); |
| 1577 | if (res < 0) { |
| 1578 | ERROR("cannot setgroups: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1579 | goto error; |
| 1580 | } |
| 1581 | |
| 1582 | res = setgid(gid); |
| 1583 | if (res < 0) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1584 | ERROR("cannot setgid: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1585 | goto error; |
| 1586 | } |
| 1587 | |
| 1588 | res = setuid(uid); |
| 1589 | if (res < 0) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1590 | ERROR("cannot setuid: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1591 | goto error; |
| 1592 | } |
| 1593 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1594 | fuse_init(&fuse, fd, source_path, derive_perms); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1595 | |
| 1596 | umask(0); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1597 | res = ignite_fuse(&fuse, num_threads); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1598 | |
| 1599 | /* we do not attempt to umount the file system here because we are no longer |
| 1600 | * running as the root user */ |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1601 | |
| 1602 | error: |
| 1603 | close(fd); |
| 1604 | return res; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1605 | } |
| 1606 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1607 | int main(int argc, char **argv) |
| 1608 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1609 | int res; |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1610 | const char *source_path = NULL; |
| 1611 | const char *dest_path = NULL; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1612 | uid_t uid = 0; |
| 1613 | gid_t gid = 0; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1614 | int num_threads = DEFAULT_NUM_THREADS; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1615 | bool derive_perms = false; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1616 | int i; |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 1617 | struct rlimit rlim; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1618 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1619 | int opt; |
| 1620 | while ((opt = getopt(argc, argv, "u:g:t:d")) != -1) { |
| 1621 | switch (opt) { |
| 1622 | case 'u': |
| 1623 | uid = strtoul(optarg, NULL, 10); |
| 1624 | break; |
| 1625 | case 'g': |
| 1626 | gid = strtoul(optarg, NULL, 10); |
| 1627 | break; |
| 1628 | case 't': |
| 1629 | num_threads = strtoul(optarg, NULL, 10); |
| 1630 | break; |
| 1631 | case 'd': |
| 1632 | derive_perms = true; |
| 1633 | break; |
| 1634 | case '?': |
| 1635 | default: |
| 1636 | return usage(); |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | for (i = optind; i < argc; i++) { |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1641 | char* arg = argv[i]; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1642 | if (!source_path) { |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1643 | source_path = arg; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1644 | } else if (!dest_path) { |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1645 | dest_path = arg; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1646 | } else if (!uid) { |
| 1647 | uid = strtoul(arg, NULL, 10); |
Jean-Baptiste Queru | e92372b | 2012-08-15 09:54:30 -0700 | [diff] [blame] | 1648 | } else if (!gid) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1649 | gid = strtoul(arg, NULL, 10); |
Jean-Baptiste Queru | e92372b | 2012-08-15 09:54:30 -0700 | [diff] [blame] | 1650 | } else { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1651 | ERROR("too many arguments\n"); |
| 1652 | return usage(); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1653 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1656 | if (!source_path) { |
| 1657 | ERROR("no source path specified\n"); |
| 1658 | return usage(); |
| 1659 | } |
| 1660 | if (!dest_path) { |
| 1661 | ERROR("no dest path specified\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1662 | return usage(); |
| 1663 | } |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1664 | if (!uid || !gid) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1665 | ERROR("uid and gid must be nonzero\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1666 | return usage(); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1667 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1668 | if (num_threads < 1) { |
| 1669 | ERROR("number of threads must be at least 1\n"); |
| 1670 | return usage(); |
| 1671 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1672 | |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 1673 | rlim.rlim_cur = 8192; |
| 1674 | rlim.rlim_max = 8192; |
| 1675 | if (setrlimit(RLIMIT_NOFILE, &rlim)) { |
| 1676 | ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno); |
| 1677 | } |
| 1678 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame^] | 1679 | res = run(source_path, dest_path, uid, gid, num_threads, derive_perms); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1680 | return res < 0 ? 1 : 0; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1681 | } |