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