blob: 9b55b33561b690a4a4322b29207282eff9c7fbf3 [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 Oaklandd3330872014-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 Sharkey977a9f32013-08-12 20:23:49 -0700146 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700147 PERM_ANDROID_USER,
148} perm_t;
149
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700150/* Permissions structure to derive */
151typedef enum {
152 DERIVE_NONE,
153 DERIVE_LEGACY,
154 DERIVE_UNIFIED,
155} derive_t;
156
Brian Swetland03ee9472010-08-12 18:01:08 -0700157struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700158 int fd;
159};
160
161struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700162 DIR *d;
163};
164
165struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700166 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700167 __u64 nid;
168 __u64 gen;
169
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700170 /* State derived based on current position in hierarchy. */
171 perm_t perm;
172 userid_t userid;
173 uid_t uid;
174 gid_t gid;
175 mode_t mode;
176
Paul Eastham11ccdb32010-10-14 11:04:26 -0700177 struct node *next; /* per-dir sibling list */
178 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700179 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700180
Jeff Brown6249b902012-05-26 14:32:54 -0700181 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700182 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800183 /* If non-null, this is the real name of the file in the underlying storage.
184 * This may differ from the field "name" only by case.
185 * strlen(actual_name) will always equal strlen(name), so it is safe to use
186 * namelen for both fields.
187 */
188 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700189
190 /* If non-null, an exact underlying path that should be grafted into this
191 * position. Used to support things like OBB. */
192 char* graft_path;
193 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700194};
195
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700196static int str_hash(void *key) {
197 return hashmapHash(key, strlen(key));
198}
199
Jeff Sharkey44d63422013-09-12 09:44:48 -0700200/** Test if two string keys are equal ignoring case */
201static bool str_icase_equals(void *keyA, void *keyB) {
202 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700203}
204
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700205static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800206 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700207}
208
209static bool int_equals(void *keyA, void *keyB) {
210 return keyA == keyB;
211}
212
Jeff Brown7729d242012-05-25 15:35:28 -0700213/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700214struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700215 pthread_mutex_t lock;
216
Brian Swetland03ee9472010-08-12 18:01:08 -0700217 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700218 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700219 derive_t derive;
220 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700221 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700222 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700223 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700224
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700225 Hashmap* package_to_appid;
226 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700227};
228
Jeff Brown7729d242012-05-25 15:35:28 -0700229/* Private data used by a single fuse handler. */
230struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700231 struct fuse* fuse;
232 int token;
233
Jeff Brown7729d242012-05-25 15:35:28 -0700234 /* To save memory, we never use the contents of the request buffer and the read
235 * buffer at the same time. This allows us to share the underlying storage. */
236 union {
237 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800238 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700239 };
240};
Brian Swetland03ee9472010-08-12 18:01:08 -0700241
Jeff Brown6249b902012-05-26 14:32:54 -0700242static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700243{
Jeff Brown6249b902012-05-26 14:32:54 -0700244 return (void *) (uintptr_t) nid;
245}
Brian Swetland03ee9472010-08-12 18:01:08 -0700246
Jeff Brown6249b902012-05-26 14:32:54 -0700247static inline __u64 ptr_to_id(void *ptr)
248{
249 return (__u64) (uintptr_t) ptr;
250}
Brian Swetland03ee9472010-08-12 18:01:08 -0700251
Jeff Brown6249b902012-05-26 14:32:54 -0700252static void acquire_node_locked(struct node* node)
253{
254 node->refcount++;
255 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
256}
257
258static void remove_node_from_parent_locked(struct node* node);
259
260static void release_node_locked(struct node* node)
261{
262 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
263 if (node->refcount > 0) {
264 node->refcount--;
265 if (!node->refcount) {
266 TRACE("DESTROY %p (%s)\n", node, node->name);
267 remove_node_from_parent_locked(node);
268
269 /* TODO: remove debugging - poison memory */
270 memset(node->name, 0xef, node->namelen);
271 free(node->name);
272 free(node->actual_name);
273 memset(node, 0xfc, sizeof(*node));
274 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800275 }
Jeff Brown6249b902012-05-26 14:32:54 -0700276 } else {
277 ERROR("Zero refcnt %p\n", node);
278 }
279}
280
281static void add_node_to_parent_locked(struct node *node, struct node *parent) {
282 node->parent = parent;
283 node->next = parent->child;
284 parent->child = node;
285 acquire_node_locked(parent);
286}
287
288static void remove_node_from_parent_locked(struct node* node)
289{
290 if (node->parent) {
291 if (node->parent->child == node) {
292 node->parent->child = node->parent->child->next;
293 } else {
294 struct node *node2;
295 node2 = node->parent->child;
296 while (node2->next != node)
297 node2 = node2->next;
298 node2->next = node->next;
299 }
300 release_node_locked(node->parent);
301 node->parent = NULL;
302 node->next = NULL;
303 }
304}
305
306/* Gets the absolute path to a node into the provided buffer.
307 *
308 * Populates 'buf' with the path and returns the length of the path on success,
309 * or returns -1 if the path is too long for the provided buffer.
310 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700311static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
312 const char* name;
313 size_t namelen;
314 if (node->graft_path) {
315 name = node->graft_path;
316 namelen = node->graft_pathlen;
317 } else if (node->actual_name) {
318 name = node->actual_name;
319 namelen = node->namelen;
320 } else {
321 name = node->name;
322 namelen = node->namelen;
323 }
324
Jeff Brown6249b902012-05-26 14:32:54 -0700325 if (bufsize < namelen + 1) {
326 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700327 }
328
Jeff Brown6249b902012-05-26 14:32:54 -0700329 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700330 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700331 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
332 if (pathlen < 0) {
333 return -1;
334 }
335 buf[pathlen++] = '/';
336 }
337
Jeff Brown6249b902012-05-26 14:32:54 -0700338 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
339 return pathlen + namelen;
340}
341
342/* Finds the absolute path of a file within a given directory.
343 * Performs a case-insensitive search for the file and sets the buffer to the path
344 * of the first matching file. If 'search' is zero or if no match is found, sets
345 * the buffer to the path that the file would have, assuming the name were case-sensitive.
346 *
347 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
348 * or returns NULL if the path is too long for the provided buffer.
349 */
350static char* find_file_within(const char* path, const char* name,
351 char* buf, size_t bufsize, int search)
352{
353 size_t pathlen = strlen(path);
354 size_t namelen = strlen(name);
355 size_t childlen = pathlen + namelen + 1;
356 char* actual;
357
358 if (bufsize <= childlen) {
359 return NULL;
360 }
361
362 memcpy(buf, path, pathlen);
363 buf[pathlen] = '/';
364 actual = buf + pathlen + 1;
365 memcpy(actual, name, namelen + 1);
366
367 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800368 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700369 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800370 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700371 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700372 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800373 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800374 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700375 if (!strcasecmp(entry->d_name, name)) {
376 /* we have a match - replace the name, don't need to copy the null again */
377 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800378 break;
379 }
380 }
381 closedir(dir);
382 }
Jeff Brown6249b902012-05-26 14:32:54 -0700383 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800384}
385
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700386static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800387{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700388 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700389 attr->size = s->st_size;
390 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400391 attr->atime = s->st_atime;
392 attr->mtime = s->st_mtime;
393 attr->ctime = s->st_ctime;
394 attr->atimensec = s->st_atime_nsec;
395 attr->mtimensec = s->st_mtime_nsec;
396 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700397 attr->mode = s->st_mode;
398 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700399
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700400 attr->uid = node->uid;
401 attr->gid = node->gid;
402
403 /* Filter requested mode based on underlying file, and
404 * pass through file type. */
405 int owner_mode = s->st_mode & 0700;
406 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
407 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
408}
409
Jeff Sharkey44d63422013-09-12 09:44:48 -0700410static int touch(char* path, mode_t mode) {
411 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
412 if (fd == -1) {
413 if (errno == EEXIST) {
414 return 0;
415 } else {
416 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
417 return -1;
418 }
419 }
420 close(fd);
421 return 0;
422}
423
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700424static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
425 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700426 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700427
428 /* By default, each node inherits from its parent */
429 node->perm = PERM_INHERIT;
430 node->userid = parent->userid;
431 node->uid = parent->uid;
432 node->gid = parent->gid;
433 node->mode = parent->mode;
434
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700435 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700436 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700437 }
438
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700439 /* Derive custom permissions based on parent and current node */
440 switch (parent->perm) {
441 case PERM_INHERIT:
442 /* Already inherited above */
443 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700444 case PERM_LEGACY_PRE_ROOT:
445 /* Legacy internal layout places users at top level */
446 node->perm = PERM_ROOT;
447 node->userid = strtoul(node->name, NULL, 10);
448 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700449 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700450 /* Assume masked off by default. */
451 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700452 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700453 /* App-specific directories inside; let anyone traverse */
454 node->perm = PERM_ANDROID;
455 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700456 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700457 if (!strcasecmp(node->name, "DCIM")
458 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700459 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700460 } else if (!strcasecmp(node->name, "Alarms")
461 || !strcasecmp(node->name, "Movies")
462 || !strcasecmp(node->name, "Music")
463 || !strcasecmp(node->name, "Notifications")
464 || !strcasecmp(node->name, "Podcasts")
465 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700466 node->gid = AID_SDCARD_AV;
467 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700468 }
469 break;
470 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700471 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700472 /* App-specific directories inside; let anyone traverse */
473 node->perm = PERM_ANDROID_DATA;
474 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700475 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700476 /* App-specific directories inside; let anyone traverse */
477 node->perm = PERM_ANDROID_OBB;
478 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700479 /* Single OBB directory is always shared */
480 node->graft_path = fuse->obbpath;
481 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700482 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700483 /* User directories must only be accessible to system, protected
484 * by sdcard_all. Zygote will bind mount the appropriate user-
485 * specific path. */
486 node->perm = PERM_ANDROID_USER;
487 node->gid = AID_SDCARD_ALL;
488 node->mode = 0770;
489 }
490 break;
491 case PERM_ANDROID_DATA:
492 case PERM_ANDROID_OBB:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800493 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700494 if (appid != 0) {
495 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700496 }
497 node->mode = 0770;
498 break;
499 case PERM_ANDROID_USER:
500 /* Root of a secondary user */
501 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700502 node->userid = strtoul(node->name, NULL, 10);
503 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700504 node->mode = 0771;
505 break;
506 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700507}
508
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700509/* Return if the calling UID holds sdcard_rw. */
510static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700511 /* No additional permissions enforcement */
512 if (fuse->derive == DERIVE_NONE) {
513 return true;
514 }
515
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700516 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800517 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700518}
519
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700520/* Kernel has already enforced everything we returned through
521 * derive_permissions_locked(), so this is used to lock down access
522 * even further, such as enforcing that apps hold sdcard_rw. */
523static bool check_caller_access_to_name(struct fuse* fuse,
524 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700525 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700526 /* Always block security-sensitive files at root */
527 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700528 if (!strcasecmp(name, "autorun.inf")
529 || !strcasecmp(name, ".android_secure")
530 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700531 return false;
532 }
533 }
534
535 /* No additional permissions enforcement */
536 if (fuse->derive == DERIVE_NONE) {
537 return true;
538 }
539
Jeff Sharkey44d63422013-09-12 09:44:48 -0700540 /* Root always has access; access for any other UIDs should always
541 * be controlled through packages.list. */
542 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700543 return true;
544 }
545
546 /* If asking to write, verify that caller either owns the
547 * parent or holds sdcard_rw. */
548 if (mode & W_OK) {
549 if (parent_node && hdr->uid == parent_node->uid) {
550 return true;
551 }
552
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700553 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700554 }
555
556 /* No extra permissions to enforce */
557 return true;
558}
559
560static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700561 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
562 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700563}
564
Jeff Brown6249b902012-05-26 14:32:54 -0700565struct node *create_node_locked(struct fuse* fuse,
566 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700567{
568 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700569 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700570
Paul Eastham11ccdb32010-10-14 11:04:26 -0700571 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700572 if (!node) {
573 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700574 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700575 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700576 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700577 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700578 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700579 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700580 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700581 if (strcmp(name, actual_name)) {
582 node->actual_name = malloc(namelen + 1);
583 if (!node->actual_name) {
584 free(node->name);
585 free(node);
586 return NULL;
587 }
588 memcpy(node->actual_name, actual_name, namelen + 1);
589 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700590 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700591 node->nid = ptr_to_id(node);
592 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700593
594 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700595 acquire_node_locked(node);
596 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700597 return node;
598}
599
Jeff Brown6249b902012-05-26 14:32:54 -0700600static int rename_node_locked(struct node *node, const char *name,
601 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700602{
Jeff Brown6249b902012-05-26 14:32:54 -0700603 size_t namelen = strlen(name);
604 int need_actual_name = strcmp(name, actual_name);
605
606 /* make the storage bigger without actually changing the name
607 * in case an error occurs part way */
608 if (namelen > node->namelen) {
609 char* new_name = realloc(node->name, namelen + 1);
610 if (!new_name) {
611 return -ENOMEM;
612 }
613 node->name = new_name;
614 if (need_actual_name && node->actual_name) {
615 char* new_actual_name = realloc(node->actual_name, namelen + 1);
616 if (!new_actual_name) {
617 return -ENOMEM;
618 }
619 node->actual_name = new_actual_name;
620 }
621 }
622
623 /* update the name, taking care to allocate storage before overwriting the old name */
624 if (need_actual_name) {
625 if (!node->actual_name) {
626 node->actual_name = malloc(namelen + 1);
627 if (!node->actual_name) {
628 return -ENOMEM;
629 }
630 }
631 memcpy(node->actual_name, actual_name, namelen + 1);
632 } else {
633 free(node->actual_name);
634 node->actual_name = NULL;
635 }
636 memcpy(node->name, name, namelen + 1);
637 node->namelen = namelen;
638 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700639}
640
Jeff Brown6249b902012-05-26 14:32:54 -0700641static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700642{
Jeff Brown6249b902012-05-26 14:32:54 -0700643 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700644 return &fuse->root;
645 } else {
646 return id_to_ptr(nid);
647 }
648}
649
Jeff Brown6249b902012-05-26 14:32:54 -0700650static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
651 char* buf, size_t bufsize)
652{
653 struct node* node = lookup_node_by_id_locked(fuse, nid);
654 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
655 node = NULL;
656 }
657 return node;
658}
659
660static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700661{
662 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700663 /* use exact string comparison, nodes that differ by case
664 * must be considered distinct even if they refer to the same
665 * underlying file as otherwise operations such as "mv x x"
666 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700667 if (!strcmp(name, node->name)) {
668 return node;
669 }
670 }
671 return 0;
672}
673
Jeff Brown6249b902012-05-26 14:32:54 -0700674static struct node* acquire_or_create_child_locked(
675 struct fuse* fuse, struct node* parent,
676 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700677{
Jeff Brown6249b902012-05-26 14:32:54 -0700678 struct node* child = lookup_child_by_name_locked(parent, name);
679 if (child) {
680 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800681 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700682 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800683 }
Jeff Brown6249b902012-05-26 14:32:54 -0700684 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700685}
686
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700687static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700688 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700689 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700690
Jeff Brown6249b902012-05-26 14:32:54 -0700691 fuse->fd = fd;
692 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700693 fuse->derive = derive;
694 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700695 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700696
Jeff Brown6249b902012-05-26 14:32:54 -0700697 memset(&fuse->root, 0, sizeof(fuse->root));
698 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
699 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700700 fuse->root.namelen = strlen(source_path);
701 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700702 fuse->root.userid = 0;
703 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700704
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700705 /* Set up root node for various modes of operation */
706 switch (derive) {
707 case DERIVE_NONE:
708 /* Traditional behavior that treats entire device as being accessible
709 * to sdcard_rw, and no permissions are derived. */
710 fuse->root.perm = PERM_ROOT;
711 fuse->root.mode = 0775;
712 fuse->root.gid = AID_SDCARD_RW;
713 break;
714 case DERIVE_LEGACY:
715 /* Legacy behavior used to support internal multiuser layout which
716 * places user_id at the top directory level, with the actual roots
717 * just below that. Shared OBB path is also at top level. */
718 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
719 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700720 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700721 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700722 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
723 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700724 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700725 break;
726 case DERIVE_UNIFIED:
727 /* Unified multiuser layout which places secondary user_id under
728 * /Android/user and shared OBB path under /Android/obb. */
729 fuse->root.perm = PERM_ROOT;
730 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700731 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700732 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700733 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
734 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
735 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700736 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700737}
738
Jeff Brown6249b902012-05-26 14:32:54 -0700739static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700740{
741 struct fuse_out_header hdr;
742 hdr.len = sizeof(hdr);
743 hdr.error = err;
744 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700745 write(fuse->fd, &hdr, sizeof(hdr));
746}
747
Jeff Brown6249b902012-05-26 14:32:54 -0700748static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700749{
750 struct fuse_out_header hdr;
751 struct iovec vec[2];
752 int res;
753
754 hdr.len = len + sizeof(hdr);
755 hdr.error = 0;
756 hdr.unique = unique;
757
758 vec[0].iov_base = &hdr;
759 vec[0].iov_len = sizeof(hdr);
760 vec[1].iov_base = data;
761 vec[1].iov_len = len;
762
763 res = writev(fuse->fd, vec, 2);
764 if (res < 0) {
765 ERROR("*** REPLY FAILED *** %d\n", errno);
766 }
767}
768
Jeff Brown6249b902012-05-26 14:32:54 -0700769static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
770 struct node* parent, const char* name, const char* actual_name,
771 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700772{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700773 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700774 struct fuse_entry_out out;
775 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700776
Jeff Brown6249b902012-05-26 14:32:54 -0700777 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700778 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700779 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700780
Jeff Brown6249b902012-05-26 14:32:54 -0700781 pthread_mutex_lock(&fuse->lock);
782 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
783 if (!node) {
784 pthread_mutex_unlock(&fuse->lock);
785 return -ENOMEM;
786 }
787 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700788 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700789 out.attr_valid = 10;
790 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700791 out.nodeid = node->nid;
792 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700793 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700794 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700795 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700796}
797
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700798static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700799 const char* path)
800{
801 struct fuse_attr_out out;
802 struct stat s;
803
804 if (lstat(path, &s) < 0) {
805 return -errno;
806 }
807 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700808 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700809 out.attr_valid = 10;
810 fuse_reply(fuse, unique, &out, sizeof(out));
811 return NO_STATUS;
812}
813
814static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700815 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700816{
Jeff Brown6249b902012-05-26 14:32:54 -0700817 struct node* parent_node;
818 char parent_path[PATH_MAX];
819 char child_path[PATH_MAX];
820 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700821
Jeff Brown6249b902012-05-26 14:32:54 -0700822 pthread_mutex_lock(&fuse->lock);
823 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
824 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100825 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700826 parent_node ? parent_node->name : "?");
827 pthread_mutex_unlock(&fuse->lock);
828
829 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
830 child_path, sizeof(child_path), 1))) {
831 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700832 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700833 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700834 return -EACCES;
835 }
836
Jeff Brown6249b902012-05-26 14:32:54 -0700837 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700838}
839
Jeff Brown6249b902012-05-26 14:32:54 -0700840static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700841 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
842{
Jeff Brown6249b902012-05-26 14:32:54 -0700843 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700844
Jeff Brown6249b902012-05-26 14:32:54 -0700845 pthread_mutex_lock(&fuse->lock);
846 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklandd3330872014-07-23 13:04:59 +0100847 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700848 hdr->nodeid, node ? node->name : "?");
849 if (node) {
850 __u64 n = req->nlookup;
851 while (n--) {
852 release_node_locked(node);
853 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700854 }
Jeff Brown6249b902012-05-26 14:32:54 -0700855 pthread_mutex_unlock(&fuse->lock);
856 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700857}
858
Jeff Brown6249b902012-05-26 14:32:54 -0700859static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
861{
Jeff Brown6249b902012-05-26 14:32:54 -0700862 struct node* node;
863 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700864
Jeff Brown6249b902012-05-26 14:32:54 -0700865 pthread_mutex_lock(&fuse->lock);
866 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100867 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700868 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
869 pthread_mutex_unlock(&fuse->lock);
870
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700871 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700872 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700873 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700874 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700875 return -EACCES;
876 }
877
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700878 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879}
880
Jeff Brown6249b902012-05-26 14:32:54 -0700881static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
883{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700884 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700885 struct node* node;
886 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700887 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888
Jeff Brown6249b902012-05-26 14:32:54 -0700889 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700890 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700891 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100892 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700893 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
894 pthread_mutex_unlock(&fuse->lock);
895
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700896 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700897 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700898 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700899 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700900 return -EACCES;
901 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700902
Jeff Brown6249b902012-05-26 14:32:54 -0700903 /* XXX: incomplete implementation on purpose.
904 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905
Elliott Hughes853574d2014-07-31 12:03:03 -0700906 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700907 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700908 }
909
910 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
911 * are both set, then set it to the current time. Else, set it to the
912 * time specified in the request. Same goes for mtime. Use utimensat(2)
913 * as it allows ATIME and MTIME to be changed independently, and has
914 * nanosecond resolution which fuse also has.
915 */
916 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
917 times[0].tv_nsec = UTIME_OMIT;
918 times[1].tv_nsec = UTIME_OMIT;
919 if (req->valid & FATTR_ATIME) {
920 if (req->valid & FATTR_ATIME_NOW) {
921 times[0].tv_nsec = UTIME_NOW;
922 } else {
923 times[0].tv_sec = req->atime;
924 times[0].tv_nsec = req->atimensec;
925 }
926 }
927 if (req->valid & FATTR_MTIME) {
928 if (req->valid & FATTR_MTIME_NOW) {
929 times[1].tv_nsec = UTIME_NOW;
930 } else {
931 times[1].tv_sec = req->mtime;
932 times[1].tv_nsec = req->mtimensec;
933 }
934 }
Jeff Brown6249b902012-05-26 14:32:54 -0700935 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
936 handler->token, path, times[0].tv_sec, times[1].tv_sec);
937 if (utimensat(-1, path, times, 0) < 0) {
938 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700939 }
940 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700941 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700942}
943
Jeff Brown6249b902012-05-26 14:32:54 -0700944static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
946{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700947 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700948 struct node* parent_node;
949 char parent_path[PATH_MAX];
950 char child_path[PATH_MAX];
951 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952
Jeff Brown6249b902012-05-26 14:32:54 -0700953 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700954 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700955 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
956 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100957 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700958 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
959 pthread_mutex_unlock(&fuse->lock);
960
961 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
962 child_path, sizeof(child_path), 1))) {
963 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700964 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700965 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700966 return -EACCES;
967 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700968 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700969 if (mknod(child_path, mode, req->rdev) < 0) {
970 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700971 }
Jeff Brown6249b902012-05-26 14:32:54 -0700972 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700973}
974
Jeff Brown6249b902012-05-26 14:32:54 -0700975static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700976 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
977{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700978 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700979 struct node* parent_node;
980 char parent_path[PATH_MAX];
981 char child_path[PATH_MAX];
982 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700983
Jeff Brown6249b902012-05-26 14:32:54 -0700984 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700985 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700986 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
987 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100988 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700989 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
990 pthread_mutex_unlock(&fuse->lock);
991
992 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
993 child_path, sizeof(child_path), 1))) {
994 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700995 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700996 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700997 return -EACCES;
998 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700999 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001000 if (mkdir(child_path, mode) < 0) {
1001 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001002 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001003
1004 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1005 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1006 char nomedia[PATH_MAX];
1007 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1008 if (touch(nomedia, 0664) != 0) {
1009 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1010 return -ENOENT;
1011 }
1012 }
1013 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1014 char nomedia[PATH_MAX];
1015 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1016 if (touch(nomedia, 0664) != 0) {
1017 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1018 return -ENOENT;
1019 }
1020 }
1021
Jeff Brown6249b902012-05-26 14:32:54 -07001022 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023}
1024
Jeff Brown6249b902012-05-26 14:32:54 -07001025static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026 const struct fuse_in_header* hdr, const char* name)
1027{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001028 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001029 struct node* parent_node;
1030 char parent_path[PATH_MAX];
1031 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001032
Jeff Brown6249b902012-05-26 14:32:54 -07001033 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001034 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001035 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1036 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001037 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001038 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1039 pthread_mutex_unlock(&fuse->lock);
1040
1041 if (!parent_node || !find_file_within(parent_path, name,
1042 child_path, sizeof(child_path), 1)) {
1043 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001044 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001045 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001046 return -EACCES;
1047 }
Jeff Brown6249b902012-05-26 14:32:54 -07001048 if (unlink(child_path) < 0) {
1049 return -errno;
1050 }
1051 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001052}
1053
Jeff Brown6249b902012-05-26 14:32:54 -07001054static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001055 const struct fuse_in_header* hdr, const char* name)
1056{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001057 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001058 struct node* parent_node;
1059 char parent_path[PATH_MAX];
1060 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001061
Jeff Brown6249b902012-05-26 14:32:54 -07001062 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001063 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001064 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1065 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001066 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001067 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1068 pthread_mutex_unlock(&fuse->lock);
1069
1070 if (!parent_node || !find_file_within(parent_path, name,
1071 child_path, sizeof(child_path), 1)) {
1072 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001074 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001075 return -EACCES;
1076 }
Jeff Brown6249b902012-05-26 14:32:54 -07001077 if (rmdir(child_path) < 0) {
1078 return -errno;
1079 }
1080 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001081}
1082
Jeff Brown6249b902012-05-26 14:32:54 -07001083static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001084 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001085 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001086{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001087 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001088 struct node* old_parent_node;
1089 struct node* new_parent_node;
1090 struct node* child_node;
1091 char old_parent_path[PATH_MAX];
1092 char new_parent_path[PATH_MAX];
1093 char old_child_path[PATH_MAX];
1094 char new_child_path[PATH_MAX];
1095 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001096 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001097
Jeff Brown6249b902012-05-26 14:32:54 -07001098 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001099 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001100 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1101 old_parent_path, sizeof(old_parent_path));
1102 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1103 new_parent_path, sizeof(new_parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001104 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001105 old_name, new_name,
1106 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1107 req->newdir, new_parent_node ? new_parent_node->name : "?");
1108 if (!old_parent_node || !new_parent_node) {
1109 res = -ENOENT;
1110 goto lookup_error;
1111 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001112 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001113 res = -EACCES;
1114 goto lookup_error;
1115 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001116 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001117 res = -EACCES;
1118 goto lookup_error;
1119 }
Jeff Brown6249b902012-05-26 14:32:54 -07001120 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1121 if (!child_node || get_node_path_locked(child_node,
1122 old_child_path, sizeof(old_child_path)) < 0) {
1123 res = -ENOENT;
1124 goto lookup_error;
1125 }
1126 acquire_node_locked(child_node);
1127 pthread_mutex_unlock(&fuse->lock);
1128
1129 /* Special case for renaming a file where destination is same path
1130 * differing only by case. In this case we don't want to look for a case
1131 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1132 */
1133 int search = old_parent_node != new_parent_node
1134 || strcasecmp(old_name, new_name);
1135 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1136 new_child_path, sizeof(new_child_path), search))) {
1137 res = -ENOENT;
1138 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001139 }
1140
Jeff Brown6249b902012-05-26 14:32:54 -07001141 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1142 res = rename(old_child_path, new_child_path);
1143 if (res < 0) {
1144 res = -errno;
1145 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001146 }
1147
Jeff Brown6249b902012-05-26 14:32:54 -07001148 pthread_mutex_lock(&fuse->lock);
1149 res = rename_node_locked(child_node, new_name, new_actual_name);
1150 if (!res) {
1151 remove_node_from_parent_locked(child_node);
1152 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001153 }
Jeff Brown6249b902012-05-26 14:32:54 -07001154 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001155
Jeff Brown6249b902012-05-26 14:32:54 -07001156io_error:
1157 pthread_mutex_lock(&fuse->lock);
1158done:
1159 release_node_locked(child_node);
1160lookup_error:
1161 pthread_mutex_unlock(&fuse->lock);
1162 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001163}
1164
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001165static int open_flags_to_access_mode(int open_flags) {
1166 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1167 return R_OK;
1168 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1169 return W_OK;
1170 } else {
1171 /* Probably O_RDRW, but treat as default to be safe */
1172 return R_OK | W_OK;
1173 }
1174}
1175
Jeff Brown6249b902012-05-26 14:32:54 -07001176static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001177 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1178{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001179 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001180 struct node* node;
1181 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001182 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001184
Jeff Brown6249b902012-05-26 14:32:54 -07001185 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001186 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001187 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001188 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001189 req->flags, hdr->nodeid, node ? node->name : "?");
1190 pthread_mutex_unlock(&fuse->lock);
1191
1192 if (!node) {
1193 return -ENOENT;
1194 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001195 if (!check_caller_access_to_node(fuse, hdr, node,
1196 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001197 return -EACCES;
1198 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199 h = malloc(sizeof(*h));
1200 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001201 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001202 }
Jeff Brown6249b902012-05-26 14:32:54 -07001203 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001204 h->fd = open(path, req->flags);
1205 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001206 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001207 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001208 }
1209 out.fh = ptr_to_id(h);
1210 out.open_flags = 0;
1211 out.padding = 0;
1212 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001213 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214}
1215
Jeff Brown6249b902012-05-26 14:32:54 -07001216static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1218{
1219 struct handle *h = id_to_ptr(req->fh);
1220 __u64 unique = hdr->unique;
1221 __u32 size = req->size;
1222 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001223 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001224 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001225
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001226 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1227 * overlaps the request buffer and will clobber data in the request. This
1228 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001229
Marcus Oaklandd3330872014-07-23 13:04:59 +01001230 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1231 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001232 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001233 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001234 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001235 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001236 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001237 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001238 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001239 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001240 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001241}
1242
Jeff Brown6249b902012-05-26 14:32:54 -07001243static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1245 const void* buffer)
1246{
1247 struct fuse_write_out out;
1248 struct handle *h = id_to_ptr(req->fh);
1249 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001250 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001251
Arpad Horvath49e93442014-02-18 10:18:25 +01001252 if (req->flags & O_DIRECT) {
1253 memcpy(aligned_buffer, buffer, req->size);
1254 buffer = (const __u8*) aligned_buffer;
1255 }
Jeff Brown6249b902012-05-26 14:32:54 -07001256
Marcus Oaklandd3330872014-07-23 13:04:59 +01001257 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001258 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001259 res = pwrite64(h->fd, buffer, req->size, req->offset);
1260 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001261 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262 }
1263 out.size = res;
1264 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001265 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266}
1267
Jeff Brown6249b902012-05-26 14:32:54 -07001268static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 const struct fuse_in_header* hdr)
1270{
Jeff Brown6249b902012-05-26 14:32:54 -07001271 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272 struct statfs stat;
1273 struct fuse_statfs_out out;
1274 int res;
1275
Jeff Brown6249b902012-05-26 14:32:54 -07001276 pthread_mutex_lock(&fuse->lock);
1277 TRACE("[%d] STATFS\n", handler->token);
1278 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1279 pthread_mutex_unlock(&fuse->lock);
1280 if (res < 0) {
1281 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001282 }
Jeff Brown6249b902012-05-26 14:32:54 -07001283 if (statfs(fuse->root.name, &stat) < 0) {
1284 return -errno;
1285 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001286 memset(&out, 0, sizeof(out));
1287 out.st.blocks = stat.f_blocks;
1288 out.st.bfree = stat.f_bfree;
1289 out.st.bavail = stat.f_bavail;
1290 out.st.files = stat.f_files;
1291 out.st.ffree = stat.f_ffree;
1292 out.st.bsize = stat.f_bsize;
1293 out.st.namelen = stat.f_namelen;
1294 out.st.frsize = stat.f_frsize;
1295 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001296 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297}
1298
Jeff Brown6249b902012-05-26 14:32:54 -07001299static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001300 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1301{
1302 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001303
1304 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001305 close(h->fd);
1306 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001307 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308}
1309
Jeff Brown6249b902012-05-26 14:32:54 -07001310static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001311 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1312{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001313 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1314 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001315
Elliott Hughesf6d67372014-07-08 14:38:26 -07001316 int fd = -1;
1317 if (is_dir) {
1318 struct dirhandle *dh = id_to_ptr(req->fh);
1319 fd = dirfd(dh->d);
1320 } else {
1321 struct handle *h = id_to_ptr(req->fh);
1322 fd = h->fd;
1323 }
1324
1325 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1326 is_dir ? "FSYNCDIR" : "FSYNC",
1327 id_to_ptr(req->fh), fd, is_data_sync);
1328 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1329 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001330 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001331 }
Jeff Brown6249b902012-05-26 14:32:54 -07001332 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001333}
1334
Jeff Brown6249b902012-05-26 14:32:54 -07001335static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001336 const struct fuse_in_header* hdr)
1337{
Jeff Brown6249b902012-05-26 14:32:54 -07001338 TRACE("[%d] FLUSH\n", handler->token);
1339 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001340}
1341
Jeff Brown6249b902012-05-26 14:32:54 -07001342static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001343 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1344{
Jeff Brown6249b902012-05-26 14:32:54 -07001345 struct node* node;
1346 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001347 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349
Jeff Brown6249b902012-05-26 14:32:54 -07001350 pthread_mutex_lock(&fuse->lock);
1351 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001352 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001353 hdr->nodeid, node ? node->name : "?");
1354 pthread_mutex_unlock(&fuse->lock);
1355
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001356 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001357 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001358 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001359 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001360 return -EACCES;
1361 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362 h = malloc(sizeof(*h));
1363 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001364 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001365 }
Jeff Brown6249b902012-05-26 14:32:54 -07001366 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001368 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001370 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001371 }
1372 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001373 out.open_flags = 0;
1374 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001376 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377}
1378
Jeff Brown6249b902012-05-26 14:32:54 -07001379static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1381{
1382 char buffer[8192];
1383 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1384 struct dirent *de;
1385 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001386
1387 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388 if (req->offset == 0) {
1389 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001390 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001391 rewinddir(h->d);
1392 }
1393 de = readdir(h->d);
1394 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001395 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001396 }
1397 fde->ino = FUSE_UNKNOWN_INO;
1398 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1399 fde->off = req->offset + 1;
1400 fde->type = de->d_type;
1401 fde->namelen = strlen(de->d_name);
1402 memcpy(fde->name, de->d_name, fde->namelen + 1);
1403 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001404 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1405 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001406}
1407
Jeff Brown6249b902012-05-26 14:32:54 -07001408static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001409 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1410{
1411 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001412
1413 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001414 closedir(h->d);
1415 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001416 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001417}
1418
Jeff Brown6249b902012-05-26 14:32:54 -07001419static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1421{
1422 struct fuse_init_out out;
1423
Jeff Brown6249b902012-05-26 14:32:54 -07001424 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1425 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001426 out.major = FUSE_KERNEL_VERSION;
1427 out.minor = FUSE_KERNEL_MINOR_VERSION;
1428 out.max_readahead = req->max_readahead;
1429 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1430 out.max_background = 32;
1431 out.congestion_threshold = 32;
1432 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001433 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001434 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001435}
1436
Jeff Brown6249b902012-05-26 14:32:54 -07001437static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001438 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1439{
Brian Swetland03ee9472010-08-12 18:01:08 -07001440 switch (hdr->opcode) {
1441 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001442 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001443 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001444 }
Jeff Brown84715842012-05-25 14:07:47 -07001445
Brian Swetland03ee9472010-08-12 18:01:08 -07001446 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001447 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001448 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001449 }
Jeff Brown84715842012-05-25 14:07:47 -07001450
Brian Swetland03ee9472010-08-12 18:01:08 -07001451 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001452 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001453 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001454 }
Jeff Brown84715842012-05-25 14:07:47 -07001455
Brian Swetland03ee9472010-08-12 18:01:08 -07001456 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001457 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001458 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001459 }
Jeff Brown84715842012-05-25 14:07:47 -07001460
Brian Swetland03ee9472010-08-12 18:01:08 -07001461// case FUSE_READLINK:
1462// case FUSE_SYMLINK:
1463 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001464 const struct fuse_mknod_in *req = data;
1465 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001466 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001467 }
Jeff Brown84715842012-05-25 14:07:47 -07001468
Brian Swetland03ee9472010-08-12 18:01:08 -07001469 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001470 const struct fuse_mkdir_in *req = data;
1471 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001472 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001473 }
Jeff Brown84715842012-05-25 14:07:47 -07001474
Brian Swetland03ee9472010-08-12 18:01:08 -07001475 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001476 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001477 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001478 }
Jeff Brown84715842012-05-25 14:07:47 -07001479
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001481 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001482 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001483 }
Jeff Brown84715842012-05-25 14:07:47 -07001484
Brian Swetland03ee9472010-08-12 18:01:08 -07001485 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001486 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001487 const char *old_name = ((const char*) data) + sizeof(*req);
1488 const char *new_name = old_name + strlen(old_name) + 1;
1489 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001490 }
Jeff Brown84715842012-05-25 14:07:47 -07001491
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001492// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001493 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001494 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001495 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001496 }
Jeff Brown84715842012-05-25 14:07:47 -07001497
Brian Swetland03ee9472010-08-12 18:01:08 -07001498 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001499 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001500 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001501 }
Jeff Brown84715842012-05-25 14:07:47 -07001502
Brian Swetland03ee9472010-08-12 18:01:08 -07001503 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001504 const struct fuse_write_in *req = data;
1505 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001506 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001507 }
Jeff Brown84715842012-05-25 14:07:47 -07001508
Mike Lockwood4553b082010-08-16 14:14:44 -04001509 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001510 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001511 }
Jeff Brown84715842012-05-25 14:07:47 -07001512
Brian Swetland03ee9472010-08-12 18:01:08 -07001513 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001514 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001515 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001516 }
Jeff Brown84715842012-05-25 14:07:47 -07001517
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001518 case FUSE_FSYNC:
1519 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001520 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001521 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001522 }
1523
Brian Swetland03ee9472010-08-12 18:01:08 -07001524// case FUSE_SETXATTR:
1525// case FUSE_GETXATTR:
1526// case FUSE_LISTXATTR:
1527// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001528 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001529 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001530 }
1531
Brian Swetland03ee9472010-08-12 18:01:08 -07001532 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001533 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001534 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001535 }
Jeff Brown84715842012-05-25 14:07:47 -07001536
Brian Swetland03ee9472010-08-12 18:01:08 -07001537 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001538 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001539 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001540 }
Jeff Brown84715842012-05-25 14:07:47 -07001541
Brian Swetland03ee9472010-08-12 18:01:08 -07001542 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001543 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001544 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001545 }
Jeff Brown84715842012-05-25 14:07:47 -07001546
Brian Swetland03ee9472010-08-12 18:01:08 -07001547 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001548 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001549 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 }
Jeff Brown84715842012-05-25 14:07:47 -07001551
Brian Swetland03ee9472010-08-12 18:01:08 -07001552 default: {
Marcus Oaklandd3330872014-07-23 13:04:59 +01001553 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001554 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1555 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001556 }
Jeff Brown84715842012-05-25 14:07:47 -07001557 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001558}
1559
Jeff Brown6249b902012-05-26 14:32:54 -07001560static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001561{
Jeff Brown6249b902012-05-26 14:32:54 -07001562 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001563 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001564 ssize_t len = read(fuse->fd,
1565 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001566 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001567 if (errno != EINTR) {
1568 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1569 }
1570 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001571 }
Jeff Brown84715842012-05-25 14:07:47 -07001572
1573 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001574 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1575 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001576 }
1577
Jeff Brown7729d242012-05-25 15:35:28 -07001578 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001579 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001580 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1581 handler->token, (size_t)len, hdr->len);
1582 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001583 }
1584
Jeff Brown7729d242012-05-25 15:35:28 -07001585 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001586 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001587 __u64 unique = hdr->unique;
1588 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001589
1590 /* We do not access the request again after this point because the underlying
1591 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001592
1593 if (res != NO_STATUS) {
1594 if (res) {
1595 TRACE("[%d] ERROR %d\n", handler->token, res);
1596 }
1597 fuse_status(fuse, unique, res);
1598 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001599 }
1600}
1601
Jeff Brown6249b902012-05-26 14:32:54 -07001602static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001603{
Jeff Brown6249b902012-05-26 14:32:54 -07001604 struct fuse_handler* handler = data;
1605 handle_fuse_requests(handler);
1606 return NULL;
1607}
1608
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001609static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001610 Hashmap* map = context;
1611 hashmapRemove(map, key);
1612 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001613 return true;
1614}
1615
1616static bool remove_int_to_null(void *key, void *value, void *context) {
1617 Hashmap* map = context;
1618 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001619 return true;
1620}
1621
1622static int read_package_list(struct fuse *fuse) {
1623 pthread_mutex_lock(&fuse->lock);
1624
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001625 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1626 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001627
1628 FILE* file = fopen(kPackagesListFile, "r");
1629 if (!file) {
1630 ERROR("failed to open package list: %s\n", strerror(errno));
1631 pthread_mutex_unlock(&fuse->lock);
1632 return -1;
1633 }
1634
1635 char buf[512];
1636 while (fgets(buf, sizeof(buf), file) != NULL) {
1637 char package_name[512];
1638 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001639 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001640
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001641 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1642 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001643 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001644
1645 char* token = strtok(gids, ",");
1646 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001647 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001648 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001649 break;
1650 }
1651 token = strtok(NULL, ",");
1652 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001653 }
1654 }
1655
Marcus Oaklandd3330872014-07-23 13:04:59 +01001656 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001657 hashmapSize(fuse->package_to_appid),
1658 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001659 fclose(file);
1660 pthread_mutex_unlock(&fuse->lock);
1661 return 0;
1662}
1663
1664static void watch_package_list(struct fuse* fuse) {
1665 struct inotify_event *event;
1666 char event_buf[512];
1667
1668 int nfd = inotify_init();
1669 if (nfd < 0) {
1670 ERROR("inotify_init failed: %s\n", strerror(errno));
1671 return;
1672 }
1673
1674 bool active = false;
1675 while (1) {
1676 if (!active) {
1677 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1678 if (res == -1) {
1679 if (errno == ENOENT || errno == EACCES) {
1680 /* Framework may not have created yet, sleep and retry */
1681 ERROR("missing packages.list; retrying\n");
1682 sleep(3);
1683 continue;
1684 } else {
1685 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1686 return;
1687 }
1688 }
1689
1690 /* Watch above will tell us about any future changes, so
1691 * read the current state. */
1692 if (read_package_list(fuse) == -1) {
1693 ERROR("read_package_list failed: %s\n", strerror(errno));
1694 return;
1695 }
1696 active = true;
1697 }
1698
1699 int event_pos = 0;
1700 int res = read(nfd, event_buf, sizeof(event_buf));
1701 if (res < (int) sizeof(*event)) {
1702 if (errno == EINTR)
1703 continue;
1704 ERROR("failed to read inotify event: %s\n", strerror(errno));
1705 return;
1706 }
1707
1708 while (res >= (int) sizeof(*event)) {
1709 int event_size;
1710 event = (struct inotify_event *) (event_buf + event_pos);
1711
1712 TRACE("inotify event: %08x\n", event->mask);
1713 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1714 /* Previously watched file was deleted, probably due to move
1715 * that swapped in new data; re-arm the watch and read. */
1716 active = false;
1717 }
1718
1719 event_size = sizeof(*event) + event->len;
1720 res -= event_size;
1721 event_pos += event_size;
1722 }
1723 }
1724}
1725
Jeff Brown6249b902012-05-26 14:32:54 -07001726static int ignite_fuse(struct fuse* fuse, int num_threads)
1727{
1728 struct fuse_handler* handlers;
1729 int i;
1730
1731 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1732 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001733 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001734 return -ENOMEM;
1735 }
1736
1737 for (i = 0; i < num_threads; i++) {
1738 handlers[i].fuse = fuse;
1739 handlers[i].token = i;
1740 }
1741
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001742 /* When deriving permissions, this thread is used to process inotify events,
1743 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001744 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001745 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001746 pthread_t thread;
1747 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1748 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001749 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001750 goto quit;
1751 }
1752 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001753
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001754 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001755 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001756 } else {
1757 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001758 }
1759
1760 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001761
1762 /* don't bother killing all of the other threads or freeing anything,
1763 * should never get here anyhow */
1764quit:
1765 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001766}
1767
Mike Lockwood4f35e622011-01-12 14:39:44 -05001768static int usage()
1769{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001770 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1771 " -u: specify UID to run as\n"
1772 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001773 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001774 " -t: specify number of threads to use (default %d)\n"
1775 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001776 " -l: derive file permissions based on legacy internal layout\n"
1777 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001778 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001779 return 1;
1780}
1781
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001782static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001783 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1784 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001785 int fd;
1786 char opts[256];
1787 int res;
1788 struct fuse fuse;
1789
1790 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001791 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001792
1793 fd = open("/dev/fuse", O_RDWR);
1794 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001795 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001796 return -1;
1797 }
1798
1799 snprintf(opts, sizeof(opts),
1800 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1801 fd, uid, gid);
1802
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001803 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001804 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001805 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1806 goto error;
1807 }
1808
1809 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1810 if (res < 0) {
1811 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001812 goto error;
1813 }
1814
1815 res = setgid(gid);
1816 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001817 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001818 goto error;
1819 }
1820
1821 res = setuid(uid);
1822 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001823 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001824 goto error;
1825 }
1826
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001827 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001828
1829 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001830 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001831
1832 /* we do not attempt to umount the file system here because we are no longer
1833 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001834
1835error:
1836 close(fd);
1837 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001838}
1839
Brian Swetland03ee9472010-08-12 18:01:08 -07001840int main(int argc, char **argv)
1841{
Brian Swetland03ee9472010-08-12 18:01:08 -07001842 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001843 const char *source_path = NULL;
1844 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001845 uid_t uid = 0;
1846 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001847 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001848 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001849 derive_t derive = DERIVE_NONE;
1850 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001851 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001852 struct rlimit rlim;
Nick Kralevich506edb52014-07-24 17:05:59 -07001853 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001854
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001855 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001856 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001857 switch (opt) {
1858 case 'u':
1859 uid = strtoul(optarg, NULL, 10);
1860 break;
1861 case 'g':
1862 gid = strtoul(optarg, NULL, 10);
1863 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001864 case 'w':
1865 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001866 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001867 case 't':
1868 num_threads = strtoul(optarg, NULL, 10);
1869 break;
1870 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001871 derive = DERIVE_UNIFIED;
1872 break;
1873 case 'l':
1874 derive = DERIVE_LEGACY;
1875 break;
1876 case 's':
1877 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001878 break;
1879 case '?':
1880 default:
1881 return usage();
1882 }
1883 }
1884
1885 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001886 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001887 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001888 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001889 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001890 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001891 } else if (!uid) {
1892 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001893 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001894 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001895 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001896 ERROR("too many arguments\n");
1897 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001898 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001899 }
1900
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001901 if (!source_path) {
1902 ERROR("no source path specified\n");
1903 return usage();
1904 }
1905 if (!dest_path) {
1906 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001907 return usage();
1908 }
Jeff Brown26567352012-05-25 13:27:43 -07001909 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001910 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001911 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001912 }
Jeff Brown6249b902012-05-26 14:32:54 -07001913 if (num_threads < 1) {
1914 ERROR("number of threads must be at least 1\n");
1915 return usage();
1916 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001917 if (split_perms && derive == DERIVE_NONE) {
1918 ERROR("cannot split permissions without deriving\n");
1919 return usage();
1920 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001921
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001922 rlim.rlim_cur = 8192;
1923 rlim.rlim_max = 8192;
1924 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1925 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1926 }
1927
Nick Kralevich506edb52014-07-24 17:05:59 -07001928 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1929 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1930 sleep(1);
1931 }
1932
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001933 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001934 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001935}