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