blob: e2e8ed0c4def4eef1c2ca109542220b69a57665e [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;
Daniel Micaydf9c4a02016-04-26 11:42:08 -0400829 while (n) {
830 n--;
Jeff Brown6249b902012-05-26 14:32:54 -0700831 release_node_locked(node);
832 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700833 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700834 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700835 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836}
837
Jeff Brown6249b902012-05-26 14:32:54 -0700838static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
840{
Jeff Brown6249b902012-05-26 14:32:54 -0700841 struct node* node;
842 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700843
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700844 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700845 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100846 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700847 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700848 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700849
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700850 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700851 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700852 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700853 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700854 return -EACCES;
855 }
856
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700857 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700858}
859
Jeff Brown6249b902012-05-26 14:32:54 -0700860static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
862{
Jeff Brown6249b902012-05-26 14:32:54 -0700863 struct node* node;
864 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700866
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700867 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700868 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100869 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700870 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700871 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700872
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700873 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700874 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700875 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800876
877 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700878 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700879 return -EACCES;
880 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881
Jeff Brown6249b902012-05-26 14:32:54 -0700882 /* XXX: incomplete implementation on purpose.
883 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700884
Elliott Hughes853574d2014-07-31 12:03:03 -0700885 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700886 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700887 }
888
889 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
890 * are both set, then set it to the current time. Else, set it to the
891 * time specified in the request. Same goes for mtime. Use utimensat(2)
892 * as it allows ATIME and MTIME to be changed independently, and has
893 * nanosecond resolution which fuse also has.
894 */
895 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
896 times[0].tv_nsec = UTIME_OMIT;
897 times[1].tv_nsec = UTIME_OMIT;
898 if (req->valid & FATTR_ATIME) {
899 if (req->valid & FATTR_ATIME_NOW) {
900 times[0].tv_nsec = UTIME_NOW;
901 } else {
902 times[0].tv_sec = req->atime;
903 times[0].tv_nsec = req->atimensec;
904 }
905 }
906 if (req->valid & FATTR_MTIME) {
907 if (req->valid & FATTR_MTIME_NOW) {
908 times[1].tv_nsec = UTIME_NOW;
909 } else {
910 times[1].tv_sec = req->mtime;
911 times[1].tv_nsec = req->mtimensec;
912 }
913 }
Jeff Brown6249b902012-05-26 14:32:54 -0700914 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
915 handler->token, path, times[0].tv_sec, times[1].tv_sec);
916 if (utimensat(-1, path, times, 0) < 0) {
917 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700918 }
919 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700920 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700921}
922
Jeff Brown6249b902012-05-26 14:32:54 -0700923static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700924 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
925{
Jeff Brown6249b902012-05-26 14:32:54 -0700926 struct node* parent_node;
927 char parent_path[PATH_MAX];
928 char child_path[PATH_MAX];
929 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700930
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700931 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700932 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
933 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100934 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700935 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700936 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700937
938 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
939 child_path, sizeof(child_path), 1))) {
940 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700942 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700943 return -EACCES;
944 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700946 if (mknod(child_path, mode, req->rdev) < 0) {
947 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948 }
Jeff Brown6249b902012-05-26 14:32:54 -0700949 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700950}
951
Jeff Brown6249b902012-05-26 14:32:54 -0700952static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700953 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
954{
Jeff Brown6249b902012-05-26 14:32:54 -0700955 struct node* parent_node;
956 char parent_path[PATH_MAX];
957 char child_path[PATH_MAX];
958 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700959
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700960 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700961 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
962 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100963 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700964 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700965 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700966
967 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
968 child_path, sizeof(child_path), 1))) {
969 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700971 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700972 return -EACCES;
973 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700975 if (mkdir(child_path, mode) < 0) {
976 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700977 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700978
979 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
980 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
981 char nomedia[PATH_MAX];
982 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
983 if (touch(nomedia, 0664) != 0) {
984 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
985 return -ENOENT;
986 }
987 }
988 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
989 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700990 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700991 if (touch(nomedia, 0664) != 0) {
992 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
993 return -ENOENT;
994 }
995 }
996
Jeff Brown6249b902012-05-26 14:32:54 -0700997 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700998}
999
Jeff Brown6249b902012-05-26 14:32:54 -07001000static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001 const struct fuse_in_header* hdr, const char* name)
1002{
Jeff Brown6249b902012-05-26 14:32:54 -07001003 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001004 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001005 char parent_path[PATH_MAX];
1006 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001007
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001008 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001009 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1010 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001011 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001012 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001013 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001014
1015 if (!parent_node || !find_file_within(parent_path, name,
1016 child_path, sizeof(child_path), 1)) {
1017 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001019 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001020 return -EACCES;
1021 }
Jeff Brown6249b902012-05-26 14:32:54 -07001022 if (unlink(child_path) < 0) {
1023 return -errno;
1024 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001025 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001026 child_node = lookup_child_by_name_locked(parent_node, name);
1027 if (child_node) {
1028 child_node->deleted = true;
1029 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001030 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001031 if (parent_node && child_node) {
1032 /* Tell all other views that node is gone */
1033 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1034 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1035 if (fuse != fuse->global->fuse_default) {
1036 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1037 }
1038 if (fuse != fuse->global->fuse_read) {
1039 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1040 }
1041 if (fuse != fuse->global->fuse_write) {
1042 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1043 }
1044 }
Jeff Brown6249b902012-05-26 14:32:54 -07001045 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001046}
1047
Jeff Brown6249b902012-05-26 14:32:54 -07001048static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001049 const struct fuse_in_header* hdr, const char* name)
1050{
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001051 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001052 struct node* parent_node;
1053 char parent_path[PATH_MAX];
1054 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001055
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001056 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001057 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1058 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001059 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001060 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001061 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001062
1063 if (!parent_node || !find_file_within(parent_path, name,
1064 child_path, sizeof(child_path), 1)) {
1065 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001066 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001067 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001068 return -EACCES;
1069 }
Jeff Brown6249b902012-05-26 14:32:54 -07001070 if (rmdir(child_path) < 0) {
1071 return -errno;
1072 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001073 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001074 child_node = lookup_child_by_name_locked(parent_node, name);
1075 if (child_node) {
1076 child_node->deleted = true;
1077 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001078 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001079 if (parent_node && child_node) {
1080 /* Tell all other views that node is gone */
1081 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1082 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1083 if (fuse != fuse->global->fuse_default) {
1084 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1085 }
1086 if (fuse != fuse->global->fuse_read) {
1087 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1088 }
1089 if (fuse != fuse->global->fuse_write) {
1090 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1091 }
1092 }
Jeff Brown6249b902012-05-26 14:32:54 -07001093 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001094}
1095
Jeff Brown6249b902012-05-26 14:32:54 -07001096static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001097 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001098 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001099{
Jeff Brown6249b902012-05-26 14:32:54 -07001100 struct node* old_parent_node;
1101 struct node* new_parent_node;
1102 struct node* child_node;
1103 char old_parent_path[PATH_MAX];
1104 char new_parent_path[PATH_MAX];
1105 char old_child_path[PATH_MAX];
1106 char new_child_path[PATH_MAX];
1107 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001108 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001109
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001110 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001111 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1112 old_parent_path, sizeof(old_parent_path));
1113 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1114 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001115 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001116 old_name, new_name,
1117 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1118 req->newdir, new_parent_node ? new_parent_node->name : "?");
1119 if (!old_parent_node || !new_parent_node) {
1120 res = -ENOENT;
1121 goto lookup_error;
1122 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001123 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001124 res = -EACCES;
1125 goto lookup_error;
1126 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001127 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001128 res = -EACCES;
1129 goto lookup_error;
1130 }
Jeff Brown6249b902012-05-26 14:32:54 -07001131 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1132 if (!child_node || get_node_path_locked(child_node,
1133 old_child_path, sizeof(old_child_path)) < 0) {
1134 res = -ENOENT;
1135 goto lookup_error;
1136 }
1137 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001138 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001139
1140 /* Special case for renaming a file where destination is same path
1141 * differing only by case. In this case we don't want to look for a case
1142 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1143 */
1144 int search = old_parent_node != new_parent_node
1145 || strcasecmp(old_name, new_name);
1146 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1147 new_child_path, sizeof(new_child_path), search))) {
1148 res = -ENOENT;
1149 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 }
1151
Jeff Brown6249b902012-05-26 14:32:54 -07001152 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1153 res = rename(old_child_path, new_child_path);
1154 if (res < 0) {
1155 res = -errno;
1156 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001157 }
1158
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001159 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001160 res = rename_node_locked(child_node, new_name, new_actual_name);
1161 if (!res) {
1162 remove_node_from_parent_locked(child_node);
Jeff Sharkeyfe764612015-12-14 11:02:01 -07001163 derive_permissions_locked(fuse, new_parent_node, child_node);
1164 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -07001165 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001166 }
Jeff Brown6249b902012-05-26 14:32:54 -07001167 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001168
Jeff Brown6249b902012-05-26 14:32:54 -07001169io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001170 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001171done:
1172 release_node_locked(child_node);
1173lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001174 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001175 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001176}
1177
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001178static int open_flags_to_access_mode(int open_flags) {
1179 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1180 return R_OK;
1181 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1182 return W_OK;
1183 } else {
1184 /* Probably O_RDRW, but treat as default to be safe */
1185 return R_OK | W_OK;
1186 }
1187}
1188
Jeff Brown6249b902012-05-26 14:32:54 -07001189static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1191{
Jeff Brown6249b902012-05-26 14:32:54 -07001192 struct node* node;
1193 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001194 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001196
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001197 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001198 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001199 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001200 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001201 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001202
1203 if (!node) {
1204 return -ENOENT;
1205 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001206 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001207 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001208 return -EACCES;
1209 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001210 h = malloc(sizeof(*h));
1211 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001212 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001213 }
Jeff Brown6249b902012-05-26 14:32:54 -07001214 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001215 h->fd = open(path, req->flags);
1216 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001218 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001219 }
1220 out.fh = ptr_to_id(h);
1221 out.open_flags = 0;
1222 out.padding = 0;
1223 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001224 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225}
1226
Jeff Brown6249b902012-05-26 14:32:54 -07001227static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001228 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1229{
1230 struct handle *h = id_to_ptr(req->fh);
1231 __u64 unique = hdr->unique;
1232 __u32 size = req->size;
1233 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001234 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001235 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001236
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1238 * overlaps the request buffer and will clobber data in the request. This
1239 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001240
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001241 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1242 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001243 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001244 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001246 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001247 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001248 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001249 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001250 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001251 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001252}
1253
Jeff Brown6249b902012-05-26 14:32:54 -07001254static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001255 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1256 const void* buffer)
1257{
1258 struct fuse_write_out out;
1259 struct handle *h = id_to_ptr(req->fh);
1260 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001261 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001262
Arpad Horvath49e93442014-02-18 10:18:25 +01001263 if (req->flags & O_DIRECT) {
1264 memcpy(aligned_buffer, buffer, req->size);
1265 buffer = (const __u8*) aligned_buffer;
1266 }
Jeff Brown6249b902012-05-26 14:32:54 -07001267
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001268 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001269 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001270 res = pwrite64(h->fd, buffer, req->size, req->offset);
1271 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001272 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001273 }
1274 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001275 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001276 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001277 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001278}
1279
Jeff Brown6249b902012-05-26 14:32:54 -07001280static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001281 const struct fuse_in_header* hdr)
1282{
Jeff Brown6249b902012-05-26 14:32:54 -07001283 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001284 struct statfs stat;
1285 struct fuse_statfs_out out;
1286 int res;
1287
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001288 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001289 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001290 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001291 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001292 if (res < 0) {
1293 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001294 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001295 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001296 return -errno;
1297 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001298 memset(&out, 0, sizeof(out));
1299 out.st.blocks = stat.f_blocks;
1300 out.st.bfree = stat.f_bfree;
1301 out.st.bavail = stat.f_bavail;
1302 out.st.files = stat.f_files;
1303 out.st.ffree = stat.f_ffree;
1304 out.st.bsize = stat.f_bsize;
1305 out.st.namelen = stat.f_namelen;
1306 out.st.frsize = stat.f_frsize;
1307 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001308 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001309}
1310
Jeff Brown6249b902012-05-26 14:32:54 -07001311static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001312 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1313{
1314 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001315
1316 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001317 close(h->fd);
1318 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001319 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001320}
1321
Jeff Brown6249b902012-05-26 14:32:54 -07001322static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001323 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1324{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001325 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1326 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001327
Elliott Hughesf6d67372014-07-08 14:38:26 -07001328 int fd = -1;
1329 if (is_dir) {
1330 struct dirhandle *dh = id_to_ptr(req->fh);
1331 fd = dirfd(dh->d);
1332 } else {
1333 struct handle *h = id_to_ptr(req->fh);
1334 fd = h->fd;
1335 }
1336
1337 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1338 is_dir ? "FSYNCDIR" : "FSYNC",
1339 id_to_ptr(req->fh), fd, is_data_sync);
1340 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1341 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001342 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001343 }
Jeff Brown6249b902012-05-26 14:32:54 -07001344 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001345}
1346
Jeff Brown6249b902012-05-26 14:32:54 -07001347static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348 const struct fuse_in_header* hdr)
1349{
Jeff Brown6249b902012-05-26 14:32:54 -07001350 TRACE("[%d] FLUSH\n", handler->token);
1351 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001352}
1353
Jeff Brown6249b902012-05-26 14:32:54 -07001354static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1356{
Jeff Brown6249b902012-05-26 14:32:54 -07001357 struct node* node;
1358 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001360 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001361
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001362 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001363 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001364 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001365 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001366 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001367
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001368 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001369 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001370 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001371 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001372 return -EACCES;
1373 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001374 h = malloc(sizeof(*h));
1375 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001376 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377 }
Jeff Brown6249b902012-05-26 14:32:54 -07001378 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001379 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001380 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001381 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001382 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001383 }
1384 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001385 out.open_flags = 0;
1386 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001387 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001388 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001389}
1390
Jeff Brown6249b902012-05-26 14:32:54 -07001391static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001392 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1393{
1394 char buffer[8192];
1395 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1396 struct dirent *de;
1397 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001398
1399 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001400 if (req->offset == 0) {
1401 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001402 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001403 rewinddir(h->d);
1404 }
1405 de = readdir(h->d);
1406 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001407 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001408 }
1409 fde->ino = FUSE_UNKNOWN_INO;
1410 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1411 fde->off = req->offset + 1;
1412 fde->type = de->d_type;
1413 fde->namelen = strlen(de->d_name);
1414 memcpy(fde->name, de->d_name, fde->namelen + 1);
1415 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001416 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1417 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001418}
1419
Jeff Brown6249b902012-05-26 14:32:54 -07001420static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001421 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1422{
1423 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001424
1425 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001426 closedir(h->d);
1427 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001428 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001429}
1430
Jeff Brown6249b902012-05-26 14:32:54 -07001431static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001432 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1433{
1434 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001435 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001436
Jeff Brown6249b902012-05-26 14:32:54 -07001437 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1438 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001439
1440 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1441 * defined (fuse version 7.6). The structure is the same from 7.6 through
1442 * 7.22. Beginning with 7.23, the structure increased in size and added
1443 * new parameters.
1444 */
1445 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1446 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1447 req->major, req->minor, FUSE_KERNEL_VERSION);
1448 return -1;
1449 }
1450
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001451 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1452 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001453 fuse_struct_size = sizeof(out);
1454#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1455 /* FUSE_KERNEL_VERSION >= 23. */
1456
1457 /* If the kernel only works on minor revs older than or equal to 22,
1458 * then use the older structure size since this code only uses the 7.22
1459 * version of the structure. */
1460 if (req->minor <= 22) {
1461 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1462 }
1463#endif
1464
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001465 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001466 out.max_readahead = req->max_readahead;
1467 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1468 out.max_background = 32;
1469 out.congestion_threshold = 32;
1470 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001471 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001472 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001473}
1474
Jeff Brown6249b902012-05-26 14:32:54 -07001475static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001476 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1477{
Brian Swetland03ee9472010-08-12 18:01:08 -07001478 switch (hdr->opcode) {
1479 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001480 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001481 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001482 }
Jeff Brown84715842012-05-25 14:07:47 -07001483
Brian Swetland03ee9472010-08-12 18:01:08 -07001484 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001485 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001486 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001487 }
Jeff Brown84715842012-05-25 14:07:47 -07001488
Brian Swetland03ee9472010-08-12 18:01:08 -07001489 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001490 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001491 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001492 }
Jeff Brown84715842012-05-25 14:07:47 -07001493
Brian Swetland03ee9472010-08-12 18:01:08 -07001494 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001495 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001496 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001497 }
Jeff Brown84715842012-05-25 14:07:47 -07001498
Brian Swetland03ee9472010-08-12 18:01:08 -07001499// case FUSE_READLINK:
1500// case FUSE_SYMLINK:
1501 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001502 const struct fuse_mknod_in *req = data;
1503 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001504 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001505 }
Jeff Brown84715842012-05-25 14:07:47 -07001506
Brian Swetland03ee9472010-08-12 18:01:08 -07001507 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001508 const struct fuse_mkdir_in *req = data;
1509 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001510 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001511 }
Jeff Brown84715842012-05-25 14:07:47 -07001512
Brian Swetland03ee9472010-08-12 18:01:08 -07001513 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001514 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001515 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001516 }
Jeff Brown84715842012-05-25 14:07:47 -07001517
Brian Swetland03ee9472010-08-12 18:01:08 -07001518 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001519 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001520 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001521 }
Jeff Brown84715842012-05-25 14:07:47 -07001522
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001524 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001525 const char *old_name = ((const char*) data) + sizeof(*req);
1526 const char *new_name = old_name + strlen(old_name) + 1;
1527 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001528 }
Jeff Brown84715842012-05-25 14:07:47 -07001529
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001530// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001531 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001532 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001533 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001534 }
Jeff Brown84715842012-05-25 14:07:47 -07001535
Brian Swetland03ee9472010-08-12 18:01:08 -07001536 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001537 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001538 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001539 }
Jeff Brown84715842012-05-25 14:07:47 -07001540
Brian Swetland03ee9472010-08-12 18:01:08 -07001541 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001542 const struct fuse_write_in *req = data;
1543 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001544 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001545 }
Jeff Brown84715842012-05-25 14:07:47 -07001546
Mike Lockwood4553b082010-08-16 14:14:44 -04001547 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001548 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001549 }
Jeff Brown84715842012-05-25 14:07:47 -07001550
Brian Swetland03ee9472010-08-12 18:01:08 -07001551 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001552 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001553 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001554 }
Jeff Brown84715842012-05-25 14:07:47 -07001555
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001556 case FUSE_FSYNC:
1557 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001558 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001559 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001560 }
1561
Brian Swetland03ee9472010-08-12 18:01:08 -07001562// case FUSE_SETXATTR:
1563// case FUSE_GETXATTR:
1564// case FUSE_LISTXATTR:
1565// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001566 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001567 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001568 }
1569
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001571 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001572 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001573 }
Jeff Brown84715842012-05-25 14:07:47 -07001574
Brian Swetland03ee9472010-08-12 18:01:08 -07001575 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001576 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001577 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001578 }
Jeff Brown84715842012-05-25 14:07:47 -07001579
Brian Swetland03ee9472010-08-12 18:01:08 -07001580 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001581 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001582 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001583 }
Jeff Brown84715842012-05-25 14:07:47 -07001584
Brian Swetland03ee9472010-08-12 18:01:08 -07001585 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001586 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001587 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001588 }
Jeff Brown84715842012-05-25 14:07:47 -07001589
Brian Swetland03ee9472010-08-12 18:01:08 -07001590 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001591 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001592 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1593 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001594 }
Jeff Brown84715842012-05-25 14:07:47 -07001595 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001596}
1597
Jeff Brown6249b902012-05-26 14:32:54 -07001598static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001599{
Jeff Brown6249b902012-05-26 14:32:54 -07001600 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001601 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001602 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1603 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001604 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001605 if (errno == ENODEV) {
1606 ERROR("[%d] someone stole our marbles!\n", handler->token);
1607 exit(2);
1608 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001609 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001610 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001611 }
Jeff Brown84715842012-05-25 14:07:47 -07001612
1613 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001614 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1615 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001616 }
1617
Jeff Brown7729d242012-05-25 15:35:28 -07001618 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001619 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001620 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1621 handler->token, (size_t)len, hdr->len);
1622 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001623 }
1624
Jeff Brown7729d242012-05-25 15:35:28 -07001625 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001626 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001627 __u64 unique = hdr->unique;
1628 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001629
1630 /* We do not access the request again after this point because the underlying
1631 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001632
1633 if (res != NO_STATUS) {
1634 if (res) {
1635 TRACE("[%d] ERROR %d\n", handler->token, res);
1636 }
1637 fuse_status(fuse, unique, res);
1638 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001639 }
1640}
1641
Jeff Brown6249b902012-05-26 14:32:54 -07001642static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001643{
Jeff Brown6249b902012-05-26 14:32:54 -07001644 struct fuse_handler* handler = data;
1645 handle_fuse_requests(handler);
1646 return NULL;
1647}
1648
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001649static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001650 Hashmap* map = context;
1651 hashmapRemove(map, key);
1652 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001653 return true;
1654}
1655
William Robertse5099802015-07-31 13:11:19 -07001656static bool package_parse_callback(pkg_info *info, void *userdata) {
1657 struct fuse_global *global = (struct fuse_global *)userdata;
1658
1659 char* name = strdup(info->name);
1660 hashmapPut(global->package_to_appid, name, (void*) (uintptr_t) info->uid);
1661 packagelist_free(info);
1662 return true;
1663}
1664
1665static bool read_package_list(struct fuse_global* global) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001666 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001667
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001668 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001669
William Robertse5099802015-07-31 13:11:19 -07001670 bool rc = packagelist_parse(package_parse_callback, global);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001671 TRACE("read_package_list: found %zu packages\n",
1672 hashmapSize(global->package_to_appid));
William Robertse5099802015-07-31 13:11:19 -07001673
Jeff Sharkeyfe764612015-12-14 11:02:01 -07001674 /* Regenerate ownership details using newly loaded mapping */
1675 derive_permissions_recursive_locked(global->fuse_default, &global->root);
1676
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001677 pthread_mutex_unlock(&global->lock);
William Robertse5099802015-07-31 13:11:19 -07001678
1679 return rc;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001680}
1681
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001682static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001683 struct inotify_event *event;
1684 char event_buf[512];
1685
1686 int nfd = inotify_init();
1687 if (nfd < 0) {
1688 ERROR("inotify_init failed: %s\n", strerror(errno));
1689 return;
1690 }
1691
1692 bool active = false;
1693 while (1) {
1694 if (!active) {
William Robertse5099802015-07-31 13:11:19 -07001695 int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001696 if (res == -1) {
1697 if (errno == ENOENT || errno == EACCES) {
1698 /* Framework may not have created yet, sleep and retry */
William Robertse5099802015-07-31 13:11:19 -07001699 ERROR("missing \"%s\"; retrying\n", PACKAGES_LIST_FILE);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001700 sleep(3);
1701 continue;
1702 } else {
1703 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1704 return;
1705 }
1706 }
1707
1708 /* Watch above will tell us about any future changes, so
1709 * read the current state. */
William Robertse5099802015-07-31 13:11:19 -07001710 if (read_package_list(global) == false) {
1711 ERROR("read_package_list failed\n");
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001712 return;
1713 }
1714 active = true;
1715 }
1716
1717 int event_pos = 0;
1718 int res = read(nfd, event_buf, sizeof(event_buf));
1719 if (res < (int) sizeof(*event)) {
1720 if (errno == EINTR)
1721 continue;
1722 ERROR("failed to read inotify event: %s\n", strerror(errno));
1723 return;
1724 }
1725
1726 while (res >= (int) sizeof(*event)) {
1727 int event_size;
1728 event = (struct inotify_event *) (event_buf + event_pos);
1729
1730 TRACE("inotify event: %08x\n", event->mask);
1731 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1732 /* Previously watched file was deleted, probably due to move
1733 * that swapped in new data; re-arm the watch and read. */
1734 active = false;
1735 }
1736
1737 event_size = sizeof(*event) + event->len;
1738 res -= event_size;
1739 event_pos += event_size;
1740 }
1741 }
1742}
1743
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001744static int usage() {
1745 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001746 " -u: specify UID to run as\n"
1747 " -g: specify GID to run as\n"
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001748 " -U: specify user ID that owns device\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001749 " -m: source_path is multi-user\n"
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001750 " -w: runtime write mount has full write access\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001751 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001752 return 1;
1753}
1754
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001755static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001756 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001757
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001758 fuse->fd = open("/dev/fuse", O_RDWR);
1759 if (fuse->fd == -1) {
1760 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001761 return -1;
1762 }
1763
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001764 umount2(fuse->dest_path, MNT_DETACH);
1765
Jeff Brown26567352012-05-25 13:27:43 -07001766 snprintf(opts, sizeof(opts),
1767 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001768 fuse->fd, fuse->global->uid, fuse->global->gid);
1769 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1770 MS_NOATIME, opts) != 0) {
1771 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1772 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001773 }
1774
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001775 fuse->gid = gid;
1776 fuse->mask = mask;
1777
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001778 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001779}
1780
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001781static void run(const char* source_path, const char* label, uid_t uid,
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001782 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001783 struct fuse_global global;
1784 struct fuse fuse_default;
1785 struct fuse fuse_read;
1786 struct fuse fuse_write;
1787 struct fuse_handler handler_default;
1788 struct fuse_handler handler_read;
1789 struct fuse_handler handler_write;
1790 pthread_t thread_default;
1791 pthread_t thread_read;
1792 pthread_t thread_write;
1793
1794 memset(&global, 0, sizeof(global));
1795 memset(&fuse_default, 0, sizeof(fuse_default));
1796 memset(&fuse_read, 0, sizeof(fuse_read));
1797 memset(&fuse_write, 0, sizeof(fuse_write));
1798 memset(&handler_default, 0, sizeof(handler_default));
1799 memset(&handler_read, 0, sizeof(handler_read));
1800 memset(&handler_write, 0, sizeof(handler_write));
1801
1802 pthread_mutex_init(&global.lock, NULL);
1803 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1804 global.uid = uid;
1805 global.gid = gid;
1806 global.multi_user = multi_user;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001807 global.next_generation = 0;
1808 global.inode_ctr = 1;
1809
1810 memset(&global.root, 0, sizeof(global.root));
1811 global.root.nid = FUSE_ROOT_ID; /* 1 */
1812 global.root.refcount = 2;
1813 global.root.namelen = strlen(source_path);
1814 global.root.name = strdup(source_path);
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001815 global.root.userid = userid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001816 global.root.uid = AID_ROOT;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001817 global.root.under_android = false;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001818
1819 strcpy(global.source_path, source_path);
1820
1821 if (multi_user) {
1822 global.root.perm = PERM_PRE_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001823 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
1824 } else {
1825 global.root.perm = PERM_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001826 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
1827 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001828
1829 fuse_default.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001830 fuse_read.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001831 fuse_write.global = &global;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001832
1833 global.fuse_default = &fuse_default;
1834 global.fuse_read = &fuse_read;
1835 global.fuse_write = &fuse_write;
1836
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001837 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label);
1838 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label);
1839 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001840
1841 handler_default.fuse = &fuse_default;
1842 handler_read.fuse = &fuse_read;
1843 handler_write.fuse = &fuse_write;
1844
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001845 handler_default.token = 0;
1846 handler_read.token = 1;
1847 handler_write.token = 2;
1848
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001849 umask(0);
1850
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001851 if (multi_user) {
1852 /* Multi-user storage is fully isolated per user, so "other"
1853 * permissions are completely masked off. */
1854 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1855 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1856 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1857 ERROR("failed to fuse_setup\n");
1858 exit(1);
1859 }
1860 } else {
1861 /* Physical storage is readable by all users on device, but
1862 * the Android directories are masked off to a single user
1863 * deep inside attr_from_stat(). */
1864 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1865 || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022)
1866 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) {
1867 ERROR("failed to fuse_setup\n");
1868 exit(1);
1869 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001870 }
1871
1872 /* Drop privs */
1873 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1874 ERROR("cannot setgroups: %s\n", strerror(errno));
1875 exit(1);
1876 }
1877 if (setgid(gid) < 0) {
1878 ERROR("cannot setgid: %s\n", strerror(errno));
1879 exit(1);
1880 }
1881 if (setuid(uid) < 0) {
1882 ERROR("cannot setuid: %s\n", strerror(errno));
1883 exit(1);
1884 }
1885
1886 if (multi_user) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001887 fs_prepare_dir(global.obb_path, 0775, uid, gid);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001888 }
1889
1890 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1891 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1892 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1893 ERROR("failed to pthread_create\n");
1894 exit(1);
1895 }
1896
1897 watch_package_list(&global);
1898 ERROR("terminated prematurely\n");
1899 exit(1);
1900}
1901
Daniel Rosenberg3aa261c2016-03-31 22:00:47 +00001902static int sdcardfs_setup(const char *source_path, const char *dest_path, uid_t fsuid,
1903 gid_t fsgid, bool multi_user, userid_t userid, gid_t gid, mode_t mask) {
1904 char opts[256];
1905
1906 snprintf(opts, sizeof(opts),
1907 "fsuid=%d,fsgid=%d,%smask=%d,userid=%d,gid=%d",
1908 fsuid, fsgid, multi_user?"multiuser,":"", mask, userid, gid);
1909
1910 if (mount(source_path, dest_path, "sdcardfs",
1911 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts) != 0) {
1912 ERROR("failed to mount sdcardfs filesystem: %s\n", strerror(errno));
1913 return -1;
1914 }
1915
1916 return 0;
1917}
1918
1919static void run_sdcardfs(const char* source_path, const char* label, uid_t uid,
1920 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
1921 char dest_path_default[PATH_MAX];
1922 char dest_path_read[PATH_MAX];
1923 char dest_path_write[PATH_MAX];
1924 char obb_path[PATH_MAX];
1925 snprintf(dest_path_default, PATH_MAX, "/mnt/runtime/default/%s", label);
1926 snprintf(dest_path_read, PATH_MAX, "/mnt/runtime/read/%s", label);
1927 snprintf(dest_path_write, PATH_MAX, "/mnt/runtime/write/%s", label);
1928
1929 umask(0);
1930 if (multi_user) {
1931 /* Multi-user storage is fully isolated per user, so "other"
1932 * permissions are completely masked off. */
1933 if (sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
1934 AID_SDCARD_RW, 0006)
1935 || sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid,
1936 AID_EVERYBODY, 0027)
1937 || sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid,
1938 AID_EVERYBODY, full_write ? 0007 : 0027)) {
1939 ERROR("failed to fuse_setup\n");
1940 exit(1);
1941 }
1942 } else {
1943 /* Physical storage is readable by all users on device, but
1944 * the Android directories are masked off to a single user
1945 * deep inside attr_from_stat(). */
1946 if (sdcardfs_setup(source_path, dest_path_default, uid, gid, multi_user, userid,
1947 AID_SDCARD_RW, 0006)
1948 || sdcardfs_setup(source_path, dest_path_read, uid, gid, multi_user, userid,
1949 AID_EVERYBODY, full_write ? 0027 : 0022)
1950 || sdcardfs_setup(source_path, dest_path_write, uid, gid, multi_user, userid,
1951 AID_EVERYBODY, full_write ? 0007 : 0022)) {
1952 ERROR("failed to fuse_setup\n");
1953 exit(1);
1954 }
1955 }
1956
1957 /* Drop privs */
1958 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1959 ERROR("cannot setgroups: %s\n", strerror(errno));
1960 exit(1);
1961 }
1962 if (setgid(gid) < 0) {
1963 ERROR("cannot setgid: %s\n", strerror(errno));
1964 exit(1);
1965 }
1966 if (setuid(uid) < 0) {
1967 ERROR("cannot setuid: %s\n", strerror(errno));
1968 exit(1);
1969 }
1970
1971 if (multi_user) {
1972 snprintf(obb_path, sizeof(obb_path), "%s/obb", source_path);
1973 fs_prepare_dir(&obb_path[0], 0775, uid, gid);
1974 }
1975
1976 exit(0);
1977}
1978
1979static bool supports_sdcardfs(void) {
1980 FILE *fp;
1981 char *buf = NULL;
1982 size_t buflen = 0;
1983
1984 fp = fopen("/proc/filesystems", "r");
1985 if (!fp) {
1986 ERROR("Could not read /proc/filesystems, error: %s\n", strerror(errno));
1987 return false;
1988 }
1989 while ((getline(&buf, &buflen, fp)) > 0) {
1990 if (strstr(buf, "sdcardfs\n")) {
1991 free(buf);
1992 fclose(fp);
1993 return true;
1994 }
1995 }
1996 free(buf);
1997 fclose(fp);
1998 return false;
1999}
2000
Jeff Sharkey20ca9832016-04-07 11:05:21 -06002001static bool should_use_sdcardfs(void) {
2002 char property[PROPERTY_VALUE_MAX];
2003
2004 // Allow user to have a strong opinion about state
2005 property_get(PROP_SDCARDFS_USER, property, "");
2006 if (!strcmp(property, "force_on")) {
2007 ALOGW("User explicitly enabled sdcardfs");
2008 return supports_sdcardfs();
2009 } else if (!strcmp(property, "force_off")) {
2010 ALOGW("User explicitly disabled sdcardfs");
2011 return false;
2012 }
2013
2014 // Fall back to device opinion about state
2015 if (property_get_bool(PROP_SDCARDFS_DEVICE, false)) {
2016 ALOGW("Device explicitly enabled sdcardfs");
2017 return supports_sdcardfs();
2018 } else {
2019 ALOGW("Device explicitly disabled sdcardfs");
2020 return false;
2021 }
2022}
2023
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002024int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002025 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002026 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07002027 uid_t uid = 0;
2028 gid_t gid = 0;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002029 userid_t userid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002030 bool multi_user = false;
2031 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05002032 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08002033 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07002034 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07002035
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002036 int opt;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002037 while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002038 switch (opt) {
2039 case 'u':
2040 uid = strtoul(optarg, NULL, 10);
2041 break;
2042 case 'g':
2043 gid = strtoul(optarg, NULL, 10);
2044 break;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07002045 case 'U':
2046 userid = strtoul(optarg, NULL, 10);
2047 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002048 case 'm':
2049 multi_user = true;
2050 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07002051 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002052 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002053 break;
2054 case '?':
2055 default:
2056 return usage();
2057 }
2058 }
2059
2060 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05002061 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07002062 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002063 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002064 } else if (!label) {
2065 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07002066 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08002067 ERROR("too many arguments\n");
2068 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05002069 }
Brian Swetland03ee9472010-08-12 18:01:08 -07002070 }
2071
Jeff Sharkeye169bd02012-08-13 16:44:42 -07002072 if (!source_path) {
2073 ERROR("no source path specified\n");
2074 return usage();
2075 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002076 if (!label) {
2077 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05002078 return usage();
2079 }
Jeff Brown26567352012-05-25 13:27:43 -07002080 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07002081 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05002082 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07002083 }
2084
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08002085 rlim.rlim_cur = 8192;
2086 rlim.rlim_max = 8192;
2087 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
2088 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
2089 }
2090
Nick Kralevich8d28fa72014-07-24 17:05:59 -07002091 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
2092 ERROR("installd fs upgrade not yet complete. Waiting...\n");
2093 sleep(1);
2094 }
2095
Jeff Sharkey20ca9832016-04-07 11:05:21 -06002096 if (should_use_sdcardfs()) {
Daniel Rosenberg3aa261c2016-03-31 22:00:47 +00002097 run_sdcardfs(source_path, label, uid, gid, userid, multi_user, full_write);
2098 } else {
2099 run(source_path, label, uid, gid, userid, multi_user, full_write);
2100 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07002101 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07002102}