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