blob: b53b581ecdd7a463e60ad0f69d3a01d0ef159774 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070032#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070033#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070034#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040035#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080036#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070037#include <sys/uio.h>
38#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039
Jeff Sharkey44d63422013-09-12 09:44:48 -070040#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070042#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070044
Brian Swetlandb14a2c62010-08-12 18:21:12 -070045#include <private/android_filesystem_config.h>
46
Brian Swetland03ee9472010-08-12 18:01:08 -070047/* README
48 *
49 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070050 *
Brian Swetland03ee9472010-08-12 18:01:08 -070051 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070052 * directory permissions (all files are given fixed owner, group, and
53 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070054 * changeable, symlinks and hardlinks are not createable, etc.
55 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070057 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070058 * It must be run as root, but will drop to requested UID/GID as soon as it
59 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070060 *
61 * Things I believe to be true:
62 *
63 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
64 * CREAT) must bump that node's refcount
65 * - don't forget that FORGET can forget multiple references (req->nlookup)
66 * - if an op that returns a fuse_entry fails writing the reply to the
67 * kernel, you must rollback the refcount to reflect the reference the
68 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070069 *
70 * This daemon can also derive custom filesystem permissions based on directory
71 * structure when requested. These custom permissions support several features:
72 *
73 * - Apps can access their own files in /Android/data/com.example/ without
74 * requiring any additional GIDs.
75 * - Separate permissions for protecting directories like Pictures and Music.
76 * - Multi-user separation on the same physical device.
77 *
78 * The derived permissions look like this:
79 *
80 * rwxrwx--x root:sdcard_rw /
81 * rwxrwx--- root:sdcard_pics /Pictures
82 * rwxrwx--- root:sdcard_av /Music
83 *
84 * rwxrwx--x root:sdcard_rw /Android
85 * rwxrwx--x root:sdcard_rw /Android/data
86 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
87 * rwxrwx--x root:sdcard_rw /Android/obb/
88 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
89 *
90 * rwxrwx--- root:sdcard_all /Android/user
91 * rwxrwx--x root:sdcard_rw /Android/user/10
92 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070093 */
94
95#define FUSE_TRACE 0
96
97#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070098#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070099#else
100#define TRACE(x...) do {} while (0)
101#endif
102
Elliott Hughes300d5642014-07-08 13:53:26 -0700103#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700104
105#define FUSE_UNKNOWN_INO 0xffffffff
106
Jeff Brown84715842012-05-25 14:07:47 -0700107/* Maximum number of bytes to write in one request. */
108#define MAX_WRITE (256 * 1024)
109
110/* Maximum number of bytes to read in one request. */
111#define MAX_READ (128 * 1024)
112
113/* Largest possible request.
114 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
115 * the largest possible data payload. */
116#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
117
Jeff Brown6249b902012-05-26 14:32:54 -0700118/* Default number of threads. */
119#define DEFAULT_NUM_THREADS 2
120
121/* Pseudo-error constant used to indicate that no fuse status is needed
122 * or that a reply has already been written. */
123#define NO_STATUS 1
124
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125/* Path to system-provided mapping of package name to appIds */
126static const char* const kPackagesListFile = "/data/system/packages.list";
127
128/* Supplementary groups to execute with */
129static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
130
131/* Permission mode for a specific node. Controls how file permissions
132 * are derived for children nodes. */
133typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700134 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700135 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700136 /* This node is one level above a normal root; used for legacy layouts
137 * which use the first level to represent user_id. */
138 PERM_LEGACY_PRE_ROOT,
139 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700145 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700146 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700147 /* This node is "/Android/media" */
148 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700149 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700150 PERM_ANDROID_USER,
151} perm_t;
152
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700153/* Permissions structure to derive */
154typedef enum {
155 DERIVE_NONE,
156 DERIVE_LEGACY,
157 DERIVE_UNIFIED,
158} derive_t;
159
Brian Swetland03ee9472010-08-12 18:01:08 -0700160struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700161 int fd;
162};
163
164struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700165 DIR *d;
166};
167
168struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700169 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700170 __u64 nid;
171 __u64 gen;
172
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700173 /* State derived based on current position in hierarchy. */
174 perm_t perm;
175 userid_t userid;
176 uid_t uid;
177 gid_t gid;
178 mode_t mode;
179
Paul Eastham11ccdb32010-10-14 11:04:26 -0700180 struct node *next; /* per-dir sibling list */
181 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700182 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700183
Jeff Brown6249b902012-05-26 14:32:54 -0700184 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700185 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800186 /* If non-null, this is the real name of the file in the underlying storage.
187 * This may differ from the field "name" only by case.
188 * strlen(actual_name) will always equal strlen(name), so it is safe to use
189 * namelen for both fields.
190 */
191 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700192
193 /* If non-null, an exact underlying path that should be grafted into this
194 * position. Used to support things like OBB. */
195 char* graft_path;
196 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700197};
198
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700199static int str_hash(void *key) {
200 return hashmapHash(key, strlen(key));
201}
202
Jeff Sharkey44d63422013-09-12 09:44:48 -0700203/** Test if two string keys are equal ignoring case */
204static bool str_icase_equals(void *keyA, void *keyB) {
205 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700206}
207
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700208static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800209 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700210}
211
212static bool int_equals(void *keyA, void *keyB) {
213 return keyA == keyB;
214}
215
Jeff Brown7729d242012-05-25 15:35:28 -0700216/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700217struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700218 pthread_mutex_t lock;
219
Brian Swetland03ee9472010-08-12 18:01:08 -0700220 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700221 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700222 derive_t derive;
223 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700224 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700225 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700226 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700227
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700228 Hashmap* package_to_appid;
229 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700230};
231
Jeff Brown7729d242012-05-25 15:35:28 -0700232/* Private data used by a single fuse handler. */
233struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700234 struct fuse* fuse;
235 int token;
236
Jeff Brown7729d242012-05-25 15:35:28 -0700237 /* To save memory, we never use the contents of the request buffer and the read
238 * buffer at the same time. This allows us to share the underlying storage. */
239 union {
240 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800241 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700242 };
243};
Brian Swetland03ee9472010-08-12 18:01:08 -0700244
Jeff Brown6249b902012-05-26 14:32:54 -0700245static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700246{
Jeff Brown6249b902012-05-26 14:32:54 -0700247 return (void *) (uintptr_t) nid;
248}
Brian Swetland03ee9472010-08-12 18:01:08 -0700249
Jeff Brown6249b902012-05-26 14:32:54 -0700250static inline __u64 ptr_to_id(void *ptr)
251{
252 return (__u64) (uintptr_t) ptr;
253}
Brian Swetland03ee9472010-08-12 18:01:08 -0700254
Jeff Brown6249b902012-05-26 14:32:54 -0700255static void acquire_node_locked(struct node* node)
256{
257 node->refcount++;
258 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
259}
260
261static void remove_node_from_parent_locked(struct node* node);
262
263static void release_node_locked(struct node* node)
264{
265 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
266 if (node->refcount > 0) {
267 node->refcount--;
268 if (!node->refcount) {
269 TRACE("DESTROY %p (%s)\n", node, node->name);
270 remove_node_from_parent_locked(node);
271
272 /* TODO: remove debugging - poison memory */
273 memset(node->name, 0xef, node->namelen);
274 free(node->name);
275 free(node->actual_name);
276 memset(node, 0xfc, sizeof(*node));
277 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800278 }
Jeff Brown6249b902012-05-26 14:32:54 -0700279 } else {
280 ERROR("Zero refcnt %p\n", node);
281 }
282}
283
284static void add_node_to_parent_locked(struct node *node, struct node *parent) {
285 node->parent = parent;
286 node->next = parent->child;
287 parent->child = node;
288 acquire_node_locked(parent);
289}
290
291static void remove_node_from_parent_locked(struct node* node)
292{
293 if (node->parent) {
294 if (node->parent->child == node) {
295 node->parent->child = node->parent->child->next;
296 } else {
297 struct node *node2;
298 node2 = node->parent->child;
299 while (node2->next != node)
300 node2 = node2->next;
301 node2->next = node->next;
302 }
303 release_node_locked(node->parent);
304 node->parent = NULL;
305 node->next = NULL;
306 }
307}
308
309/* Gets the absolute path to a node into the provided buffer.
310 *
311 * Populates 'buf' with the path and returns the length of the path on success,
312 * or returns -1 if the path is too long for the provided buffer.
313 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700314static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
315 const char* name;
316 size_t namelen;
317 if (node->graft_path) {
318 name = node->graft_path;
319 namelen = node->graft_pathlen;
320 } else if (node->actual_name) {
321 name = node->actual_name;
322 namelen = node->namelen;
323 } else {
324 name = node->name;
325 namelen = node->namelen;
326 }
327
Jeff Brown6249b902012-05-26 14:32:54 -0700328 if (bufsize < namelen + 1) {
329 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700330 }
331
Jeff Brown6249b902012-05-26 14:32:54 -0700332 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700333 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700334 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
335 if (pathlen < 0) {
336 return -1;
337 }
338 buf[pathlen++] = '/';
339 }
340
Jeff Brown6249b902012-05-26 14:32:54 -0700341 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
342 return pathlen + namelen;
343}
344
345/* Finds the absolute path of a file within a given directory.
346 * Performs a case-insensitive search for the file and sets the buffer to the path
347 * of the first matching file. If 'search' is zero or if no match is found, sets
348 * the buffer to the path that the file would have, assuming the name were case-sensitive.
349 *
350 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
351 * or returns NULL if the path is too long for the provided buffer.
352 */
353static char* find_file_within(const char* path, const char* name,
354 char* buf, size_t bufsize, int search)
355{
356 size_t pathlen = strlen(path);
357 size_t namelen = strlen(name);
358 size_t childlen = pathlen + namelen + 1;
359 char* actual;
360
361 if (bufsize <= childlen) {
362 return NULL;
363 }
364
365 memcpy(buf, path, pathlen);
366 buf[pathlen] = '/';
367 actual = buf + pathlen + 1;
368 memcpy(actual, name, namelen + 1);
369
370 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800371 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700372 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800373 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700374 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700375 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800376 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800377 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700378 if (!strcasecmp(entry->d_name, name)) {
379 /* we have a match - replace the name, don't need to copy the null again */
380 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800381 break;
382 }
383 }
384 closedir(dir);
385 }
Jeff Brown6249b902012-05-26 14:32:54 -0700386 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800387}
388
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700389static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800390{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700391 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700392 attr->size = s->st_size;
393 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400394 attr->atime = s->st_atime;
395 attr->mtime = s->st_mtime;
396 attr->ctime = s->st_ctime;
397 attr->atimensec = s->st_atime_nsec;
398 attr->mtimensec = s->st_mtime_nsec;
399 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700400 attr->mode = s->st_mode;
401 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700402
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700403 attr->uid = node->uid;
404 attr->gid = node->gid;
405
406 /* Filter requested mode based on underlying file, and
407 * pass through file type. */
408 int owner_mode = s->st_mode & 0700;
409 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
410 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
411}
412
Jeff Sharkey44d63422013-09-12 09:44:48 -0700413static int touch(char* path, mode_t mode) {
414 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
415 if (fd == -1) {
416 if (errno == EEXIST) {
417 return 0;
418 } else {
419 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
420 return -1;
421 }
422 }
423 close(fd);
424 return 0;
425}
426
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700427static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
428 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700429 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700430
431 /* By default, each node inherits from its parent */
432 node->perm = PERM_INHERIT;
433 node->userid = parent->userid;
434 node->uid = parent->uid;
435 node->gid = parent->gid;
436 node->mode = parent->mode;
437
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700438 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700439 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700440 }
441
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700442 /* Derive custom permissions based on parent and current node */
443 switch (parent->perm) {
444 case PERM_INHERIT:
445 /* Already inherited above */
446 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700447 case PERM_LEGACY_PRE_ROOT:
448 /* Legacy internal layout places users at top level */
449 node->perm = PERM_ROOT;
450 node->userid = strtoul(node->name, NULL, 10);
451 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700452 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700453 /* Assume masked off by default. */
454 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700455 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700456 /* App-specific directories inside; let anyone traverse */
457 node->perm = PERM_ANDROID;
458 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700459 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700460 if (!strcasecmp(node->name, "DCIM")
461 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700462 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700463 } else if (!strcasecmp(node->name, "Alarms")
464 || !strcasecmp(node->name, "Movies")
465 || !strcasecmp(node->name, "Music")
466 || !strcasecmp(node->name, "Notifications")
467 || !strcasecmp(node->name, "Podcasts")
468 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700469 node->gid = AID_SDCARD_AV;
470 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700471 }
472 break;
473 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700474 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700475 /* App-specific directories inside; let anyone traverse */
476 node->perm = PERM_ANDROID_DATA;
477 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* App-specific directories inside; let anyone traverse */
480 node->perm = PERM_ANDROID_OBB;
481 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700482 /* Single OBB directory is always shared */
483 node->graft_path = fuse->obbpath;
484 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700485 } else if (!strcasecmp(node->name, "media")) {
486 /* App-specific directories inside; let anyone traverse */
487 node->perm = PERM_ANDROID_MEDIA;
488 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700489 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700490 /* User directories must only be accessible to system, protected
491 * by sdcard_all. Zygote will bind mount the appropriate user-
492 * specific path. */
493 node->perm = PERM_ANDROID_USER;
494 node->gid = AID_SDCARD_ALL;
495 node->mode = 0770;
496 }
497 break;
498 case PERM_ANDROID_DATA:
499 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700500 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800501 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700502 if (appid != 0) {
503 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700504 }
505 node->mode = 0770;
506 break;
507 case PERM_ANDROID_USER:
508 /* Root of a secondary user */
509 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700510 node->userid = strtoul(node->name, NULL, 10);
511 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700512 node->mode = 0771;
513 break;
514 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700515}
516
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700517/* Return if the calling UID holds sdcard_rw. */
518static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700519 /* No additional permissions enforcement */
520 if (fuse->derive == DERIVE_NONE) {
521 return true;
522 }
523
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700524 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800525 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700526}
527
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700528/* Kernel has already enforced everything we returned through
529 * derive_permissions_locked(), so this is used to lock down access
530 * even further, such as enforcing that apps hold sdcard_rw. */
531static bool check_caller_access_to_name(struct fuse* fuse,
532 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700533 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700534 /* Always block security-sensitive files at root */
535 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700536 if (!strcasecmp(name, "autorun.inf")
537 || !strcasecmp(name, ".android_secure")
538 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700539 return false;
540 }
541 }
542
543 /* No additional permissions enforcement */
544 if (fuse->derive == DERIVE_NONE) {
545 return true;
546 }
547
Jeff Sharkey44d63422013-09-12 09:44:48 -0700548 /* Root always has access; access for any other UIDs should always
549 * be controlled through packages.list. */
550 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700551 return true;
552 }
553
554 /* If asking to write, verify that caller either owns the
555 * parent or holds sdcard_rw. */
556 if (mode & W_OK) {
557 if (parent_node && hdr->uid == parent_node->uid) {
558 return true;
559 }
560
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700561 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700562 }
563
564 /* No extra permissions to enforce */
565 return true;
566}
567
568static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700569 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
570 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700571}
572
Jeff Brown6249b902012-05-26 14:32:54 -0700573struct node *create_node_locked(struct fuse* fuse,
574 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700575{
576 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700577 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700578
Paul Eastham11ccdb32010-10-14 11:04:26 -0700579 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700580 if (!node) {
581 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700582 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700583 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700584 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700585 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700586 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700587 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700588 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700589 if (strcmp(name, actual_name)) {
590 node->actual_name = malloc(namelen + 1);
591 if (!node->actual_name) {
592 free(node->name);
593 free(node);
594 return NULL;
595 }
596 memcpy(node->actual_name, actual_name, namelen + 1);
597 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700598 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700599 node->nid = ptr_to_id(node);
600 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700601
602 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700603 acquire_node_locked(node);
604 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700605 return node;
606}
607
Jeff Brown6249b902012-05-26 14:32:54 -0700608static int rename_node_locked(struct node *node, const char *name,
609 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700610{
Jeff Brown6249b902012-05-26 14:32:54 -0700611 size_t namelen = strlen(name);
612 int need_actual_name = strcmp(name, actual_name);
613
614 /* make the storage bigger without actually changing the name
615 * in case an error occurs part way */
616 if (namelen > node->namelen) {
617 char* new_name = realloc(node->name, namelen + 1);
618 if (!new_name) {
619 return -ENOMEM;
620 }
621 node->name = new_name;
622 if (need_actual_name && node->actual_name) {
623 char* new_actual_name = realloc(node->actual_name, namelen + 1);
624 if (!new_actual_name) {
625 return -ENOMEM;
626 }
627 node->actual_name = new_actual_name;
628 }
629 }
630
631 /* update the name, taking care to allocate storage before overwriting the old name */
632 if (need_actual_name) {
633 if (!node->actual_name) {
634 node->actual_name = malloc(namelen + 1);
635 if (!node->actual_name) {
636 return -ENOMEM;
637 }
638 }
639 memcpy(node->actual_name, actual_name, namelen + 1);
640 } else {
641 free(node->actual_name);
642 node->actual_name = NULL;
643 }
644 memcpy(node->name, name, namelen + 1);
645 node->namelen = namelen;
646 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700647}
648
Jeff Brown6249b902012-05-26 14:32:54 -0700649static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700650{
Jeff Brown6249b902012-05-26 14:32:54 -0700651 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700652 return &fuse->root;
653 } else {
654 return id_to_ptr(nid);
655 }
656}
657
Jeff Brown6249b902012-05-26 14:32:54 -0700658static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
659 char* buf, size_t bufsize)
660{
661 struct node* node = lookup_node_by_id_locked(fuse, nid);
662 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
663 node = NULL;
664 }
665 return node;
666}
667
668static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700669{
670 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700671 /* use exact string comparison, nodes that differ by case
672 * must be considered distinct even if they refer to the same
673 * underlying file as otherwise operations such as "mv x x"
674 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700675 if (!strcmp(name, node->name)) {
676 return node;
677 }
678 }
679 return 0;
680}
681
Jeff Brown6249b902012-05-26 14:32:54 -0700682static struct node* acquire_or_create_child_locked(
683 struct fuse* fuse, struct node* parent,
684 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700685{
Jeff Brown6249b902012-05-26 14:32:54 -0700686 struct node* child = lookup_child_by_name_locked(parent, name);
687 if (child) {
688 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800689 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700690 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800691 }
Jeff Brown6249b902012-05-26 14:32:54 -0700692 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700693}
694
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700695static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700696 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700697 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700698
Jeff Brown6249b902012-05-26 14:32:54 -0700699 fuse->fd = fd;
700 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700701 fuse->derive = derive;
702 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700703 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700704
Jeff Brown6249b902012-05-26 14:32:54 -0700705 memset(&fuse->root, 0, sizeof(fuse->root));
706 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
707 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700708 fuse->root.namelen = strlen(source_path);
709 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700710 fuse->root.userid = 0;
711 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700712
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700713 /* Set up root node for various modes of operation */
714 switch (derive) {
715 case DERIVE_NONE:
716 /* Traditional behavior that treats entire device as being accessible
717 * to sdcard_rw, and no permissions are derived. */
718 fuse->root.perm = PERM_ROOT;
719 fuse->root.mode = 0775;
720 fuse->root.gid = AID_SDCARD_RW;
721 break;
722 case DERIVE_LEGACY:
723 /* Legacy behavior used to support internal multiuser layout which
724 * places user_id at the top directory level, with the actual roots
725 * just below that. Shared OBB path is also at top level. */
726 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
727 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700728 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700729 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700730 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
731 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700732 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700733 break;
734 case DERIVE_UNIFIED:
735 /* Unified multiuser layout which places secondary user_id under
736 * /Android/user and shared OBB path under /Android/obb. */
737 fuse->root.perm = PERM_ROOT;
738 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700739 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700740 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700741 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
742 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
743 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700744 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700745}
746
Jeff Brown6249b902012-05-26 14:32:54 -0700747static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700748{
749 struct fuse_out_header hdr;
750 hdr.len = sizeof(hdr);
751 hdr.error = err;
752 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700753 write(fuse->fd, &hdr, sizeof(hdr));
754}
755
Jeff Brown6249b902012-05-26 14:32:54 -0700756static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700757{
758 struct fuse_out_header hdr;
759 struct iovec vec[2];
760 int res;
761
762 hdr.len = len + sizeof(hdr);
763 hdr.error = 0;
764 hdr.unique = unique;
765
766 vec[0].iov_base = &hdr;
767 vec[0].iov_len = sizeof(hdr);
768 vec[1].iov_base = data;
769 vec[1].iov_len = len;
770
771 res = writev(fuse->fd, vec, 2);
772 if (res < 0) {
773 ERROR("*** REPLY FAILED *** %d\n", errno);
774 }
775}
776
Jeff Brown6249b902012-05-26 14:32:54 -0700777static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
778 struct node* parent, const char* name, const char* actual_name,
779 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700780{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700781 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700782 struct fuse_entry_out out;
783 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700784
Jeff Brown6249b902012-05-26 14:32:54 -0700785 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700786 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700787 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700788
Jeff Brown6249b902012-05-26 14:32:54 -0700789 pthread_mutex_lock(&fuse->lock);
790 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
791 if (!node) {
792 pthread_mutex_unlock(&fuse->lock);
793 return -ENOMEM;
794 }
795 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700796 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700797 out.attr_valid = 10;
798 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700799 out.nodeid = node->nid;
800 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700801 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700802 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700803 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700804}
805
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700806static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700807 const char* path)
808{
809 struct fuse_attr_out out;
810 struct stat s;
811
812 if (lstat(path, &s) < 0) {
813 return -errno;
814 }
815 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700816 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700817 out.attr_valid = 10;
818 fuse_reply(fuse, unique, &out, sizeof(out));
819 return NO_STATUS;
820}
821
822static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700823 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700824{
Jeff Brown6249b902012-05-26 14:32:54 -0700825 struct node* parent_node;
826 char parent_path[PATH_MAX];
827 char child_path[PATH_MAX];
828 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700829
Jeff Brown6249b902012-05-26 14:32:54 -0700830 pthread_mutex_lock(&fuse->lock);
831 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
832 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100833 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700834 parent_node ? parent_node->name : "?");
835 pthread_mutex_unlock(&fuse->lock);
836
837 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
838 child_path, sizeof(child_path), 1))) {
839 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700840 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700841 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700842 return -EACCES;
843 }
844
Jeff Brown6249b902012-05-26 14:32:54 -0700845 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700846}
847
Jeff Brown6249b902012-05-26 14:32:54 -0700848static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700849 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
850{
Jeff Brown6249b902012-05-26 14:32:54 -0700851 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700852
Jeff Brown6249b902012-05-26 14:32:54 -0700853 pthread_mutex_lock(&fuse->lock);
854 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100855 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700856 hdr->nodeid, node ? node->name : "?");
857 if (node) {
858 __u64 n = req->nlookup;
859 while (n--) {
860 release_node_locked(node);
861 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700862 }
Jeff Brown6249b902012-05-26 14:32:54 -0700863 pthread_mutex_unlock(&fuse->lock);
864 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865}
866
Jeff Brown6249b902012-05-26 14:32:54 -0700867static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
869{
Jeff Brown6249b902012-05-26 14:32:54 -0700870 struct node* node;
871 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700872
Jeff Brown6249b902012-05-26 14:32:54 -0700873 pthread_mutex_lock(&fuse->lock);
874 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100875 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700876 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
877 pthread_mutex_unlock(&fuse->lock);
878
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700880 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700882 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700883 return -EACCES;
884 }
885
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700886 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700887}
888
Jeff Brown6249b902012-05-26 14:32:54 -0700889static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700890 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
891{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700892 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700893 struct node* node;
894 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700895 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700896
Jeff Brown6249b902012-05-26 14:32:54 -0700897 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700898 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700899 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100900 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700901 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
902 pthread_mutex_unlock(&fuse->lock);
903
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700904 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700905 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700906 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700907 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700908 return -EACCES;
909 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700910
Jeff Brown6249b902012-05-26 14:32:54 -0700911 /* XXX: incomplete implementation on purpose.
912 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700913
Elliott Hughes853574d2014-07-31 12:03:03 -0700914 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700915 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700916 }
917
918 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
919 * are both set, then set it to the current time. Else, set it to the
920 * time specified in the request. Same goes for mtime. Use utimensat(2)
921 * as it allows ATIME and MTIME to be changed independently, and has
922 * nanosecond resolution which fuse also has.
923 */
924 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
925 times[0].tv_nsec = UTIME_OMIT;
926 times[1].tv_nsec = UTIME_OMIT;
927 if (req->valid & FATTR_ATIME) {
928 if (req->valid & FATTR_ATIME_NOW) {
929 times[0].tv_nsec = UTIME_NOW;
930 } else {
931 times[0].tv_sec = req->atime;
932 times[0].tv_nsec = req->atimensec;
933 }
934 }
935 if (req->valid & FATTR_MTIME) {
936 if (req->valid & FATTR_MTIME_NOW) {
937 times[1].tv_nsec = UTIME_NOW;
938 } else {
939 times[1].tv_sec = req->mtime;
940 times[1].tv_nsec = req->mtimensec;
941 }
942 }
Jeff Brown6249b902012-05-26 14:32:54 -0700943 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
944 handler->token, path, times[0].tv_sec, times[1].tv_sec);
945 if (utimensat(-1, path, times, 0) < 0) {
946 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700947 }
948 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700949 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700950}
951
Jeff Brown6249b902012-05-26 14:32:54 -0700952static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700953 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
954{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700955 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700956 struct node* parent_node;
957 char parent_path[PATH_MAX];
958 char child_path[PATH_MAX];
959 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700960
Jeff Brown6249b902012-05-26 14:32:54 -0700961 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700962 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700963 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
964 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100965 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700966 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
967 pthread_mutex_unlock(&fuse->lock);
968
969 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
970 child_path, sizeof(child_path), 1))) {
971 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700972 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700973 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700974 return -EACCES;
975 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700976 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700977 if (mknod(child_path, mode, req->rdev) < 0) {
978 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979 }
Jeff Brown6249b902012-05-26 14:32:54 -0700980 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700981}
982
Jeff Brown6249b902012-05-26 14:32:54 -0700983static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
985{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700986 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700987 struct node* parent_node;
988 char parent_path[PATH_MAX];
989 char child_path[PATH_MAX];
990 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700991
Jeff Brown6249b902012-05-26 14:32:54 -0700992 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700993 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700994 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
995 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100996 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700997 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
998 pthread_mutex_unlock(&fuse->lock);
999
1000 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1001 child_path, sizeof(child_path), 1))) {
1002 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001003 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001004 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001005 return -EACCES;
1006 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001007 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001008 if (mkdir(child_path, mode) < 0) {
1009 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001010 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001011
1012 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1013 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1014 char nomedia[PATH_MAX];
1015 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1016 if (touch(nomedia, 0664) != 0) {
1017 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1018 return -ENOENT;
1019 }
1020 }
1021 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1022 char nomedia[PATH_MAX];
1023 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1024 if (touch(nomedia, 0664) != 0) {
1025 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1026 return -ENOENT;
1027 }
1028 }
1029
Jeff Brown6249b902012-05-26 14:32:54 -07001030 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001031}
1032
Jeff Brown6249b902012-05-26 14:32:54 -07001033static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001034 const struct fuse_in_header* hdr, const char* name)
1035{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001036 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001037 struct node* parent_node;
1038 char parent_path[PATH_MAX];
1039 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001040
Jeff Brown6249b902012-05-26 14:32:54 -07001041 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001042 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001043 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1044 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001045 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001046 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1047 pthread_mutex_unlock(&fuse->lock);
1048
1049 if (!parent_node || !find_file_within(parent_path, name,
1050 child_path, sizeof(child_path), 1)) {
1051 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001052 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001053 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001054 return -EACCES;
1055 }
Jeff Brown6249b902012-05-26 14:32:54 -07001056 if (unlink(child_path) < 0) {
1057 return -errno;
1058 }
1059 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001060}
1061
Jeff Brown6249b902012-05-26 14:32:54 -07001062static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001063 const struct fuse_in_header* hdr, const char* name)
1064{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001065 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001066 struct node* parent_node;
1067 char parent_path[PATH_MAX];
1068 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001069
Jeff Brown6249b902012-05-26 14:32:54 -07001070 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001071 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001072 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1073 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001074 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001075 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1076 pthread_mutex_unlock(&fuse->lock);
1077
1078 if (!parent_node || !find_file_within(parent_path, name,
1079 child_path, sizeof(child_path), 1)) {
1080 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001081 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001082 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001083 return -EACCES;
1084 }
Jeff Brown6249b902012-05-26 14:32:54 -07001085 if (rmdir(child_path) < 0) {
1086 return -errno;
1087 }
1088 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089}
1090
Jeff Brown6249b902012-05-26 14:32:54 -07001091static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001093 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001094{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001095 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001096 struct node* old_parent_node;
1097 struct node* new_parent_node;
1098 struct node* child_node;
1099 char old_parent_path[PATH_MAX];
1100 char new_parent_path[PATH_MAX];
1101 char old_child_path[PATH_MAX];
1102 char new_child_path[PATH_MAX];
1103 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001104 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001105
Jeff Brown6249b902012-05-26 14:32:54 -07001106 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001107 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001108 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1109 old_parent_path, sizeof(old_parent_path));
1110 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1111 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001112 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001113 old_name, new_name,
1114 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1115 req->newdir, new_parent_node ? new_parent_node->name : "?");
1116 if (!old_parent_node || !new_parent_node) {
1117 res = -ENOENT;
1118 goto lookup_error;
1119 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001120 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001121 res = -EACCES;
1122 goto lookup_error;
1123 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001124 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001125 res = -EACCES;
1126 goto lookup_error;
1127 }
Jeff Brown6249b902012-05-26 14:32:54 -07001128 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1129 if (!child_node || get_node_path_locked(child_node,
1130 old_child_path, sizeof(old_child_path)) < 0) {
1131 res = -ENOENT;
1132 goto lookup_error;
1133 }
1134 acquire_node_locked(child_node);
1135 pthread_mutex_unlock(&fuse->lock);
1136
1137 /* Special case for renaming a file where destination is same path
1138 * differing only by case. In this case we don't want to look for a case
1139 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1140 */
1141 int search = old_parent_node != new_parent_node
1142 || strcasecmp(old_name, new_name);
1143 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1144 new_child_path, sizeof(new_child_path), search))) {
1145 res = -ENOENT;
1146 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001147 }
1148
Jeff Brown6249b902012-05-26 14:32:54 -07001149 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1150 res = rename(old_child_path, new_child_path);
1151 if (res < 0) {
1152 res = -errno;
1153 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001154 }
1155
Jeff Brown6249b902012-05-26 14:32:54 -07001156 pthread_mutex_lock(&fuse->lock);
1157 res = rename_node_locked(child_node, new_name, new_actual_name);
1158 if (!res) {
1159 remove_node_from_parent_locked(child_node);
1160 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001161 }
Jeff Brown6249b902012-05-26 14:32:54 -07001162 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001163
Jeff Brown6249b902012-05-26 14:32:54 -07001164io_error:
1165 pthread_mutex_lock(&fuse->lock);
1166done:
1167 release_node_locked(child_node);
1168lookup_error:
1169 pthread_mutex_unlock(&fuse->lock);
1170 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001171}
1172
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001173static int open_flags_to_access_mode(int open_flags) {
1174 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1175 return R_OK;
1176 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1177 return W_OK;
1178 } else {
1179 /* Probably O_RDRW, but treat as default to be safe */
1180 return R_OK | W_OK;
1181 }
1182}
1183
Jeff Brown6249b902012-05-26 14:32:54 -07001184static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001185 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1186{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001187 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001188 struct node* node;
1189 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001191 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001192
Jeff Brown6249b902012-05-26 14:32:54 -07001193 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001194 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001195 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001196 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001197 req->flags, hdr->nodeid, node ? node->name : "?");
1198 pthread_mutex_unlock(&fuse->lock);
1199
1200 if (!node) {
1201 return -ENOENT;
1202 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001203 if (!check_caller_access_to_node(fuse, hdr, node,
1204 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001205 return -EACCES;
1206 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 h = malloc(sizeof(*h));
1208 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001209 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001210 }
Jeff Brown6249b902012-05-26 14:32:54 -07001211 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 h->fd = open(path, req->flags);
1213 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001215 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216 }
1217 out.fh = ptr_to_id(h);
1218 out.open_flags = 0;
1219 out.padding = 0;
1220 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001221 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222}
1223
Jeff Brown6249b902012-05-26 14:32:54 -07001224static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1226{
1227 struct handle *h = id_to_ptr(req->fh);
1228 __u64 unique = hdr->unique;
1229 __u32 size = req->size;
1230 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001231 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001232 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001233
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001234 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1235 * overlaps the request buffer and will clobber data in the request. This
1236 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001237
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001238 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1239 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001240 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001241 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001242 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001243 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001245 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001246 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001247 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001248 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001249}
1250
Jeff Brown6249b902012-05-26 14:32:54 -07001251static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001252 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1253 const void* buffer)
1254{
1255 struct fuse_write_out out;
1256 struct handle *h = id_to_ptr(req->fh);
1257 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001258 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001259
Arpad Horvath49e93442014-02-18 10:18:25 +01001260 if (req->flags & O_DIRECT) {
1261 memcpy(aligned_buffer, buffer, req->size);
1262 buffer = (const __u8*) aligned_buffer;
1263 }
Jeff Brown6249b902012-05-26 14:32:54 -07001264
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001265 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001266 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001267 res = pwrite64(h->fd, buffer, req->size, req->offset);
1268 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001269 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001270 }
1271 out.size = res;
1272 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001273 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274}
1275
Jeff Brown6249b902012-05-26 14:32:54 -07001276static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001277 const struct fuse_in_header* hdr)
1278{
Jeff Brown6249b902012-05-26 14:32:54 -07001279 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001280 struct statfs stat;
1281 struct fuse_statfs_out out;
1282 int res;
1283
Jeff Brown6249b902012-05-26 14:32:54 -07001284 pthread_mutex_lock(&fuse->lock);
1285 TRACE("[%d] STATFS\n", handler->token);
1286 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1287 pthread_mutex_unlock(&fuse->lock);
1288 if (res < 0) {
1289 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001290 }
Jeff Brown6249b902012-05-26 14:32:54 -07001291 if (statfs(fuse->root.name, &stat) < 0) {
1292 return -errno;
1293 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001294 memset(&out, 0, sizeof(out));
1295 out.st.blocks = stat.f_blocks;
1296 out.st.bfree = stat.f_bfree;
1297 out.st.bavail = stat.f_bavail;
1298 out.st.files = stat.f_files;
1299 out.st.ffree = stat.f_ffree;
1300 out.st.bsize = stat.f_bsize;
1301 out.st.namelen = stat.f_namelen;
1302 out.st.frsize = stat.f_frsize;
1303 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001304 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001305}
1306
Jeff Brown6249b902012-05-26 14:32:54 -07001307static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1309{
1310 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001311
1312 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001313 close(h->fd);
1314 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001315 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001316}
1317
Jeff Brown6249b902012-05-26 14:32:54 -07001318static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001319 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1320{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001321 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1322 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001323
Elliott Hughesf6d67372014-07-08 14:38:26 -07001324 int fd = -1;
1325 if (is_dir) {
1326 struct dirhandle *dh = id_to_ptr(req->fh);
1327 fd = dirfd(dh->d);
1328 } else {
1329 struct handle *h = id_to_ptr(req->fh);
1330 fd = h->fd;
1331 }
1332
1333 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1334 is_dir ? "FSYNCDIR" : "FSYNC",
1335 id_to_ptr(req->fh), fd, is_data_sync);
1336 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1337 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001338 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001339 }
Jeff Brown6249b902012-05-26 14:32:54 -07001340 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341}
1342
Jeff Brown6249b902012-05-26 14:32:54 -07001343static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344 const struct fuse_in_header* hdr)
1345{
Jeff Brown6249b902012-05-26 14:32:54 -07001346 TRACE("[%d] FLUSH\n", handler->token);
1347 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348}
1349
Jeff Brown6249b902012-05-26 14:32:54 -07001350static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001351 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1352{
Jeff Brown6249b902012-05-26 14:32:54 -07001353 struct node* node;
1354 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001356 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001357
Jeff Brown6249b902012-05-26 14:32:54 -07001358 pthread_mutex_lock(&fuse->lock);
1359 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001360 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001361 hdr->nodeid, node ? node->name : "?");
1362 pthread_mutex_unlock(&fuse->lock);
1363
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001364 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001365 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001366 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001367 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001368 return -EACCES;
1369 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001370 h = malloc(sizeof(*h));
1371 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001372 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001373 }
Jeff Brown6249b902012-05-26 14:32:54 -07001374 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001376 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001378 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001379 }
1380 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001381 out.open_flags = 0;
1382 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001383 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001384 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001385}
1386
Jeff Brown6249b902012-05-26 14:32:54 -07001387static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1389{
1390 char buffer[8192];
1391 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1392 struct dirent *de;
1393 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001394
1395 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001396 if (req->offset == 0) {
1397 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001398 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001399 rewinddir(h->d);
1400 }
1401 de = readdir(h->d);
1402 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001403 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001404 }
1405 fde->ino = FUSE_UNKNOWN_INO;
1406 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1407 fde->off = req->offset + 1;
1408 fde->type = de->d_type;
1409 fde->namelen = strlen(de->d_name);
1410 memcpy(fde->name, de->d_name, fde->namelen + 1);
1411 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001412 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1413 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001414}
1415
Jeff Brown6249b902012-05-26 14:32:54 -07001416static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001417 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1418{
1419 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001420
1421 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001422 closedir(h->d);
1423 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001424 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425}
1426
Jeff Brown6249b902012-05-26 14:32:54 -07001427static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001428 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1429{
1430 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001431 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001432
Jeff Brown6249b902012-05-26 14:32:54 -07001433 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1434 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001435
1436 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1437 * defined (fuse version 7.6). The structure is the same from 7.6 through
1438 * 7.22. Beginning with 7.23, the structure increased in size and added
1439 * new parameters.
1440 */
1441 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1442 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1443 req->major, req->minor, FUSE_KERNEL_VERSION);
1444 return -1;
1445 }
1446
1447 out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
1448 fuse_struct_size = sizeof(out);
1449#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1450 /* FUSE_KERNEL_VERSION >= 23. */
1451
1452 /* If the kernel only works on minor revs older than or equal to 22,
1453 * then use the older structure size since this code only uses the 7.22
1454 * version of the structure. */
1455 if (req->minor <= 22) {
1456 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1457 }
1458#endif
1459
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001460 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001461 out.max_readahead = req->max_readahead;
1462 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1463 out.max_background = 32;
1464 out.congestion_threshold = 32;
1465 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001466 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001467 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001468}
1469
Jeff Brown6249b902012-05-26 14:32:54 -07001470static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001471 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1472{
Brian Swetland03ee9472010-08-12 18:01:08 -07001473 switch (hdr->opcode) {
1474 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001475 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001476 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001477 }
Jeff Brown84715842012-05-25 14:07:47 -07001478
Brian Swetland03ee9472010-08-12 18:01:08 -07001479 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001480 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001481 return handle_forget(fuse, handler, hdr, req);
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_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001485 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001486 return handle_getattr(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_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001490 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001491 return handle_setattr(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_READLINK:
1495// case FUSE_SYMLINK:
1496 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001497 const struct fuse_mknod_in *req = data;
1498 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001499 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001500 }
Jeff Brown84715842012-05-25 14:07:47 -07001501
Brian Swetland03ee9472010-08-12 18:01:08 -07001502 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001503 const struct fuse_mkdir_in *req = data;
1504 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001505 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001506 }
Jeff Brown84715842012-05-25 14:07:47 -07001507
Brian Swetland03ee9472010-08-12 18:01:08 -07001508 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001509 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001510 return handle_unlink(fuse, handler, hdr, 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_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001514 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001515 return handle_rmdir(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_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001519 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001520 const char *old_name = ((const char*) data) + sizeof(*req);
1521 const char *new_name = old_name + strlen(old_name) + 1;
1522 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 }
Jeff Brown84715842012-05-25 14:07:47 -07001524
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001525// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001526 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001527 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001528 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 }
Jeff Brown84715842012-05-25 14:07:47 -07001530
Brian Swetland03ee9472010-08-12 18:01:08 -07001531 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001532 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001533 return handle_read(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_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001537 const struct fuse_write_in *req = data;
1538 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001539 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001540 }
Jeff Brown84715842012-05-25 14:07:47 -07001541
Mike Lockwood4553b082010-08-16 14:14:44 -04001542 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001543 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001544 }
Jeff Brown84715842012-05-25 14:07:47 -07001545
Brian Swetland03ee9472010-08-12 18:01:08 -07001546 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001547 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001548 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001549 }
Jeff Brown84715842012-05-25 14:07:47 -07001550
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001551 case FUSE_FSYNC:
1552 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001553 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001554 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001555 }
1556
Brian Swetland03ee9472010-08-12 18:01:08 -07001557// case FUSE_SETXATTR:
1558// case FUSE_GETXATTR:
1559// case FUSE_LISTXATTR:
1560// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001561 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001562 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001563 }
1564
Brian Swetland03ee9472010-08-12 18:01:08 -07001565 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001566 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001567 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001568 }
Jeff Brown84715842012-05-25 14:07:47 -07001569
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001571 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001572 return handle_readdir(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_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001576 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001577 return handle_releasedir(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_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001581 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001582 return handle_init(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 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001586 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001587 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1588 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001589 }
Jeff Brown84715842012-05-25 14:07:47 -07001590 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001591}
1592
Jeff Brown6249b902012-05-26 14:32:54 -07001593static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001594{
Jeff Brown6249b902012-05-26 14:32:54 -07001595 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001596 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001597 ssize_t len = read(fuse->fd,
1598 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001599 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001600 if (errno != EINTR) {
1601 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1602 }
1603 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001604 }
Jeff Brown84715842012-05-25 14:07:47 -07001605
1606 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001607 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1608 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001609 }
1610
Jeff Brown7729d242012-05-25 15:35:28 -07001611 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001612 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001613 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1614 handler->token, (size_t)len, hdr->len);
1615 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001616 }
1617
Jeff Brown7729d242012-05-25 15:35:28 -07001618 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001619 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001620 __u64 unique = hdr->unique;
1621 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001622
1623 /* We do not access the request again after this point because the underlying
1624 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001625
1626 if (res != NO_STATUS) {
1627 if (res) {
1628 TRACE("[%d] ERROR %d\n", handler->token, res);
1629 }
1630 fuse_status(fuse, unique, res);
1631 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001632 }
1633}
1634
Jeff Brown6249b902012-05-26 14:32:54 -07001635static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001636{
Jeff Brown6249b902012-05-26 14:32:54 -07001637 struct fuse_handler* handler = data;
1638 handle_fuse_requests(handler);
1639 return NULL;
1640}
1641
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001642static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001643 Hashmap* map = context;
1644 hashmapRemove(map, key);
1645 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001646 return true;
1647}
1648
1649static bool remove_int_to_null(void *key, void *value, void *context) {
1650 Hashmap* map = context;
1651 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001652 return true;
1653}
1654
1655static int read_package_list(struct fuse *fuse) {
1656 pthread_mutex_lock(&fuse->lock);
1657
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001658 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1659 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001660
1661 FILE* file = fopen(kPackagesListFile, "r");
1662 if (!file) {
1663 ERROR("failed to open package list: %s\n", strerror(errno));
1664 pthread_mutex_unlock(&fuse->lock);
1665 return -1;
1666 }
1667
1668 char buf[512];
1669 while (fgets(buf, sizeof(buf), file) != NULL) {
1670 char package_name[512];
1671 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001672 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001673
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001674 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1675 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001676 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001677
1678 char* token = strtok(gids, ",");
1679 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001680 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001681 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001682 break;
1683 }
1684 token = strtok(NULL, ",");
1685 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001686 }
1687 }
1688
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001689 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001690 hashmapSize(fuse->package_to_appid),
1691 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001692 fclose(file);
1693 pthread_mutex_unlock(&fuse->lock);
1694 return 0;
1695}
1696
1697static void watch_package_list(struct fuse* fuse) {
1698 struct inotify_event *event;
1699 char event_buf[512];
1700
1701 int nfd = inotify_init();
1702 if (nfd < 0) {
1703 ERROR("inotify_init failed: %s\n", strerror(errno));
1704 return;
1705 }
1706
1707 bool active = false;
1708 while (1) {
1709 if (!active) {
1710 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1711 if (res == -1) {
1712 if (errno == ENOENT || errno == EACCES) {
1713 /* Framework may not have created yet, sleep and retry */
1714 ERROR("missing packages.list; retrying\n");
1715 sleep(3);
1716 continue;
1717 } else {
1718 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1719 return;
1720 }
1721 }
1722
1723 /* Watch above will tell us about any future changes, so
1724 * read the current state. */
1725 if (read_package_list(fuse) == -1) {
1726 ERROR("read_package_list failed: %s\n", strerror(errno));
1727 return;
1728 }
1729 active = true;
1730 }
1731
1732 int event_pos = 0;
1733 int res = read(nfd, event_buf, sizeof(event_buf));
1734 if (res < (int) sizeof(*event)) {
1735 if (errno == EINTR)
1736 continue;
1737 ERROR("failed to read inotify event: %s\n", strerror(errno));
1738 return;
1739 }
1740
1741 while (res >= (int) sizeof(*event)) {
1742 int event_size;
1743 event = (struct inotify_event *) (event_buf + event_pos);
1744
1745 TRACE("inotify event: %08x\n", event->mask);
1746 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1747 /* Previously watched file was deleted, probably due to move
1748 * that swapped in new data; re-arm the watch and read. */
1749 active = false;
1750 }
1751
1752 event_size = sizeof(*event) + event->len;
1753 res -= event_size;
1754 event_pos += event_size;
1755 }
1756 }
1757}
1758
Jeff Brown6249b902012-05-26 14:32:54 -07001759static int ignite_fuse(struct fuse* fuse, int num_threads)
1760{
1761 struct fuse_handler* handlers;
1762 int i;
1763
1764 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1765 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001766 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001767 return -ENOMEM;
1768 }
1769
1770 for (i = 0; i < num_threads; i++) {
1771 handlers[i].fuse = fuse;
1772 handlers[i].token = i;
1773 }
1774
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001775 /* When deriving permissions, this thread is used to process inotify events,
1776 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001777 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001778 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001779 pthread_t thread;
1780 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1781 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001782 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001783 goto quit;
1784 }
1785 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001786
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001787 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001788 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001789 } else {
1790 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001791 }
1792
1793 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001794
1795 /* don't bother killing all of the other threads or freeing anything,
1796 * should never get here anyhow */
1797quit:
1798 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001799}
1800
Mike Lockwood4f35e622011-01-12 14:39:44 -05001801static int usage()
1802{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001803 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1804 " -u: specify UID to run as\n"
1805 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001806 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001807 " -t: specify number of threads to use (default %d)\n"
1808 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001809 " -l: derive file permissions based on legacy internal layout\n"
1810 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001811 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001812 return 1;
1813}
1814
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001815static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001816 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1817 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001818 int fd;
1819 char opts[256];
1820 int res;
1821 struct fuse fuse;
1822
1823 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001824 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001825
1826 fd = open("/dev/fuse", O_RDWR);
1827 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001828 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001829 return -1;
1830 }
1831
1832 snprintf(opts, sizeof(opts),
1833 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1834 fd, uid, gid);
1835
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001836 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001837 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001838 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1839 goto error;
1840 }
1841
1842 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1843 if (res < 0) {
1844 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001845 goto error;
1846 }
1847
1848 res = setgid(gid);
1849 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001850 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001851 goto error;
1852 }
1853
1854 res = setuid(uid);
1855 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001856 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001857 goto error;
1858 }
1859
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001860 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001861
1862 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001863 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001864
1865 /* we do not attempt to umount the file system here because we are no longer
1866 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001867
1868error:
1869 close(fd);
1870 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001871}
1872
Brian Swetland03ee9472010-08-12 18:01:08 -07001873int main(int argc, char **argv)
1874{
Brian Swetland03ee9472010-08-12 18:01:08 -07001875 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001876 const char *source_path = NULL;
1877 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001878 uid_t uid = 0;
1879 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001880 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001881 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001882 derive_t derive = DERIVE_NONE;
1883 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001884 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001885 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001886 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001887
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001888 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001889 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001890 switch (opt) {
1891 case 'u':
1892 uid = strtoul(optarg, NULL, 10);
1893 break;
1894 case 'g':
1895 gid = strtoul(optarg, NULL, 10);
1896 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001897 case 'w':
1898 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001899 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001900 case 't':
1901 num_threads = strtoul(optarg, NULL, 10);
1902 break;
1903 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001904 derive = DERIVE_UNIFIED;
1905 break;
1906 case 'l':
1907 derive = DERIVE_LEGACY;
1908 break;
1909 case 's':
1910 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001911 break;
1912 case '?':
1913 default:
1914 return usage();
1915 }
1916 }
1917
1918 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001919 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001920 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001921 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001922 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001923 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001924 } else if (!uid) {
1925 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001926 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001927 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001928 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001929 ERROR("too many arguments\n");
1930 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001931 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001932 }
1933
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001934 if (!source_path) {
1935 ERROR("no source path specified\n");
1936 return usage();
1937 }
1938 if (!dest_path) {
1939 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001940 return usage();
1941 }
Jeff Brown26567352012-05-25 13:27:43 -07001942 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001943 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001944 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001945 }
Jeff Brown6249b902012-05-26 14:32:54 -07001946 if (num_threads < 1) {
1947 ERROR("number of threads must be at least 1\n");
1948 return usage();
1949 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001950 if (split_perms && derive == DERIVE_NONE) {
1951 ERROR("cannot split permissions without deriving\n");
1952 return usage();
1953 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001954
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001955 rlim.rlim_cur = 8192;
1956 rlim.rlim_max = 8192;
1957 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1958 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1959 }
1960
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001961 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1962 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1963 sleep(1);
1964 }
1965
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001966 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001967 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001968}