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