blob: 5c18f26a4aa0829ef2d888df7aba05c6e1fa0c41 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
William Robertse5099802015-07-31 13:11:19 -070027#include <stdbool.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070031#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070032#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070033#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070034#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070035#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040036#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080037#include <sys/time.h>
William Robertse5099802015-07-31 13:11:19 -070038#include <sys/types.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070039#include <sys/uio.h>
40#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041
Jeff Sharkey44d63422013-09-12 09:44:48 -070042#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070044#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070045#include <cutils/multiuser.h>
Jeff Sharkey20ca9832016-04-07 11:05:21 -060046#include <cutils/properties.h>
William Robertse5099802015-07-31 13:11:19 -070047#include <packagelistparser/packagelistparser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070048
Brian Swetlandb14a2c62010-08-12 18:21:12 -070049#include <private/android_filesystem_config.h>
50
Brian Swetland03ee9472010-08-12 18:01:08 -070051/* README
52 *
53 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070054 *
Brian Swetland03ee9472010-08-12 18:01:08 -070055 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070056 * directory permissions (all files are given fixed owner, group, and
57 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070058 * changeable, symlinks and hardlinks are not createable, etc.
59 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070060 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070061 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070062 * It must be run as root, but will drop to requested UID/GID as soon as it
63 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070064 *
65 * Things I believe to be true:
66 *
67 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
68 * CREAT) must bump that node's refcount
69 * - don't forget that FORGET can forget multiple references (req->nlookup)
70 * - if an op that returns a fuse_entry fails writing the reply to the
71 * kernel, you must rollback the refcount to reflect the reference the
72 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070073 *
74 * This daemon can also derive custom filesystem permissions based on directory
75 * structure when requested. These custom permissions support several features:
76 *
77 * - Apps can access their own files in /Android/data/com.example/ without
78 * requiring any additional GIDs.
79 * - Separate permissions for protecting directories like Pictures and Music.
80 * - Multi-user separation on the same physical device.
Brian Swetland03ee9472010-08-12 18:01:08 -070081 */
82
83#define FUSE_TRACE 0
84
85#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070086#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070087#else
88#define TRACE(x...) do {} while (0)
89#endif
90
Elliott Hughes300d5642014-07-08 13:53:26 -070091#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070092
Jeff Sharkey20ca9832016-04-07 11:05:21 -060093#define PROP_SDCARDFS_DEVICE "ro.sys.sdcardfs"
94#define PROP_SDCARDFS_USER "persist.sys.sdcardfs"
95
Brian Swetland03ee9472010-08-12 18:01:08 -070096#define FUSE_UNKNOWN_INO 0xffffffff
97
Jeff Brown84715842012-05-25 14:07:47 -070098/* Maximum number of bytes to write in one request. */
99#define MAX_WRITE (256 * 1024)
100
101/* Maximum number of bytes to read in one request. */
102#define MAX_READ (128 * 1024)
103
104/* Largest possible request.
105 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
106 * the largest possible data payload. */
107#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
108
Jeff Brown6249b902012-05-26 14:32:54 -0700109/* Pseudo-error constant used to indicate that no fuse status is needed
110 * or that a reply has already been written. */
111#define NO_STATUS 1
112
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700113/* Supplementary groups to execute with */
114static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
115
116/* Permission mode for a specific node. Controls how file permissions
117 * are derived for children nodes. */
118typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700119 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700120 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700121 /* This node is one level above a normal root; used for legacy layouts
122 * which use the first level to represent user_id. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700123 PERM_PRE_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700124 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700126 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700127 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700128 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700129 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700130 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700131 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700132 /* This node is "/Android/media" */
133 PERM_ANDROID_MEDIA,
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700134} perm_t;
135
Brian Swetland03ee9472010-08-12 18:01:08 -0700136struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700137 int fd;
138};
139
140struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700141 DIR *d;
142};
143
144struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700145 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700146 __u64 nid;
147 __u64 gen;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000148 /*
149 * The inode number for this FUSE node. Note that this isn't stable across
150 * multiple invocations of the FUSE daemon.
151 */
152 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700153
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700154 /* State derived based on current position in hierarchy. */
155 perm_t perm;
156 userid_t userid;
157 uid_t uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700158 bool under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700159
Paul Eastham11ccdb32010-10-14 11:04:26 -0700160 struct node *next; /* per-dir sibling list */
161 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700162 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700163
Jeff Brown6249b902012-05-26 14:32:54 -0700164 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700165 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800166 /* If non-null, this is the real name of the file in the underlying storage.
167 * This may differ from the field "name" only by case.
168 * strlen(actual_name) will always equal strlen(name), so it is safe to use
169 * namelen for both fields.
170 */
171 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700172
173 /* If non-null, an exact underlying path that should be grafted into this
174 * position. Used to support things like OBB. */
175 char* graft_path;
176 size_t graft_pathlen;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200177
178 bool deleted;
Brian Swetland03ee9472010-08-12 18:01:08 -0700179};
180
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700181static int str_hash(void *key) {
182 return hashmapHash(key, strlen(key));
183}
184
Jeff Sharkey44d63422013-09-12 09:44:48 -0700185/** Test if two string keys are equal ignoring case */
186static bool str_icase_equals(void *keyA, void *keyB) {
187 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700188}
189
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700190/* Global data for all FUSE mounts */
191struct fuse_global {
Jeff Brown6249b902012-05-26 14:32:54 -0700192 pthread_mutex_t lock;
193
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700194 uid_t uid;
195 gid_t gid;
196 bool multi_user;
197
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700198 char source_path[PATH_MAX];
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700199 char obb_path[PATH_MAX];
200
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700201 Hashmap* package_to_appid;
202
Brian Swetland03ee9472010-08-12 18:01:08 -0700203 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700204 struct node root;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700205
Narayan Kamathfaa09352015-01-13 18:21:10 +0000206 /* Used to allocate unique inode numbers for fuse nodes. We use
207 * a simple counter based scheme where inode numbers from deleted
208 * nodes aren't reused. Note that inode allocations are not stable
209 * across multiple invocation of the sdcard daemon, but that shouldn't
210 * be a huge problem in practice.
211 *
212 * Note that we restrict inodes to 32 bit unsigned integers to prevent
213 * truncation on 32 bit processes when unsigned long long stat.st_ino is
214 * assigned to an unsigned long ino_t type in an LP32 process.
215 *
216 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
217 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
218 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
219 * in fs/fuse/inode.c).
220 *
221 * Accesses must be guarded by |lock|.
222 */
223 __u32 inode_ctr;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700224
225 struct fuse* fuse_default;
226 struct fuse* fuse_read;
227 struct fuse* fuse_write;
228};
229
230/* Single FUSE mount */
231struct fuse {
232 struct fuse_global* global;
233
234 char dest_path[PATH_MAX];
235
236 int fd;
237
238 gid_t gid;
239 mode_t mask;
Brian Swetland03ee9472010-08-12 18:01:08 -0700240};
241
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700242/* Private data used by a single FUSE handler */
Jeff Brown7729d242012-05-25 15:35:28 -0700243struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700244 struct fuse* fuse;
245 int token;
246
Jeff Brown7729d242012-05-25 15:35:28 -0700247 /* To save memory, we never use the contents of the request buffer and the read
248 * buffer at the same time. This allows us to share the underlying storage. */
249 union {
250 __u8 request_buffer[MAX_REQUEST_SIZE];
Elliott Hughese24e9a52015-07-28 16:36:47 -0700251 __u8 read_buffer[MAX_READ + PAGE_SIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700252 };
253};
Brian Swetland03ee9472010-08-12 18:01:08 -0700254
Jeff Brown6249b902012-05-26 14:32:54 -0700255static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700256{
Jeff Brown6249b902012-05-26 14:32:54 -0700257 return (void *) (uintptr_t) nid;
258}
Brian Swetland03ee9472010-08-12 18:01:08 -0700259
Jeff Brown6249b902012-05-26 14:32:54 -0700260static inline __u64 ptr_to_id(void *ptr)
261{
262 return (__u64) (uintptr_t) ptr;
263}
Brian Swetland03ee9472010-08-12 18:01:08 -0700264
Jeff Brown6249b902012-05-26 14:32:54 -0700265static void acquire_node_locked(struct node* node)
266{
267 node->refcount++;
268 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
269}
270
271static void remove_node_from_parent_locked(struct node* node);
272
273static void release_node_locked(struct node* node)
274{
275 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
276 if (node->refcount > 0) {
277 node->refcount--;
278 if (!node->refcount) {
279 TRACE("DESTROY %p (%s)\n", node, node->name);
280 remove_node_from_parent_locked(node);
281
282 /* TODO: remove debugging - poison memory */
283 memset(node->name, 0xef, node->namelen);
284 free(node->name);
285 free(node->actual_name);
286 memset(node, 0xfc, sizeof(*node));
287 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800288 }
Jeff Brown6249b902012-05-26 14:32:54 -0700289 } else {
290 ERROR("Zero refcnt %p\n", node);
291 }
292}
293
294static void add_node_to_parent_locked(struct node *node, struct node *parent) {
295 node->parent = parent;
296 node->next = parent->child;
297 parent->child = node;
298 acquire_node_locked(parent);
299}
300
301static void remove_node_from_parent_locked(struct node* node)
302{
303 if (node->parent) {
304 if (node->parent->child == node) {
305 node->parent->child = node->parent->child->next;
306 } else {
307 struct node *node2;
308 node2 = node->parent->child;
309 while (node2->next != node)
310 node2 = node2->next;
311 node2->next = node->next;
312 }
313 release_node_locked(node->parent);
314 node->parent = NULL;
315 node->next = NULL;
316 }
317}
318
319/* Gets the absolute path to a node into the provided buffer.
320 *
321 * Populates 'buf' with the path and returns the length of the path on success,
322 * or returns -1 if the path is too long for the provided buffer.
323 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700324static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
325 const char* name;
326 size_t namelen;
327 if (node->graft_path) {
328 name = node->graft_path;
329 namelen = node->graft_pathlen;
330 } else if (node->actual_name) {
331 name = node->actual_name;
332 namelen = node->namelen;
333 } else {
334 name = node->name;
335 namelen = node->namelen;
336 }
337
Jeff Brown6249b902012-05-26 14:32:54 -0700338 if (bufsize < namelen + 1) {
339 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700340 }
341
Jeff Brown6249b902012-05-26 14:32:54 -0700342 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700343 if (node->parent && node->graft_path == NULL) {
Daniel Rosenbergdb4638e2016-04-12 16:30:28 -0700344 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700345 if (pathlen < 0) {
346 return -1;
347 }
348 buf[pathlen++] = '/';
349 }
350
Jeff Brown6249b902012-05-26 14:32:54 -0700351 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
352 return pathlen + namelen;
353}
354
355/* Finds the absolute path of a file within a given directory.
356 * Performs a case-insensitive search for the file and sets the buffer to the path
357 * of the first matching file. If 'search' is zero or if no match is found, sets
358 * the buffer to the path that the file would have, assuming the name were case-sensitive.
359 *
360 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
361 * or returns NULL if the path is too long for the provided buffer.
362 */
363static char* find_file_within(const char* path, const char* name,
364 char* buf, size_t bufsize, int search)
365{
366 size_t pathlen = strlen(path);
367 size_t namelen = strlen(name);
368 size_t childlen = pathlen + namelen + 1;
369 char* actual;
370
371 if (bufsize <= childlen) {
372 return NULL;
373 }
374
375 memcpy(buf, path, pathlen);
376 buf[pathlen] = '/';
377 actual = buf + pathlen + 1;
378 memcpy(actual, name, namelen + 1);
379
380 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800381 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700382 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800383 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700384 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700385 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800386 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800387 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700388 if (!strcasecmp(entry->d_name, name)) {
389 /* we have a match - replace the name, don't need to copy the null again */
390 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800391 break;
392 }
393 }
394 closedir(dir);
395 }
Jeff Brown6249b902012-05-26 14:32:54 -0700396 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800397}
398
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700399static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
400 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000401 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700402 attr->size = s->st_size;
403 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800404 attr->atime = s->st_atim.tv_sec;
405 attr->mtime = s->st_mtim.tv_sec;
406 attr->ctime = s->st_ctim.tv_sec;
407 attr->atimensec = s->st_atim.tv_nsec;
408 attr->mtimensec = s->st_mtim.tv_nsec;
409 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700410 attr->mode = s->st_mode;
411 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700412
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700413 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700414
415 if (fuse->gid == AID_SDCARD_RW) {
416 /* As an optimization, certain trusted system components only run
417 * as owner but operate across all users. Since we're now handing
418 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
419 * the user boundary enforcement for the default view. The UIDs
420 * assigned to app directories are still multiuser aware. */
421 attr->gid = AID_SDCARD_RW;
422 } else {
423 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
424 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700425
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700426 int visible_mode = 0775 & ~fuse->mask;
427 if (node->perm == PERM_PRE_ROOT) {
428 /* Top of multi-user view should always be visible to ensure
429 * secondary users can traverse inside. */
430 visible_mode = 0711;
431 } else if (node->under_android) {
432 /* Block "other" access to Android directories, since only apps
433 * belonging to a specific user should be in there; we still
434 * leave +x open for the default view. */
435 if (fuse->gid == AID_SDCARD_RW) {
436 visible_mode = visible_mode & ~0006;
437 } else {
438 visible_mode = visible_mode & ~0007;
439 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700440 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700441 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700442 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700443 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
444}
445
Jeff Sharkey44d63422013-09-12 09:44:48 -0700446static int touch(char* path, mode_t mode) {
447 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
448 if (fd == -1) {
449 if (errno == EEXIST) {
450 return 0;
451 } else {
452 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
453 return -1;
454 }
455 }
456 close(fd);
457 return 0;
458}
459
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700460static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
461 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700462 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700463
464 /* By default, each node inherits from its parent */
465 node->perm = PERM_INHERIT;
466 node->userid = parent->userid;
467 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700468 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700469
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700470 /* Derive custom permissions based on parent and current node */
471 switch (parent->perm) {
472 case PERM_INHERIT:
473 /* Already inherited above */
474 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700475 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700476 /* Legacy internal layout places users at top level */
477 node->perm = PERM_ROOT;
478 node->userid = strtoul(node->name, NULL, 10);
479 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700480 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700481 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700482 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700483 /* App-specific directories inside; let anyone traverse */
484 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700485 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700486 }
487 break;
488 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700489 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700490 /* App-specific directories inside; let anyone traverse */
491 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700492 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700493 /* App-specific directories inside; let anyone traverse */
494 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700495 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700496 node->graft_path = fuse->global->obb_path;
497 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700498 } else if (!strcasecmp(node->name, "media")) {
499 /* App-specific directories inside; let anyone traverse */
500 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700501 }
502 break;
503 case PERM_ANDROID_DATA:
504 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700505 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700506 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700507 if (appid != 0) {
508 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700509 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700510 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700511 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700512}
513
Jeff Sharkeyfe764612015-12-14 11:02:01 -0700514static void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
515 struct node *node;
516 for (node = parent->child; node; node = node->next) {
517 derive_permissions_locked(fuse, parent, node);
518 if (node->child) {
519 derive_permissions_recursive_locked(fuse, node);
520 }
521 }
522}
523
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700524/* Kernel has already enforced everything we returned through
525 * derive_permissions_locked(), so this is used to lock down access
526 * even further, such as enforcing that apps hold sdcard_rw. */
527static bool check_caller_access_to_name(struct fuse* fuse,
528 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700529 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700530 /* Always block security-sensitive files at root */
531 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700532 if (!strcasecmp(name, "autorun.inf")
533 || !strcasecmp(name, ".android_secure")
534 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700535 return false;
536 }
537 }
538
Jeff Sharkey44d63422013-09-12 09:44:48 -0700539 /* Root always has access; access for any other UIDs should always
540 * be controlled through packages.list. */
541 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700542 return true;
543 }
544
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700545 /* No extra permissions to enforce */
546 return true;
547}
548
549static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700550 const struct fuse_in_header *hdr, const struct node* node, int mode) {
551 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700552}
553
Jeff Brown6249b902012-05-26 14:32:54 -0700554struct node *create_node_locked(struct fuse* fuse,
555 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700556{
557 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700558 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700559
Narayan Kamathfaa09352015-01-13 18:21:10 +0000560 // Detect overflows in the inode counter. "4 billion nodes should be enough
561 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700562 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000563 ERROR("No more inode numbers available");
564 return NULL;
565 }
566
Paul Eastham11ccdb32010-10-14 11:04:26 -0700567 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700568 if (!node) {
569 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700570 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700571 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700572 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700573 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700574 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700575 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700576 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700577 if (strcmp(name, actual_name)) {
578 node->actual_name = malloc(namelen + 1);
579 if (!node->actual_name) {
580 free(node->name);
581 free(node);
582 return NULL;
583 }
584 memcpy(node->actual_name, actual_name, namelen + 1);
585 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700586 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700587 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700588 node->ino = fuse->global->inode_ctr++;
589 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700590
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200591 node->deleted = false;
592
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700593 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700594 acquire_node_locked(node);
595 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700596 return node;
597}
598
Jeff Brown6249b902012-05-26 14:32:54 -0700599static int rename_node_locked(struct node *node, const char *name,
600 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700601{
Jeff Brown6249b902012-05-26 14:32:54 -0700602 size_t namelen = strlen(name);
603 int need_actual_name = strcmp(name, actual_name);
604
605 /* make the storage bigger without actually changing the name
606 * in case an error occurs part way */
607 if (namelen > node->namelen) {
608 char* new_name = realloc(node->name, namelen + 1);
609 if (!new_name) {
610 return -ENOMEM;
611 }
612 node->name = new_name;
613 if (need_actual_name && node->actual_name) {
614 char* new_actual_name = realloc(node->actual_name, namelen + 1);
615 if (!new_actual_name) {
616 return -ENOMEM;
617 }
618 node->actual_name = new_actual_name;
619 }
620 }
621
622 /* update the name, taking care to allocate storage before overwriting the old name */
623 if (need_actual_name) {
624 if (!node->actual_name) {
625 node->actual_name = malloc(namelen + 1);
626 if (!node->actual_name) {
627 return -ENOMEM;
628 }
629 }
630 memcpy(node->actual_name, actual_name, namelen + 1);
631 } else {
632 free(node->actual_name);
633 node->actual_name = NULL;
634 }
635 memcpy(node->name, name, namelen + 1);
636 node->namelen = namelen;
637 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700638}
639
Jeff Brown6249b902012-05-26 14:32:54 -0700640static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700641{
Jeff Brown6249b902012-05-26 14:32:54 -0700642 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700643 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700644 } else {
645 return id_to_ptr(nid);
646 }
647}
648
Jeff Brown6249b902012-05-26 14:32:54 -0700649static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
650 char* buf, size_t bufsize)
651{
652 struct node* node = lookup_node_by_id_locked(fuse, nid);
653 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
654 node = NULL;
655 }
656 return node;
657}
658
659static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700660{
661 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700662 /* use exact string comparison, nodes that differ by case
663 * must be considered distinct even if they refer to the same
664 * underlying file as otherwise operations such as "mv x x"
665 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200666 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700667 return node;
668 }
669 }
670 return 0;
671}
672
Jeff Brown6249b902012-05-26 14:32:54 -0700673static struct node* acquire_or_create_child_locked(
674 struct fuse* fuse, struct node* parent,
675 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700676{
Jeff Brown6249b902012-05-26 14:32:54 -0700677 struct node* child = lookup_child_by_name_locked(parent, name);
678 if (child) {
679 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800680 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700681 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800682 }
Jeff Brown6249b902012-05-26 14:32:54 -0700683 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700684}
685
Jeff Brown6249b902012-05-26 14:32:54 -0700686static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700687{
688 struct fuse_out_header hdr;
689 hdr.len = sizeof(hdr);
690 hdr.error = err;
691 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700692 write(fuse->fd, &hdr, sizeof(hdr));
693}
694
Jeff Brown6249b902012-05-26 14:32:54 -0700695static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700696{
697 struct fuse_out_header hdr;
698 struct iovec vec[2];
699 int res;
700
701 hdr.len = len + sizeof(hdr);
702 hdr.error = 0;
703 hdr.unique = unique;
704
705 vec[0].iov_base = &hdr;
706 vec[0].iov_len = sizeof(hdr);
707 vec[1].iov_base = data;
708 vec[1].iov_len = len;
709
710 res = writev(fuse->fd, vec, 2);
711 if (res < 0) {
712 ERROR("*** REPLY FAILED *** %d\n", errno);
713 }
714}
715
Jeff Brown6249b902012-05-26 14:32:54 -0700716static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
717 struct node* parent, const char* name, const char* actual_name,
718 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700719{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700720 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700721 struct fuse_entry_out out;
722 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700723
Jeff Brown6249b902012-05-26 14:32:54 -0700724 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700725 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700726 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700727
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700728 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700729 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
730 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700731 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700732 return -ENOMEM;
733 }
734 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700735 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700736 out.attr_valid = 10;
737 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700738 out.nodeid = node->nid;
739 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700740 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700741 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700742 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700743}
744
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700745static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700746 const char* path)
747{
748 struct fuse_attr_out out;
749 struct stat s;
750
751 if (lstat(path, &s) < 0) {
752 return -errno;
753 }
754 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700755 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700756 out.attr_valid = 10;
757 fuse_reply(fuse, unique, &out, sizeof(out));
758 return NO_STATUS;
759}
760
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700761static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
762 const __u64 child, const char* name) {
763 struct fuse_out_header hdr;
764 struct fuse_notify_delete_out data;
765 struct iovec vec[3];
766 size_t namelen = strlen(name);
767 int res;
768
769 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
770 hdr.error = FUSE_NOTIFY_DELETE;
771 hdr.unique = 0;
772
773 data.parent = parent;
774 data.child = child;
775 data.namelen = namelen;
776 data.padding = 0;
777
778 vec[0].iov_base = &hdr;
779 vec[0].iov_len = sizeof(hdr);
780 vec[1].iov_base = &data;
781 vec[1].iov_len = sizeof(data);
782 vec[2].iov_base = (void*) name;
783 vec[2].iov_len = namelen + 1;
784
785 res = writev(fuse->fd, vec, 3);
786 /* Ignore ENOENT, since other views may not have seen the entry */
787 if (res < 0 && errno != ENOENT) {
788 ERROR("*** NOTIFY FAILED *** %d\n", errno);
789 }
790}
791
Jeff Brown6249b902012-05-26 14:32:54 -0700792static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700793 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700794{
Jeff Brown6249b902012-05-26 14:32:54 -0700795 struct node* parent_node;
796 char parent_path[PATH_MAX];
797 char child_path[PATH_MAX];
798 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700799
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700800 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700801 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
802 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100803 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700804 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700805 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700806
807 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
808 child_path, sizeof(child_path), 1))) {
809 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700810 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700811 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700812 return -EACCES;
813 }
814
Jeff Brown6249b902012-05-26 14:32:54 -0700815 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700816}
817
Jeff Brown6249b902012-05-26 14:32:54 -0700818static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700819 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
820{
Jeff Brown6249b902012-05-26 14:32:54 -0700821 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700822
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700823 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700824 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100825 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700826 hdr->nodeid, node ? node->name : "?");
827 if (node) {
828 __u64 n = req->nlookup;
829 while (n--) {
830 release_node_locked(node);
831 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700832 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700833 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700834 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700835}
836
Jeff Brown6249b902012-05-26 14:32:54 -0700837static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700838 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
839{
Jeff Brown6249b902012-05-26 14:32:54 -0700840 struct node* node;
841 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700842
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700843 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700844 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100845 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700846 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700847 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700848
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700849 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700850 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700851 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700852 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700853 return -EACCES;
854 }
855
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700856 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700857}
858
Jeff Brown6249b902012-05-26 14:32:54 -0700859static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860 const struct fuse_in_header *hdr, const struct fuse_setattr_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 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700866 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700867 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100868 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700869 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700870 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700871
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700872 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700873 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700874 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800875
876 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700877 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700878 return -EACCES;
879 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880
Jeff Brown6249b902012-05-26 14:32:54 -0700881 /* XXX: incomplete implementation on purpose.
882 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883
Elliott Hughes853574d2014-07-31 12:03:03 -0700884 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700885 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886 }
887
888 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
889 * are both set, then set it to the current time. Else, set it to the
890 * time specified in the request. Same goes for mtime. Use utimensat(2)
891 * as it allows ATIME and MTIME to be changed independently, and has
892 * nanosecond resolution which fuse also has.
893 */
894 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
895 times[0].tv_nsec = UTIME_OMIT;
896 times[1].tv_nsec = UTIME_OMIT;
897 if (req->valid & FATTR_ATIME) {
898 if (req->valid & FATTR_ATIME_NOW) {
899 times[0].tv_nsec = UTIME_NOW;
900 } else {
901 times[0].tv_sec = req->atime;
902 times[0].tv_nsec = req->atimensec;
903 }
904 }
905 if (req->valid & FATTR_MTIME) {
906 if (req->valid & FATTR_MTIME_NOW) {
907 times[1].tv_nsec = UTIME_NOW;
908 } else {
909 times[1].tv_sec = req->mtime;
910 times[1].tv_nsec = req->mtimensec;
911 }
912 }
Jeff Brown6249b902012-05-26 14:32:54 -0700913 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
914 handler->token, path, times[0].tv_sec, times[1].tv_sec);
915 if (utimensat(-1, path, times, 0) < 0) {
916 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700917 }
918 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700919 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700920}
921
Jeff Brown6249b902012-05-26 14:32:54 -0700922static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700923 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
924{
Jeff Brown6249b902012-05-26 14:32:54 -0700925 struct node* parent_node;
926 char parent_path[PATH_MAX];
927 char child_path[PATH_MAX];
928 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700929
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700930 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700931 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
932 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100933 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700934 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700935 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700936
937 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
938 child_path, sizeof(child_path), 1))) {
939 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700940 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700941 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700942 return -EACCES;
943 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700944 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700945 if (mknod(child_path, mode, req->rdev) < 0) {
946 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700947 }
Jeff Brown6249b902012-05-26 14:32:54 -0700948 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700949}
950
Jeff Brown6249b902012-05-26 14:32:54 -0700951static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
953{
Jeff Brown6249b902012-05-26 14:32:54 -0700954 struct node* parent_node;
955 char parent_path[PATH_MAX];
956 char child_path[PATH_MAX];
957 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700958
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700959 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700960 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
961 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100962 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700963 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700964 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700965
966 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
967 child_path, sizeof(child_path), 1))) {
968 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700970 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700971 return -EACCES;
972 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700973 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700974 if (mkdir(child_path, mode) < 0) {
975 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700976 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700977
978 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
979 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
980 char nomedia[PATH_MAX];
981 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
982 if (touch(nomedia, 0664) != 0) {
983 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
984 return -ENOENT;
985 }
986 }
987 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
988 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700989 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700990 if (touch(nomedia, 0664) != 0) {
991 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
992 return -ENOENT;
993 }
994 }
995
Jeff Brown6249b902012-05-26 14:32:54 -0700996 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700997}
998
Jeff Brown6249b902012-05-26 14:32:54 -0700999static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001000 const struct fuse_in_header* hdr, const char* name)
1001{
Jeff Brown6249b902012-05-26 14:32:54 -07001002 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001003 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001004 char parent_path[PATH_MAX];
1005 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001006
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001007 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001008 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1009 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001010 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001011 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001012 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001013
1014 if (!parent_node || !find_file_within(parent_path, name,
1015 child_path, sizeof(child_path), 1)) {
1016 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001017 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001018 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001019 return -EACCES;
1020 }
Jeff Brown6249b902012-05-26 14:32:54 -07001021 if (unlink(child_path) < 0) {
1022 return -errno;
1023 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001024 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001025 child_node = lookup_child_by_name_locked(parent_node, name);
1026 if (child_node) {
1027 child_node->deleted = true;
1028 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001029 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001030 if (parent_node && child_node) {
1031 /* Tell all other views that node is gone */
1032 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1033 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1034 if (fuse != fuse->global->fuse_default) {
1035 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1036 }
1037 if (fuse != fuse->global->fuse_read) {
1038 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1039 }
1040 if (fuse != fuse->global->fuse_write) {
1041 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1042 }
1043 }
Jeff Brown6249b902012-05-26 14:32:54 -07001044 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001045}
1046
Jeff Brown6249b902012-05-26 14:32:54 -07001047static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001048 const struct fuse_in_header* hdr, const char* name)
1049{
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001050 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001051 struct node* parent_node;
1052 char parent_path[PATH_MAX];
1053 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001054
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001055 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001056 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1057 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001058 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001059 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001060 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001061
1062 if (!parent_node || !find_file_within(parent_path, name,
1063 child_path, sizeof(child_path), 1)) {
1064 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001065 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001066 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001067 return -EACCES;
1068 }
Jeff Brown6249b902012-05-26 14:32:54 -07001069 if (rmdir(child_path) < 0) {
1070 return -errno;
1071 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001072 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001073 child_node = lookup_child_by_name_locked(parent_node, name);
1074 if (child_node) {
1075 child_node->deleted = true;
1076 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001077 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001078 if (parent_node && child_node) {
1079 /* Tell all other views that node is gone */
1080 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1081 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1082 if (fuse != fuse->global->fuse_default) {
1083 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1084 }
1085 if (fuse != fuse->global->fuse_read) {
1086 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1087 }
1088 if (fuse != fuse->global->fuse_write) {
1089 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1090 }
1091 }
Jeff Brown6249b902012-05-26 14:32:54 -07001092 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001093}
1094
Jeff Brown6249b902012-05-26 14:32:54 -07001095static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001096 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001097 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001098{
Jeff Brown6249b902012-05-26 14:32:54 -07001099 struct node* old_parent_node;
1100 struct node* new_parent_node;
1101 struct node* child_node;
1102 char old_parent_path[PATH_MAX];
1103 char new_parent_path[PATH_MAX];
1104 char old_child_path[PATH_MAX];
1105 char new_child_path[PATH_MAX];
1106 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001108
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001109 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001110 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1111 old_parent_path, sizeof(old_parent_path));
1112 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1113 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001114 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001115 old_name, new_name,
1116 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1117 req->newdir, new_parent_node ? new_parent_node->name : "?");
1118 if (!old_parent_node || !new_parent_node) {
1119 res = -ENOENT;
1120 goto lookup_error;
1121 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001122 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001123 res = -EACCES;
1124 goto lookup_error;
1125 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001126 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001127 res = -EACCES;
1128 goto lookup_error;
1129 }
Jeff Brown6249b902012-05-26 14:32:54 -07001130 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1131 if (!child_node || get_node_path_locked(child_node,
1132 old_child_path, sizeof(old_child_path)) < 0) {
1133 res = -ENOENT;
1134 goto lookup_error;
1135 }
1136 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001137 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001138
1139 /* Special case for renaming a file where destination is same path
1140 * differing only by case. In this case we don't want to look for a case
1141 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1142 */
1143 int search = old_parent_node != new_parent_node
1144 || strcasecmp(old_name, new_name);
1145 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1146 new_child_path, sizeof(new_child_path), search))) {
1147 res = -ENOENT;
1148 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001149 }
1150
Jeff Brown6249b902012-05-26 14:32:54 -07001151 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1152 res = rename(old_child_path, new_child_path);
1153 if (res < 0) {
1154 res = -errno;
1155 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156 }
1157
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001158 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001159 res = rename_node_locked(child_node, new_name, new_actual_name);
1160 if (!res) {
1161 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -07001162 derive_permissions_locked(fuse, new_parent_node, child_node);
1163 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -07001164 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165 }
Jeff Brown6249b902012-05-26 14:32:54 -07001166 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001167
Jeff Brown6249b902012-05-26 14:32:54 -07001168io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001169 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001170done:
1171 release_node_locked(child_node);
1172lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001173 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001174 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001175}
1176
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001177static int open_flags_to_access_mode(int open_flags) {
1178 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1179 return R_OK;
1180 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1181 return W_OK;
1182 } else {
1183 /* Probably O_RDRW, but treat as default to be safe */
1184 return R_OK | W_OK;
1185 }
1186}
1187
Jeff Brown6249b902012-05-26 14:32:54 -07001188static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1190{
Jeff Brown6249b902012-05-26 14:32:54 -07001191 struct node* node;
1192 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001194 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001196 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001197 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001198 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001199 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001200 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001201
1202 if (!node) {
1203 return -ENOENT;
1204 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001205 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001206 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001207 return -EACCES;
1208 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209 h = malloc(sizeof(*h));
1210 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001211 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 }
Jeff Brown6249b902012-05-26 14:32:54 -07001213 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214 h->fd = open(path, req->flags);
1215 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001217 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001218 }
1219 out.fh = ptr_to_id(h);
1220 out.open_flags = 0;
1221 out.padding = 0;
1222 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001223 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224}
1225
Jeff Brown6249b902012-05-26 14:32:54 -07001226static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1228{
1229 struct handle *h = id_to_ptr(req->fh);
1230 __u64 unique = hdr->unique;
1231 __u32 size = req->size;
1232 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001233 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001234 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001235
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001236 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1237 * overlaps the request buffer and will clobber data in the request. This
1238 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001239
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001240 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1241 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001242 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001243 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001245 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001246 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001247 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001249 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001250 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001251}
1252
Jeff Brown6249b902012-05-26 14:32:54 -07001253static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001254 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1255 const void* buffer)
1256{
1257 struct fuse_write_out out;
1258 struct handle *h = id_to_ptr(req->fh);
1259 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001260 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001261
Arpad Horvath49e93442014-02-18 10:18:25 +01001262 if (req->flags & O_DIRECT) {
1263 memcpy(aligned_buffer, buffer, req->size);
1264 buffer = (const __u8*) aligned_buffer;
1265 }
Jeff Brown6249b902012-05-26 14:32:54 -07001266
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001267 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001268 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 res = pwrite64(h->fd, buffer, req->size, req->offset);
1270 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001271 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272 }
1273 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001274 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001276 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001277}
1278
Jeff Brown6249b902012-05-26 14:32:54 -07001279static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001280 const struct fuse_in_header* hdr)
1281{
Jeff Brown6249b902012-05-26 14:32:54 -07001282 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283 struct statfs stat;
1284 struct fuse_statfs_out out;
1285 int res;
1286
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001287 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001288 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001289 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001290 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001291 if (res < 0) {
1292 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001293 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001294 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001295 return -errno;
1296 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 memset(&out, 0, sizeof(out));
1298 out.st.blocks = stat.f_blocks;
1299 out.st.bfree = stat.f_bfree;
1300 out.st.bavail = stat.f_bavail;
1301 out.st.files = stat.f_files;
1302 out.st.ffree = stat.f_ffree;
1303 out.st.bsize = stat.f_bsize;
1304 out.st.namelen = stat.f_namelen;
1305 out.st.frsize = stat.f_frsize;
1306 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001307 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308}
1309
Jeff Brown6249b902012-05-26 14:32:54 -07001310static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001311 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1312{
1313 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001314
1315 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001316 close(h->fd);
1317 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001318 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001319}
1320
Jeff Brown6249b902012-05-26 14:32:54 -07001321static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001322 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1323{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001324 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1325 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001326
Elliott Hughesf6d67372014-07-08 14:38:26 -07001327 int fd = -1;
1328 if (is_dir) {
1329 struct dirhandle *dh = id_to_ptr(req->fh);
1330 fd = dirfd(dh->d);
1331 } else {
1332 struct handle *h = id_to_ptr(req->fh);
1333 fd = h->fd;
1334 }
1335
1336 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1337 is_dir ? "FSYNCDIR" : "FSYNC",
1338 id_to_ptr(req->fh), fd, is_data_sync);
1339 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1340 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001341 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001342 }
Jeff Brown6249b902012-05-26 14:32:54 -07001343 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344}
1345
Jeff Brown6249b902012-05-26 14:32:54 -07001346static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001347 const struct fuse_in_header* hdr)
1348{
Jeff Brown6249b902012-05-26 14:32:54 -07001349 TRACE("[%d] FLUSH\n", handler->token);
1350 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001351}
1352
Jeff Brown6249b902012-05-26 14:32:54 -07001353static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1355{
Jeff Brown6249b902012-05-26 14:32:54 -07001356 struct node* node;
1357 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001358 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001360
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001361 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001362 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001363 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001364 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001365 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001366
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001368 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001370 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001371 return -EACCES;
1372 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001373 h = malloc(sizeof(*h));
1374 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001375 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376 }
Jeff Brown6249b902012-05-26 14:32:54 -07001377 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001379 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001381 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001382 }
1383 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001384 out.open_flags = 0;
1385 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001386 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001387 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388}
1389
Jeff Brown6249b902012-05-26 14:32:54 -07001390static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001391 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1392{
1393 char buffer[8192];
1394 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1395 struct dirent *de;
1396 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001397
1398 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001399 if (req->offset == 0) {
1400 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001401 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001402 rewinddir(h->d);
1403 }
1404 de = readdir(h->d);
1405 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001406 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001407 }
1408 fde->ino = FUSE_UNKNOWN_INO;
1409 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1410 fde->off = req->offset + 1;
1411 fde->type = de->d_type;
1412 fde->namelen = strlen(de->d_name);
1413 memcpy(fde->name, de->d_name, fde->namelen + 1);
1414 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001415 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1416 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001417}
1418
Jeff Brown6249b902012-05-26 14:32:54 -07001419static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1421{
1422 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001423
1424 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425 closedir(h->d);
1426 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001427 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001428}
1429
Jeff Brown6249b902012-05-26 14:32:54 -07001430static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001431 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1432{
1433 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001434 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001435
Jeff Brown6249b902012-05-26 14:32:54 -07001436 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1437 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001438
1439 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1440 * defined (fuse version 7.6). The structure is the same from 7.6 through
1441 * 7.22. Beginning with 7.23, the structure increased in size and added
1442 * new parameters.
1443 */
1444 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1445 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1446 req->major, req->minor, FUSE_KERNEL_VERSION);
1447 return -1;
1448 }
1449
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001450 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1451 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001452 fuse_struct_size = sizeof(out);
1453#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1454 /* FUSE_KERNEL_VERSION >= 23. */
1455
1456 /* If the kernel only works on minor revs older than or equal to 22,
1457 * then use the older structure size since this code only uses the 7.22
1458 * version of the structure. */
1459 if (req->minor <= 22) {
1460 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1461 }
1462#endif
1463
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001464 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001465 out.max_readahead = req->max_readahead;
1466 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1467 out.max_background = 32;
1468 out.congestion_threshold = 32;
1469 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001470 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001471 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001472}
1473
Jeff Brown6249b902012-05-26 14:32:54 -07001474static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001475 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1476{
Brian Swetland03ee9472010-08-12 18:01:08 -07001477 switch (hdr->opcode) {
1478 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001479 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001480 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001481 }
Jeff Brown84715842012-05-25 14:07:47 -07001482
Brian Swetland03ee9472010-08-12 18:01:08 -07001483 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001484 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001485 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001486 }
Jeff Brown84715842012-05-25 14:07:47 -07001487
Brian Swetland03ee9472010-08-12 18:01:08 -07001488 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001489 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001490 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001491 }
Jeff Brown84715842012-05-25 14:07:47 -07001492
Brian Swetland03ee9472010-08-12 18:01:08 -07001493 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001494 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001495 return handle_setattr(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_READLINK:
1499// case FUSE_SYMLINK:
1500 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001501 const struct fuse_mknod_in *req = data;
1502 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001503 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001504 }
Jeff Brown84715842012-05-25 14:07:47 -07001505
Brian Swetland03ee9472010-08-12 18:01:08 -07001506 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001507 const struct fuse_mkdir_in *req = data;
1508 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001509 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001510 }
Jeff Brown84715842012-05-25 14:07:47 -07001511
Brian Swetland03ee9472010-08-12 18:01:08 -07001512 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001513 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001514 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001515 }
Jeff Brown84715842012-05-25 14:07:47 -07001516
Brian Swetland03ee9472010-08-12 18:01:08 -07001517 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001518 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001519 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001520 }
Jeff Brown84715842012-05-25 14:07:47 -07001521
Brian Swetland03ee9472010-08-12 18:01:08 -07001522 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001523 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001524 const char *old_name = ((const char*) data) + sizeof(*req);
1525 const char *new_name = old_name + strlen(old_name) + 1;
1526 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001527 }
Jeff Brown84715842012-05-25 14:07:47 -07001528
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001529// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001530 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001531 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001532 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001533 }
Jeff Brown84715842012-05-25 14:07:47 -07001534
Brian Swetland03ee9472010-08-12 18:01:08 -07001535 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001536 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001537 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001538 }
Jeff Brown84715842012-05-25 14:07:47 -07001539
Brian Swetland03ee9472010-08-12 18:01:08 -07001540 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001541 const struct fuse_write_in *req = data;
1542 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001543 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001544 }
Jeff Brown84715842012-05-25 14:07:47 -07001545
Mike Lockwood4553b082010-08-16 14:14:44 -04001546 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001547 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001548 }
Jeff Brown84715842012-05-25 14:07:47 -07001549
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001551 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001552 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 }
Jeff Brown84715842012-05-25 14:07:47 -07001554
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001555 case FUSE_FSYNC:
1556 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001557 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001558 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001559 }
1560
Brian Swetland03ee9472010-08-12 18:01:08 -07001561// case FUSE_SETXATTR:
1562// case FUSE_GETXATTR:
1563// case FUSE_LISTXATTR:
1564// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001565 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001566 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001567 }
1568
Brian Swetland03ee9472010-08-12 18:01:08 -07001569 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001570 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001571 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001572 }
Jeff Brown84715842012-05-25 14:07:47 -07001573
Brian Swetland03ee9472010-08-12 18:01:08 -07001574 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001575 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001576 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001577 }
Jeff Brown84715842012-05-25 14:07:47 -07001578
Brian Swetland03ee9472010-08-12 18:01:08 -07001579 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001580 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001581 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001582 }
Jeff Brown84715842012-05-25 14:07:47 -07001583
Brian Swetland03ee9472010-08-12 18:01:08 -07001584 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001585 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001586 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001587 }
Jeff Brown84715842012-05-25 14:07:47 -07001588
Brian Swetland03ee9472010-08-12 18:01:08 -07001589 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001590 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001591 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1592 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001593 }
Jeff Brown84715842012-05-25 14:07:47 -07001594 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001595}
1596
Jeff Brown6249b902012-05-26 14:32:54 -07001597static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001598{
Jeff Brown6249b902012-05-26 14:32:54 -07001599 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001600 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001601 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1602 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001603 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001604 if (errno == ENODEV) {
1605 ERROR("[%d] someone stole our marbles!\n", handler->token);
1606 exit(2);
1607 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001608 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001609 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001610 }
Jeff Brown84715842012-05-25 14:07:47 -07001611
1612 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001613 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1614 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001615 }
1616
Jeff Brown7729d242012-05-25 15:35:28 -07001617 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001618 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001619 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1620 handler->token, (size_t)len, hdr->len);
1621 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001622 }
1623
Jeff Brown7729d242012-05-25 15:35:28 -07001624 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001625 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001626 __u64 unique = hdr->unique;
1627 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001628
1629 /* We do not access the request again after this point because the underlying
1630 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001631
1632 if (res != NO_STATUS) {
1633 if (res) {
1634 TRACE("[%d] ERROR %d\n", handler->token, res);
1635 }
1636 fuse_status(fuse, unique, res);
1637 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001638 }
1639}
1640
Jeff Brown6249b902012-05-26 14:32:54 -07001641static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001642{
Jeff Brown6249b902012-05-26 14:32:54 -07001643 struct fuse_handler* handler = data;
1644 handle_fuse_requests(handler);
1645 return NULL;
1646}
1647
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001648static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001649 Hashmap* map = context;
1650 hashmapRemove(map, key);
1651 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001652 return true;
1653}
1654
William Robertse5099802015-07-31 13:11:19 -07001655static bool package_parse_callback(pkg_info *info, void *userdata) {
1656 struct fuse_global *global = (struct fuse_global *)userdata;
1657
1658 char* name = strdup(info->name);
1659 hashmapPut(global->package_to_appid, name, (void*) (uintptr_t) info->uid);
1660 packagelist_free(info);
1661 return true;
1662}
1663
1664static bool read_package_list(struct fuse_global* global) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001665 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001666
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001667 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001668
William Robertse5099802015-07-31 13:11:19 -07001669 bool rc = packagelist_parse(package_parse_callback, global);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001670 TRACE("read_package_list: found %zu packages\n",
1671 hashmapSize(global->package_to_appid));
William Robertse5099802015-07-31 13:11:19 -07001672
Jeff Sharkeyfe764612015-12-14 11:02:01 -07001673 /* Regenerate ownership details using newly loaded mapping */
1674 derive_permissions_recursive_locked(global->fuse_default, &global->root);
1675
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001676 pthread_mutex_unlock(&global->lock);
William Robertse5099802015-07-31 13:11:19 -07001677
1678 return rc;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001679}
1680
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001681static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001682 struct inotify_event *event;
1683 char event_buf[512];
1684
1685 int nfd = inotify_init();
1686 if (nfd < 0) {
1687 ERROR("inotify_init failed: %s\n", strerror(errno));
1688 return;
1689 }
1690
1691 bool active = false;
1692 while (1) {
1693 if (!active) {
William Robertse5099802015-07-31 13:11:19 -07001694 int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001695 if (res == -1) {
1696 if (errno == ENOENT || errno == EACCES) {
1697 /* Framework may not have created yet, sleep and retry */
William Robertse5099802015-07-31 13:11:19 -07001698 ERROR("missing \"%s\"; retrying\n", PACKAGES_LIST_FILE);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001699 sleep(3);
1700 continue;
1701 } else {
1702 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1703 return;
1704 }
1705 }
1706
1707 /* Watch above will tell us about any future changes, so
1708 * read the current state. */
William Robertse5099802015-07-31 13:11:19 -07001709 if (read_package_list(global) == false) {
1710 ERROR("read_package_list failed\n");
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001711 return;
1712 }
1713 active = true;
1714 }
1715
1716 int event_pos = 0;
1717 int res = read(nfd, event_buf, sizeof(event_buf));
1718 if (res < (int) sizeof(*event)) {
1719 if (errno == EINTR)
1720 continue;
1721 ERROR("failed to read inotify event: %s\n", strerror(errno));
1722 return;
1723 }
1724
1725 while (res >= (int) sizeof(*event)) {
1726 int event_size;
1727 event = (struct inotify_event *) (event_buf + event_pos);
1728
1729 TRACE("inotify event: %08x\n", event->mask);
1730 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1731 /* Previously watched file was deleted, probably due to move
1732 * that swapped in new data; re-arm the watch and read. */
1733 active = false;
1734 }
1735
1736 event_size = sizeof(*event) + event->len;
1737 res -= event_size;
1738 event_pos += event_size;
1739 }
1740 }
1741}
1742
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001743static int usage() {
1744 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001745 " -u: specify UID to run as\n"
1746 " -g: specify GID to run as\n"
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001747 " -U: specify user ID that owns device\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001748 " -m: source_path is multi-user\n"
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001749 " -w: runtime write mount has full write access\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001750 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001751 return 1;
1752}
1753
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001754static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001755 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001756
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001757 fuse->fd = open("/dev/fuse", O_RDWR);
1758 if (fuse->fd == -1) {
1759 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001760 return -1;
1761 }
1762
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001763 umount2(fuse->dest_path, MNT_DETACH);
1764
Jeff Brown26567352012-05-25 13:27:43 -07001765 snprintf(opts, sizeof(opts),
1766 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001767 fuse->fd, fuse->global->uid, fuse->global->gid);
1768 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1769 MS_NOATIME, opts) != 0) {
1770 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1771 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001772 }
1773
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001774 fuse->gid = gid;
1775 fuse->mask = mask;
1776
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001777 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001778}
1779
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001780static void run(const char* source_path, const char* label, uid_t uid,
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001781 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001782 struct fuse_global global;
1783 struct fuse fuse_default;
1784 struct fuse fuse_read;
1785 struct fuse fuse_write;
1786 struct fuse_handler handler_default;
1787 struct fuse_handler handler_read;
1788 struct fuse_handler handler_write;
1789 pthread_t thread_default;
1790 pthread_t thread_read;
1791 pthread_t thread_write;
1792
1793 memset(&global, 0, sizeof(global));
1794 memset(&fuse_default, 0, sizeof(fuse_default));
1795 memset(&fuse_read, 0, sizeof(fuse_read));
1796 memset(&fuse_write, 0, sizeof(fuse_write));
1797 memset(&handler_default, 0, sizeof(handler_default));
1798 memset(&handler_read, 0, sizeof(handler_read));
1799 memset(&handler_write, 0, sizeof(handler_write));
1800
1801 pthread_mutex_init(&global.lock, NULL);
1802 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1803 global.uid = uid;
1804 global.gid = gid;
1805 global.multi_user = multi_user;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001806 global.next_generation = 0;
1807 global.inode_ctr = 1;
1808
1809 memset(&global.root, 0, sizeof(global.root));
1810 global.root.nid = FUSE_ROOT_ID; /* 1 */
1811 global.root.refcount = 2;
1812 global.root.namelen = strlen(source_path);
1813 global.root.name = strdup(source_path);
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001814 global.root.userid = userid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001815 global.root.uid = AID_ROOT;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001816 global.root.under_android = false;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001817
1818 strcpy(global.source_path, source_path);
1819
1820 if (multi_user) {
1821 global.root.perm = PERM_PRE_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001822 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
1823 } else {
1824 global.root.perm = PERM_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001825 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
1826 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001827
1828 fuse_default.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001829 fuse_read.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001830 fuse_write.global = &global;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001831
1832 global.fuse_default = &fuse_default;
1833 global.fuse_read = &fuse_read;
1834 global.fuse_write = &fuse_write;
1835
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001836 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label);
1837 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label);
1838 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001839
1840 handler_default.fuse = &fuse_default;
1841 handler_read.fuse = &fuse_read;
1842 handler_write.fuse = &fuse_write;
1843
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001844 handler_default.token = 0;
1845 handler_read.token = 1;
1846 handler_write.token = 2;
1847
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001848 umask(0);
1849
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001850 if (multi_user) {
1851 /* Multi-user storage is fully isolated per user, so "other"
1852 * permissions are completely masked off. */
1853 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1854 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1855 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1856 ERROR("failed to fuse_setup\n");
1857 exit(1);
1858 }
1859 } else {
1860 /* Physical storage is readable by all users on device, but
1861 * the Android directories are masked off to a single user
1862 * deep inside attr_from_stat(). */
1863 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1864 || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022)
1865 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) {
1866 ERROR("failed to fuse_setup\n");
1867 exit(1);
1868 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001869 }
1870
1871 /* Drop privs */
1872 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1873 ERROR("cannot setgroups: %s\n", strerror(errno));
1874 exit(1);
1875 }
1876 if (setgid(gid) < 0) {
1877 ERROR("cannot setgid: %s\n", strerror(errno));
1878 exit(1);
1879 }
1880 if (setuid(uid) < 0) {
1881 ERROR("cannot setuid: %s\n", strerror(errno));
1882 exit(1);
1883 }
1884
1885 if (multi_user) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001886 fs_prepare_dir(global.obb_path, 0775, uid, gid);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001887 }
1888
1889 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1890 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1891 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1892 ERROR("failed to pthread_create\n");
1893 exit(1);
1894 }
1895
1896 watch_package_list(&global);
1897 ERROR("terminated prematurely\n");
1898 exit(1);
1899}
1900
Daniel Rosenberg3aa261c2016-03-31 22:00:47 +00001901static int sdcardfs_setup(const char *source_path, const char *dest_path, uid_t fsuid,
1902 gid_t fsgid, bool multi_user, userid_t userid, gid_t gid, mode_t mask) {
1903 char opts[256];
1904
1905 snprintf(opts, sizeof(opts),
1906 "fsuid=%d,fsgid=%d,%smask=%d,userid=%d,gid=%d",
1907 fsuid, fsgid, multi_user?"multiuser,":"", mask, userid, gid);
1908
1909 if (mount(source_path, dest_path, "sdcardfs",
1910 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts) != 0) {
1911 ERROR("failed to mount sdcardfs filesystem: %s\n", strerror(errno));
1912 return -1;
1913 }
1914
1915 return 0;
1916}
1917
1918static void run_sdcardfs(const char* source_path, const char* label, uid_t uid,
1919 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
1920 char dest_path_default[PATH_MAX];
1921 char dest_path_read[PATH_MAX];
1922 char dest_path_write[PATH_MAX];
1923 char obb_path[PATH_MAX];
1924 snprintf(dest_path_default, PATH_MAX, "/mnt/runtime/default/%s", label);
1925 snprintf(dest_path_read, PATH_MAX, "/mnt/runtime/read/%s", label);
1926 snprintf(dest_path_write, PATH_MAX, "/mnt/runtime/write/%s", label);
1927
1928 umask(0);
1929 if (multi_user) {
1930 /* Multi-user storage is fully isolated per user, so "other"
1931 * permissions are completely masked off. */
1932 if (sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
1933 AID_SDCARD_RW, 0006)
1934 || sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid,
1935 AID_EVERYBODY, 0027)
1936 || sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid,
1937 AID_EVERYBODY, full_write ? 0007 : 0027)) {
1938 ERROR("failed to fuse_setup\n");
1939 exit(1);
1940 }
1941 } else {
1942 /* Physical storage is readable by all users on device, but
1943 * the Android directories are masked off to a single user
1944 * deep inside attr_from_stat(). */
1945 if (sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
1946 AID_SDCARD_RW, 0006)
1947 || sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid,
1948 AID_EVERYBODY, full_write ? 0027 : 0022)
1949 || sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid,
1950 AID_EVERYBODY, full_write ? 0007 : 0022)) {
1951 ERROR("failed to fuse_setup\n");
1952 exit(1);
1953 }
1954 }
1955
1956 /* Drop privs */
1957 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1958 ERROR("cannot setgroups: %s\n", strerror(errno));
1959 exit(1);
1960 }
1961 if (setgid(gid) < 0) {
1962 ERROR("cannot setgid: %s\n", strerror(errno));
1963 exit(1);
1964 }
1965 if (setuid(uid) < 0) {
1966 ERROR("cannot setuid: %s\n", strerror(errno));
1967 exit(1);
1968 }
1969
1970 if (multi_user) {
1971 snprintf(obb_path, sizeof(obb_path), "%s/obb", source_path);
1972 fs_prepare_dir(&obb_path[0], 0775, uid, gid);
1973 }
1974
1975 exit(0);
1976}
1977
1978static bool supports_sdcardfs(void) {
1979 FILE *fp;
1980 char *buf = NULL;
1981 size_t buflen = 0;
1982
1983 fp = fopen("/proc/filesystems", "r");
1984 if (!fp) {
1985 ERROR("Could not read /proc/filesystems, error: %s\n", strerror(errno));
1986 return false;
1987 }
1988 while ((getline(&buf, &buflen, fp)) > 0) {
1989 if (strstr(buf, "sdcardfs\n")) {
1990 free(buf);
1991 fclose(fp);
1992 return true;
1993 }
1994 }
1995 free(buf);
1996 fclose(fp);
1997 return false;
1998}
1999
Jeff Sharkey20ca9832016-04-07 11:05:21 -06002000static bool should_use_sdcardfs(void) {
2001 char property[PROPERTY_VALUE_MAX];
2002
2003 // Allow user to have a strong opinion about state
2004 property_get(PROP_SDCARDFS_USER, property, "");
2005 if (!strcmp(property, "force_on")) {
2006 ALOGW("User explicitly enabled sdcardfs");
2007 return supports_sdcardfs();
2008 } else if (!strcmp(property, "force_off")) {
2009 ALOGW("User explicitly disabled sdcardfs");
2010 return false;
2011 }
2012
2013 // Fall back to device opinion about state
2014 if (property_get_bool(PROP_SDCARDFS_DEVICE, false)) {
2015 ALOGW("Device explicitly enabled sdcardfs");
2016 return supports_sdcardfs();
2017 } else {
2018 ALOGW("Device explicitly disabled sdcardfs");
2019 return false;
2020 }
2021}
2022
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002023int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002024 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002025 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07002026 uid_t uid = 0;
2027 gid_t gid = 0;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002028 userid_t userid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002029 bool multi_user = false;
2030 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05002031 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08002032 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07002033 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07002034
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002035 int opt;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002036 while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002037 switch (opt) {
2038 case 'u':
2039 uid = strtoul(optarg, NULL, 10);
2040 break;
2041 case 'g':
2042 gid = strtoul(optarg, NULL, 10);
2043 break;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002044 case 'U':
2045 userid = strtoul(optarg, NULL, 10);
2046 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002047 case 'm':
2048 multi_user = true;
2049 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07002050 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002051 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002052 break;
2053 case '?':
2054 default:
2055 return usage();
2056 }
2057 }
2058
2059 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05002060 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002061 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002062 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002063 } else if (!label) {
2064 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07002065 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08002066 ERROR("too many arguments\n");
2067 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05002068 }
Brian Swetland03ee9472010-08-12 18:01:08 -07002069 }
2070
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002071 if (!source_path) {
2072 ERROR("no source path specified\n");
2073 return usage();
2074 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002075 if (!label) {
2076 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05002077 return usage();
2078 }
Jeff Brown26567352012-05-25 13:27:43 -07002079 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07002080 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05002081 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07002082 }
2083
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08002084 rlim.rlim_cur = 8192;
2085 rlim.rlim_max = 8192;
2086 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
2087 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
2088 }
2089
Nick Kralevich8d28fa72014-07-24 17:05:59 -07002090 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
2091 ERROR("installd fs upgrade not yet complete. Waiting...\n");
2092 sleep(1);
2093 }
2094
Jeff Sharkey20ca9832016-04-07 11:05:21 -06002095 if (should_use_sdcardfs()) {
Daniel Rosenberg3aa261c2016-03-31 22:00:47 +00002096 run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write);
2097 } else {
2098 run(source_path, label, uid, gid, userid, multi_user, full_write);
2099 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002100 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07002101}