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