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