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