blob: f55a98a341804ded9e16d80aefc0c99e835159f4 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklandd3330872014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
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 Sharkey977a9f32013-08-12 20:23:49 -0700147 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700148 PERM_ANDROID_USER,
149} perm_t;
150
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700151/* Permissions structure to derive */
152typedef enum {
153 DERIVE_NONE,
154 DERIVE_LEGACY,
155 DERIVE_UNIFIED,
156} derive_t;
157
Brian Swetland03ee9472010-08-12 18:01:08 -0700158struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700159 int fd;
160};
161
162struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700163 DIR *d;
164};
165
166struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700167 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700168 __u64 nid;
169 __u64 gen;
170
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700171 /* State derived based on current position in hierarchy. */
172 perm_t perm;
173 userid_t userid;
174 uid_t uid;
175 gid_t gid;
176 mode_t mode;
177
Paul Eastham11ccdb32010-10-14 11:04:26 -0700178 struct node *next; /* per-dir sibling list */
179 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700180 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700181
Jeff Brown6249b902012-05-26 14:32:54 -0700182 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700183 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800184 /* If non-null, this is the real name of the file in the underlying storage.
185 * This may differ from the field "name" only by case.
186 * strlen(actual_name) will always equal strlen(name), so it is safe to use
187 * namelen for both fields.
188 */
189 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700190
191 /* If non-null, an exact underlying path that should be grafted into this
192 * position. Used to support things like OBB. */
193 char* graft_path;
194 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700195};
196
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700197static int str_hash(void *key) {
198 return hashmapHash(key, strlen(key));
199}
200
Jeff Sharkey44d63422013-09-12 09:44:48 -0700201/** Test if two string keys are equal ignoring case */
202static bool str_icase_equals(void *keyA, void *keyB) {
203 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700204}
205
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700206static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800207 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700208}
209
210static bool int_equals(void *keyA, void *keyB) {
211 return keyA == keyB;
212}
213
Jeff Brown7729d242012-05-25 15:35:28 -0700214/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700215struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700216 pthread_mutex_t lock;
217
Brian Swetland03ee9472010-08-12 18:01:08 -0700218 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700219 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700220 derive_t derive;
221 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700222 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700223 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700224 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700225
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700226 Hashmap* package_to_appid;
227 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700228};
229
Jeff Brown7729d242012-05-25 15:35:28 -0700230/* Private data used by a single fuse handler. */
231struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700232 struct fuse* fuse;
233 int token;
234
Jeff Brown7729d242012-05-25 15:35:28 -0700235 /* To save memory, we never use the contents of the request buffer and the read
236 * buffer at the same time. This allows us to share the underlying storage. */
237 union {
238 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800239 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700240 };
241};
Brian Swetland03ee9472010-08-12 18:01:08 -0700242
Jeff Brown6249b902012-05-26 14:32:54 -0700243static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700244{
Jeff Brown6249b902012-05-26 14:32:54 -0700245 return (void *) (uintptr_t) nid;
246}
Brian Swetland03ee9472010-08-12 18:01:08 -0700247
Jeff Brown6249b902012-05-26 14:32:54 -0700248static inline __u64 ptr_to_id(void *ptr)
249{
250 return (__u64) (uintptr_t) ptr;
251}
Brian Swetland03ee9472010-08-12 18:01:08 -0700252
Jeff Brown6249b902012-05-26 14:32:54 -0700253static void acquire_node_locked(struct node* node)
254{
255 node->refcount++;
256 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
257}
258
259static void remove_node_from_parent_locked(struct node* node);
260
261static void release_node_locked(struct node* node)
262{
263 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
264 if (node->refcount > 0) {
265 node->refcount--;
266 if (!node->refcount) {
267 TRACE("DESTROY %p (%s)\n", node, node->name);
268 remove_node_from_parent_locked(node);
269
270 /* TODO: remove debugging - poison memory */
271 memset(node->name, 0xef, node->namelen);
272 free(node->name);
273 free(node->actual_name);
274 memset(node, 0xfc, sizeof(*node));
275 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800276 }
Jeff Brown6249b902012-05-26 14:32:54 -0700277 } else {
278 ERROR("Zero refcnt %p\n", node);
279 }
280}
281
282static void add_node_to_parent_locked(struct node *node, struct node *parent) {
283 node->parent = parent;
284 node->next = parent->child;
285 parent->child = node;
286 acquire_node_locked(parent);
287}
288
289static void remove_node_from_parent_locked(struct node* node)
290{
291 if (node->parent) {
292 if (node->parent->child == node) {
293 node->parent->child = node->parent->child->next;
294 } else {
295 struct node *node2;
296 node2 = node->parent->child;
297 while (node2->next != node)
298 node2 = node2->next;
299 node2->next = node->next;
300 }
301 release_node_locked(node->parent);
302 node->parent = NULL;
303 node->next = NULL;
304 }
305}
306
307/* Gets the absolute path to a node into the provided buffer.
308 *
309 * Populates 'buf' with the path and returns the length of the path on success,
310 * or returns -1 if the path is too long for the provided buffer.
311 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700312static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
313 const char* name;
314 size_t namelen;
315 if (node->graft_path) {
316 name = node->graft_path;
317 namelen = node->graft_pathlen;
318 } else if (node->actual_name) {
319 name = node->actual_name;
320 namelen = node->namelen;
321 } else {
322 name = node->name;
323 namelen = node->namelen;
324 }
325
Jeff Brown6249b902012-05-26 14:32:54 -0700326 if (bufsize < namelen + 1) {
327 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700328 }
329
Jeff Brown6249b902012-05-26 14:32:54 -0700330 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700331 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700332 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
333 if (pathlen < 0) {
334 return -1;
335 }
336 buf[pathlen++] = '/';
337 }
338
Jeff Brown6249b902012-05-26 14:32:54 -0700339 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
340 return pathlen + namelen;
341}
342
343/* Finds the absolute path of a file within a given directory.
344 * Performs a case-insensitive search for the file and sets the buffer to the path
345 * of the first matching file. If 'search' is zero or if no match is found, sets
346 * the buffer to the path that the file would have, assuming the name were case-sensitive.
347 *
348 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
349 * or returns NULL if the path is too long for the provided buffer.
350 */
351static char* find_file_within(const char* path, const char* name,
352 char* buf, size_t bufsize, int search)
353{
354 size_t pathlen = strlen(path);
355 size_t namelen = strlen(name);
356 size_t childlen = pathlen + namelen + 1;
357 char* actual;
358
359 if (bufsize <= childlen) {
360 return NULL;
361 }
362
363 memcpy(buf, path, pathlen);
364 buf[pathlen] = '/';
365 actual = buf + pathlen + 1;
366 memcpy(actual, name, namelen + 1);
367
368 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800369 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700370 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800371 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700372 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700373 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800374 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800375 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700376 if (!strcasecmp(entry->d_name, name)) {
377 /* we have a match - replace the name, don't need to copy the null again */
378 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800379 break;
380 }
381 }
382 closedir(dir);
383 }
Jeff Brown6249b902012-05-26 14:32:54 -0700384 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800385}
386
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700387static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800388{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700389 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700390 attr->size = s->st_size;
391 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400392 attr->atime = s->st_atime;
393 attr->mtime = s->st_mtime;
394 attr->ctime = s->st_ctime;
395 attr->atimensec = s->st_atime_nsec;
396 attr->mtimensec = s->st_mtime_nsec;
397 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700398 attr->mode = s->st_mode;
399 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700400
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700401 attr->uid = node->uid;
402 attr->gid = node->gid;
403
404 /* Filter requested mode based on underlying file, and
405 * pass through file type. */
406 int owner_mode = s->st_mode & 0700;
407 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
408 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
409}
410
Jeff Sharkey44d63422013-09-12 09:44:48 -0700411static int touch(char* path, mode_t mode) {
412 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
413 if (fd == -1) {
414 if (errno == EEXIST) {
415 return 0;
416 } else {
417 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
418 return -1;
419 }
420 }
421 close(fd);
422 return 0;
423}
424
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700425static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
426 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700427 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700428
429 /* By default, each node inherits from its parent */
430 node->perm = PERM_INHERIT;
431 node->userid = parent->userid;
432 node->uid = parent->uid;
433 node->gid = parent->gid;
434 node->mode = parent->mode;
435
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700436 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700437 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700438 }
439
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700440 /* Derive custom permissions based on parent and current node */
441 switch (parent->perm) {
442 case PERM_INHERIT:
443 /* Already inherited above */
444 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700445 case PERM_LEGACY_PRE_ROOT:
446 /* Legacy internal layout places users at top level */
447 node->perm = PERM_ROOT;
448 node->userid = strtoul(node->name, NULL, 10);
449 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700450 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700451 /* Assume masked off by default. */
452 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700453 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700454 /* App-specific directories inside; let anyone traverse */
455 node->perm = PERM_ANDROID;
456 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700457 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700458 if (!strcasecmp(node->name, "DCIM")
459 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700460 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700461 } else if (!strcasecmp(node->name, "Alarms")
462 || !strcasecmp(node->name, "Movies")
463 || !strcasecmp(node->name, "Music")
464 || !strcasecmp(node->name, "Notifications")
465 || !strcasecmp(node->name, "Podcasts")
466 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700467 node->gid = AID_SDCARD_AV;
468 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700469 }
470 break;
471 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700472 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700473 /* App-specific directories inside; let anyone traverse */
474 node->perm = PERM_ANDROID_DATA;
475 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700476 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700477 /* App-specific directories inside; let anyone traverse */
478 node->perm = PERM_ANDROID_OBB;
479 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700480 /* Single OBB directory is always shared */
481 node->graft_path = fuse->obbpath;
482 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700483 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700484 /* User directories must only be accessible to system, protected
485 * by sdcard_all. Zygote will bind mount the appropriate user-
486 * specific path. */
487 node->perm = PERM_ANDROID_USER;
488 node->gid = AID_SDCARD_ALL;
489 node->mode = 0770;
490 }
491 break;
492 case PERM_ANDROID_DATA:
493 case PERM_ANDROID_OBB:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800494 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700495 if (appid != 0) {
496 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700497 }
498 node->mode = 0770;
499 break;
500 case PERM_ANDROID_USER:
501 /* Root of a secondary user */
502 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700503 node->userid = strtoul(node->name, NULL, 10);
504 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700505 node->mode = 0771;
506 break;
507 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700508}
509
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700510/* Return if the calling UID holds sdcard_rw. */
511static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700512 /* No additional permissions enforcement */
513 if (fuse->derive == DERIVE_NONE) {
514 return true;
515 }
516
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700517 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800518 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700519}
520
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700521/* Kernel has already enforced everything we returned through
522 * derive_permissions_locked(), so this is used to lock down access
523 * even further, such as enforcing that apps hold sdcard_rw. */
524static bool check_caller_access_to_name(struct fuse* fuse,
525 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700526 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700527 /* Always block security-sensitive files at root */
528 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700529 if (!strcasecmp(name, "autorun.inf")
530 || !strcasecmp(name, ".android_secure")
531 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700532 return false;
533 }
534 }
535
536 /* No additional permissions enforcement */
537 if (fuse->derive == DERIVE_NONE) {
538 return true;
539 }
540
Jeff Sharkey44d63422013-09-12 09:44:48 -0700541 /* Root always has access; access for any other UIDs should always
542 * be controlled through packages.list. */
543 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700544 return true;
545 }
546
547 /* If asking to write, verify that caller either owns the
548 * parent or holds sdcard_rw. */
549 if (mode & W_OK) {
550 if (parent_node && hdr->uid == parent_node->uid) {
551 return true;
552 }
553
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700554 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700555 }
556
557 /* No extra permissions to enforce */
558 return true;
559}
560
561static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700562 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
563 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700564}
565
Jeff Brown6249b902012-05-26 14:32:54 -0700566struct node *create_node_locked(struct fuse* fuse,
567 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700568{
569 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700570 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700571
Paul Eastham11ccdb32010-10-14 11:04:26 -0700572 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700573 if (!node) {
574 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700575 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700576 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700577 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700578 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700579 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700580 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700581 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700582 if (strcmp(name, actual_name)) {
583 node->actual_name = malloc(namelen + 1);
584 if (!node->actual_name) {
585 free(node->name);
586 free(node);
587 return NULL;
588 }
589 memcpy(node->actual_name, actual_name, namelen + 1);
590 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700591 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700592 node->nid = ptr_to_id(node);
593 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700594
595 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700596 acquire_node_locked(node);
597 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700598 return node;
599}
600
Jeff Brown6249b902012-05-26 14:32:54 -0700601static int rename_node_locked(struct node *node, const char *name,
602 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700603{
Jeff Brown6249b902012-05-26 14:32:54 -0700604 size_t namelen = strlen(name);
605 int need_actual_name = strcmp(name, actual_name);
606
607 /* make the storage bigger without actually changing the name
608 * in case an error occurs part way */
609 if (namelen > node->namelen) {
610 char* new_name = realloc(node->name, namelen + 1);
611 if (!new_name) {
612 return -ENOMEM;
613 }
614 node->name = new_name;
615 if (need_actual_name && node->actual_name) {
616 char* new_actual_name = realloc(node->actual_name, namelen + 1);
617 if (!new_actual_name) {
618 return -ENOMEM;
619 }
620 node->actual_name = new_actual_name;
621 }
622 }
623
624 /* update the name, taking care to allocate storage before overwriting the old name */
625 if (need_actual_name) {
626 if (!node->actual_name) {
627 node->actual_name = malloc(namelen + 1);
628 if (!node->actual_name) {
629 return -ENOMEM;
630 }
631 }
632 memcpy(node->actual_name, actual_name, namelen + 1);
633 } else {
634 free(node->actual_name);
635 node->actual_name = NULL;
636 }
637 memcpy(node->name, name, namelen + 1);
638 node->namelen = namelen;
639 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700640}
641
Jeff Brown6249b902012-05-26 14:32:54 -0700642static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700643{
Jeff Brown6249b902012-05-26 14:32:54 -0700644 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700645 return &fuse->root;
646 } else {
647 return id_to_ptr(nid);
648 }
649}
650
Jeff Brown6249b902012-05-26 14:32:54 -0700651static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
652 char* buf, size_t bufsize)
653{
654 struct node* node = lookup_node_by_id_locked(fuse, nid);
655 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
656 node = NULL;
657 }
658 return node;
659}
660
661static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700662{
663 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700664 /* use exact string comparison, nodes that differ by case
665 * must be considered distinct even if they refer to the same
666 * underlying file as otherwise operations such as "mv x x"
667 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700668 if (!strcmp(name, node->name)) {
669 return node;
670 }
671 }
672 return 0;
673}
674
Jeff Brown6249b902012-05-26 14:32:54 -0700675static struct node* acquire_or_create_child_locked(
676 struct fuse* fuse, struct node* parent,
677 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700678{
Jeff Brown6249b902012-05-26 14:32:54 -0700679 struct node* child = lookup_child_by_name_locked(parent, name);
680 if (child) {
681 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800682 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700683 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800684 }
Jeff Brown6249b902012-05-26 14:32:54 -0700685 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700686}
687
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700688static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700689 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700690 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700691
Jeff Brown6249b902012-05-26 14:32:54 -0700692 fuse->fd = fd;
693 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700694 fuse->derive = derive;
695 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700696 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700697
Jeff Brown6249b902012-05-26 14:32:54 -0700698 memset(&fuse->root, 0, sizeof(fuse->root));
699 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
700 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700701 fuse->root.namelen = strlen(source_path);
702 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700703 fuse->root.userid = 0;
704 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700705
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700706 /* Set up root node for various modes of operation */
707 switch (derive) {
708 case DERIVE_NONE:
709 /* Traditional behavior that treats entire device as being accessible
710 * to sdcard_rw, and no permissions are derived. */
711 fuse->root.perm = PERM_ROOT;
712 fuse->root.mode = 0775;
713 fuse->root.gid = AID_SDCARD_RW;
714 break;
715 case DERIVE_LEGACY:
716 /* Legacy behavior used to support internal multiuser layout which
717 * places user_id at the top directory level, with the actual roots
718 * just below that. Shared OBB path is also at top level. */
719 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
720 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700721 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700722 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700723 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
724 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700725 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700726 break;
727 case DERIVE_UNIFIED:
728 /* Unified multiuser layout which places secondary user_id under
729 * /Android/user and shared OBB path under /Android/obb. */
730 fuse->root.perm = PERM_ROOT;
731 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700732 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700733 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700734 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
735 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
736 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700737 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700738}
739
Jeff Brown6249b902012-05-26 14:32:54 -0700740static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700741{
742 struct fuse_out_header hdr;
743 hdr.len = sizeof(hdr);
744 hdr.error = err;
745 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700746 write(fuse->fd, &hdr, sizeof(hdr));
747}
748
Jeff Brown6249b902012-05-26 14:32:54 -0700749static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700750{
751 struct fuse_out_header hdr;
752 struct iovec vec[2];
753 int res;
754
755 hdr.len = len + sizeof(hdr);
756 hdr.error = 0;
757 hdr.unique = unique;
758
759 vec[0].iov_base = &hdr;
760 vec[0].iov_len = sizeof(hdr);
761 vec[1].iov_base = data;
762 vec[1].iov_len = len;
763
764 res = writev(fuse->fd, vec, 2);
765 if (res < 0) {
766 ERROR("*** REPLY FAILED *** %d\n", errno);
767 }
768}
769
Jeff Brown6249b902012-05-26 14:32:54 -0700770static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
771 struct node* parent, const char* name, const char* actual_name,
772 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700773{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700774 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700775 struct fuse_entry_out out;
776 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700777
Jeff Brown6249b902012-05-26 14:32:54 -0700778 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700779 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700780 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700781
Jeff Brown6249b902012-05-26 14:32:54 -0700782 pthread_mutex_lock(&fuse->lock);
783 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
784 if (!node) {
785 pthread_mutex_unlock(&fuse->lock);
786 return -ENOMEM;
787 }
788 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700789 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700790 out.attr_valid = 10;
791 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700792 out.nodeid = node->nid;
793 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700794 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700795 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700796 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700797}
798
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700799static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700800 const char* path)
801{
802 struct fuse_attr_out out;
803 struct stat s;
804
805 if (lstat(path, &s) < 0) {
806 return -errno;
807 }
808 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700809 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700810 out.attr_valid = 10;
811 fuse_reply(fuse, unique, &out, sizeof(out));
812 return NO_STATUS;
813}
814
815static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700816 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700817{
Jeff Brown6249b902012-05-26 14:32:54 -0700818 struct node* parent_node;
819 char parent_path[PATH_MAX];
820 char child_path[PATH_MAX];
821 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700822
Jeff Brown6249b902012-05-26 14:32:54 -0700823 pthread_mutex_lock(&fuse->lock);
824 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
825 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100826 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700827 parent_node ? parent_node->name : "?");
828 pthread_mutex_unlock(&fuse->lock);
829
830 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
831 child_path, sizeof(child_path), 1))) {
832 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700833 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700834 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700835 return -EACCES;
836 }
837
Jeff Brown6249b902012-05-26 14:32:54 -0700838 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839}
840
Jeff Brown6249b902012-05-26 14:32:54 -0700841static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700842 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
843{
Jeff Brown6249b902012-05-26 14:32:54 -0700844 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700845
Jeff Brown6249b902012-05-26 14:32:54 -0700846 pthread_mutex_lock(&fuse->lock);
847 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklandd3330872014-07-23 13:04:59 +0100848 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700849 hdr->nodeid, node ? node->name : "?");
850 if (node) {
851 __u64 n = req->nlookup;
852 while (n--) {
853 release_node_locked(node);
854 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700855 }
Jeff Brown6249b902012-05-26 14:32:54 -0700856 pthread_mutex_unlock(&fuse->lock);
857 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700858}
859
Jeff Brown6249b902012-05-26 14:32:54 -0700860static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
862{
Jeff Brown6249b902012-05-26 14:32:54 -0700863 struct node* node;
864 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865
Jeff Brown6249b902012-05-26 14:32:54 -0700866 pthread_mutex_lock(&fuse->lock);
867 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100868 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700869 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
870 pthread_mutex_unlock(&fuse->lock);
871
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700872 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700873 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700874 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700875 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700876 return -EACCES;
877 }
878
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700879 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880}
881
Jeff Brown6249b902012-05-26 14:32:54 -0700882static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
884{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700885 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700886 struct node* node;
887 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700889
Jeff Brown6249b902012-05-26 14:32:54 -0700890 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700891 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700892 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100893 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700894 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
895 pthread_mutex_unlock(&fuse->lock);
896
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700898 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700899 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700900 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700901 return -EACCES;
902 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700903
Jeff Brown6249b902012-05-26 14:32:54 -0700904 /* XXX: incomplete implementation on purpose.
905 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700906
Elliott Hughes853574d2014-07-31 12:03:03 -0700907 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700908 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700909 }
910
911 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
912 * are both set, then set it to the current time. Else, set it to the
913 * time specified in the request. Same goes for mtime. Use utimensat(2)
914 * as it allows ATIME and MTIME to be changed independently, and has
915 * nanosecond resolution which fuse also has.
916 */
917 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
918 times[0].tv_nsec = UTIME_OMIT;
919 times[1].tv_nsec = UTIME_OMIT;
920 if (req->valid & FATTR_ATIME) {
921 if (req->valid & FATTR_ATIME_NOW) {
922 times[0].tv_nsec = UTIME_NOW;
923 } else {
924 times[0].tv_sec = req->atime;
925 times[0].tv_nsec = req->atimensec;
926 }
927 }
928 if (req->valid & FATTR_MTIME) {
929 if (req->valid & FATTR_MTIME_NOW) {
930 times[1].tv_nsec = UTIME_NOW;
931 } else {
932 times[1].tv_sec = req->mtime;
933 times[1].tv_nsec = req->mtimensec;
934 }
935 }
Jeff Brown6249b902012-05-26 14:32:54 -0700936 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
937 handler->token, path, times[0].tv_sec, times[1].tv_sec);
938 if (utimensat(-1, path, times, 0) < 0) {
939 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700940 }
941 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700942 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700943}
944
Jeff Brown6249b902012-05-26 14:32:54 -0700945static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700946 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
947{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700948 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700949 struct node* parent_node;
950 char parent_path[PATH_MAX];
951 char child_path[PATH_MAX];
952 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700953
Jeff Brown6249b902012-05-26 14:32:54 -0700954 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700955 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700956 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
957 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100958 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700959 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
960 pthread_mutex_unlock(&fuse->lock);
961
962 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
963 child_path, sizeof(child_path), 1))) {
964 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700965 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700966 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700967 return -EACCES;
968 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700970 if (mknod(child_path, mode, req->rdev) < 0) {
971 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700972 }
Jeff Brown6249b902012-05-26 14:32:54 -0700973 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974}
975
Jeff Brown6249b902012-05-26 14:32:54 -0700976static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700977 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
978{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700979 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700980 struct node* parent_node;
981 char parent_path[PATH_MAX];
982 char child_path[PATH_MAX];
983 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984
Jeff Brown6249b902012-05-26 14:32:54 -0700985 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700986 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700987 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
988 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +0100989 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700990 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
991 pthread_mutex_unlock(&fuse->lock);
992
993 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
994 child_path, sizeof(child_path), 1))) {
995 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700997 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700998 return -EACCES;
999 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001000 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001001 if (mkdir(child_path, mode) < 0) {
1002 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001003 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001004
1005 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1006 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1007 char nomedia[PATH_MAX];
1008 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1009 if (touch(nomedia, 0664) != 0) {
1010 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1011 return -ENOENT;
1012 }
1013 }
1014 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1015 char nomedia[PATH_MAX];
1016 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1017 if (touch(nomedia, 0664) != 0) {
1018 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1019 return -ENOENT;
1020 }
1021 }
1022
Jeff Brown6249b902012-05-26 14:32:54 -07001023 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001024}
1025
Jeff Brown6249b902012-05-26 14:32:54 -07001026static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001027 const struct fuse_in_header* hdr, const char* name)
1028{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001029 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001030 struct node* parent_node;
1031 char parent_path[PATH_MAX];
1032 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001033
Jeff Brown6249b902012-05-26 14:32:54 -07001034 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001035 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001036 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1037 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001038 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001039 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1040 pthread_mutex_unlock(&fuse->lock);
1041
1042 if (!parent_node || !find_file_within(parent_path, name,
1043 child_path, sizeof(child_path), 1)) {
1044 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001045 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001046 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001047 return -EACCES;
1048 }
Jeff Brown6249b902012-05-26 14:32:54 -07001049 if (unlink(child_path) < 0) {
1050 return -errno;
1051 }
1052 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001053}
1054
Jeff Brown6249b902012-05-26 14:32:54 -07001055static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001056 const struct fuse_in_header* hdr, const char* name)
1057{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001058 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001059 struct node* parent_node;
1060 char parent_path[PATH_MAX];
1061 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001062
Jeff Brown6249b902012-05-26 14:32:54 -07001063 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001064 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001065 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1066 parent_path, sizeof(parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001067 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001068 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1069 pthread_mutex_unlock(&fuse->lock);
1070
1071 if (!parent_node || !find_file_within(parent_path, name,
1072 child_path, sizeof(child_path), 1)) {
1073 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001074 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001075 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001076 return -EACCES;
1077 }
Jeff Brown6249b902012-05-26 14:32:54 -07001078 if (rmdir(child_path) < 0) {
1079 return -errno;
1080 }
1081 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001082}
1083
Jeff Brown6249b902012-05-26 14:32:54 -07001084static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001085 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001086 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001087{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001088 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001089 struct node* old_parent_node;
1090 struct node* new_parent_node;
1091 struct node* child_node;
1092 char old_parent_path[PATH_MAX];
1093 char new_parent_path[PATH_MAX];
1094 char old_child_path[PATH_MAX];
1095 char new_child_path[PATH_MAX];
1096 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001097 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001098
Jeff Brown6249b902012-05-26 14:32:54 -07001099 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001100 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001101 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1102 old_parent_path, sizeof(old_parent_path));
1103 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1104 new_parent_path, sizeof(new_parent_path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001105 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001106 old_name, new_name,
1107 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1108 req->newdir, new_parent_node ? new_parent_node->name : "?");
1109 if (!old_parent_node || !new_parent_node) {
1110 res = -ENOENT;
1111 goto lookup_error;
1112 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001113 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001114 res = -EACCES;
1115 goto lookup_error;
1116 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001117 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001118 res = -EACCES;
1119 goto lookup_error;
1120 }
Jeff Brown6249b902012-05-26 14:32:54 -07001121 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1122 if (!child_node || get_node_path_locked(child_node,
1123 old_child_path, sizeof(old_child_path)) < 0) {
1124 res = -ENOENT;
1125 goto lookup_error;
1126 }
1127 acquire_node_locked(child_node);
1128 pthread_mutex_unlock(&fuse->lock);
1129
1130 /* Special case for renaming a file where destination is same path
1131 * differing only by case. In this case we don't want to look for a case
1132 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1133 */
1134 int search = old_parent_node != new_parent_node
1135 || strcasecmp(old_name, new_name);
1136 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1137 new_child_path, sizeof(new_child_path), search))) {
1138 res = -ENOENT;
1139 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001140 }
1141
Jeff Brown6249b902012-05-26 14:32:54 -07001142 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1143 res = rename(old_child_path, new_child_path);
1144 if (res < 0) {
1145 res = -errno;
1146 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001147 }
1148
Jeff Brown6249b902012-05-26 14:32:54 -07001149 pthread_mutex_lock(&fuse->lock);
1150 res = rename_node_locked(child_node, new_name, new_actual_name);
1151 if (!res) {
1152 remove_node_from_parent_locked(child_node);
1153 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001154 }
Jeff Brown6249b902012-05-26 14:32:54 -07001155 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156
Jeff Brown6249b902012-05-26 14:32:54 -07001157io_error:
1158 pthread_mutex_lock(&fuse->lock);
1159done:
1160 release_node_locked(child_node);
1161lookup_error:
1162 pthread_mutex_unlock(&fuse->lock);
1163 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001164}
1165
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001166static int open_flags_to_access_mode(int open_flags) {
1167 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1168 return R_OK;
1169 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1170 return W_OK;
1171 } else {
1172 /* Probably O_RDRW, but treat as default to be safe */
1173 return R_OK | W_OK;
1174 }
1175}
1176
Jeff Brown6249b902012-05-26 14:32:54 -07001177static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001178 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1179{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001180 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001181 struct node* node;
1182 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001184 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001185
Jeff Brown6249b902012-05-26 14:32:54 -07001186 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001187 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001188 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001189 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001190 req->flags, hdr->nodeid, node ? node->name : "?");
1191 pthread_mutex_unlock(&fuse->lock);
1192
1193 if (!node) {
1194 return -ENOENT;
1195 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001196 if (!check_caller_access_to_node(fuse, hdr, node,
1197 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001198 return -EACCES;
1199 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001200 h = malloc(sizeof(*h));
1201 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001202 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203 }
Jeff Brown6249b902012-05-26 14:32:54 -07001204 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205 h->fd = open(path, req->flags);
1206 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001208 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209 }
1210 out.fh = ptr_to_id(h);
1211 out.open_flags = 0;
1212 out.padding = 0;
1213 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001214 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001215}
1216
Jeff Brown6249b902012-05-26 14:32:54 -07001217static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001218 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1219{
1220 struct handle *h = id_to_ptr(req->fh);
1221 __u64 unique = hdr->unique;
1222 __u32 size = req->size;
1223 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001224 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001225 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001226
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1228 * overlaps the request buffer and will clobber data in the request. This
1229 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001230
Marcus Oaklandd3330872014-07-23 13:04:59 +01001231 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1232 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001233 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001234 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001235 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001236 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001238 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001239 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001240 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001241 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001242}
1243
Jeff Brown6249b902012-05-26 14:32:54 -07001244static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1246 const void* buffer)
1247{
1248 struct fuse_write_out out;
1249 struct handle *h = id_to_ptr(req->fh);
1250 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001251 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001252
Arpad Horvath49e93442014-02-18 10:18:25 +01001253 if (req->flags & O_DIRECT) {
1254 memcpy(aligned_buffer, buffer, req->size);
1255 buffer = (const __u8*) aligned_buffer;
1256 }
Jeff Brown6249b902012-05-26 14:32:54 -07001257
Marcus Oaklandd3330872014-07-23 13:04:59 +01001258 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001259 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001260 res = pwrite64(h->fd, buffer, req->size, req->offset);
1261 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001262 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001263 }
1264 out.size = res;
1265 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001266 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001267}
1268
Jeff Brown6249b902012-05-26 14:32:54 -07001269static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001270 const struct fuse_in_header* hdr)
1271{
Jeff Brown6249b902012-05-26 14:32:54 -07001272 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001273 struct statfs stat;
1274 struct fuse_statfs_out out;
1275 int res;
1276
Jeff Brown6249b902012-05-26 14:32:54 -07001277 pthread_mutex_lock(&fuse->lock);
1278 TRACE("[%d] STATFS\n", handler->token);
1279 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1280 pthread_mutex_unlock(&fuse->lock);
1281 if (res < 0) {
1282 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283 }
Jeff Brown6249b902012-05-26 14:32:54 -07001284 if (statfs(fuse->root.name, &stat) < 0) {
1285 return -errno;
1286 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001287 memset(&out, 0, sizeof(out));
1288 out.st.blocks = stat.f_blocks;
1289 out.st.bfree = stat.f_bfree;
1290 out.st.bavail = stat.f_bavail;
1291 out.st.files = stat.f_files;
1292 out.st.ffree = stat.f_ffree;
1293 out.st.bsize = stat.f_bsize;
1294 out.st.namelen = stat.f_namelen;
1295 out.st.frsize = stat.f_frsize;
1296 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001297 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001298}
1299
Jeff Brown6249b902012-05-26 14:32:54 -07001300static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001301 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1302{
1303 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001304
1305 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001306 close(h->fd);
1307 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001308 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001309}
1310
Jeff Brown6249b902012-05-26 14:32:54 -07001311static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001312 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1313{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001314 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1315 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001316
Elliott Hughesf6d67372014-07-08 14:38:26 -07001317 int fd = -1;
1318 if (is_dir) {
1319 struct dirhandle *dh = id_to_ptr(req->fh);
1320 fd = dirfd(dh->d);
1321 } else {
1322 struct handle *h = id_to_ptr(req->fh);
1323 fd = h->fd;
1324 }
1325
1326 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1327 is_dir ? "FSYNCDIR" : "FSYNC",
1328 id_to_ptr(req->fh), fd, is_data_sync);
1329 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1330 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001331 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001332 }
Jeff Brown6249b902012-05-26 14:32:54 -07001333 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001334}
1335
Jeff Brown6249b902012-05-26 14:32:54 -07001336static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337 const struct fuse_in_header* hdr)
1338{
Jeff Brown6249b902012-05-26 14:32:54 -07001339 TRACE("[%d] FLUSH\n", handler->token);
1340 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341}
1342
Jeff Brown6249b902012-05-26 14:32:54 -07001343static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1345{
Jeff Brown6249b902012-05-26 14:32:54 -07001346 struct node* node;
1347 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001350
Jeff Brown6249b902012-05-26 14:32:54 -07001351 pthread_mutex_lock(&fuse->lock);
1352 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklandd3330872014-07-23 13:04:59 +01001353 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001354 hdr->nodeid, node ? node->name : "?");
1355 pthread_mutex_unlock(&fuse->lock);
1356
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001357 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001358 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001360 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001361 return -EACCES;
1362 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001363 h = malloc(sizeof(*h));
1364 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001365 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001366 }
Jeff Brown6249b902012-05-26 14:32:54 -07001367 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001368 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001369 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001370 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001371 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001372 }
1373 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001374 out.open_flags = 0;
1375 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001377 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378}
1379
Jeff Brown6249b902012-05-26 14:32:54 -07001380static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001381 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1382{
1383 char buffer[8192];
1384 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1385 struct dirent *de;
1386 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001387
1388 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001389 if (req->offset == 0) {
1390 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001391 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001392 rewinddir(h->d);
1393 }
1394 de = readdir(h->d);
1395 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001396 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001397 }
1398 fde->ino = FUSE_UNKNOWN_INO;
1399 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1400 fde->off = req->offset + 1;
1401 fde->type = de->d_type;
1402 fde->namelen = strlen(de->d_name);
1403 memcpy(fde->name, de->d_name, fde->namelen + 1);
1404 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001405 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1406 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001407}
1408
Jeff Brown6249b902012-05-26 14:32:54 -07001409static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001410 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1411{
1412 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001413
1414 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001415 closedir(h->d);
1416 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001417 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001418}
1419
Jeff Brown6249b902012-05-26 14:32:54 -07001420static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001421 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1422{
1423 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001424 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425
Jeff Brown6249b902012-05-26 14:32:54 -07001426 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1427 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001428
1429 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1430 * defined (fuse version 7.6). The structure is the same from 7.6 through
1431 * 7.22. Beginning with 7.23, the structure increased in size and added
1432 * new parameters.
1433 */
1434 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1435 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1436 req->major, req->minor, FUSE_KERNEL_VERSION);
1437 return -1;
1438 }
1439
1440 out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
1441 fuse_struct_size = sizeof(out);
1442#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1443 /* FUSE_KERNEL_VERSION >= 23. */
1444
1445 /* If the kernel only works on minor revs older than or equal to 22,
1446 * then use the older structure size since this code only uses the 7.22
1447 * version of the structure. */
1448 if (req->minor <= 22) {
1449 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1450 }
1451#endif
1452
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001453 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001454 out.max_readahead = req->max_readahead;
1455 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1456 out.max_background = 32;
1457 out.congestion_threshold = 32;
1458 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001459 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001460 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001461}
1462
Jeff Brown6249b902012-05-26 14:32:54 -07001463static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001464 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1465{
Brian Swetland03ee9472010-08-12 18:01:08 -07001466 switch (hdr->opcode) {
1467 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001468 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001469 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001470 }
Jeff Brown84715842012-05-25 14:07:47 -07001471
Brian Swetland03ee9472010-08-12 18:01:08 -07001472 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001473 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001474 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001475 }
Jeff Brown84715842012-05-25 14:07:47 -07001476
Brian Swetland03ee9472010-08-12 18:01:08 -07001477 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001478 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001479 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 }
Jeff Brown84715842012-05-25 14:07:47 -07001481
Brian Swetland03ee9472010-08-12 18:01:08 -07001482 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001483 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001484 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001485 }
Jeff Brown84715842012-05-25 14:07:47 -07001486
Brian Swetland03ee9472010-08-12 18:01:08 -07001487// case FUSE_READLINK:
1488// case FUSE_SYMLINK:
1489 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001490 const struct fuse_mknod_in *req = data;
1491 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001492 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001493 }
Jeff Brown84715842012-05-25 14:07:47 -07001494
Brian Swetland03ee9472010-08-12 18:01:08 -07001495 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001496 const struct fuse_mkdir_in *req = data;
1497 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001498 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001499 }
Jeff Brown84715842012-05-25 14:07:47 -07001500
Brian Swetland03ee9472010-08-12 18:01:08 -07001501 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001502 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001503 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001504 }
Jeff Brown84715842012-05-25 14:07:47 -07001505
Brian Swetland03ee9472010-08-12 18:01:08 -07001506 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001507 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001508 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001509 }
Jeff Brown84715842012-05-25 14:07:47 -07001510
Brian Swetland03ee9472010-08-12 18:01:08 -07001511 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001512 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001513 const char *old_name = ((const char*) data) + sizeof(*req);
1514 const char *new_name = old_name + strlen(old_name) + 1;
1515 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001516 }
Jeff Brown84715842012-05-25 14:07:47 -07001517
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001518// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001519 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001520 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001521 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001522 }
Jeff Brown84715842012-05-25 14:07:47 -07001523
Brian Swetland03ee9472010-08-12 18:01:08 -07001524 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001525 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001526 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001527 }
Jeff Brown84715842012-05-25 14:07:47 -07001528
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001530 const struct fuse_write_in *req = data;
1531 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001532 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001533 }
Jeff Brown84715842012-05-25 14:07:47 -07001534
Mike Lockwood4553b082010-08-16 14:14:44 -04001535 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001536 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001537 }
Jeff Brown84715842012-05-25 14:07:47 -07001538
Brian Swetland03ee9472010-08-12 18:01:08 -07001539 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001540 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001541 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001542 }
Jeff Brown84715842012-05-25 14:07:47 -07001543
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001544 case FUSE_FSYNC:
1545 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001546 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001547 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001548 }
1549
Brian Swetland03ee9472010-08-12 18:01:08 -07001550// case FUSE_SETXATTR:
1551// case FUSE_GETXATTR:
1552// case FUSE_LISTXATTR:
1553// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001554 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001555 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001556 }
1557
Brian Swetland03ee9472010-08-12 18:01:08 -07001558 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001559 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001560 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001561 }
Jeff Brown84715842012-05-25 14:07:47 -07001562
Brian Swetland03ee9472010-08-12 18:01:08 -07001563 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001564 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001565 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001566 }
Jeff Brown84715842012-05-25 14:07:47 -07001567
Brian Swetland03ee9472010-08-12 18:01:08 -07001568 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001569 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001570 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001571 }
Jeff Brown84715842012-05-25 14:07:47 -07001572
Brian Swetland03ee9472010-08-12 18:01:08 -07001573 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001574 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001575 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001576 }
Jeff Brown84715842012-05-25 14:07:47 -07001577
Brian Swetland03ee9472010-08-12 18:01:08 -07001578 default: {
Marcus Oaklandd3330872014-07-23 13:04:59 +01001579 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001580 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1581 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001582 }
Jeff Brown84715842012-05-25 14:07:47 -07001583 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001584}
1585
Jeff Brown6249b902012-05-26 14:32:54 -07001586static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001587{
Jeff Brown6249b902012-05-26 14:32:54 -07001588 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001589 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001590 ssize_t len = read(fuse->fd,
1591 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001592 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001593 if (errno != EINTR) {
1594 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1595 }
1596 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001597 }
Jeff Brown84715842012-05-25 14:07:47 -07001598
1599 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001600 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1601 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001602 }
1603
Jeff Brown7729d242012-05-25 15:35:28 -07001604 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001605 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001606 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1607 handler->token, (size_t)len, hdr->len);
1608 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001609 }
1610
Jeff Brown7729d242012-05-25 15:35:28 -07001611 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001612 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001613 __u64 unique = hdr->unique;
1614 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001615
1616 /* We do not access the request again after this point because the underlying
1617 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001618
1619 if (res != NO_STATUS) {
1620 if (res) {
1621 TRACE("[%d] ERROR %d\n", handler->token, res);
1622 }
1623 fuse_status(fuse, unique, res);
1624 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001625 }
1626}
1627
Jeff Brown6249b902012-05-26 14:32:54 -07001628static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001629{
Jeff Brown6249b902012-05-26 14:32:54 -07001630 struct fuse_handler* handler = data;
1631 handle_fuse_requests(handler);
1632 return NULL;
1633}
1634
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001635static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001636 Hashmap* map = context;
1637 hashmapRemove(map, key);
1638 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001639 return true;
1640}
1641
1642static bool remove_int_to_null(void *key, void *value, void *context) {
1643 Hashmap* map = context;
1644 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001645 return true;
1646}
1647
1648static int read_package_list(struct fuse *fuse) {
1649 pthread_mutex_lock(&fuse->lock);
1650
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001651 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1652 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001653
1654 FILE* file = fopen(kPackagesListFile, "r");
1655 if (!file) {
1656 ERROR("failed to open package list: %s\n", strerror(errno));
1657 pthread_mutex_unlock(&fuse->lock);
1658 return -1;
1659 }
1660
1661 char buf[512];
1662 while (fgets(buf, sizeof(buf), file) != NULL) {
1663 char package_name[512];
1664 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001665 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001666
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001667 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1668 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001669 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001670
1671 char* token = strtok(gids, ",");
1672 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001673 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001674 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001675 break;
1676 }
1677 token = strtok(NULL, ",");
1678 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001679 }
1680 }
1681
Marcus Oaklandd3330872014-07-23 13:04:59 +01001682 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001683 hashmapSize(fuse->package_to_appid),
1684 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001685 fclose(file);
1686 pthread_mutex_unlock(&fuse->lock);
1687 return 0;
1688}
1689
1690static void watch_package_list(struct fuse* fuse) {
1691 struct inotify_event *event;
1692 char event_buf[512];
1693
1694 int nfd = inotify_init();
1695 if (nfd < 0) {
1696 ERROR("inotify_init failed: %s\n", strerror(errno));
1697 return;
1698 }
1699
1700 bool active = false;
1701 while (1) {
1702 if (!active) {
1703 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1704 if (res == -1) {
1705 if (errno == ENOENT || errno == EACCES) {
1706 /* Framework may not have created yet, sleep and retry */
1707 ERROR("missing packages.list; retrying\n");
1708 sleep(3);
1709 continue;
1710 } else {
1711 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1712 return;
1713 }
1714 }
1715
1716 /* Watch above will tell us about any future changes, so
1717 * read the current state. */
1718 if (read_package_list(fuse) == -1) {
1719 ERROR("read_package_list failed: %s\n", strerror(errno));
1720 return;
1721 }
1722 active = true;
1723 }
1724
1725 int event_pos = 0;
1726 int res = read(nfd, event_buf, sizeof(event_buf));
1727 if (res < (int) sizeof(*event)) {
1728 if (errno == EINTR)
1729 continue;
1730 ERROR("failed to read inotify event: %s\n", strerror(errno));
1731 return;
1732 }
1733
1734 while (res >= (int) sizeof(*event)) {
1735 int event_size;
1736 event = (struct inotify_event *) (event_buf + event_pos);
1737
1738 TRACE("inotify event: %08x\n", event->mask);
1739 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1740 /* Previously watched file was deleted, probably due to move
1741 * that swapped in new data; re-arm the watch and read. */
1742 active = false;
1743 }
1744
1745 event_size = sizeof(*event) + event->len;
1746 res -= event_size;
1747 event_pos += event_size;
1748 }
1749 }
1750}
1751
Jeff Brown6249b902012-05-26 14:32:54 -07001752static int ignite_fuse(struct fuse* fuse, int num_threads)
1753{
1754 struct fuse_handler* handlers;
1755 int i;
1756
1757 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1758 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001759 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001760 return -ENOMEM;
1761 }
1762
1763 for (i = 0; i < num_threads; i++) {
1764 handlers[i].fuse = fuse;
1765 handlers[i].token = i;
1766 }
1767
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001768 /* When deriving permissions, this thread is used to process inotify events,
1769 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001770 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001771 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001772 pthread_t thread;
1773 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1774 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001775 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001776 goto quit;
1777 }
1778 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001779
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001780 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001781 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001782 } else {
1783 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001784 }
1785
1786 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001787
1788 /* don't bother killing all of the other threads or freeing anything,
1789 * should never get here anyhow */
1790quit:
1791 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001792}
1793
Mike Lockwood4f35e622011-01-12 14:39:44 -05001794static int usage()
1795{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001796 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1797 " -u: specify UID to run as\n"
1798 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001799 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001800 " -t: specify number of threads to use (default %d)\n"
1801 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001802 " -l: derive file permissions based on legacy internal layout\n"
1803 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001804 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001805 return 1;
1806}
1807
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001808static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001809 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1810 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001811 int fd;
1812 char opts[256];
1813 int res;
1814 struct fuse fuse;
1815
1816 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001817 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001818
1819 fd = open("/dev/fuse", O_RDWR);
1820 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001821 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001822 return -1;
1823 }
1824
1825 snprintf(opts, sizeof(opts),
1826 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1827 fd, uid, gid);
1828
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001829 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001830 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001831 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1832 goto error;
1833 }
1834
1835 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1836 if (res < 0) {
1837 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001838 goto error;
1839 }
1840
1841 res = setgid(gid);
1842 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001843 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001844 goto error;
1845 }
1846
1847 res = setuid(uid);
1848 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001849 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001850 goto error;
1851 }
1852
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001853 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001854
1855 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001856 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001857
1858 /* we do not attempt to umount the file system here because we are no longer
1859 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001860
1861error:
1862 close(fd);
1863 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001864}
1865
Brian Swetland03ee9472010-08-12 18:01:08 -07001866int main(int argc, char **argv)
1867{
Brian Swetland03ee9472010-08-12 18:01:08 -07001868 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001869 const char *source_path = NULL;
1870 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001871 uid_t uid = 0;
1872 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001873 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001874 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001875 derive_t derive = DERIVE_NONE;
1876 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001877 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001878 struct rlimit rlim;
Nick Kralevich506edb52014-07-24 17:05:59 -07001879 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001880
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001881 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001882 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001883 switch (opt) {
1884 case 'u':
1885 uid = strtoul(optarg, NULL, 10);
1886 break;
1887 case 'g':
1888 gid = strtoul(optarg, NULL, 10);
1889 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001890 case 'w':
1891 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001892 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001893 case 't':
1894 num_threads = strtoul(optarg, NULL, 10);
1895 break;
1896 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001897 derive = DERIVE_UNIFIED;
1898 break;
1899 case 'l':
1900 derive = DERIVE_LEGACY;
1901 break;
1902 case 's':
1903 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001904 break;
1905 case '?':
1906 default:
1907 return usage();
1908 }
1909 }
1910
1911 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001912 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001913 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001914 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001915 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001916 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001917 } else if (!uid) {
1918 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001919 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001920 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001921 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001922 ERROR("too many arguments\n");
1923 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001924 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001925 }
1926
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001927 if (!source_path) {
1928 ERROR("no source path specified\n");
1929 return usage();
1930 }
1931 if (!dest_path) {
1932 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001933 return usage();
1934 }
Jeff Brown26567352012-05-25 13:27:43 -07001935 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001936 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001937 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001938 }
Jeff Brown6249b902012-05-26 14:32:54 -07001939 if (num_threads < 1) {
1940 ERROR("number of threads must be at least 1\n");
1941 return usage();
1942 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001943 if (split_perms && derive == DERIVE_NONE) {
1944 ERROR("cannot split permissions without deriving\n");
1945 return usage();
1946 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001947
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001948 rlim.rlim_cur = 8192;
1949 rlim.rlim_max = 8192;
1950 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1951 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1952 }
1953
Nick Kralevich506edb52014-07-24 17:05:59 -07001954 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1955 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1956 sleep(1);
1957 }
1958
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001959 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001960 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001961}