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 | 25aabb9 | 2015-07-06 09:46:08 -0700 | [diff] [blame^] | 447 | if (fuse->gid == AID_SDCARD_RW) { |
| 448 | /* As an optimization, certain trusted system components only run |
| 449 | * as owner but operate across all users. Since we're now handing |
| 450 | * out the sdcard_rw GID only to trusted apps, we're okay relaxing |
| 451 | * the user boundary enforcement for the default view. The UIDs |
| 452 | * assigned to app directories are still multiuser aware. */ |
| 453 | node->gid = fuse->gid; |
| 454 | } else { |
| 455 | node->gid = multiuser_get_uid(node->userid, fuse->gid); |
| 456 | } |
Jeff Sharkey | fc00048 | 2015-03-16 10:17:47 -0700 | [diff] [blame] | 457 | node->mode = 0771; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 458 | break; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 459 | case PERM_ROOT: |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 460 | /* Assume masked off by default. */ |
| 461 | node->mode = 0770; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 462 | if (!strcasecmp(node->name, "Android")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 463 | /* App-specific directories inside; let anyone traverse */ |
| 464 | node->perm = PERM_ANDROID; |
| 465 | node->mode = 0771; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 466 | } |
| 467 | break; |
| 468 | case PERM_ANDROID: |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 469 | if (!strcasecmp(node->name, "data")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 470 | /* App-specific directories inside; let anyone traverse */ |
| 471 | node->perm = PERM_ANDROID_DATA; |
| 472 | node->mode = 0771; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 473 | } else if (!strcasecmp(node->name, "obb")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 474 | /* App-specific directories inside; let anyone traverse */ |
| 475 | node->perm = PERM_ANDROID_OBB; |
| 476 | node->mode = 0771; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 477 | /* Single OBB directory is always shared */ |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 478 | node->graft_path = fuse->obb_path; |
| 479 | node->graft_pathlen = strlen(fuse->obb_path); |
Jeff Sharkey | 2e7d80d | 2014-05-30 15:38:31 -0700 | [diff] [blame] | 480 | } else if (!strcasecmp(node->name, "media")) { |
| 481 | /* App-specific directories inside; let anyone traverse */ |
| 482 | node->perm = PERM_ANDROID_MEDIA; |
| 483 | node->mode = 0771; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 484 | } |
| 485 | break; |
| 486 | case PERM_ANDROID_DATA: |
| 487 | case PERM_ANDROID_OBB: |
Jeff Sharkey | 2e7d80d | 2014-05-30 15:38:31 -0700 | [diff] [blame] | 488 | case PERM_ANDROID_MEDIA: |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 489 | 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] | 490 | if (appid != 0) { |
| 491 | node->uid = multiuser_get_uid(parent->userid, appid); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 492 | } |
| 493 | node->mode = 0770; |
| 494 | break; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 495 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 496 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 497 | node->mode = node->mode & ~fuse->mask; |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 500 | /* Kernel has already enforced everything we returned through |
| 501 | * derive_permissions_locked(), so this is used to lock down access |
| 502 | * even further, such as enforcing that apps hold sdcard_rw. */ |
| 503 | static bool check_caller_access_to_name(struct fuse* fuse, |
| 504 | const struct fuse_in_header *hdr, const struct node* parent_node, |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 505 | const char* name, int mode) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 506 | /* Always block security-sensitive files at root */ |
| 507 | if (parent_node && parent_node->perm == PERM_ROOT) { |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 508 | if (!strcasecmp(name, "autorun.inf") |
| 509 | || !strcasecmp(name, ".android_secure") |
| 510 | || !strcasecmp(name, "android_secure")) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | } |
| 514 | |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 515 | /* Root always has access; access for any other UIDs should always |
| 516 | * be controlled through packages.list. */ |
| 517 | if (hdr->uid == 0) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 518 | return true; |
| 519 | } |
| 520 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 521 | /* No extra permissions to enforce */ |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | static bool check_caller_access_to_node(struct fuse* fuse, |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 526 | const struct fuse_in_header *hdr, const struct node* node, int mode) { |
| 527 | 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] | 528 | } |
| 529 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 530 | struct node *create_node_locked(struct fuse* fuse, |
| 531 | struct node *parent, const char *name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 532 | { |
| 533 | struct node *node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 534 | size_t namelen = strlen(name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 535 | |
Narayan Kamath | faa0935 | 2015-01-13 18:21:10 +0000 | [diff] [blame] | 536 | // Detect overflows in the inode counter. "4 billion nodes should be enough |
| 537 | // for everybody". |
| 538 | if (fuse->inode_ctr == 0) { |
| 539 | ERROR("No more inode numbers available"); |
| 540 | return NULL; |
| 541 | } |
| 542 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 543 | node = calloc(1, sizeof(struct node)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 544 | if (!node) { |
| 545 | return NULL; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 546 | } |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 547 | node->name = malloc(namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 548 | if (!node->name) { |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 549 | free(node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 550 | return NULL; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 551 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 552 | memcpy(node->name, name, namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 553 | if (strcmp(name, actual_name)) { |
| 554 | node->actual_name = malloc(namelen + 1); |
| 555 | if (!node->actual_name) { |
| 556 | free(node->name); |
| 557 | free(node); |
| 558 | return NULL; |
| 559 | } |
| 560 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 561 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 562 | node->namelen = namelen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 563 | node->nid = ptr_to_id(node); |
Narayan Kamath | faa0935 | 2015-01-13 18:21:10 +0000 | [diff] [blame] | 564 | node->ino = fuse->inode_ctr++; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 565 | node->gen = fuse->next_generation++; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 566 | |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 567 | node->deleted = false; |
| 568 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 569 | derive_permissions_locked(fuse, parent, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 570 | acquire_node_locked(node); |
| 571 | add_node_to_parent_locked(node, parent); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 572 | return node; |
| 573 | } |
| 574 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 575 | static int rename_node_locked(struct node *node, const char *name, |
| 576 | const char* actual_name) |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 577 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 578 | size_t namelen = strlen(name); |
| 579 | int need_actual_name = strcmp(name, actual_name); |
| 580 | |
| 581 | /* make the storage bigger without actually changing the name |
| 582 | * in case an error occurs part way */ |
| 583 | if (namelen > node->namelen) { |
| 584 | char* new_name = realloc(node->name, namelen + 1); |
| 585 | if (!new_name) { |
| 586 | return -ENOMEM; |
| 587 | } |
| 588 | node->name = new_name; |
| 589 | if (need_actual_name && node->actual_name) { |
| 590 | char* new_actual_name = realloc(node->actual_name, namelen + 1); |
| 591 | if (!new_actual_name) { |
| 592 | return -ENOMEM; |
| 593 | } |
| 594 | node->actual_name = new_actual_name; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /* update the name, taking care to allocate storage before overwriting the old name */ |
| 599 | if (need_actual_name) { |
| 600 | if (!node->actual_name) { |
| 601 | node->actual_name = malloc(namelen + 1); |
| 602 | if (!node->actual_name) { |
| 603 | return -ENOMEM; |
| 604 | } |
| 605 | } |
| 606 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 607 | } else { |
| 608 | free(node->actual_name); |
| 609 | node->actual_name = NULL; |
| 610 | } |
| 611 | memcpy(node->name, name, namelen + 1); |
| 612 | node->namelen = namelen; |
| 613 | return 0; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 616 | 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] | 617 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 618 | if (nid == FUSE_ROOT_ID) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 619 | return &fuse->root; |
| 620 | } else { |
| 621 | return id_to_ptr(nid); |
| 622 | } |
| 623 | } |
| 624 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 625 | static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid, |
| 626 | char* buf, size_t bufsize) |
| 627 | { |
| 628 | struct node* node = lookup_node_by_id_locked(fuse, nid); |
| 629 | if (node && get_node_path_locked(node, buf, bufsize) < 0) { |
| 630 | node = NULL; |
| 631 | } |
| 632 | return node; |
| 633 | } |
| 634 | |
| 635 | 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] | 636 | { |
| 637 | for (node = node->child; node; node = node->next) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 638 | /* use exact string comparison, nodes that differ by case |
| 639 | * must be considered distinct even if they refer to the same |
| 640 | * underlying file as otherwise operations such as "mv x x" |
| 641 | * will not work because the source and target nodes are the same. */ |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 642 | if (!strcmp(name, node->name) && !node->deleted) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 643 | return node; |
| 644 | } |
| 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 649 | static struct node* acquire_or_create_child_locked( |
| 650 | struct fuse* fuse, struct node* parent, |
| 651 | const char* name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 652 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 653 | struct node* child = lookup_child_by_name_locked(parent, name); |
| 654 | if (child) { |
| 655 | acquire_node_locked(child); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 656 | } else { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 657 | child = create_node_locked(fuse, parent, name, actual_name); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 658 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 659 | return child; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 662 | static void fuse_status(struct fuse *fuse, __u64 unique, int err) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 663 | { |
| 664 | struct fuse_out_header hdr; |
| 665 | hdr.len = sizeof(hdr); |
| 666 | hdr.error = err; |
| 667 | hdr.unique = unique; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 668 | write(fuse->fd, &hdr, sizeof(hdr)); |
| 669 | } |
| 670 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 671 | 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] | 672 | { |
| 673 | struct fuse_out_header hdr; |
| 674 | struct iovec vec[2]; |
| 675 | int res; |
| 676 | |
| 677 | hdr.len = len + sizeof(hdr); |
| 678 | hdr.error = 0; |
| 679 | hdr.unique = unique; |
| 680 | |
| 681 | vec[0].iov_base = &hdr; |
| 682 | vec[0].iov_len = sizeof(hdr); |
| 683 | vec[1].iov_base = data; |
| 684 | vec[1].iov_len = len; |
| 685 | |
| 686 | res = writev(fuse->fd, vec, 2); |
| 687 | if (res < 0) { |
| 688 | ERROR("*** REPLY FAILED *** %d\n", errno); |
| 689 | } |
| 690 | } |
| 691 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 692 | static int fuse_reply_entry(struct fuse* fuse, __u64 unique, |
| 693 | struct node* parent, const char* name, const char* actual_name, |
| 694 | const char* path) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 695 | { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 696 | struct node* node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 697 | struct fuse_entry_out out; |
| 698 | struct stat s; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 699 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 700 | if (lstat(path, &s) < 0) { |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 701 | return -errno; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 702 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 703 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 704 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 705 | node = acquire_or_create_child_locked(fuse, parent, name, actual_name); |
| 706 | if (!node) { |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 707 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 708 | return -ENOMEM; |
| 709 | } |
| 710 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 711 | attr_from_stat(&out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 712 | out.attr_valid = 10; |
| 713 | out.entry_valid = 10; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 714 | out.nodeid = node->nid; |
| 715 | out.generation = node->gen; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 716 | pthread_mutex_unlock(&fuse->global->lock); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 717 | fuse_reply(fuse, unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 718 | return NO_STATUS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 721 | 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] | 722 | const char* path) |
| 723 | { |
| 724 | struct fuse_attr_out out; |
| 725 | struct stat s; |
| 726 | |
| 727 | if (lstat(path, &s) < 0) { |
| 728 | return -errno; |
| 729 | } |
| 730 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 731 | attr_from_stat(&out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 732 | out.attr_valid = 10; |
| 733 | fuse_reply(fuse, unique, &out, sizeof(out)); |
| 734 | return NO_STATUS; |
| 735 | } |
| 736 | |
| 737 | static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 738 | const struct fuse_in_header *hdr, const char* name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 739 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 740 | struct node* parent_node; |
| 741 | char parent_path[PATH_MAX]; |
| 742 | char child_path[PATH_MAX]; |
| 743 | const char* actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 744 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 745 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 746 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 747 | parent_path, sizeof(parent_path)); |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 748 | TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 749 | parent_node ? parent_node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 750 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 751 | |
| 752 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 753 | child_path, sizeof(child_path), 1))) { |
| 754 | return -ENOENT; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 755 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 756 | 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] | 757 | return -EACCES; |
| 758 | } |
| 759 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 760 | 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] | 761 | } |
| 762 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 763 | static int handle_forget(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 764 | const struct fuse_in_header *hdr, const struct fuse_forget_in *req) |
| 765 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 766 | struct node* node; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 767 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 768 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 769 | node = lookup_node_by_id_locked(fuse, hdr->nodeid); |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 770 | TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 771 | hdr->nodeid, node ? node->name : "?"); |
| 772 | if (node) { |
| 773 | __u64 n = req->nlookup; |
| 774 | while (n--) { |
| 775 | release_node_locked(node); |
| 776 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 777 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 778 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 779 | return NO_STATUS; /* no reply */ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 782 | static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 783 | const struct fuse_in_header *hdr, const struct fuse_getattr_in *req) |
| 784 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 785 | struct node* node; |
| 786 | char path[PATH_MAX]; |
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 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 789 | 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] | 790 | TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 791 | req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 792 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 793 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 794 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 795 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 796 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 797 | if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 798 | return -EACCES; |
| 799 | } |
| 800 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 801 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 804 | static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 805 | const struct fuse_in_header *hdr, const struct fuse_setattr_in *req) |
| 806 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 807 | struct node* node; |
| 808 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 809 | struct timespec times[2]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 810 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 811 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 812 | 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] | 813 | TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 814 | req->fh, req->valid, hdr->nodeid, node ? node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 815 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 816 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 817 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 818 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 819 | } |
Marco Nelissen | a80f098 | 2014-12-10 10:44:20 -0800 | [diff] [blame] | 820 | |
| 821 | if (!(req->valid & FATTR_FH) && |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 822 | !check_caller_access_to_node(fuse, hdr, node, W_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 823 | return -EACCES; |
| 824 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 825 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 826 | /* XXX: incomplete implementation on purpose. |
| 827 | * chmod/chown should NEVER be implemented.*/ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 828 | |
Elliott Hughes | 853574d | 2014-07-31 12:03:03 -0700 | [diff] [blame] | 829 | if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 830 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 834 | * are both set, then set it to the current time. Else, set it to the |
| 835 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 836 | * as it allows ATIME and MTIME to be changed independently, and has |
| 837 | * nanosecond resolution which fuse also has. |
| 838 | */ |
| 839 | if (req->valid & (FATTR_ATIME | FATTR_MTIME)) { |
| 840 | times[0].tv_nsec = UTIME_OMIT; |
| 841 | times[1].tv_nsec = UTIME_OMIT; |
| 842 | if (req->valid & FATTR_ATIME) { |
| 843 | if (req->valid & FATTR_ATIME_NOW) { |
| 844 | times[0].tv_nsec = UTIME_NOW; |
| 845 | } else { |
| 846 | times[0].tv_sec = req->atime; |
| 847 | times[0].tv_nsec = req->atimensec; |
| 848 | } |
| 849 | } |
| 850 | if (req->valid & FATTR_MTIME) { |
| 851 | if (req->valid & FATTR_MTIME_NOW) { |
| 852 | times[1].tv_nsec = UTIME_NOW; |
| 853 | } else { |
| 854 | times[1].tv_sec = req->mtime; |
| 855 | times[1].tv_nsec = req->mtimensec; |
| 856 | } |
| 857 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 858 | TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n", |
| 859 | handler->token, path, times[0].tv_sec, times[1].tv_sec); |
| 860 | if (utimensat(-1, path, times, 0) < 0) { |
| 861 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 864 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 867 | static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 868 | const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name) |
| 869 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 870 | struct node* parent_node; |
| 871 | char parent_path[PATH_MAX]; |
| 872 | char child_path[PATH_MAX]; |
| 873 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 874 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 875 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 876 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 877 | parent_path, sizeof(parent_path)); |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 878 | TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 879 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 880 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 881 | |
| 882 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 883 | child_path, sizeof(child_path), 1))) { |
| 884 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 885 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 886 | 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] | 887 | return -EACCES; |
| 888 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 889 | __u32 mode = (req->mode & (~0777)) | 0664; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 890 | if (mknod(child_path, mode, req->rdev) < 0) { |
| 891 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 892 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 893 | 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] | 894 | } |
| 895 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 896 | static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 897 | const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name) |
| 898 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 899 | struct node* parent_node; |
| 900 | char parent_path[PATH_MAX]; |
| 901 | char child_path[PATH_MAX]; |
| 902 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 903 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 904 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 905 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 906 | parent_path, sizeof(parent_path)); |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 907 | TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 908 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 909 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 910 | |
| 911 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 912 | child_path, sizeof(child_path), 1))) { |
| 913 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 914 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 915 | 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] | 916 | return -EACCES; |
| 917 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 918 | __u32 mode = (req->mode & (~0777)) | 0775; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 919 | if (mkdir(child_path, mode) < 0) { |
| 920 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 921 | } |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 922 | |
| 923 | /* When creating /Android/data and /Android/obb, mark them as .nomedia */ |
| 924 | if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) { |
| 925 | char nomedia[PATH_MAX]; |
| 926 | snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path); |
| 927 | if (touch(nomedia, 0664) != 0) { |
| 928 | ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno)); |
| 929 | return -ENOENT; |
| 930 | } |
| 931 | } |
| 932 | if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) { |
| 933 | char nomedia[PATH_MAX]; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 934 | snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obb_path); |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 935 | if (touch(nomedia, 0664) != 0) { |
| 936 | ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno)); |
| 937 | return -ENOENT; |
| 938 | } |
| 939 | } |
| 940 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 941 | 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] | 942 | } |
| 943 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 944 | static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 945 | const struct fuse_in_header* hdr, const char* name) |
| 946 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 947 | struct node* parent_node; |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 948 | struct node* child_node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 949 | char parent_path[PATH_MAX]; |
| 950 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 951 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 952 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 953 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 954 | parent_path, sizeof(parent_path)); |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 955 | TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 956 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 957 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 958 | |
| 959 | if (!parent_node || !find_file_within(parent_path, name, |
| 960 | child_path, sizeof(child_path), 1)) { |
| 961 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 962 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 963 | 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] | 964 | return -EACCES; |
| 965 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 966 | if (unlink(child_path) < 0) { |
| 967 | return -errno; |
| 968 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 969 | pthread_mutex_lock(&fuse->global->lock); |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 970 | child_node = lookup_child_by_name_locked(parent_node, name); |
| 971 | if (child_node) { |
| 972 | child_node->deleted = true; |
| 973 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 974 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 975 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 978 | static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 979 | const struct fuse_in_header* hdr, const char* name) |
| 980 | { |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 981 | struct node* child_node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 982 | struct node* parent_node; |
| 983 | char parent_path[PATH_MAX]; |
| 984 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 985 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 986 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 987 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 988 | parent_path, sizeof(parent_path)); |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 989 | TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 990 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 991 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 992 | |
| 993 | if (!parent_node || !find_file_within(parent_path, name, |
| 994 | child_path, sizeof(child_path), 1)) { |
| 995 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 996 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 997 | 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] | 998 | return -EACCES; |
| 999 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1000 | if (rmdir(child_path) < 0) { |
| 1001 | return -errno; |
| 1002 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1003 | pthread_mutex_lock(&fuse->global->lock); |
Krzysztof Adamski | c535312 | 2014-07-16 08:34:30 +0200 | [diff] [blame] | 1004 | child_node = lookup_child_by_name_locked(parent_node, name); |
| 1005 | if (child_node) { |
| 1006 | child_node->deleted = true; |
| 1007 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1008 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1009 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1012 | static int handle_rename(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1013 | const struct fuse_in_header* hdr, const struct fuse_rename_in* req, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1014 | const char* old_name, const char* new_name) |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1015 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1016 | struct node* old_parent_node; |
| 1017 | struct node* new_parent_node; |
| 1018 | struct node* child_node; |
| 1019 | char old_parent_path[PATH_MAX]; |
| 1020 | char new_parent_path[PATH_MAX]; |
| 1021 | char old_child_path[PATH_MAX]; |
| 1022 | char new_child_path[PATH_MAX]; |
| 1023 | const char* new_actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1024 | int res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1025 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1026 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1027 | old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 1028 | old_parent_path, sizeof(old_parent_path)); |
| 1029 | new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir, |
| 1030 | new_parent_path, sizeof(new_parent_path)); |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 1031 | TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1032 | old_name, new_name, |
| 1033 | hdr->nodeid, old_parent_node ? old_parent_node->name : "?", |
| 1034 | req->newdir, new_parent_node ? new_parent_node->name : "?"); |
| 1035 | if (!old_parent_node || !new_parent_node) { |
| 1036 | res = -ENOENT; |
| 1037 | goto lookup_error; |
| 1038 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1039 | 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] | 1040 | res = -EACCES; |
| 1041 | goto lookup_error; |
| 1042 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1043 | 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] | 1044 | res = -EACCES; |
| 1045 | goto lookup_error; |
| 1046 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1047 | child_node = lookup_child_by_name_locked(old_parent_node, old_name); |
| 1048 | if (!child_node || get_node_path_locked(child_node, |
| 1049 | old_child_path, sizeof(old_child_path)) < 0) { |
| 1050 | res = -ENOENT; |
| 1051 | goto lookup_error; |
| 1052 | } |
| 1053 | acquire_node_locked(child_node); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1054 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* Special case for renaming a file where destination is same path |
| 1057 | * differing only by case. In this case we don't want to look for a case |
| 1058 | * insensitive match. This allows commands like "mv foo FOO" to work as expected. |
| 1059 | */ |
| 1060 | int search = old_parent_node != new_parent_node |
| 1061 | || strcasecmp(old_name, new_name); |
| 1062 | if (!(new_actual_name = find_file_within(new_parent_path, new_name, |
| 1063 | new_child_path, sizeof(new_child_path), search))) { |
| 1064 | res = -ENOENT; |
| 1065 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1068 | TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path); |
| 1069 | res = rename(old_child_path, new_child_path); |
| 1070 | if (res < 0) { |
| 1071 | res = -errno; |
| 1072 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
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 | res = rename_node_locked(child_node, new_name, new_actual_name); |
| 1077 | if (!res) { |
| 1078 | remove_node_from_parent_locked(child_node); |
| 1079 | add_node_to_parent_locked(child_node, new_parent_node); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1080 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1081 | goto done; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1082 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1083 | io_error: |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1084 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1085 | done: |
| 1086 | release_node_locked(child_node); |
| 1087 | lookup_error: |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1088 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1089 | return res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1092 | static int open_flags_to_access_mode(int open_flags) { |
| 1093 | if ((open_flags & O_ACCMODE) == O_RDONLY) { |
| 1094 | return R_OK; |
| 1095 | } else if ((open_flags & O_ACCMODE) == O_WRONLY) { |
| 1096 | return W_OK; |
| 1097 | } else { |
| 1098 | /* Probably O_RDRW, but treat as default to be safe */ |
| 1099 | return R_OK | W_OK; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1103 | static int handle_open(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1104 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 1105 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1106 | struct node* node; |
| 1107 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1108 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1109 | struct handle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1110 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1111 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1112 | 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] | 1113 | TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1114 | req->flags, hdr->nodeid, node ? node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1115 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1116 | |
| 1117 | if (!node) { |
| 1118 | return -ENOENT; |
| 1119 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1120 | if (!check_caller_access_to_node(fuse, hdr, node, |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1121 | open_flags_to_access_mode(req->flags))) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1122 | return -EACCES; |
| 1123 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1124 | h = malloc(sizeof(*h)); |
| 1125 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1126 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1127 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1128 | TRACE("[%d] OPEN %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1129 | h->fd = open(path, req->flags); |
| 1130 | if (h->fd < 0) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1131 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1132 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1133 | } |
| 1134 | out.fh = ptr_to_id(h); |
| 1135 | out.open_flags = 0; |
| 1136 | out.padding = 0; |
| 1137 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1138 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1141 | static int handle_read(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1142 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1143 | { |
| 1144 | struct handle *h = id_to_ptr(req->fh); |
| 1145 | __u64 unique = hdr->unique; |
| 1146 | __u32 size = req->size; |
| 1147 | __u64 offset = req->offset; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1148 | int res; |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1149 | __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] | 1150 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1151 | /* Don't access any other fields of hdr or req beyond this point, the read buffer |
| 1152 | * overlaps the request buffer and will clobber data in the request. This |
| 1153 | * 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] | 1154 | |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 1155 | TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token, |
| 1156 | h, h->fd, size, (uint64_t) offset); |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1157 | if (size > MAX_READ) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1158 | return -EINVAL; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1159 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1160 | res = pread64(h->fd, read_buffer, size, offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1161 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1162 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1163 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1164 | fuse_reply(fuse, unique, read_buffer, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1165 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1168 | static int handle_write(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1169 | const struct fuse_in_header* hdr, const struct fuse_write_in* req, |
| 1170 | const void* buffer) |
| 1171 | { |
| 1172 | struct fuse_write_out out; |
| 1173 | struct handle *h = id_to_ptr(req->fh); |
| 1174 | int res; |
Arpad Horvath | 49e9344 | 2014-02-18 10:18:25 +0100 | [diff] [blame] | 1175 | __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE))); |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 1176 | |
Arpad Horvath | 49e9344 | 2014-02-18 10:18:25 +0100 | [diff] [blame] | 1177 | if (req->flags & O_DIRECT) { |
| 1178 | memcpy(aligned_buffer, buffer, req->size); |
| 1179 | buffer = (const __u8*) aligned_buffer; |
| 1180 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1181 | |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 1182 | TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1183 | h, h->fd, req->size, req->offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1184 | res = pwrite64(h->fd, buffer, req->size, req->offset); |
| 1185 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1186 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1187 | } |
| 1188 | out.size = res; |
Daisuke Okitsu | 19ec886 | 2013-08-05 12:18:15 +0900 | [diff] [blame] | 1189 | out.padding = 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1190 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1191 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1194 | static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1195 | const struct fuse_in_header* hdr) |
| 1196 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1197 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1198 | struct statfs stat; |
| 1199 | struct fuse_statfs_out out; |
| 1200 | int res; |
| 1201 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1202 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1203 | TRACE("[%d] STATFS\n", handler->token); |
| 1204 | res = get_node_path_locked(&fuse->root, path, sizeof(path)); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1205 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1206 | if (res < 0) { |
| 1207 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1208 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1209 | if (statfs(fuse->root.name, &stat) < 0) { |
| 1210 | return -errno; |
| 1211 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1212 | memset(&out, 0, sizeof(out)); |
| 1213 | out.st.blocks = stat.f_blocks; |
| 1214 | out.st.bfree = stat.f_bfree; |
| 1215 | out.st.bavail = stat.f_bavail; |
| 1216 | out.st.files = stat.f_files; |
| 1217 | out.st.ffree = stat.f_ffree; |
| 1218 | out.st.bsize = stat.f_bsize; |
| 1219 | out.st.namelen = stat.f_namelen; |
| 1220 | out.st.frsize = stat.f_frsize; |
| 1221 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1222 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1225 | static int handle_release(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1226 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1227 | { |
| 1228 | struct handle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1229 | |
| 1230 | TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1231 | close(h->fd); |
| 1232 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1233 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1236 | static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1237 | const struct fuse_in_header* hdr, const struct fuse_fsync_in* req) |
| 1238 | { |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1239 | bool is_dir = (hdr->opcode == FUSE_FSYNCDIR); |
| 1240 | bool is_data_sync = req->fsync_flags & 1; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1241 | |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame] | 1242 | int fd = -1; |
| 1243 | if (is_dir) { |
| 1244 | struct dirhandle *dh = id_to_ptr(req->fh); |
| 1245 | fd = dirfd(dh->d); |
| 1246 | } else { |
| 1247 | struct handle *h = id_to_ptr(req->fh); |
| 1248 | fd = h->fd; |
| 1249 | } |
| 1250 | |
| 1251 | TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token, |
| 1252 | is_dir ? "FSYNCDIR" : "FSYNC", |
| 1253 | id_to_ptr(req->fh), fd, is_data_sync); |
| 1254 | int res = is_data_sync ? fdatasync(fd) : fsync(fd); |
| 1255 | if (res == -1) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1256 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1257 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1258 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1261 | static int handle_flush(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1262 | const struct fuse_in_header* hdr) |
| 1263 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1264 | TRACE("[%d] FLUSH\n", handler->token); |
| 1265 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1268 | static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1269 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 1270 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1271 | struct node* node; |
| 1272 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1273 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1274 | struct dirhandle *h; |
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 | pthread_mutex_lock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1277 | 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] | 1278 | TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1279 | hdr->nodeid, node ? node->name : "?"); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1280 | pthread_mutex_unlock(&fuse->global->lock); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1281 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1282 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1283 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1284 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1285 | if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1286 | return -EACCES; |
| 1287 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1288 | h = malloc(sizeof(*h)); |
| 1289 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1290 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1291 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1292 | TRACE("[%d] OPENDIR %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1293 | h->d = opendir(path); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1294 | if (!h->d) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1295 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1296 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1297 | } |
| 1298 | out.fh = ptr_to_id(h); |
Ken Sumrall | 3a87688 | 2013-08-14 20:02:13 -0700 | [diff] [blame] | 1299 | out.open_flags = 0; |
| 1300 | out.padding = 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1301 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1302 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1305 | static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1306 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1307 | { |
| 1308 | char buffer[8192]; |
| 1309 | struct fuse_dirent *fde = (struct fuse_dirent*) buffer; |
| 1310 | struct dirent *de; |
| 1311 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1312 | |
| 1313 | TRACE("[%d] READDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1314 | if (req->offset == 0) { |
| 1315 | /* rewinddir() might have been called above us, so rewind here too */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1316 | TRACE("[%d] calling rewinddir()\n", handler->token); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1317 | rewinddir(h->d); |
| 1318 | } |
| 1319 | de = readdir(h->d); |
| 1320 | if (!de) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1321 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1322 | } |
| 1323 | fde->ino = FUSE_UNKNOWN_INO; |
| 1324 | /* increment the offset so we can detect when rewinddir() seeks back to the beginning */ |
| 1325 | fde->off = req->offset + 1; |
| 1326 | fde->type = de->d_type; |
| 1327 | fde->namelen = strlen(de->d_name); |
| 1328 | memcpy(fde->name, de->d_name, fde->namelen + 1); |
| 1329 | fuse_reply(fuse, hdr->unique, fde, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1330 | FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); |
| 1331 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1334 | static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1335 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1336 | { |
| 1337 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1338 | |
| 1339 | TRACE("[%d] RELEASEDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1340 | closedir(h->d); |
| 1341 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1342 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1345 | static int handle_init(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1346 | const struct fuse_in_header* hdr, const struct fuse_init_in* req) |
| 1347 | { |
| 1348 | struct fuse_init_out out; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1349 | size_t fuse_struct_size; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1350 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1351 | TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n", |
| 1352 | handler->token, req->major, req->minor, req->max_readahead, req->flags); |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1353 | |
| 1354 | /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out |
| 1355 | * defined (fuse version 7.6). The structure is the same from 7.6 through |
| 1356 | * 7.22. Beginning with 7.23, the structure increased in size and added |
| 1357 | * new parameters. |
| 1358 | */ |
| 1359 | if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) { |
| 1360 | ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6", |
| 1361 | req->major, req->minor, FUSE_KERNEL_VERSION); |
| 1362 | return -1; |
| 1363 | } |
| 1364 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1365 | /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */ |
| 1366 | out.minor = MIN(req->minor, 15); |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1367 | fuse_struct_size = sizeof(out); |
| 1368 | #if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) |
| 1369 | /* FUSE_KERNEL_VERSION >= 23. */ |
| 1370 | |
| 1371 | /* If the kernel only works on minor revs older than or equal to 22, |
| 1372 | * then use the older structure size since this code only uses the 7.22 |
| 1373 | * version of the structure. */ |
| 1374 | if (req->minor <= 22) { |
| 1375 | fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE; |
| 1376 | } |
| 1377 | #endif |
| 1378 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1379 | out.major = FUSE_KERNEL_VERSION; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1380 | out.max_readahead = req->max_readahead; |
| 1381 | out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
| 1382 | out.max_background = 32; |
| 1383 | out.congestion_threshold = 32; |
| 1384 | out.max_write = MAX_WRITE; |
Christopher Ferris | ff649ea | 2014-09-13 13:53:08 -0700 | [diff] [blame] | 1385 | fuse_reply(fuse, hdr->unique, &out, fuse_struct_size); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1386 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1389 | static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1390 | const struct fuse_in_header *hdr, const void *data, size_t data_len) |
| 1391 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1392 | switch (hdr->opcode) { |
| 1393 | case FUSE_LOOKUP: { /* bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1394 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1395 | return handle_lookup(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1396 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1397 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1398 | case FUSE_FORGET: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1399 | const struct fuse_forget_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1400 | return handle_forget(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1401 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1402 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1403 | case FUSE_GETATTR: { /* getattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1404 | const struct fuse_getattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1405 | return handle_getattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1406 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1407 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1408 | case FUSE_SETATTR: { /* setattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1409 | const struct fuse_setattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1410 | return handle_setattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1411 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1412 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1413 | // case FUSE_READLINK: |
| 1414 | // case FUSE_SYMLINK: |
| 1415 | case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1416 | const struct fuse_mknod_in *req = data; |
| 1417 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1418 | return handle_mknod(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1419 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1420 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1421 | case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1422 | const struct fuse_mkdir_in *req = data; |
| 1423 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1424 | return handle_mkdir(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1425 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1426 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1427 | case FUSE_UNLINK: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1428 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1429 | return handle_unlink(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1430 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1431 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1432 | case FUSE_RMDIR: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1433 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1434 | return handle_rmdir(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1435 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1436 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1437 | case FUSE_RENAME: { /* rename_in, oldname, newname -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1438 | const struct fuse_rename_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1439 | const char *old_name = ((const char*) data) + sizeof(*req); |
| 1440 | const char *new_name = old_name + strlen(old_name) + 1; |
| 1441 | return handle_rename(fuse, handler, hdr, req, old_name, new_name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1442 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1443 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1444 | // case FUSE_LINK: |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1445 | case FUSE_OPEN: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1446 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1447 | return handle_open(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1448 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1449 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1450 | case FUSE_READ: { /* read_in -> byte[] */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1451 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1452 | return handle_read(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1453 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1454 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1455 | case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1456 | const struct fuse_write_in *req = data; |
| 1457 | const void* buffer = (const __u8*)data + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1458 | return handle_write(fuse, handler, hdr, req, buffer); |
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 | |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1461 | case FUSE_STATFS: { /* getattr_in -> attr_out */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1462 | return handle_statfs(fuse, handler, hdr); |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1463 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1464 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1465 | case FUSE_RELEASE: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1466 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1467 | return handle_release(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1468 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1469 | |
Daisuke Okitsu | b2831a2 | 2014-02-17 10:33:11 +0100 | [diff] [blame] | 1470 | case FUSE_FSYNC: |
| 1471 | case FUSE_FSYNCDIR: { |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1472 | const struct fuse_fsync_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1473 | return handle_fsync(fuse, handler, hdr, req); |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1476 | // case FUSE_SETXATTR: |
| 1477 | // case FUSE_GETXATTR: |
| 1478 | // case FUSE_LISTXATTR: |
| 1479 | // case FUSE_REMOVEXATTR: |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1480 | case FUSE_FLUSH: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1481 | return handle_flush(fuse, handler, hdr); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1484 | case FUSE_OPENDIR: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1485 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1486 | return handle_opendir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1487 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1488 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1489 | case FUSE_READDIR: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1490 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1491 | return handle_readdir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1492 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1493 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1494 | case FUSE_RELEASEDIR: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1495 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1496 | return handle_releasedir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1497 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1498 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1499 | case FUSE_INIT: { /* init_in -> init_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1500 | const struct fuse_init_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1501 | return handle_init(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1502 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1503 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1504 | default: { |
Marcus Oakland | e43b99a | 2014-07-23 13:04:59 +0100 | [diff] [blame] | 1505 | TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n", |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1506 | handler->token, hdr->opcode, hdr->unique, hdr->nodeid); |
| 1507 | return -ENOSYS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1508 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1509 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1512 | static void handle_fuse_requests(struct fuse_handler* handler) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1513 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1514 | struct fuse* fuse = handler->fuse; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1515 | for (;;) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1516 | ssize_t len = read(fuse->fd, |
| 1517 | handler->request_buffer, sizeof(handler->request_buffer)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1518 | if (len < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1519 | if (errno != EINTR) { |
| 1520 | ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno); |
| 1521 | } |
Jeff Sharkey | 4a48581 | 2015-06-30 16:02:40 -0700 | [diff] [blame] | 1522 | if (errno == ENODEV) { |
| 1523 | ERROR("[%d] someone stole our marbles!\n", handler->token); |
| 1524 | exit(2); |
| 1525 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1526 | continue; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1527 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1528 | |
| 1529 | if ((size_t)len < sizeof(struct fuse_in_header)) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1530 | ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len); |
| 1531 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1534 | const struct fuse_in_header *hdr = (void*)handler->request_buffer; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1535 | if (hdr->len != (size_t)len) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1536 | ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n", |
| 1537 | handler->token, (size_t)len, hdr->len); |
| 1538 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1541 | const void *data = handler->request_buffer + sizeof(struct fuse_in_header); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1542 | size_t data_len = len - sizeof(struct fuse_in_header); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1543 | __u64 unique = hdr->unique; |
| 1544 | int res = handle_fuse_request(fuse, handler, hdr, data, data_len); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1545 | |
| 1546 | /* We do not access the request again after this point because the underlying |
| 1547 | * buffer storage may have been reused while processing the request. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1548 | |
| 1549 | if (res != NO_STATUS) { |
| 1550 | if (res) { |
| 1551 | TRACE("[%d] ERROR %d\n", handler->token, res); |
| 1552 | } |
| 1553 | fuse_status(fuse, unique, res); |
| 1554 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1558 | static void* start_handler(void* data) |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1559 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1560 | struct fuse_handler* handler = data; |
| 1561 | handle_fuse_requests(handler); |
| 1562 | return NULL; |
| 1563 | } |
| 1564 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1565 | static bool remove_str_to_int(void *key, void *value, void *context) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1566 | Hashmap* map = context; |
| 1567 | hashmapRemove(map, key); |
| 1568 | free(key); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1569 | return true; |
| 1570 | } |
| 1571 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1572 | static int read_package_list(struct fuse_global* global) { |
| 1573 | pthread_mutex_lock(&global->lock); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1574 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1575 | 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] | 1576 | |
| 1577 | FILE* file = fopen(kPackagesListFile, "r"); |
| 1578 | if (!file) { |
| 1579 | ERROR("failed to open package list: %s\n", strerror(errno)); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1580 | pthread_mutex_unlock(&global->lock); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1581 | return -1; |
| 1582 | } |
| 1583 | |
| 1584 | char buf[512]; |
| 1585 | while (fgets(buf, sizeof(buf), file) != NULL) { |
| 1586 | char package_name[512]; |
| 1587 | int appid; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1588 | char gids[512]; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1589 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1590 | if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) { |
| 1591 | char* package_name_dup = strdup(package_name); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1592 | hashmapPut(global->package_to_appid, package_name_dup, (void*) (uintptr_t) appid); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1596 | TRACE("read_package_list: found %zu packages\n", |
| 1597 | hashmapSize(global->package_to_appid)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1598 | fclose(file); |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1599 | pthread_mutex_unlock(&global->lock); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1600 | return 0; |
| 1601 | } |
| 1602 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1603 | static void watch_package_list(struct fuse_global* global) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1604 | struct inotify_event *event; |
| 1605 | char event_buf[512]; |
| 1606 | |
| 1607 | int nfd = inotify_init(); |
| 1608 | if (nfd < 0) { |
| 1609 | ERROR("inotify_init failed: %s\n", strerror(errno)); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | bool active = false; |
| 1614 | while (1) { |
| 1615 | if (!active) { |
| 1616 | int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF); |
| 1617 | if (res == -1) { |
| 1618 | if (errno == ENOENT || errno == EACCES) { |
| 1619 | /* Framework may not have created yet, sleep and retry */ |
| 1620 | ERROR("missing packages.list; retrying\n"); |
| 1621 | sleep(3); |
| 1622 | continue; |
| 1623 | } else { |
| 1624 | ERROR("inotify_add_watch failed: %s\n", strerror(errno)); |
| 1625 | return; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /* Watch above will tell us about any future changes, so |
| 1630 | * read the current state. */ |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1631 | if (read_package_list(global) == -1) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1632 | ERROR("read_package_list failed: %s\n", strerror(errno)); |
| 1633 | return; |
| 1634 | } |
| 1635 | active = true; |
| 1636 | } |
| 1637 | |
| 1638 | int event_pos = 0; |
| 1639 | int res = read(nfd, event_buf, sizeof(event_buf)); |
| 1640 | if (res < (int) sizeof(*event)) { |
| 1641 | if (errno == EINTR) |
| 1642 | continue; |
| 1643 | ERROR("failed to read inotify event: %s\n", strerror(errno)); |
| 1644 | return; |
| 1645 | } |
| 1646 | |
| 1647 | while (res >= (int) sizeof(*event)) { |
| 1648 | int event_size; |
| 1649 | event = (struct inotify_event *) (event_buf + event_pos); |
| 1650 | |
| 1651 | TRACE("inotify event: %08x\n", event->mask); |
| 1652 | if ((event->mask & IN_IGNORED) == IN_IGNORED) { |
| 1653 | /* Previously watched file was deleted, probably due to move |
| 1654 | * that swapped in new data; re-arm the watch and read. */ |
| 1655 | active = false; |
| 1656 | } |
| 1657 | |
| 1658 | event_size = sizeof(*event) + event->len; |
| 1659 | res -= event_size; |
| 1660 | event_pos += event_size; |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1665 | static int usage() { |
| 1666 | ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n" |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1667 | " -u: specify UID to run as\n" |
| 1668 | " -g: specify GID to run as\n" |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1669 | " -m: source_path is multi-user\n" |
| 1670 | " -w: runtime_write mount has full write access\n" |
| 1671 | "\n"); |
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 | 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] | 1676 | char opts[256]; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1677 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1678 | fuse->fd = open("/dev/fuse", O_RDWR); |
| 1679 | if (fuse->fd == -1) { |
| 1680 | ERROR("failed to open fuse device: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1681 | return -1; |
| 1682 | } |
| 1683 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1684 | umount2(fuse->dest_path, MNT_DETACH); |
| 1685 | |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1686 | snprintf(opts, sizeof(opts), |
| 1687 | "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] | 1688 | fuse->fd, fuse->global->uid, fuse->global->gid); |
| 1689 | if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC | |
| 1690 | MS_NOATIME, opts) != 0) { |
| 1691 | ERROR("failed to mount fuse filesystem: %s\n", strerror(errno)); |
| 1692 | return -1; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1695 | fuse->next_generation = 0; |
| 1696 | fuse->inode_ctr = 1; |
| 1697 | fuse->gid = gid; |
| 1698 | fuse->mask = mask; |
| 1699 | |
| 1700 | memset(&fuse->root, 0, sizeof(fuse->root)); |
| 1701 | fuse->root.nid = FUSE_ROOT_ID; /* 1 */ |
| 1702 | fuse->root.refcount = 2; |
| 1703 | fuse->root.namelen = strlen(fuse->source_path); |
| 1704 | fuse->root.name = strdup(fuse->source_path); |
| 1705 | fuse->root.userid = 0; |
| 1706 | fuse->root.uid = AID_ROOT; |
| 1707 | fuse->root.gid = fuse->gid; |
| 1708 | |
| 1709 | if (fuse->global->multi_user) { |
| 1710 | fuse->root.perm = PERM_PRE_ROOT; |
| 1711 | fuse->root.mode = 0711; |
| 1712 | snprintf(fuse->obb_path, sizeof(fuse->obb_path), "%s/obb", fuse->source_path); |
| 1713 | } else { |
| 1714 | fuse->root.perm = PERM_ROOT; |
| 1715 | fuse->root.mode = 0771 & ~mask; |
| 1716 | 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] | 1717 | } |
| 1718 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1719 | return 0; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1720 | } |
| 1721 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1722 | static void run(const char* source_path, const char* label, uid_t uid, |
| 1723 | gid_t gid, bool multi_user, bool full_write) { |
| 1724 | struct fuse_global global; |
| 1725 | struct fuse fuse_default; |
| 1726 | struct fuse fuse_read; |
| 1727 | struct fuse fuse_write; |
| 1728 | struct fuse_handler handler_default; |
| 1729 | struct fuse_handler handler_read; |
| 1730 | struct fuse_handler handler_write; |
| 1731 | pthread_t thread_default; |
| 1732 | pthread_t thread_read; |
| 1733 | pthread_t thread_write; |
| 1734 | |
| 1735 | memset(&global, 0, sizeof(global)); |
| 1736 | memset(&fuse_default, 0, sizeof(fuse_default)); |
| 1737 | memset(&fuse_read, 0, sizeof(fuse_read)); |
| 1738 | memset(&fuse_write, 0, sizeof(fuse_write)); |
| 1739 | memset(&handler_default, 0, sizeof(handler_default)); |
| 1740 | memset(&handler_read, 0, sizeof(handler_read)); |
| 1741 | memset(&handler_write, 0, sizeof(handler_write)); |
| 1742 | |
| 1743 | pthread_mutex_init(&global.lock, NULL); |
| 1744 | global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals); |
| 1745 | global.uid = uid; |
| 1746 | global.gid = gid; |
| 1747 | global.multi_user = multi_user; |
| 1748 | |
| 1749 | fuse_default.global = &global; |
| 1750 | strcpy(fuse_default.source_path, source_path); |
| 1751 | snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime_default/%s", label); |
| 1752 | |
| 1753 | fuse_read.global = &global; |
| 1754 | strcpy(fuse_read.source_path, source_path); |
| 1755 | snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime_read/%s", label); |
| 1756 | |
| 1757 | fuse_write.global = &global; |
| 1758 | strcpy(fuse_write.source_path, source_path); |
| 1759 | snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime_write/%s", label); |
| 1760 | |
| 1761 | handler_default.fuse = &fuse_default; |
| 1762 | handler_read.fuse = &fuse_read; |
| 1763 | handler_write.fuse = &fuse_write; |
| 1764 | |
| 1765 | umask(0); |
| 1766 | |
| 1767 | if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006) |
| 1768 | || fuse_setup(&fuse_read, AID_EVERYBODY, 0027) |
| 1769 | || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) { |
| 1770 | ERROR("failed to fuse_setup\n"); |
| 1771 | exit(1); |
| 1772 | } |
| 1773 | |
| 1774 | /* Drop privs */ |
| 1775 | if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) { |
| 1776 | ERROR("cannot setgroups: %s\n", strerror(errno)); |
| 1777 | exit(1); |
| 1778 | } |
| 1779 | if (setgid(gid) < 0) { |
| 1780 | ERROR("cannot setgid: %s\n", strerror(errno)); |
| 1781 | exit(1); |
| 1782 | } |
| 1783 | if (setuid(uid) < 0) { |
| 1784 | ERROR("cannot setuid: %s\n", strerror(errno)); |
| 1785 | exit(1); |
| 1786 | } |
| 1787 | |
| 1788 | if (multi_user) { |
| 1789 | fs_prepare_dir(fuse_default.obb_path, 0775, uid, gid); |
| 1790 | fs_prepare_dir(fuse_read.obb_path, 0775, uid, gid); |
| 1791 | fs_prepare_dir(fuse_write.obb_path, 0775, uid, gid); |
| 1792 | } |
| 1793 | |
| 1794 | if (pthread_create(&thread_default, NULL, start_handler, &handler_default) |
| 1795 | || pthread_create(&thread_read, NULL, start_handler, &handler_read) |
| 1796 | || pthread_create(&thread_write, NULL, start_handler, &handler_write)) { |
| 1797 | ERROR("failed to pthread_create\n"); |
| 1798 | exit(1); |
| 1799 | } |
| 1800 | |
| 1801 | watch_package_list(&global); |
| 1802 | ERROR("terminated prematurely\n"); |
| 1803 | exit(1); |
| 1804 | } |
| 1805 | |
| 1806 | int main(int argc, char **argv) { |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1807 | const char *source_path = NULL; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1808 | const char *label = NULL; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1809 | uid_t uid = 0; |
| 1810 | gid_t gid = 0; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1811 | bool multi_user = false; |
| 1812 | bool full_write = false; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1813 | int i; |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 1814 | struct rlimit rlim; |
Nick Kralevich | 8d28fa7 | 2014-07-24 17:05:59 -0700 | [diff] [blame] | 1815 | int fs_version; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1816 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1817 | int opt; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1818 | while ((opt = getopt(argc, argv, "u:g:mw")) != -1) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1819 | switch (opt) { |
| 1820 | case 'u': |
| 1821 | uid = strtoul(optarg, NULL, 10); |
| 1822 | break; |
| 1823 | case 'g': |
| 1824 | gid = strtoul(optarg, NULL, 10); |
| 1825 | break; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1826 | case 'm': |
| 1827 | multi_user = true; |
| 1828 | break; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1829 | case 'w': |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1830 | full_write = true; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1831 | break; |
| 1832 | case '?': |
| 1833 | default: |
| 1834 | return usage(); |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | for (i = optind; i < argc; i++) { |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1839 | char* arg = argv[i]; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1840 | if (!source_path) { |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1841 | source_path = arg; |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1842 | } else if (!label) { |
| 1843 | label = arg; |
Jean-Baptiste Queru | e92372b | 2012-08-15 09:54:30 -0700 | [diff] [blame] | 1844 | } else { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1845 | ERROR("too many arguments\n"); |
| 1846 | return usage(); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1847 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1850 | if (!source_path) { |
| 1851 | ERROR("no source path specified\n"); |
| 1852 | return usage(); |
| 1853 | } |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1854 | if (!label) { |
| 1855 | ERROR("no label specified\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1856 | return usage(); |
| 1857 | } |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1858 | if (!uid || !gid) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1859 | ERROR("uid and gid must be nonzero\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1860 | return usage(); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 1863 | rlim.rlim_cur = 8192; |
| 1864 | rlim.rlim_max = 8192; |
| 1865 | if (setrlimit(RLIMIT_NOFILE, &rlim)) { |
| 1866 | ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno); |
| 1867 | } |
| 1868 | |
Nick Kralevich | 8d28fa7 | 2014-07-24 17:05:59 -0700 | [diff] [blame] | 1869 | while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) { |
| 1870 | ERROR("installd fs upgrade not yet complete. Waiting...\n"); |
| 1871 | sleep(1); |
| 1872 | } |
| 1873 | |
Jeff Sharkey | f38f29c | 2015-06-23 14:30:37 -0700 | [diff] [blame] | 1874 | run(source_path, label, uid, gid, multi_user, full_write); |
| 1875 | return 1; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1876 | } |