blob: 558d7f4991dd879f80a6c25f07ddb6089f891d14 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
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 Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070032#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070033#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040034#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080035#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070036#include <sys/uio.h>
37#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070038
Jeff Sharkey44d63422013-09-12 09:44:48 -070039#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070040#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070041#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070042#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070043
Brian Swetlandb14a2c62010-08-12 18:21:12 -070044#include <private/android_filesystem_config.h>
45
Brian Swetland03ee9472010-08-12 18:01:08 -070046/* README
47 *
48 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070049 *
Brian Swetland03ee9472010-08-12 18:01:08 -070050 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070051 * directory permissions (all files are given fixed owner, group, and
52 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070053 * changeable, symlinks and hardlinks are not createable, etc.
54 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070055 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070056 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070057 * It must be run as root, but will drop to requested UID/GID as soon as it
58 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070059 *
60 * Things I believe to be true:
61 *
62 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
63 * CREAT) must bump that node's refcount
64 * - don't forget that FORGET can forget multiple references (req->nlookup)
65 * - if an op that returns a fuse_entry fails writing the reply to the
66 * kernel, you must rollback the refcount to reflect the reference the
67 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070068 *
69 * This daemon can also derive custom filesystem permissions based on directory
70 * structure when requested. These custom permissions support several features:
71 *
72 * - Apps can access their own files in /Android/data/com.example/ without
73 * requiring any additional GIDs.
74 * - Separate permissions for protecting directories like Pictures and Music.
75 * - Multi-user separation on the same physical device.
76 *
77 * The derived permissions look like this:
78 *
79 * rwxrwx--x root:sdcard_rw /
80 * rwxrwx--- root:sdcard_pics /Pictures
81 * rwxrwx--- root:sdcard_av /Music
82 *
83 * rwxrwx--x root:sdcard_rw /Android
84 * rwxrwx--x root:sdcard_rw /Android/data
85 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
86 * rwxrwx--x root:sdcard_rw /Android/obb/
87 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
88 *
89 * rwxrwx--- root:sdcard_all /Android/user
90 * rwxrwx--x root:sdcard_rw /Android/user/10
91 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070092 */
93
94#define FUSE_TRACE 0
95
96#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070097#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070098#else
99#define TRACE(x...) do {} while (0)
100#endif
101
Elliott Hughes300d5642014-07-08 13:53:26 -0700102#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700103
104#define FUSE_UNKNOWN_INO 0xffffffff
105
Jeff Brown84715842012-05-25 14:07:47 -0700106/* Maximum number of bytes to write in one request. */
107#define MAX_WRITE (256 * 1024)
108
109/* Maximum number of bytes to read in one request. */
110#define MAX_READ (128 * 1024)
111
112/* Largest possible request.
113 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
114 * the largest possible data payload. */
115#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
116
Jeff Brown6249b902012-05-26 14:32:54 -0700117/* Default number of threads. */
118#define DEFAULT_NUM_THREADS 2
119
120/* Pseudo-error constant used to indicate that no fuse status is needed
121 * or that a reply has already been written. */
122#define NO_STATUS 1
123
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700124/* Path to system-provided mapping of package name to appIds */
125static const char* const kPackagesListFile = "/data/system/packages.list";
126
127/* Supplementary groups to execute with */
128static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
129
130/* Permission mode for a specific node. Controls how file permissions
131 * are derived for children nodes. */
132typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700133 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700134 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700135 /* This node is one level above a normal root; used for legacy layouts
136 * which use the first level to represent user_id. */
137 PERM_LEGACY_PRE_ROOT,
138 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700139 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700140 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700141 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700142 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700143 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700144 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700145 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700146 /* This node is "/Android/media" */
147 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700148 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700149 PERM_ANDROID_USER,
150} perm_t;
151
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700152/* Permissions structure to derive */
153typedef enum {
154 DERIVE_NONE,
155 DERIVE_LEGACY,
156 DERIVE_UNIFIED,
157} derive_t;
158
Brian Swetland03ee9472010-08-12 18:01:08 -0700159struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700160 int fd;
161};
162
163struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700164 DIR *d;
165};
166
167struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700168 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700169 __u64 nid;
170 __u64 gen;
171
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700172 /* State derived based on current position in hierarchy. */
173 perm_t perm;
174 userid_t userid;
175 uid_t uid;
176 gid_t gid;
177 mode_t mode;
178
Paul Eastham11ccdb32010-10-14 11:04:26 -0700179 struct node *next; /* per-dir sibling list */
180 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700181 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700182
Jeff Brown6249b902012-05-26 14:32:54 -0700183 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700184 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800185 /* If non-null, this is the real name of the file in the underlying storage.
186 * This may differ from the field "name" only by case.
187 * strlen(actual_name) will always equal strlen(name), so it is safe to use
188 * namelen for both fields.
189 */
190 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700191
192 /* If non-null, an exact underlying path that should be grafted into this
193 * position. Used to support things like OBB. */
194 char* graft_path;
195 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700196};
197
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700198static int str_hash(void *key) {
199 return hashmapHash(key, strlen(key));
200}
201
Jeff Sharkey44d63422013-09-12 09:44:48 -0700202/** Test if two string keys are equal ignoring case */
203static bool str_icase_equals(void *keyA, void *keyB) {
204 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700205}
206
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700207static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800208 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700209}
210
211static bool int_equals(void *keyA, void *keyB) {
212 return keyA == keyB;
213}
214
Jeff Brown7729d242012-05-25 15:35:28 -0700215/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700216struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700217 pthread_mutex_t lock;
218
Brian Swetland03ee9472010-08-12 18:01:08 -0700219 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700220 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700221 derive_t derive;
222 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700223 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700224 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700225 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700226
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700227 Hashmap* package_to_appid;
228 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700229};
230
Jeff Brown7729d242012-05-25 15:35:28 -0700231/* Private data used by a single fuse handler. */
232struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700233 struct fuse* fuse;
234 int token;
235
Jeff Brown7729d242012-05-25 15:35:28 -0700236 /* To save memory, we never use the contents of the request buffer and the read
237 * buffer at the same time. This allows us to share the underlying storage. */
238 union {
239 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800240 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700241 };
242};
Brian Swetland03ee9472010-08-12 18:01:08 -0700243
Jeff Brown6249b902012-05-26 14:32:54 -0700244static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700245{
Jeff Brown6249b902012-05-26 14:32:54 -0700246 return (void *) (uintptr_t) nid;
247}
Brian Swetland03ee9472010-08-12 18:01:08 -0700248
Jeff Brown6249b902012-05-26 14:32:54 -0700249static inline __u64 ptr_to_id(void *ptr)
250{
251 return (__u64) (uintptr_t) ptr;
252}
Brian Swetland03ee9472010-08-12 18:01:08 -0700253
Jeff Brown6249b902012-05-26 14:32:54 -0700254static void acquire_node_locked(struct node* node)
255{
256 node->refcount++;
257 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
258}
259
260static void remove_node_from_parent_locked(struct node* node);
261
262static void release_node_locked(struct node* node)
263{
264 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
265 if (node->refcount > 0) {
266 node->refcount--;
267 if (!node->refcount) {
268 TRACE("DESTROY %p (%s)\n", node, node->name);
269 remove_node_from_parent_locked(node);
270
271 /* TODO: remove debugging - poison memory */
272 memset(node->name, 0xef, node->namelen);
273 free(node->name);
274 free(node->actual_name);
275 memset(node, 0xfc, sizeof(*node));
276 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800277 }
Jeff Brown6249b902012-05-26 14:32:54 -0700278 } else {
279 ERROR("Zero refcnt %p\n", node);
280 }
281}
282
283static void add_node_to_parent_locked(struct node *node, struct node *parent) {
284 node->parent = parent;
285 node->next = parent->child;
286 parent->child = node;
287 acquire_node_locked(parent);
288}
289
290static void remove_node_from_parent_locked(struct node* node)
291{
292 if (node->parent) {
293 if (node->parent->child == node) {
294 node->parent->child = node->parent->child->next;
295 } else {
296 struct node *node2;
297 node2 = node->parent->child;
298 while (node2->next != node)
299 node2 = node2->next;
300 node2->next = node->next;
301 }
302 release_node_locked(node->parent);
303 node->parent = NULL;
304 node->next = NULL;
305 }
306}
307
308/* Gets the absolute path to a node into the provided buffer.
309 *
310 * Populates 'buf' with the path and returns the length of the path on success,
311 * or returns -1 if the path is too long for the provided buffer.
312 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700313static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
314 const char* name;
315 size_t namelen;
316 if (node->graft_path) {
317 name = node->graft_path;
318 namelen = node->graft_pathlen;
319 } else if (node->actual_name) {
320 name = node->actual_name;
321 namelen = node->namelen;
322 } else {
323 name = node->name;
324 namelen = node->namelen;
325 }
326
Jeff Brown6249b902012-05-26 14:32:54 -0700327 if (bufsize < namelen + 1) {
328 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700329 }
330
Jeff Brown6249b902012-05-26 14:32:54 -0700331 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700332 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700333 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
334 if (pathlen < 0) {
335 return -1;
336 }
337 buf[pathlen++] = '/';
338 }
339
Jeff Brown6249b902012-05-26 14:32:54 -0700340 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
341 return pathlen + namelen;
342}
343
344/* Finds the absolute path of a file within a given directory.
345 * Performs a case-insensitive search for the file and sets the buffer to the path
346 * of the first matching file. If 'search' is zero or if no match is found, sets
347 * the buffer to the path that the file would have, assuming the name were case-sensitive.
348 *
349 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
350 * or returns NULL if the path is too long for the provided buffer.
351 */
352static char* find_file_within(const char* path, const char* name,
353 char* buf, size_t bufsize, int search)
354{
355 size_t pathlen = strlen(path);
356 size_t namelen = strlen(name);
357 size_t childlen = pathlen + namelen + 1;
358 char* actual;
359
360 if (bufsize <= childlen) {
361 return NULL;
362 }
363
364 memcpy(buf, path, pathlen);
365 buf[pathlen] = '/';
366 actual = buf + pathlen + 1;
367 memcpy(actual, name, namelen + 1);
368
369 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800370 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700371 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800372 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700373 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700374 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800375 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800376 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700377 if (!strcasecmp(entry->d_name, name)) {
378 /* we have a match - replace the name, don't need to copy the null again */
379 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800380 break;
381 }
382 }
383 closedir(dir);
384 }
Jeff Brown6249b902012-05-26 14:32:54 -0700385 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800386}
387
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700388static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800389{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700390 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700391 attr->size = s->st_size;
392 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400393 attr->atime = s->st_atime;
394 attr->mtime = s->st_mtime;
395 attr->ctime = s->st_ctime;
396 attr->atimensec = s->st_atime_nsec;
397 attr->mtimensec = s->st_mtime_nsec;
398 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700399 attr->mode = s->st_mode;
400 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700401
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700402 attr->uid = node->uid;
403 attr->gid = node->gid;
404
405 /* Filter requested mode based on underlying file, and
406 * pass through file type. */
407 int owner_mode = s->st_mode & 0700;
408 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
409 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
410}
411
Jeff Sharkey44d63422013-09-12 09:44:48 -0700412static int touch(char* path, mode_t mode) {
413 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
414 if (fd == -1) {
415 if (errno == EEXIST) {
416 return 0;
417 } else {
418 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
419 return -1;
420 }
421 }
422 close(fd);
423 return 0;
424}
425
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700426static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
427 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700428 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700429
430 /* By default, each node inherits from its parent */
431 node->perm = PERM_INHERIT;
432 node->userid = parent->userid;
433 node->uid = parent->uid;
434 node->gid = parent->gid;
435 node->mode = parent->mode;
436
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700437 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700438 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700439 }
440
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700441 /* Derive custom permissions based on parent and current node */
442 switch (parent->perm) {
443 case PERM_INHERIT:
444 /* Already inherited above */
445 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700446 case PERM_LEGACY_PRE_ROOT:
447 /* Legacy internal layout places users at top level */
448 node->perm = PERM_ROOT;
449 node->userid = strtoul(node->name, NULL, 10);
450 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700451 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700452 /* Assume masked off by default. */
453 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700454 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700455 /* App-specific directories inside; let anyone traverse */
456 node->perm = PERM_ANDROID;
457 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700458 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700459 if (!strcasecmp(node->name, "DCIM")
460 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700461 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700462 } else if (!strcasecmp(node->name, "Alarms")
463 || !strcasecmp(node->name, "Movies")
464 || !strcasecmp(node->name, "Music")
465 || !strcasecmp(node->name, "Notifications")
466 || !strcasecmp(node->name, "Podcasts")
467 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700468 node->gid = AID_SDCARD_AV;
469 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700470 }
471 break;
472 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700473 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700474 /* App-specific directories inside; let anyone traverse */
475 node->perm = PERM_ANDROID_DATA;
476 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700477 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700478 /* App-specific directories inside; let anyone traverse */
479 node->perm = PERM_ANDROID_OBB;
480 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700481 /* Single OBB directory is always shared */
482 node->graft_path = fuse->obbpath;
483 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700484 } else if (!strcasecmp(node->name, "media")) {
485 /* App-specific directories inside; let anyone traverse */
486 node->perm = PERM_ANDROID_MEDIA;
487 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700488 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700489 /* User directories must only be accessible to system, protected
490 * by sdcard_all. Zygote will bind mount the appropriate user-
491 * specific path. */
492 node->perm = PERM_ANDROID_USER;
493 node->gid = AID_SDCARD_ALL;
494 node->mode = 0770;
495 }
496 break;
497 case PERM_ANDROID_DATA:
498 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700499 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800500 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700501 if (appid != 0) {
502 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700503 }
504 node->mode = 0770;
505 break;
506 case PERM_ANDROID_USER:
507 /* Root of a secondary user */
508 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700509 node->userid = strtoul(node->name, NULL, 10);
510 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700511 node->mode = 0771;
512 break;
513 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700514}
515
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700516/* Return if the calling UID holds sdcard_rw. */
517static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700518 /* No additional permissions enforcement */
519 if (fuse->derive == DERIVE_NONE) {
520 return true;
521 }
522
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700523 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800524 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700525}
526
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700527/* Kernel has already enforced everything we returned through
528 * derive_permissions_locked(), so this is used to lock down access
529 * even further, such as enforcing that apps hold sdcard_rw. */
530static bool check_caller_access_to_name(struct fuse* fuse,
531 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700532 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700533 /* Always block security-sensitive files at root */
534 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700535 if (!strcasecmp(name, "autorun.inf")
536 || !strcasecmp(name, ".android_secure")
537 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700538 return false;
539 }
540 }
541
542 /* No additional permissions enforcement */
543 if (fuse->derive == DERIVE_NONE) {
544 return true;
545 }
546
Jeff Sharkey44d63422013-09-12 09:44:48 -0700547 /* Root always has access; access for any other UIDs should always
548 * be controlled through packages.list. */
549 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700550 return true;
551 }
552
553 /* If asking to write, verify that caller either owns the
554 * parent or holds sdcard_rw. */
555 if (mode & W_OK) {
556 if (parent_node && hdr->uid == parent_node->uid) {
557 return true;
558 }
559
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700560 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700561 }
562
563 /* No extra permissions to enforce */
564 return true;
565}
566
567static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700568 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
569 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700570}
571
Jeff Brown6249b902012-05-26 14:32:54 -0700572struct node *create_node_locked(struct fuse* fuse,
573 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700574{
575 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700576 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700577
Paul Eastham11ccdb32010-10-14 11:04:26 -0700578 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700579 if (!node) {
580 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700581 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700582 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700583 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700584 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700585 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700586 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700587 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700588 if (strcmp(name, actual_name)) {
589 node->actual_name = malloc(namelen + 1);
590 if (!node->actual_name) {
591 free(node->name);
592 free(node);
593 return NULL;
594 }
595 memcpy(node->actual_name, actual_name, namelen + 1);
596 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700597 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700598 node->nid = ptr_to_id(node);
599 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700600
601 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700602 acquire_node_locked(node);
603 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700604 return node;
605}
606
Jeff Brown6249b902012-05-26 14:32:54 -0700607static int rename_node_locked(struct node *node, const char *name,
608 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700609{
Jeff Brown6249b902012-05-26 14:32:54 -0700610 size_t namelen = strlen(name);
611 int need_actual_name = strcmp(name, actual_name);
612
613 /* make the storage bigger without actually changing the name
614 * in case an error occurs part way */
615 if (namelen > node->namelen) {
616 char* new_name = realloc(node->name, namelen + 1);
617 if (!new_name) {
618 return -ENOMEM;
619 }
620 node->name = new_name;
621 if (need_actual_name && node->actual_name) {
622 char* new_actual_name = realloc(node->actual_name, namelen + 1);
623 if (!new_actual_name) {
624 return -ENOMEM;
625 }
626 node->actual_name = new_actual_name;
627 }
628 }
629
630 /* update the name, taking care to allocate storage before overwriting the old name */
631 if (need_actual_name) {
632 if (!node->actual_name) {
633 node->actual_name = malloc(namelen + 1);
634 if (!node->actual_name) {
635 return -ENOMEM;
636 }
637 }
638 memcpy(node->actual_name, actual_name, namelen + 1);
639 } else {
640 free(node->actual_name);
641 node->actual_name = NULL;
642 }
643 memcpy(node->name, name, namelen + 1);
644 node->namelen = namelen;
645 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700646}
647
Jeff Brown6249b902012-05-26 14:32:54 -0700648static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700649{
Jeff Brown6249b902012-05-26 14:32:54 -0700650 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700651 return &fuse->root;
652 } else {
653 return id_to_ptr(nid);
654 }
655}
656
Jeff Brown6249b902012-05-26 14:32:54 -0700657static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
658 char* buf, size_t bufsize)
659{
660 struct node* node = lookup_node_by_id_locked(fuse, nid);
661 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
662 node = NULL;
663 }
664 return node;
665}
666
667static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700668{
669 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700670 /* use exact string comparison, nodes that differ by case
671 * must be considered distinct even if they refer to the same
672 * underlying file as otherwise operations such as "mv x x"
673 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700674 if (!strcmp(name, node->name)) {
675 return node;
676 }
677 }
678 return 0;
679}
680
Jeff Brown6249b902012-05-26 14:32:54 -0700681static struct node* acquire_or_create_child_locked(
682 struct fuse* fuse, struct node* parent,
683 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700684{
Jeff Brown6249b902012-05-26 14:32:54 -0700685 struct node* child = lookup_child_by_name_locked(parent, name);
686 if (child) {
687 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800688 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700689 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800690 }
Jeff Brown6249b902012-05-26 14:32:54 -0700691 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700692}
693
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700694static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700695 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700696 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700697
Jeff Brown6249b902012-05-26 14:32:54 -0700698 fuse->fd = fd;
699 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700700 fuse->derive = derive;
701 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700702 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700703
Jeff Brown6249b902012-05-26 14:32:54 -0700704 memset(&fuse->root, 0, sizeof(fuse->root));
705 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
706 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700707 fuse->root.namelen = strlen(source_path);
708 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700709 fuse->root.userid = 0;
710 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700711
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700712 /* Set up root node for various modes of operation */
713 switch (derive) {
714 case DERIVE_NONE:
715 /* Traditional behavior that treats entire device as being accessible
716 * to sdcard_rw, and no permissions are derived. */
717 fuse->root.perm = PERM_ROOT;
718 fuse->root.mode = 0775;
719 fuse->root.gid = AID_SDCARD_RW;
720 break;
721 case DERIVE_LEGACY:
722 /* Legacy behavior used to support internal multiuser layout which
723 * places user_id at the top directory level, with the actual roots
724 * just below that. Shared OBB path is also at top level. */
725 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
726 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700727 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700728 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700729 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
730 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700731 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700732 break;
733 case DERIVE_UNIFIED:
734 /* Unified multiuser layout which places secondary user_id under
735 * /Android/user and shared OBB path under /Android/obb. */
736 fuse->root.perm = PERM_ROOT;
737 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700738 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700739 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700740 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
741 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
742 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700743 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700744}
745
Jeff Brown6249b902012-05-26 14:32:54 -0700746static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700747{
748 struct fuse_out_header hdr;
749 hdr.len = sizeof(hdr);
750 hdr.error = err;
751 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700752 write(fuse->fd, &hdr, sizeof(hdr));
753}
754
Jeff Brown6249b902012-05-26 14:32:54 -0700755static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700756{
757 struct fuse_out_header hdr;
758 struct iovec vec[2];
759 int res;
760
761 hdr.len = len + sizeof(hdr);
762 hdr.error = 0;
763 hdr.unique = unique;
764
765 vec[0].iov_base = &hdr;
766 vec[0].iov_len = sizeof(hdr);
767 vec[1].iov_base = data;
768 vec[1].iov_len = len;
769
770 res = writev(fuse->fd, vec, 2);
771 if (res < 0) {
772 ERROR("*** REPLY FAILED *** %d\n", errno);
773 }
774}
775
Jeff Brown6249b902012-05-26 14:32:54 -0700776static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
777 struct node* parent, const char* name, const char* actual_name,
778 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700779{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700780 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700781 struct fuse_entry_out out;
782 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700783
Jeff Brown6249b902012-05-26 14:32:54 -0700784 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700785 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700786 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700787
Jeff Brown6249b902012-05-26 14:32:54 -0700788 pthread_mutex_lock(&fuse->lock);
789 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
790 if (!node) {
791 pthread_mutex_unlock(&fuse->lock);
792 return -ENOMEM;
793 }
794 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700795 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700796 out.attr_valid = 10;
797 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700798 out.nodeid = node->nid;
799 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700800 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700801 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700802 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700803}
804
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700805static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700806 const char* path)
807{
808 struct fuse_attr_out out;
809 struct stat s;
810
811 if (lstat(path, &s) < 0) {
812 return -errno;
813 }
814 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700815 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700816 out.attr_valid = 10;
817 fuse_reply(fuse, unique, &out, sizeof(out));
818 return NO_STATUS;
819}
820
821static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700822 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700823{
Jeff Brown6249b902012-05-26 14:32:54 -0700824 struct node* parent_node;
825 char parent_path[PATH_MAX];
826 char child_path[PATH_MAX];
827 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700828
Jeff Brown6249b902012-05-26 14:32:54 -0700829 pthread_mutex_lock(&fuse->lock);
830 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
831 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100832 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700833 parent_node ? parent_node->name : "?");
834 pthread_mutex_unlock(&fuse->lock);
835
836 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
837 child_path, sizeof(child_path), 1))) {
838 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700839 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700840 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700841 return -EACCES;
842 }
843
Jeff Brown6249b902012-05-26 14:32:54 -0700844 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700845}
846
Jeff Brown6249b902012-05-26 14:32:54 -0700847static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700848 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
849{
Jeff Brown6249b902012-05-26 14:32:54 -0700850 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700851
Jeff Brown6249b902012-05-26 14:32:54 -0700852 pthread_mutex_lock(&fuse->lock);
853 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100854 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700855 hdr->nodeid, node ? node->name : "?");
856 if (node) {
857 __u64 n = req->nlookup;
858 while (n--) {
859 release_node_locked(node);
860 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861 }
Jeff Brown6249b902012-05-26 14:32:54 -0700862 pthread_mutex_unlock(&fuse->lock);
863 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700864}
865
Jeff Brown6249b902012-05-26 14:32:54 -0700866static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700867 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
868{
Jeff Brown6249b902012-05-26 14:32:54 -0700869 struct node* node;
870 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700871
Jeff Brown6249b902012-05-26 14:32:54 -0700872 pthread_mutex_lock(&fuse->lock);
873 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100874 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700875 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
876 pthread_mutex_unlock(&fuse->lock);
877
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700878 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700879 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700881 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700882 return -EACCES;
883 }
884
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700885 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886}
887
Jeff Brown6249b902012-05-26 14:32:54 -0700888static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700889 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
890{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700891 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700892 struct node* node;
893 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700895
Jeff Brown6249b902012-05-26 14:32:54 -0700896 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700897 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700898 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100899 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700900 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
901 pthread_mutex_unlock(&fuse->lock);
902
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700903 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700904 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700906 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700907 return -EACCES;
908 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700909
Jeff Brown6249b902012-05-26 14:32:54 -0700910 /* XXX: incomplete implementation on purpose.
911 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700912
Jeff Brown6249b902012-05-26 14:32:54 -0700913 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
914 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700915 }
916
917 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
918 * are both set, then set it to the current time. Else, set it to the
919 * time specified in the request. Same goes for mtime. Use utimensat(2)
920 * as it allows ATIME and MTIME to be changed independently, and has
921 * nanosecond resolution which fuse also has.
922 */
923 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
924 times[0].tv_nsec = UTIME_OMIT;
925 times[1].tv_nsec = UTIME_OMIT;
926 if (req->valid & FATTR_ATIME) {
927 if (req->valid & FATTR_ATIME_NOW) {
928 times[0].tv_nsec = UTIME_NOW;
929 } else {
930 times[0].tv_sec = req->atime;
931 times[0].tv_nsec = req->atimensec;
932 }
933 }
934 if (req->valid & FATTR_MTIME) {
935 if (req->valid & FATTR_MTIME_NOW) {
936 times[1].tv_nsec = UTIME_NOW;
937 } else {
938 times[1].tv_sec = req->mtime;
939 times[1].tv_nsec = req->mtimensec;
940 }
941 }
Jeff Brown6249b902012-05-26 14:32:54 -0700942 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
943 handler->token, path, times[0].tv_sec, times[1].tv_sec);
944 if (utimensat(-1, path, times, 0) < 0) {
945 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700946 }
947 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700948 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700949}
950
Jeff Brown6249b902012-05-26 14:32:54 -0700951static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
953{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700954 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700955 struct node* parent_node;
956 char parent_path[PATH_MAX];
957 char child_path[PATH_MAX];
958 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700959
Jeff Brown6249b902012-05-26 14:32:54 -0700960 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700961 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700962 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
963 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100964 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700965 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
966 pthread_mutex_unlock(&fuse->lock);
967
968 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
969 child_path, sizeof(child_path), 1))) {
970 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700971 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700972 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700973 return -EACCES;
974 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700975 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700976 if (mknod(child_path, mode, req->rdev) < 0) {
977 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700978 }
Jeff Brown6249b902012-05-26 14:32:54 -0700979 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980}
981
Jeff Brown6249b902012-05-26 14:32:54 -0700982static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700983 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
984{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700985 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700986 struct node* parent_node;
987 char parent_path[PATH_MAX];
988 char child_path[PATH_MAX];
989 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700990
Jeff Brown6249b902012-05-26 14:32:54 -0700991 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700992 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700993 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
994 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100995 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700996 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
997 pthread_mutex_unlock(&fuse->lock);
998
999 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1000 child_path, sizeof(child_path), 1))) {
1001 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001002 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001003 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001004 return -EACCES;
1005 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001006 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001007 if (mkdir(child_path, mode) < 0) {
1008 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001009 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001010
1011 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1012 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1013 char nomedia[PATH_MAX];
1014 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1015 if (touch(nomedia, 0664) != 0) {
1016 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1017 return -ENOENT;
1018 }
1019 }
1020 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1021 char nomedia[PATH_MAX];
1022 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1023 if (touch(nomedia, 0664) != 0) {
1024 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1025 return -ENOENT;
1026 }
1027 }
1028
Jeff Brown6249b902012-05-26 14:32:54 -07001029 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001030}
1031
Jeff Brown6249b902012-05-26 14:32:54 -07001032static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001033 const struct fuse_in_header* hdr, const char* name)
1034{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001035 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001036 struct node* parent_node;
1037 char parent_path[PATH_MAX];
1038 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001039
Jeff Brown6249b902012-05-26 14:32:54 -07001040 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001041 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001042 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1043 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001044 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001045 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1046 pthread_mutex_unlock(&fuse->lock);
1047
1048 if (!parent_node || !find_file_within(parent_path, name,
1049 child_path, sizeof(child_path), 1)) {
1050 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001051 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001052 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001053 return -EACCES;
1054 }
Jeff Brown6249b902012-05-26 14:32:54 -07001055 if (unlink(child_path) < 0) {
1056 return -errno;
1057 }
1058 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001059}
1060
Jeff Brown6249b902012-05-26 14:32:54 -07001061static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001062 const struct fuse_in_header* hdr, const char* name)
1063{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001064 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001065 struct node* parent_node;
1066 char parent_path[PATH_MAX];
1067 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068
Jeff Brown6249b902012-05-26 14:32:54 -07001069 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001070 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001071 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1072 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001073 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001074 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1075 pthread_mutex_unlock(&fuse->lock);
1076
1077 if (!parent_node || !find_file_within(parent_path, name,
1078 child_path, sizeof(child_path), 1)) {
1079 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001080 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001081 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001082 return -EACCES;
1083 }
Jeff Brown6249b902012-05-26 14:32:54 -07001084 if (rmdir(child_path) < 0) {
1085 return -errno;
1086 }
1087 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001088}
1089
Jeff Brown6249b902012-05-26 14:32:54 -07001090static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001091 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001092 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001093{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001094 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001095 struct node* old_parent_node;
1096 struct node* new_parent_node;
1097 struct node* child_node;
1098 char old_parent_path[PATH_MAX];
1099 char new_parent_path[PATH_MAX];
1100 char old_child_path[PATH_MAX];
1101 char new_child_path[PATH_MAX];
1102 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001103 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001104
Jeff Brown6249b902012-05-26 14:32:54 -07001105 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001106 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001107 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 Oaklande43b99a2014-07-23 13:04:59 +01001111 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001112 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 Sharkeyaa04e812013-08-30 10:26:15 -07001119 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001120 res = -EACCES;
1121 goto lookup_error;
1122 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001123 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001124 res = -EACCES;
1125 goto lookup_error;
1126 }
Jeff Brown6249b902012-05-26 14:32:54 -07001127 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);
1134 pthread_mutex_unlock(&fuse->lock);
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 Brownfc1e1a02012-05-25 17:24:17 -07001146 }
1147
Jeff Brown6249b902012-05-26 14:32:54 -07001148 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 Brownfc1e1a02012-05-25 17:24:17 -07001153 }
1154
Jeff Brown6249b902012-05-26 14:32:54 -07001155 pthread_mutex_lock(&fuse->lock);
1156 res = rename_node_locked(child_node, new_name, new_actual_name);
1157 if (!res) {
1158 remove_node_from_parent_locked(child_node);
1159 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160 }
Jeff Brown6249b902012-05-26 14:32:54 -07001161 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001162
Jeff Brown6249b902012-05-26 14:32:54 -07001163io_error:
1164 pthread_mutex_lock(&fuse->lock);
1165done:
1166 release_node_locked(child_node);
1167lookup_error:
1168 pthread_mutex_unlock(&fuse->lock);
1169 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001170}
1171
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001172static int open_flags_to_access_mode(int open_flags) {
1173 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1174 return R_OK;
1175 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1176 return W_OK;
1177 } else {
1178 /* Probably O_RDRW, but treat as default to be safe */
1179 return R_OK | W_OK;
1180 }
1181}
1182
Jeff Brown6249b902012-05-26 14:32:54 -07001183static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001184 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1185{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001186 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001187 struct node* node;
1188 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001191
Jeff Brown6249b902012-05-26 14:32:54 -07001192 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001193 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001194 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001195 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001196 req->flags, hdr->nodeid, node ? node->name : "?");
1197 pthread_mutex_unlock(&fuse->lock);
1198
1199 if (!node) {
1200 return -ENOENT;
1201 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001202 if (!check_caller_access_to_node(fuse, hdr, node,
1203 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001204 return -EACCES;
1205 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001206 h = malloc(sizeof(*h));
1207 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001208 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209 }
Jeff Brown6249b902012-05-26 14:32:54 -07001210 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211 h->fd = open(path, req->flags);
1212 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001213 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001214 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001215 }
1216 out.fh = ptr_to_id(h);
1217 out.open_flags = 0;
1218 out.padding = 0;
1219 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001220 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001221}
1222
Jeff Brown6249b902012-05-26 14:32:54 -07001223static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1225{
1226 struct handle *h = id_to_ptr(req->fh);
1227 __u64 unique = hdr->unique;
1228 __u32 size = req->size;
1229 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001230 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001231 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001232
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001233 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1234 * overlaps the request buffer and will clobber data in the request. This
1235 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001236
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001237 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1238 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001239 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001240 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001241 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001242 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001243 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001244 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001246 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001247 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248}
1249
Jeff Brown6249b902012-05-26 14:32:54 -07001250static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001251 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1252 const void* buffer)
1253{
1254 struct fuse_write_out out;
1255 struct handle *h = id_to_ptr(req->fh);
1256 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001257 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001258
Arpad Horvath49e93442014-02-18 10:18:25 +01001259 if (req->flags & O_DIRECT) {
1260 memcpy(aligned_buffer, buffer, req->size);
1261 buffer = (const __u8*) aligned_buffer;
1262 }
Jeff Brown6249b902012-05-26 14:32:54 -07001263
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001264 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001265 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266 res = pwrite64(h->fd, buffer, req->size, req->offset);
1267 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001268 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 }
1270 out.size = res;
1271 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001272 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001273}
1274
Jeff Brown6249b902012-05-26 14:32:54 -07001275static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001276 const struct fuse_in_header* hdr)
1277{
Jeff Brown6249b902012-05-26 14:32:54 -07001278 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001279 struct statfs stat;
1280 struct fuse_statfs_out out;
1281 int res;
1282
Jeff Brown6249b902012-05-26 14:32:54 -07001283 pthread_mutex_lock(&fuse->lock);
1284 TRACE("[%d] STATFS\n", handler->token);
1285 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1286 pthread_mutex_unlock(&fuse->lock);
1287 if (res < 0) {
1288 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001289 }
Jeff Brown6249b902012-05-26 14:32:54 -07001290 if (statfs(fuse->root.name, &stat) < 0) {
1291 return -errno;
1292 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001293 memset(&out, 0, sizeof(out));
1294 out.st.blocks = stat.f_blocks;
1295 out.st.bfree = stat.f_bfree;
1296 out.st.bavail = stat.f_bavail;
1297 out.st.files = stat.f_files;
1298 out.st.ffree = stat.f_ffree;
1299 out.st.bsize = stat.f_bsize;
1300 out.st.namelen = stat.f_namelen;
1301 out.st.frsize = stat.f_frsize;
1302 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001303 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001304}
1305
Jeff Brown6249b902012-05-26 14:32:54 -07001306static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001307 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1308{
1309 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001310
1311 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001312 close(h->fd);
1313 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001314 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001315}
1316
Jeff Brown6249b902012-05-26 14:32:54 -07001317static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001318 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1319{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001320 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1321 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001322
Elliott Hughesf6d67372014-07-08 14:38:26 -07001323 int fd = -1;
1324 if (is_dir) {
1325 struct dirhandle *dh = id_to_ptr(req->fh);
1326 fd = dirfd(dh->d);
1327 } else {
1328 struct handle *h = id_to_ptr(req->fh);
1329 fd = h->fd;
1330 }
1331
1332 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1333 is_dir ? "FSYNCDIR" : "FSYNC",
1334 id_to_ptr(req->fh), fd, is_data_sync);
1335 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1336 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001337 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001338 }
Jeff Brown6249b902012-05-26 14:32:54 -07001339 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001340}
1341
Jeff Brown6249b902012-05-26 14:32:54 -07001342static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001343 const struct fuse_in_header* hdr)
1344{
Jeff Brown6249b902012-05-26 14:32:54 -07001345 TRACE("[%d] FLUSH\n", handler->token);
1346 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001347}
1348
Jeff Brown6249b902012-05-26 14:32:54 -07001349static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001350 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1351{
Jeff Brown6249b902012-05-26 14:32:54 -07001352 struct node* node;
1353 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001356
Jeff Brown6249b902012-05-26 14:32:54 -07001357 pthread_mutex_lock(&fuse->lock);
1358 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001359 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001360 hdr->nodeid, node ? node->name : "?");
1361 pthread_mutex_unlock(&fuse->lock);
1362
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001363 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001364 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001365 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001366 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001367 return -EACCES;
1368 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369 h = malloc(sizeof(*h));
1370 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001371 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001372 }
Jeff Brown6249b902012-05-26 14:32:54 -07001373 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001374 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001375 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001377 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378 }
1379 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001380 out.open_flags = 0;
1381 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001382 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001383 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001384}
1385
Jeff Brown6249b902012-05-26 14:32:54 -07001386static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001387 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1388{
1389 char buffer[8192];
1390 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1391 struct dirent *de;
1392 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001393
1394 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001395 if (req->offset == 0) {
1396 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001397 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001398 rewinddir(h->d);
1399 }
1400 de = readdir(h->d);
1401 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001402 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001403 }
1404 fde->ino = FUSE_UNKNOWN_INO;
1405 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1406 fde->off = req->offset + 1;
1407 fde->type = de->d_type;
1408 fde->namelen = strlen(de->d_name);
1409 memcpy(fde->name, de->d_name, fde->namelen + 1);
1410 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001411 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1412 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001413}
1414
Jeff Brown6249b902012-05-26 14:32:54 -07001415static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001416 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1417{
1418 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001419
1420 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001421 closedir(h->d);
1422 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001423 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001424}
1425
Jeff Brown6249b902012-05-26 14:32:54 -07001426static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001427 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1428{
1429 struct fuse_init_out out;
1430
Jeff Brown6249b902012-05-26 14:32:54 -07001431 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1432 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001433 out.major = FUSE_KERNEL_VERSION;
1434 out.minor = FUSE_KERNEL_MINOR_VERSION;
1435 out.max_readahead = req->max_readahead;
1436 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1437 out.max_background = 32;
1438 out.congestion_threshold = 32;
1439 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001440 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001441 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001442}
1443
Jeff Brown6249b902012-05-26 14:32:54 -07001444static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001445 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1446{
Brian Swetland03ee9472010-08-12 18:01:08 -07001447 switch (hdr->opcode) {
1448 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001449 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001450 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001451 }
Jeff Brown84715842012-05-25 14:07:47 -07001452
Brian Swetland03ee9472010-08-12 18:01:08 -07001453 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001454 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001455 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001456 }
Jeff Brown84715842012-05-25 14:07:47 -07001457
Brian Swetland03ee9472010-08-12 18:01:08 -07001458 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001459 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001460 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001461 }
Jeff Brown84715842012-05-25 14:07:47 -07001462
Brian Swetland03ee9472010-08-12 18:01:08 -07001463 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001464 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001465 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001466 }
Jeff Brown84715842012-05-25 14:07:47 -07001467
Brian Swetland03ee9472010-08-12 18:01:08 -07001468// case FUSE_READLINK:
1469// case FUSE_SYMLINK:
1470 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001471 const struct fuse_mknod_in *req = data;
1472 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001473 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001474 }
Jeff Brown84715842012-05-25 14:07:47 -07001475
Brian Swetland03ee9472010-08-12 18:01:08 -07001476 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001477 const struct fuse_mkdir_in *req = data;
1478 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001479 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 }
Jeff Brown84715842012-05-25 14:07:47 -07001481
Brian Swetland03ee9472010-08-12 18:01:08 -07001482 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001483 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001484 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001485 }
Jeff Brown84715842012-05-25 14:07:47 -07001486
Brian Swetland03ee9472010-08-12 18:01:08 -07001487 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001488 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001489 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001490 }
Jeff Brown84715842012-05-25 14:07:47 -07001491
Brian Swetland03ee9472010-08-12 18:01:08 -07001492 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001493 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001494 const char *old_name = ((const char*) data) + sizeof(*req);
1495 const char *new_name = old_name + strlen(old_name) + 1;
1496 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001497 }
Jeff Brown84715842012-05-25 14:07:47 -07001498
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001499// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001500 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001501 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001502 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001503 }
Jeff Brown84715842012-05-25 14:07:47 -07001504
Brian Swetland03ee9472010-08-12 18:01:08 -07001505 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001506 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001507 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001508 }
Jeff Brown84715842012-05-25 14:07:47 -07001509
Brian Swetland03ee9472010-08-12 18:01:08 -07001510 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001511 const struct fuse_write_in *req = data;
1512 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001513 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001514 }
Jeff Brown84715842012-05-25 14:07:47 -07001515
Mike Lockwood4553b082010-08-16 14:14:44 -04001516 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001517 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001518 }
Jeff Brown84715842012-05-25 14:07:47 -07001519
Brian Swetland03ee9472010-08-12 18:01:08 -07001520 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001521 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001522 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 }
Jeff Brown84715842012-05-25 14:07:47 -07001524
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001525 case FUSE_FSYNC:
1526 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001527 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001528 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001529 }
1530
Brian Swetland03ee9472010-08-12 18:01:08 -07001531// case FUSE_SETXATTR:
1532// case FUSE_GETXATTR:
1533// case FUSE_LISTXATTR:
1534// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001535 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001536 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001537 }
1538
Brian Swetland03ee9472010-08-12 18:01:08 -07001539 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001540 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001541 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001542 }
Jeff Brown84715842012-05-25 14:07:47 -07001543
Brian Swetland03ee9472010-08-12 18:01:08 -07001544 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001545 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001546 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001547 }
Jeff Brown84715842012-05-25 14:07:47 -07001548
Brian Swetland03ee9472010-08-12 18:01:08 -07001549 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001550 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001551 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001552 }
Jeff Brown84715842012-05-25 14:07:47 -07001553
Brian Swetland03ee9472010-08-12 18:01:08 -07001554 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001555 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001556 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001557 }
Jeff Brown84715842012-05-25 14:07:47 -07001558
Brian Swetland03ee9472010-08-12 18:01:08 -07001559 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001560 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001561 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1562 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001563 }
Jeff Brown84715842012-05-25 14:07:47 -07001564 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001565}
1566
Jeff Brown6249b902012-05-26 14:32:54 -07001567static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001568{
Jeff Brown6249b902012-05-26 14:32:54 -07001569 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001571 ssize_t len = read(fuse->fd,
1572 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001573 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001574 if (errno != EINTR) {
1575 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1576 }
1577 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001578 }
Jeff Brown84715842012-05-25 14:07:47 -07001579
1580 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001581 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1582 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001583 }
1584
Jeff Brown7729d242012-05-25 15:35:28 -07001585 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001586 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001587 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1588 handler->token, (size_t)len, hdr->len);
1589 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001590 }
1591
Jeff Brown7729d242012-05-25 15:35:28 -07001592 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001593 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001594 __u64 unique = hdr->unique;
1595 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001596
1597 /* We do not access the request again after this point because the underlying
1598 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001599
1600 if (res != NO_STATUS) {
1601 if (res) {
1602 TRACE("[%d] ERROR %d\n", handler->token, res);
1603 }
1604 fuse_status(fuse, unique, res);
1605 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001606 }
1607}
1608
Jeff Brown6249b902012-05-26 14:32:54 -07001609static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001610{
Jeff Brown6249b902012-05-26 14:32:54 -07001611 struct fuse_handler* handler = data;
1612 handle_fuse_requests(handler);
1613 return NULL;
1614}
1615
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001616static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001617 Hashmap* map = context;
1618 hashmapRemove(map, key);
1619 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001620 return true;
1621}
1622
1623static bool remove_int_to_null(void *key, void *value, void *context) {
1624 Hashmap* map = context;
1625 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001626 return true;
1627}
1628
1629static int read_package_list(struct fuse *fuse) {
1630 pthread_mutex_lock(&fuse->lock);
1631
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001632 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1633 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001634
1635 FILE* file = fopen(kPackagesListFile, "r");
1636 if (!file) {
1637 ERROR("failed to open package list: %s\n", strerror(errno));
1638 pthread_mutex_unlock(&fuse->lock);
1639 return -1;
1640 }
1641
1642 char buf[512];
1643 while (fgets(buf, sizeof(buf), file) != NULL) {
1644 char package_name[512];
1645 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001646 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001647
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001648 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1649 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001650 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001651
1652 char* token = strtok(gids, ",");
1653 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001654 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001655 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001656 break;
1657 }
1658 token = strtok(NULL, ",");
1659 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001660 }
1661 }
1662
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001663 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001664 hashmapSize(fuse->package_to_appid),
1665 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001666 fclose(file);
1667 pthread_mutex_unlock(&fuse->lock);
1668 return 0;
1669}
1670
1671static void watch_package_list(struct fuse* fuse) {
1672 struct inotify_event *event;
1673 char event_buf[512];
1674
1675 int nfd = inotify_init();
1676 if (nfd < 0) {
1677 ERROR("inotify_init failed: %s\n", strerror(errno));
1678 return;
1679 }
1680
1681 bool active = false;
1682 while (1) {
1683 if (!active) {
1684 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1685 if (res == -1) {
1686 if (errno == ENOENT || errno == EACCES) {
1687 /* Framework may not have created yet, sleep and retry */
1688 ERROR("missing packages.list; retrying\n");
1689 sleep(3);
1690 continue;
1691 } else {
1692 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1693 return;
1694 }
1695 }
1696
1697 /* Watch above will tell us about any future changes, so
1698 * read the current state. */
1699 if (read_package_list(fuse) == -1) {
1700 ERROR("read_package_list failed: %s\n", strerror(errno));
1701 return;
1702 }
1703 active = true;
1704 }
1705
1706 int event_pos = 0;
1707 int res = read(nfd, event_buf, sizeof(event_buf));
1708 if (res < (int) sizeof(*event)) {
1709 if (errno == EINTR)
1710 continue;
1711 ERROR("failed to read inotify event: %s\n", strerror(errno));
1712 return;
1713 }
1714
1715 while (res >= (int) sizeof(*event)) {
1716 int event_size;
1717 event = (struct inotify_event *) (event_buf + event_pos);
1718
1719 TRACE("inotify event: %08x\n", event->mask);
1720 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1721 /* Previously watched file was deleted, probably due to move
1722 * that swapped in new data; re-arm the watch and read. */
1723 active = false;
1724 }
1725
1726 event_size = sizeof(*event) + event->len;
1727 res -= event_size;
1728 event_pos += event_size;
1729 }
1730 }
1731}
1732
Jeff Brown6249b902012-05-26 14:32:54 -07001733static int ignite_fuse(struct fuse* fuse, int num_threads)
1734{
1735 struct fuse_handler* handlers;
1736 int i;
1737
1738 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1739 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001740 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001741 return -ENOMEM;
1742 }
1743
1744 for (i = 0; i < num_threads; i++) {
1745 handlers[i].fuse = fuse;
1746 handlers[i].token = i;
1747 }
1748
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001749 /* When deriving permissions, this thread is used to process inotify events,
1750 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001751 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001752 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001753 pthread_t thread;
1754 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1755 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001756 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001757 goto quit;
1758 }
1759 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001760
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001761 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001762 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001763 } else {
1764 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001765 }
1766
1767 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001768
1769 /* don't bother killing all of the other threads or freeing anything,
1770 * should never get here anyhow */
1771quit:
1772 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001773}
1774
Mike Lockwood4f35e622011-01-12 14:39:44 -05001775static int usage()
1776{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001777 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1778 " -u: specify UID to run as\n"
1779 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001780 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001781 " -t: specify number of threads to use (default %d)\n"
1782 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001783 " -l: derive file permissions based on legacy internal layout\n"
1784 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001785 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001786 return 1;
1787}
1788
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001789static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001790 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1791 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001792 int fd;
1793 char opts[256];
1794 int res;
1795 struct fuse fuse;
1796
1797 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001798 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001799
1800 fd = open("/dev/fuse", O_RDWR);
1801 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001802 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001803 return -1;
1804 }
1805
1806 snprintf(opts, sizeof(opts),
1807 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1808 fd, uid, gid);
1809
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001810 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001811 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001812 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1813 goto error;
1814 }
1815
1816 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1817 if (res < 0) {
1818 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001819 goto error;
1820 }
1821
1822 res = setgid(gid);
1823 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001824 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001825 goto error;
1826 }
1827
1828 res = setuid(uid);
1829 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001830 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001831 goto error;
1832 }
1833
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001834 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001835
1836 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001837 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001838
1839 /* we do not attempt to umount the file system here because we are no longer
1840 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001841
1842error:
1843 close(fd);
1844 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001845}
1846
Brian Swetland03ee9472010-08-12 18:01:08 -07001847int main(int argc, char **argv)
1848{
Brian Swetland03ee9472010-08-12 18:01:08 -07001849 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001850 const char *source_path = NULL;
1851 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001852 uid_t uid = 0;
1853 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001854 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001855 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001856 derive_t derive = DERIVE_NONE;
1857 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001858 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001859 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001860
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001861 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001862 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001863 switch (opt) {
1864 case 'u':
1865 uid = strtoul(optarg, NULL, 10);
1866 break;
1867 case 'g':
1868 gid = strtoul(optarg, NULL, 10);
1869 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001870 case 'w':
1871 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001872 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001873 case 't':
1874 num_threads = strtoul(optarg, NULL, 10);
1875 break;
1876 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001877 derive = DERIVE_UNIFIED;
1878 break;
1879 case 'l':
1880 derive = DERIVE_LEGACY;
1881 break;
1882 case 's':
1883 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001884 break;
1885 case '?':
1886 default:
1887 return usage();
1888 }
1889 }
1890
1891 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001892 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001893 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001894 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001895 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001896 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001897 } else if (!uid) {
1898 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001899 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001900 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001901 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001902 ERROR("too many arguments\n");
1903 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001904 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001905 }
1906
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001907 if (!source_path) {
1908 ERROR("no source path specified\n");
1909 return usage();
1910 }
1911 if (!dest_path) {
1912 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001913 return usage();
1914 }
Jeff Brown26567352012-05-25 13:27:43 -07001915 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001916 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001917 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001918 }
Jeff Brown6249b902012-05-26 14:32:54 -07001919 if (num_threads < 1) {
1920 ERROR("number of threads must be at least 1\n");
1921 return usage();
1922 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001923 if (split_perms && derive == DERIVE_NONE) {
1924 ERROR("cannot split permissions without deriving\n");
1925 return usage();
1926 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001927
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001928 rlim.rlim_cur = 8192;
1929 rlim.rlim_max = 8192;
1930 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1931 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1932 }
1933
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001934 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001935 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001936}