blob: 4d50bf0d65157e16cb2f9029f84dbfd530882704 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070032#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070033#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070034#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040035#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080036#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070037#include <sys/uio.h>
38#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039
Jeff Sharkey44d63422013-09-12 09:44:48 -070040#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070042#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070044
Brian Swetlandb14a2c62010-08-12 18:21:12 -070045#include <private/android_filesystem_config.h>
46
Brian Swetland03ee9472010-08-12 18:01:08 -070047/* README
48 *
49 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070050 *
Brian Swetland03ee9472010-08-12 18:01:08 -070051 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070052 * directory permissions (all files are given fixed owner, group, and
53 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070054 * changeable, symlinks and hardlinks are not createable, etc.
55 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070057 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070058 * It must be run as root, but will drop to requested UID/GID as soon as it
59 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070060 *
61 * Things I believe to be true:
62 *
63 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
64 * CREAT) must bump that node's refcount
65 * - don't forget that FORGET can forget multiple references (req->nlookup)
66 * - if an op that returns a fuse_entry fails writing the reply to the
67 * kernel, you must rollback the refcount to reflect the reference the
68 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070069 *
70 * This daemon can also derive custom filesystem permissions based on directory
71 * structure when requested. These custom permissions support several features:
72 *
73 * - Apps can access their own files in /Android/data/com.example/ without
74 * requiring any additional GIDs.
75 * - Separate permissions for protecting directories like Pictures and Music.
76 * - Multi-user separation on the same physical device.
77 *
78 * The derived permissions look like this:
79 *
80 * rwxrwx--x root:sdcard_rw /
81 * rwxrwx--- root:sdcard_pics /Pictures
82 * rwxrwx--- root:sdcard_av /Music
83 *
84 * rwxrwx--x root:sdcard_rw /Android
85 * rwxrwx--x root:sdcard_rw /Android/data
86 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
87 * rwxrwx--x root:sdcard_rw /Android/obb/
88 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
89 *
90 * rwxrwx--- root:sdcard_all /Android/user
91 * rwxrwx--x root:sdcard_rw /Android/user/10
92 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070093 */
94
95#define FUSE_TRACE 0
96
97#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070098#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070099#else
100#define TRACE(x...) do {} while (0)
101#endif
102
Elliott Hughes300d5642014-07-08 13:53:26 -0700103#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700104
105#define FUSE_UNKNOWN_INO 0xffffffff
106
Jeff Brown84715842012-05-25 14:07:47 -0700107/* Maximum number of bytes to write in one request. */
108#define MAX_WRITE (256 * 1024)
109
110/* Maximum number of bytes to read in one request. */
111#define MAX_READ (128 * 1024)
112
113/* Largest possible request.
114 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
115 * the largest possible data payload. */
116#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
117
Jeff Brown6249b902012-05-26 14:32:54 -0700118/* Default number of threads. */
119#define DEFAULT_NUM_THREADS 2
120
121/* Pseudo-error constant used to indicate that no fuse status is needed
122 * or that a reply has already been written. */
123#define NO_STATUS 1
124
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125/* Path to system-provided mapping of package name to appIds */
126static const char* const kPackagesListFile = "/data/system/packages.list";
127
128/* Supplementary groups to execute with */
129static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
130
131/* Permission mode for a specific node. Controls how file permissions
132 * are derived for children nodes. */
133typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700134 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700135 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700136 /* This node is one level above a normal root; used for legacy layouts
137 * which use the first level to represent user_id. */
138 PERM_LEGACY_PRE_ROOT,
139 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700145 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700146 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700147 /* This node is "/Android/media" */
148 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700149 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700150 PERM_ANDROID_USER,
151} perm_t;
152
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700153/* Permissions structure to derive */
154typedef enum {
155 DERIVE_NONE,
156 DERIVE_LEGACY,
157 DERIVE_UNIFIED,
158} derive_t;
159
Brian Swetland03ee9472010-08-12 18:01:08 -0700160struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700161 int fd;
162};
163
164struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700165 DIR *d;
166};
167
168struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700169 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700170 __u64 nid;
171 __u64 gen;
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000172 /*
173 * The inode number for this FUSE node. Note that this isn't stable across
174 * multiple invocations of the FUSE daemon.
175 */
176 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700177
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700178 /* State derived based on current position in hierarchy. */
179 perm_t perm;
180 userid_t userid;
181 uid_t uid;
182 gid_t gid;
183 mode_t mode;
184
Paul Eastham11ccdb32010-10-14 11:04:26 -0700185 struct node *next; /* per-dir sibling list */
186 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700187 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700188
Jeff Brown6249b902012-05-26 14:32:54 -0700189 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700190 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800191 /* If non-null, this is the real name of the file in the underlying storage.
192 * This may differ from the field "name" only by case.
193 * strlen(actual_name) will always equal strlen(name), so it is safe to use
194 * namelen for both fields.
195 */
196 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700197
198 /* If non-null, an exact underlying path that should be grafted into this
199 * position. Used to support things like OBB. */
200 char* graft_path;
201 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700202};
203
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700204static int str_hash(void *key) {
205 return hashmapHash(key, strlen(key));
206}
207
Jeff Sharkey44d63422013-09-12 09:44:48 -0700208/** Test if two string keys are equal ignoring case */
209static bool str_icase_equals(void *keyA, void *keyB) {
210 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700211}
212
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700213static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800214 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700215}
216
217static bool int_equals(void *keyA, void *keyB) {
218 return keyA == keyB;
219}
220
Jeff Brown7729d242012-05-25 15:35:28 -0700221/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700222struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700223 pthread_mutex_t lock;
224
Brian Swetland03ee9472010-08-12 18:01:08 -0700225 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700226 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700227 derive_t derive;
228 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700229 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700230 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700231 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700232
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000233 /* Used to allocate unique inode numbers for fuse nodes. We use
234 * a simple counter based scheme where inode numbers from deleted
235 * nodes aren't reused. Note that inode allocations are not stable
236 * across multiple invocation of the sdcard daemon, but that shouldn't
237 * be a huge problem in practice.
238 *
239 * Note that we restrict inodes to 32 bit unsigned integers to prevent
240 * truncation on 32 bit processes when unsigned long long stat.st_ino is
241 * assigned to an unsigned long ino_t type in an LP32 process.
242 *
243 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
244 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
245 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
246 * in fs/fuse/inode.c).
247 *
248 * Accesses must be guarded by |lock|.
249 */
250 __u32 inode_ctr;
251
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700252 Hashmap* package_to_appid;
253 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700254};
255
Jeff Brown7729d242012-05-25 15:35:28 -0700256/* Private data used by a single fuse handler. */
257struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700258 struct fuse* fuse;
259 int token;
260
Jeff Brown7729d242012-05-25 15:35:28 -0700261 /* To save memory, we never use the contents of the request buffer and the read
262 * buffer at the same time. This allows us to share the underlying storage. */
263 union {
264 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800265 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700266 };
267};
Brian Swetland03ee9472010-08-12 18:01:08 -0700268
Jeff Brown6249b902012-05-26 14:32:54 -0700269static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700270{
Jeff Brown6249b902012-05-26 14:32:54 -0700271 return (void *) (uintptr_t) nid;
272}
Brian Swetland03ee9472010-08-12 18:01:08 -0700273
Jeff Brown6249b902012-05-26 14:32:54 -0700274static inline __u64 ptr_to_id(void *ptr)
275{
276 return (__u64) (uintptr_t) ptr;
277}
Brian Swetland03ee9472010-08-12 18:01:08 -0700278
Jeff Brown6249b902012-05-26 14:32:54 -0700279static void acquire_node_locked(struct node* node)
280{
281 node->refcount++;
282 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
283}
284
285static void remove_node_from_parent_locked(struct node* node);
286
287static void release_node_locked(struct node* node)
288{
289 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
290 if (node->refcount > 0) {
291 node->refcount--;
292 if (!node->refcount) {
293 TRACE("DESTROY %p (%s)\n", node, node->name);
294 remove_node_from_parent_locked(node);
295
296 /* TODO: remove debugging - poison memory */
297 memset(node->name, 0xef, node->namelen);
298 free(node->name);
299 free(node->actual_name);
300 memset(node, 0xfc, sizeof(*node));
301 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800302 }
Jeff Brown6249b902012-05-26 14:32:54 -0700303 } else {
304 ERROR("Zero refcnt %p\n", node);
305 }
306}
307
308static void add_node_to_parent_locked(struct node *node, struct node *parent) {
309 node->parent = parent;
310 node->next = parent->child;
311 parent->child = node;
312 acquire_node_locked(parent);
313}
314
315static void remove_node_from_parent_locked(struct node* node)
316{
317 if (node->parent) {
318 if (node->parent->child == node) {
319 node->parent->child = node->parent->child->next;
320 } else {
321 struct node *node2;
322 node2 = node->parent->child;
323 while (node2->next != node)
324 node2 = node2->next;
325 node2->next = node->next;
326 }
327 release_node_locked(node->parent);
328 node->parent = NULL;
329 node->next = NULL;
330 }
331}
332
333/* Gets the absolute path to a node into the provided buffer.
334 *
335 * Populates 'buf' with the path and returns the length of the path on success,
336 * or returns -1 if the path is too long for the provided buffer.
337 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700338static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
339 const char* name;
340 size_t namelen;
341 if (node->graft_path) {
342 name = node->graft_path;
343 namelen = node->graft_pathlen;
344 } else if (node->actual_name) {
345 name = node->actual_name;
346 namelen = node->namelen;
347 } else {
348 name = node->name;
349 namelen = node->namelen;
350 }
351
Jeff Brown6249b902012-05-26 14:32:54 -0700352 if (bufsize < namelen + 1) {
353 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700354 }
355
Jeff Brown6249b902012-05-26 14:32:54 -0700356 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700357 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700358 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
359 if (pathlen < 0) {
360 return -1;
361 }
362 buf[pathlen++] = '/';
363 }
364
Jeff Brown6249b902012-05-26 14:32:54 -0700365 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
366 return pathlen + namelen;
367}
368
369/* Finds the absolute path of a file within a given directory.
370 * Performs a case-insensitive search for the file and sets the buffer to the path
371 * of the first matching file. If 'search' is zero or if no match is found, sets
372 * the buffer to the path that the file would have, assuming the name were case-sensitive.
373 *
374 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
375 * or returns NULL if the path is too long for the provided buffer.
376 */
377static char* find_file_within(const char* path, const char* name,
378 char* buf, size_t bufsize, int search)
379{
380 size_t pathlen = strlen(path);
381 size_t namelen = strlen(name);
382 size_t childlen = pathlen + namelen + 1;
383 char* actual;
384
385 if (bufsize <= childlen) {
386 return NULL;
387 }
388
389 memcpy(buf, path, pathlen);
390 buf[pathlen] = '/';
391 actual = buf + pathlen + 1;
392 memcpy(actual, name, namelen + 1);
393
394 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800395 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700396 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800397 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700398 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700399 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800400 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800401 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700402 if (!strcasecmp(entry->d_name, name)) {
403 /* we have a match - replace the name, don't need to copy the null again */
404 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800405 break;
406 }
407 }
408 closedir(dir);
409 }
Jeff Brown6249b902012-05-26 14:32:54 -0700410 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800411}
412
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700413static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800414{
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000415 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700416 attr->size = s->st_size;
417 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800418 attr->atime = s->st_atim.tv_sec;
419 attr->mtime = s->st_mtim.tv_sec;
420 attr->ctime = s->st_ctim.tv_sec;
421 attr->atimensec = s->st_atim.tv_nsec;
422 attr->mtimensec = s->st_mtim.tv_nsec;
423 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700424 attr->mode = s->st_mode;
425 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700426
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700427 attr->uid = node->uid;
428 attr->gid = node->gid;
429
430 /* Filter requested mode based on underlying file, and
431 * pass through file type. */
432 int owner_mode = s->st_mode & 0700;
433 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
434 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
435}
436
Jeff Sharkey44d63422013-09-12 09:44:48 -0700437static int touch(char* path, mode_t mode) {
438 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
439 if (fd == -1) {
440 if (errno == EEXIST) {
441 return 0;
442 } else {
443 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
444 return -1;
445 }
446 }
447 close(fd);
448 return 0;
449}
450
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700451static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
452 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700453 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700454
455 /* By default, each node inherits from its parent */
456 node->perm = PERM_INHERIT;
457 node->userid = parent->userid;
458 node->uid = parent->uid;
459 node->gid = parent->gid;
460 node->mode = parent->mode;
461
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700462 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700463 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700464 }
465
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700466 /* Derive custom permissions based on parent and current node */
467 switch (parent->perm) {
468 case PERM_INHERIT:
469 /* Already inherited above */
470 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700471 case PERM_LEGACY_PRE_ROOT:
472 /* Legacy internal layout places users at top level */
473 node->perm = PERM_ROOT;
474 node->userid = strtoul(node->name, NULL, 10);
475 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700476 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700477 /* Assume masked off by default. */
478 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700479 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700480 /* App-specific directories inside; let anyone traverse */
481 node->perm = PERM_ANDROID;
482 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700483 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700484 if (!strcasecmp(node->name, "DCIM")
485 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700486 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700487 } else if (!strcasecmp(node->name, "Alarms")
488 || !strcasecmp(node->name, "Movies")
489 || !strcasecmp(node->name, "Music")
490 || !strcasecmp(node->name, "Notifications")
491 || !strcasecmp(node->name, "Podcasts")
492 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700493 node->gid = AID_SDCARD_AV;
494 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700495 }
496 break;
497 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700498 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700499 /* App-specific directories inside; let anyone traverse */
500 node->perm = PERM_ANDROID_DATA;
501 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700502 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700503 /* App-specific directories inside; let anyone traverse */
504 node->perm = PERM_ANDROID_OBB;
505 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700506 /* Single OBB directory is always shared */
507 node->graft_path = fuse->obbpath;
508 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700509 } else if (!strcasecmp(node->name, "media")) {
510 /* App-specific directories inside; let anyone traverse */
511 node->perm = PERM_ANDROID_MEDIA;
512 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700513 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700514 /* User directories must only be accessible to system, protected
515 * by sdcard_all. Zygote will bind mount the appropriate user-
516 * specific path. */
517 node->perm = PERM_ANDROID_USER;
518 node->gid = AID_SDCARD_ALL;
519 node->mode = 0770;
520 }
521 break;
522 case PERM_ANDROID_DATA:
523 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700524 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800525 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700526 if (appid != 0) {
527 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700528 }
529 node->mode = 0770;
530 break;
531 case PERM_ANDROID_USER:
532 /* Root of a secondary user */
533 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700534 node->userid = strtoul(node->name, NULL, 10);
535 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700536 node->mode = 0771;
537 break;
538 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700539}
540
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700541/* Return if the calling UID holds sdcard_rw. */
542static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700543 /* No additional permissions enforcement */
544 if (fuse->derive == DERIVE_NONE) {
545 return true;
546 }
547
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700548 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800549 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700550}
551
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700552/* Kernel has already enforced everything we returned through
553 * derive_permissions_locked(), so this is used to lock down access
554 * even further, such as enforcing that apps hold sdcard_rw. */
555static bool check_caller_access_to_name(struct fuse* fuse,
556 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700557 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700558 /* Always block security-sensitive files at root */
559 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700560 if (!strcasecmp(name, "autorun.inf")
561 || !strcasecmp(name, ".android_secure")
562 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700563 return false;
564 }
565 }
566
567 /* No additional permissions enforcement */
568 if (fuse->derive == DERIVE_NONE) {
569 return true;
570 }
571
Jeff Sharkey44d63422013-09-12 09:44:48 -0700572 /* Root always has access; access for any other UIDs should always
573 * be controlled through packages.list. */
574 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700575 return true;
576 }
577
578 /* If asking to write, verify that caller either owns the
579 * parent or holds sdcard_rw. */
580 if (mode & W_OK) {
581 if (parent_node && hdr->uid == parent_node->uid) {
582 return true;
583 }
584
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700585 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700586 }
587
588 /* No extra permissions to enforce */
589 return true;
590}
591
592static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700593 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
594 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700595}
596
Jeff Brown6249b902012-05-26 14:32:54 -0700597struct node *create_node_locked(struct fuse* fuse,
598 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700599{
600 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700601 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700602
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000603 // Detect overflows in the inode counter. "4 billion nodes should be enough
604 // for everybody".
605 if (fuse->inode_ctr == 0) {
606 ERROR("No more inode numbers available");
607 return NULL;
608 }
609
Paul Eastham11ccdb32010-10-14 11:04:26 -0700610 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700611 if (!node) {
612 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700613 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700614 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700615 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700616 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700617 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700618 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700619 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700620 if (strcmp(name, actual_name)) {
621 node->actual_name = malloc(namelen + 1);
622 if (!node->actual_name) {
623 free(node->name);
624 free(node);
625 return NULL;
626 }
627 memcpy(node->actual_name, actual_name, namelen + 1);
628 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700629 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700630 node->nid = ptr_to_id(node);
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000631 node->ino = fuse->inode_ctr++;
Jeff Brown6249b902012-05-26 14:32:54 -0700632 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700633
634 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700635 acquire_node_locked(node);
636 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700637 return node;
638}
639
Jeff Brown6249b902012-05-26 14:32:54 -0700640static int rename_node_locked(struct node *node, const char *name,
641 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700642{
Jeff Brown6249b902012-05-26 14:32:54 -0700643 size_t namelen = strlen(name);
644 int need_actual_name = strcmp(name, actual_name);
645
646 /* make the storage bigger without actually changing the name
647 * in case an error occurs part way */
648 if (namelen > node->namelen) {
649 char* new_name = realloc(node->name, namelen + 1);
650 if (!new_name) {
651 return -ENOMEM;
652 }
653 node->name = new_name;
654 if (need_actual_name && node->actual_name) {
655 char* new_actual_name = realloc(node->actual_name, namelen + 1);
656 if (!new_actual_name) {
657 return -ENOMEM;
658 }
659 node->actual_name = new_actual_name;
660 }
661 }
662
663 /* update the name, taking care to allocate storage before overwriting the old name */
664 if (need_actual_name) {
665 if (!node->actual_name) {
666 node->actual_name = malloc(namelen + 1);
667 if (!node->actual_name) {
668 return -ENOMEM;
669 }
670 }
671 memcpy(node->actual_name, actual_name, namelen + 1);
672 } else {
673 free(node->actual_name);
674 node->actual_name = NULL;
675 }
676 memcpy(node->name, name, namelen + 1);
677 node->namelen = namelen;
678 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700679}
680
Jeff Brown6249b902012-05-26 14:32:54 -0700681static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700682{
Jeff Brown6249b902012-05-26 14:32:54 -0700683 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700684 return &fuse->root;
685 } else {
686 return id_to_ptr(nid);
687 }
688}
689
Jeff Brown6249b902012-05-26 14:32:54 -0700690static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
691 char* buf, size_t bufsize)
692{
693 struct node* node = lookup_node_by_id_locked(fuse, nid);
694 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
695 node = NULL;
696 }
697 return node;
698}
699
700static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700701{
702 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700703 /* use exact string comparison, nodes that differ by case
704 * must be considered distinct even if they refer to the same
705 * underlying file as otherwise operations such as "mv x x"
706 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700707 if (!strcmp(name, node->name)) {
708 return node;
709 }
710 }
711 return 0;
712}
713
Jeff Brown6249b902012-05-26 14:32:54 -0700714static struct node* acquire_or_create_child_locked(
715 struct fuse* fuse, struct node* parent,
716 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700717{
Jeff Brown6249b902012-05-26 14:32:54 -0700718 struct node* child = lookup_child_by_name_locked(parent, name);
719 if (child) {
720 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800721 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700722 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800723 }
Jeff Brown6249b902012-05-26 14:32:54 -0700724 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700725}
726
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700727static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700728 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700729 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700730
Jeff Brown6249b902012-05-26 14:32:54 -0700731 fuse->fd = fd;
732 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700733 fuse->derive = derive;
734 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700735 fuse->write_gid = write_gid;
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000736 fuse->inode_ctr = 1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700737
Jeff Brown6249b902012-05-26 14:32:54 -0700738 memset(&fuse->root, 0, sizeof(fuse->root));
739 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
740 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700741 fuse->root.namelen = strlen(source_path);
742 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700743 fuse->root.userid = 0;
744 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700745
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700746 /* Set up root node for various modes of operation */
747 switch (derive) {
748 case DERIVE_NONE:
749 /* Traditional behavior that treats entire device as being accessible
750 * to sdcard_rw, and no permissions are derived. */
751 fuse->root.perm = PERM_ROOT;
752 fuse->root.mode = 0775;
753 fuse->root.gid = AID_SDCARD_RW;
754 break;
755 case DERIVE_LEGACY:
756 /* Legacy behavior used to support internal multiuser layout which
757 * places user_id at the top directory level, with the actual roots
758 * just below that. Shared OBB path is also at top level. */
759 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
760 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700761 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700762 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700763 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
764 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700765 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700766 break;
767 case DERIVE_UNIFIED:
768 /* Unified multiuser layout which places secondary user_id under
769 * /Android/user and shared OBB path under /Android/obb. */
770 fuse->root.perm = PERM_ROOT;
771 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700772 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700773 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700774 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
775 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
776 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700777 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700778}
779
Jeff Brown6249b902012-05-26 14:32:54 -0700780static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700781{
782 struct fuse_out_header hdr;
783 hdr.len = sizeof(hdr);
784 hdr.error = err;
785 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700786 write(fuse->fd, &hdr, sizeof(hdr));
787}
788
Jeff Brown6249b902012-05-26 14:32:54 -0700789static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700790{
791 struct fuse_out_header hdr;
792 struct iovec vec[2];
793 int res;
794
795 hdr.len = len + sizeof(hdr);
796 hdr.error = 0;
797 hdr.unique = unique;
798
799 vec[0].iov_base = &hdr;
800 vec[0].iov_len = sizeof(hdr);
801 vec[1].iov_base = data;
802 vec[1].iov_len = len;
803
804 res = writev(fuse->fd, vec, 2);
805 if (res < 0) {
806 ERROR("*** REPLY FAILED *** %d\n", errno);
807 }
808}
809
Jeff Brown6249b902012-05-26 14:32:54 -0700810static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
811 struct node* parent, const char* name, const char* actual_name,
812 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700813{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700814 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700815 struct fuse_entry_out out;
816 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700817
Jeff Brown6249b902012-05-26 14:32:54 -0700818 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700819 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700820 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700821
Jeff Brown6249b902012-05-26 14:32:54 -0700822 pthread_mutex_lock(&fuse->lock);
823 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
824 if (!node) {
825 pthread_mutex_unlock(&fuse->lock);
826 return -ENOMEM;
827 }
828 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700829 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700830 out.attr_valid = 10;
831 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700832 out.nodeid = node->nid;
833 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700834 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700835 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700836 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700837}
838
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700839static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700840 const char* path)
841{
842 struct fuse_attr_out out;
843 struct stat s;
844
845 if (lstat(path, &s) < 0) {
846 return -errno;
847 }
848 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700849 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700850 out.attr_valid = 10;
851 fuse_reply(fuse, unique, &out, sizeof(out));
852 return NO_STATUS;
853}
854
855static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700856 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700857{
Jeff Brown6249b902012-05-26 14:32:54 -0700858 struct node* parent_node;
859 char parent_path[PATH_MAX];
860 char child_path[PATH_MAX];
861 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700862
Jeff Brown6249b902012-05-26 14:32:54 -0700863 pthread_mutex_lock(&fuse->lock);
864 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
865 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100866 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700867 parent_node ? parent_node->name : "?");
868 pthread_mutex_unlock(&fuse->lock);
869
870 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
871 child_path, sizeof(child_path), 1))) {
872 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700873 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700874 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700875 return -EACCES;
876 }
877
Jeff Brown6249b902012-05-26 14:32:54 -0700878 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879}
880
Jeff Brown6249b902012-05-26 14:32:54 -0700881static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
883{
Jeff Brown6249b902012-05-26 14:32:54 -0700884 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885
Jeff Brown6249b902012-05-26 14:32:54 -0700886 pthread_mutex_lock(&fuse->lock);
887 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100888 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700889 hdr->nodeid, node ? node->name : "?");
890 if (node) {
891 __u64 n = req->nlookup;
892 while (n--) {
893 release_node_locked(node);
894 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700895 }
Jeff Brown6249b902012-05-26 14:32:54 -0700896 pthread_mutex_unlock(&fuse->lock);
897 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700898}
899
Jeff Brown6249b902012-05-26 14:32:54 -0700900static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700901 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
902{
Jeff Brown6249b902012-05-26 14:32:54 -0700903 struct node* node;
904 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905
Jeff Brown6249b902012-05-26 14:32:54 -0700906 pthread_mutex_lock(&fuse->lock);
907 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100908 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700909 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
910 pthread_mutex_unlock(&fuse->lock);
911
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700912 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700913 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700914 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700915 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700916 return -EACCES;
917 }
918
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700919 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700920}
921
Jeff Brown6249b902012-05-26 14:32:54 -0700922static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700923 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
924{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700925 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700926 struct node* node;
927 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700928 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700929
Jeff Brown6249b902012-05-26 14:32:54 -0700930 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700931 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700932 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100933 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700934 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
935 pthread_mutex_unlock(&fuse->lock);
936
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700937 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700938 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700939 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800940
941 if (!(req->valid & FATTR_FH) &&
942 !check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700943 return -EACCES;
944 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945
Jeff Brown6249b902012-05-26 14:32:54 -0700946 /* XXX: incomplete implementation on purpose.
947 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948
Elliott Hughes853574d2014-07-31 12:03:03 -0700949 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700950 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951 }
952
953 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
954 * are both set, then set it to the current time. Else, set it to the
955 * time specified in the request. Same goes for mtime. Use utimensat(2)
956 * as it allows ATIME and MTIME to be changed independently, and has
957 * nanosecond resolution which fuse also has.
958 */
959 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
960 times[0].tv_nsec = UTIME_OMIT;
961 times[1].tv_nsec = UTIME_OMIT;
962 if (req->valid & FATTR_ATIME) {
963 if (req->valid & FATTR_ATIME_NOW) {
964 times[0].tv_nsec = UTIME_NOW;
965 } else {
966 times[0].tv_sec = req->atime;
967 times[0].tv_nsec = req->atimensec;
968 }
969 }
970 if (req->valid & FATTR_MTIME) {
971 if (req->valid & FATTR_MTIME_NOW) {
972 times[1].tv_nsec = UTIME_NOW;
973 } else {
974 times[1].tv_sec = req->mtime;
975 times[1].tv_nsec = req->mtimensec;
976 }
977 }
Jeff Brown6249b902012-05-26 14:32:54 -0700978 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
979 handler->token, path, times[0].tv_sec, times[1].tv_sec);
980 if (utimensat(-1, path, times, 0) < 0) {
981 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700982 }
983 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700984 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700985}
986
Jeff Brown6249b902012-05-26 14:32:54 -0700987static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700988 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
989{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700990 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700991 struct node* parent_node;
992 char parent_path[PATH_MAX];
993 char child_path[PATH_MAX];
994 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700995
Jeff Brown6249b902012-05-26 14:32:54 -0700996 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700997 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700998 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
999 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001000 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001001 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
1002 pthread_mutex_unlock(&fuse->lock);
1003
1004 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1005 child_path, sizeof(child_path), 1))) {
1006 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001007 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001008 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001009 return -EACCES;
1010 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001011 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -07001012 if (mknod(child_path, mode, req->rdev) < 0) {
1013 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001014 }
Jeff Brown6249b902012-05-26 14:32:54 -07001015 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001016}
1017
Jeff Brown6249b902012-05-26 14:32:54 -07001018static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001019 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
1020{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001021 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001022 struct node* parent_node;
1023 char parent_path[PATH_MAX];
1024 char child_path[PATH_MAX];
1025 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026
Jeff Brown6249b902012-05-26 14:32:54 -07001027 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001028 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001029 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1030 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001031 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001032 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
1033 pthread_mutex_unlock(&fuse->lock);
1034
1035 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1036 child_path, sizeof(child_path), 1))) {
1037 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001038 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001039 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001040 return -EACCES;
1041 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001043 if (mkdir(child_path, mode) < 0) {
1044 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001045 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001046
1047 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1048 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1049 char nomedia[PATH_MAX];
1050 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1051 if (touch(nomedia, 0664) != 0) {
1052 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1053 return -ENOENT;
1054 }
1055 }
1056 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1057 char nomedia[PATH_MAX];
1058 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1059 if (touch(nomedia, 0664) != 0) {
1060 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1061 return -ENOENT;
1062 }
1063 }
1064
Jeff Brown6249b902012-05-26 14:32:54 -07001065 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001066}
1067
Jeff Brown6249b902012-05-26 14:32:54 -07001068static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001069 const struct fuse_in_header* hdr, const char* name)
1070{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001071 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001072 struct node* parent_node;
1073 char parent_path[PATH_MAX];
1074 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001075
Jeff Brown6249b902012-05-26 14:32:54 -07001076 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001077 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001078 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1079 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001080 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001081 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1082 pthread_mutex_unlock(&fuse->lock);
1083
1084 if (!parent_node || !find_file_within(parent_path, name,
1085 child_path, sizeof(child_path), 1)) {
1086 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001087 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001088 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001089 return -EACCES;
1090 }
Jeff Brown6249b902012-05-26 14:32:54 -07001091 if (unlink(child_path) < 0) {
1092 return -errno;
1093 }
1094 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001095}
1096
Jeff Brown6249b902012-05-26 14:32:54 -07001097static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001098 const struct fuse_in_header* hdr, const char* name)
1099{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001100 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001101 struct node* parent_node;
1102 char parent_path[PATH_MAX];
1103 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001104
Jeff Brown6249b902012-05-26 14:32:54 -07001105 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001106 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001107 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1108 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001109 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001110 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1111 pthread_mutex_unlock(&fuse->lock);
1112
1113 if (!parent_node || !find_file_within(parent_path, name,
1114 child_path, sizeof(child_path), 1)) {
1115 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001116 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001117 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001118 return -EACCES;
1119 }
Jeff Brown6249b902012-05-26 14:32:54 -07001120 if (rmdir(child_path) < 0) {
1121 return -errno;
1122 }
1123 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001124}
1125
Jeff Brown6249b902012-05-26 14:32:54 -07001126static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001127 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001128 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001129{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001130 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001131 struct node* old_parent_node;
1132 struct node* new_parent_node;
1133 struct node* child_node;
1134 char old_parent_path[PATH_MAX];
1135 char new_parent_path[PATH_MAX];
1136 char old_child_path[PATH_MAX];
1137 char new_child_path[PATH_MAX];
1138 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001139 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001140
Jeff Brown6249b902012-05-26 14:32:54 -07001141 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001142 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001143 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1144 old_parent_path, sizeof(old_parent_path));
1145 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1146 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001147 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001148 old_name, new_name,
1149 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1150 req->newdir, new_parent_node ? new_parent_node->name : "?");
1151 if (!old_parent_node || !new_parent_node) {
1152 res = -ENOENT;
1153 goto lookup_error;
1154 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001155 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001156 res = -EACCES;
1157 goto lookup_error;
1158 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001159 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001160 res = -EACCES;
1161 goto lookup_error;
1162 }
Jeff Brown6249b902012-05-26 14:32:54 -07001163 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1164 if (!child_node || get_node_path_locked(child_node,
1165 old_child_path, sizeof(old_child_path)) < 0) {
1166 res = -ENOENT;
1167 goto lookup_error;
1168 }
1169 acquire_node_locked(child_node);
1170 pthread_mutex_unlock(&fuse->lock);
1171
1172 /* Special case for renaming a file where destination is same path
1173 * differing only by case. In this case we don't want to look for a case
1174 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1175 */
1176 int search = old_parent_node != new_parent_node
1177 || strcasecmp(old_name, new_name);
1178 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1179 new_child_path, sizeof(new_child_path), search))) {
1180 res = -ENOENT;
1181 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001182 }
1183
Jeff Brown6249b902012-05-26 14:32:54 -07001184 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1185 res = rename(old_child_path, new_child_path);
1186 if (res < 0) {
1187 res = -errno;
1188 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 }
1190
Jeff Brown6249b902012-05-26 14:32:54 -07001191 pthread_mutex_lock(&fuse->lock);
1192 res = rename_node_locked(child_node, new_name, new_actual_name);
1193 if (!res) {
1194 remove_node_from_parent_locked(child_node);
1195 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001196 }
Jeff Brown6249b902012-05-26 14:32:54 -07001197 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198
Jeff Brown6249b902012-05-26 14:32:54 -07001199io_error:
1200 pthread_mutex_lock(&fuse->lock);
1201done:
1202 release_node_locked(child_node);
1203lookup_error:
1204 pthread_mutex_unlock(&fuse->lock);
1205 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001206}
1207
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001208static int open_flags_to_access_mode(int open_flags) {
1209 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1210 return R_OK;
1211 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1212 return W_OK;
1213 } else {
1214 /* Probably O_RDRW, but treat as default to be safe */
1215 return R_OK | W_OK;
1216 }
1217}
1218
Jeff Brown6249b902012-05-26 14:32:54 -07001219static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001220 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1221{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001222 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001223 struct node* node;
1224 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001226 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227
Jeff Brown6249b902012-05-26 14:32:54 -07001228 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001229 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001230 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001231 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001232 req->flags, hdr->nodeid, node ? node->name : "?");
1233 pthread_mutex_unlock(&fuse->lock);
1234
1235 if (!node) {
1236 return -ENOENT;
1237 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001238 if (!check_caller_access_to_node(fuse, hdr, node,
1239 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001240 return -EACCES;
1241 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001242 h = malloc(sizeof(*h));
1243 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001244 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245 }
Jeff Brown6249b902012-05-26 14:32:54 -07001246 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001247 h->fd = open(path, req->flags);
1248 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001249 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001250 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001251 }
1252 out.fh = ptr_to_id(h);
1253 out.open_flags = 0;
1254 out.padding = 0;
1255 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001256 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001257}
1258
Jeff Brown6249b902012-05-26 14:32:54 -07001259static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001260 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1261{
1262 struct handle *h = id_to_ptr(req->fh);
1263 __u64 unique = hdr->unique;
1264 __u32 size = req->size;
1265 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001266 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001267 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001268
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1270 * overlaps the request buffer and will clobber data in the request. This
1271 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001272
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001273 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1274 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001275 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001276 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001277 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001278 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001279 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001280 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001281 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001282 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001283 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001284}
1285
Jeff Brown6249b902012-05-26 14:32:54 -07001286static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001287 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1288 const void* buffer)
1289{
1290 struct fuse_write_out out;
1291 struct handle *h = id_to_ptr(req->fh);
1292 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001293 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001294
Arpad Horvath49e93442014-02-18 10:18:25 +01001295 if (req->flags & O_DIRECT) {
1296 memcpy(aligned_buffer, buffer, req->size);
1297 buffer = (const __u8*) aligned_buffer;
1298 }
Jeff Brown6249b902012-05-26 14:32:54 -07001299
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001300 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001301 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001302 res = pwrite64(h->fd, buffer, req->size, req->offset);
1303 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001304 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001305 }
1306 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001307 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001309 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001310}
1311
Jeff Brown6249b902012-05-26 14:32:54 -07001312static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001313 const struct fuse_in_header* hdr)
1314{
Jeff Brown6249b902012-05-26 14:32:54 -07001315 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001316 struct statfs stat;
1317 struct fuse_statfs_out out;
1318 int res;
1319
Jeff Brown6249b902012-05-26 14:32:54 -07001320 pthread_mutex_lock(&fuse->lock);
1321 TRACE("[%d] STATFS\n", handler->token);
1322 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1323 pthread_mutex_unlock(&fuse->lock);
1324 if (res < 0) {
1325 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326 }
Jeff Brown6249b902012-05-26 14:32:54 -07001327 if (statfs(fuse->root.name, &stat) < 0) {
1328 return -errno;
1329 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001330 memset(&out, 0, sizeof(out));
1331 out.st.blocks = stat.f_blocks;
1332 out.st.bfree = stat.f_bfree;
1333 out.st.bavail = stat.f_bavail;
1334 out.st.files = stat.f_files;
1335 out.st.ffree = stat.f_ffree;
1336 out.st.bsize = stat.f_bsize;
1337 out.st.namelen = stat.f_namelen;
1338 out.st.frsize = stat.f_frsize;
1339 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001340 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341}
1342
Jeff Brown6249b902012-05-26 14:32:54 -07001343static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1345{
1346 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001347
1348 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349 close(h->fd);
1350 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001351 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001352}
1353
Jeff Brown6249b902012-05-26 14:32:54 -07001354static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1356{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001357 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1358 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001359
Elliott Hughesf6d67372014-07-08 14:38:26 -07001360 int fd = -1;
1361 if (is_dir) {
1362 struct dirhandle *dh = id_to_ptr(req->fh);
1363 fd = dirfd(dh->d);
1364 } else {
1365 struct handle *h = id_to_ptr(req->fh);
1366 fd = h->fd;
1367 }
1368
1369 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1370 is_dir ? "FSYNCDIR" : "FSYNC",
1371 id_to_ptr(req->fh), fd, is_data_sync);
1372 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1373 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001374 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 }
Jeff Brown6249b902012-05-26 14:32:54 -07001376 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377}
1378
Jeff Brown6249b902012-05-26 14:32:54 -07001379static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 const struct fuse_in_header* hdr)
1381{
Jeff Brown6249b902012-05-26 14:32:54 -07001382 TRACE("[%d] FLUSH\n", handler->token);
1383 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001384}
1385
Jeff Brown6249b902012-05-26 14:32:54 -07001386static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001387 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1388{
Jeff Brown6249b902012-05-26 14:32:54 -07001389 struct node* node;
1390 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001391 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001392 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001393
Jeff Brown6249b902012-05-26 14:32:54 -07001394 pthread_mutex_lock(&fuse->lock);
1395 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001396 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001397 hdr->nodeid, node ? node->name : "?");
1398 pthread_mutex_unlock(&fuse->lock);
1399
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001400 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001401 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001402 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001403 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001404 return -EACCES;
1405 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001406 h = malloc(sizeof(*h));
1407 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001408 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001409 }
Jeff Brown6249b902012-05-26 14:32:54 -07001410 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001411 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001412 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001413 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001414 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001415 }
1416 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001417 out.open_flags = 0;
1418 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001419 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001420 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001421}
1422
Jeff Brown6249b902012-05-26 14:32:54 -07001423static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001424 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1425{
1426 char buffer[8192];
1427 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1428 struct dirent *de;
1429 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001430
1431 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001432 if (req->offset == 0) {
1433 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001434 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001435 rewinddir(h->d);
1436 }
1437 de = readdir(h->d);
1438 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001439 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001440 }
1441 fde->ino = FUSE_UNKNOWN_INO;
1442 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1443 fde->off = req->offset + 1;
1444 fde->type = de->d_type;
1445 fde->namelen = strlen(de->d_name);
1446 memcpy(fde->name, de->d_name, fde->namelen + 1);
1447 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001448 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1449 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001450}
1451
Jeff Brown6249b902012-05-26 14:32:54 -07001452static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001453 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1454{
1455 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001456
1457 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001458 closedir(h->d);
1459 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001460 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001461}
1462
Jeff Brown6249b902012-05-26 14:32:54 -07001463static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001464 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1465{
1466 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001467 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001468
Jeff Brown6249b902012-05-26 14:32:54 -07001469 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1470 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001471
1472 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1473 * defined (fuse version 7.6). The structure is the same from 7.6 through
1474 * 7.22. Beginning with 7.23, the structure increased in size and added
1475 * new parameters.
1476 */
1477 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1478 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1479 req->major, req->minor, FUSE_KERNEL_VERSION);
1480 return -1;
1481 }
1482
1483 out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
1484 fuse_struct_size = sizeof(out);
1485#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1486 /* FUSE_KERNEL_VERSION >= 23. */
1487
1488 /* If the kernel only works on minor revs older than or equal to 22,
1489 * then use the older structure size since this code only uses the 7.22
1490 * version of the structure. */
1491 if (req->minor <= 22) {
1492 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1493 }
1494#endif
1495
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001496 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001497 out.max_readahead = req->max_readahead;
1498 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1499 out.max_background = 32;
1500 out.congestion_threshold = 32;
1501 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001502 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001503 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001504}
1505
Jeff Brown6249b902012-05-26 14:32:54 -07001506static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001507 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1508{
Brian Swetland03ee9472010-08-12 18:01:08 -07001509 switch (hdr->opcode) {
1510 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001511 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001512 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001513 }
Jeff Brown84715842012-05-25 14:07:47 -07001514
Brian Swetland03ee9472010-08-12 18:01:08 -07001515 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001516 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001517 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001518 }
Jeff Brown84715842012-05-25 14:07:47 -07001519
Brian Swetland03ee9472010-08-12 18:01:08 -07001520 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001521 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001522 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 }
Jeff Brown84715842012-05-25 14:07:47 -07001524
Brian Swetland03ee9472010-08-12 18:01:08 -07001525 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001526 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001527 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001528 }
Jeff Brown84715842012-05-25 14:07:47 -07001529
Brian Swetland03ee9472010-08-12 18:01:08 -07001530// case FUSE_READLINK:
1531// case FUSE_SYMLINK:
1532 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001533 const struct fuse_mknod_in *req = data;
1534 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001535 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001536 }
Jeff Brown84715842012-05-25 14:07:47 -07001537
Brian Swetland03ee9472010-08-12 18:01:08 -07001538 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001539 const struct fuse_mkdir_in *req = data;
1540 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001541 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001542 }
Jeff Brown84715842012-05-25 14:07:47 -07001543
Brian Swetland03ee9472010-08-12 18:01:08 -07001544 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001545 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001546 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001547 }
Jeff Brown84715842012-05-25 14:07:47 -07001548
Brian Swetland03ee9472010-08-12 18:01:08 -07001549 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001550 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001551 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001552 }
Jeff Brown84715842012-05-25 14:07:47 -07001553
Brian Swetland03ee9472010-08-12 18:01:08 -07001554 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001555 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001556 const char *old_name = ((const char*) data) + sizeof(*req);
1557 const char *new_name = old_name + strlen(old_name) + 1;
1558 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001559 }
Jeff Brown84715842012-05-25 14:07:47 -07001560
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001561// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001562 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001563 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001564 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001565 }
Jeff Brown84715842012-05-25 14:07:47 -07001566
Brian Swetland03ee9472010-08-12 18:01:08 -07001567 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001568 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001569 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 }
Jeff Brown84715842012-05-25 14:07:47 -07001571
Brian Swetland03ee9472010-08-12 18:01:08 -07001572 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001573 const struct fuse_write_in *req = data;
1574 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001575 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001576 }
Jeff Brown84715842012-05-25 14:07:47 -07001577
Mike Lockwood4553b082010-08-16 14:14:44 -04001578 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001579 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001580 }
Jeff Brown84715842012-05-25 14:07:47 -07001581
Brian Swetland03ee9472010-08-12 18:01:08 -07001582 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001583 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001584 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001585 }
Jeff Brown84715842012-05-25 14:07:47 -07001586
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001587 case FUSE_FSYNC:
1588 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001589 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001590 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001591 }
1592
Brian Swetland03ee9472010-08-12 18:01:08 -07001593// case FUSE_SETXATTR:
1594// case FUSE_GETXATTR:
1595// case FUSE_LISTXATTR:
1596// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001597 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001598 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001599 }
1600
Brian Swetland03ee9472010-08-12 18:01:08 -07001601 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001602 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001603 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001604 }
Jeff Brown84715842012-05-25 14:07:47 -07001605
Brian Swetland03ee9472010-08-12 18:01:08 -07001606 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001607 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001608 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001609 }
Jeff Brown84715842012-05-25 14:07:47 -07001610
Brian Swetland03ee9472010-08-12 18:01:08 -07001611 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001612 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001613 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001614 }
Jeff Brown84715842012-05-25 14:07:47 -07001615
Brian Swetland03ee9472010-08-12 18:01:08 -07001616 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001617 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001618 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001619 }
Jeff Brown84715842012-05-25 14:07:47 -07001620
Brian Swetland03ee9472010-08-12 18:01:08 -07001621 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001622 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001623 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1624 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001625 }
Jeff Brown84715842012-05-25 14:07:47 -07001626 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001627}
1628
Jeff Brown6249b902012-05-26 14:32:54 -07001629static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001630{
Jeff Brown6249b902012-05-26 14:32:54 -07001631 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001632 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001633 ssize_t len = read(fuse->fd,
1634 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001635 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001636 if (errno != EINTR) {
1637 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1638 }
1639 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001640 }
Jeff Brown84715842012-05-25 14:07:47 -07001641
1642 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001643 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1644 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001645 }
1646
Jeff Brown7729d242012-05-25 15:35:28 -07001647 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001648 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001649 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1650 handler->token, (size_t)len, hdr->len);
1651 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001652 }
1653
Jeff Brown7729d242012-05-25 15:35:28 -07001654 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001655 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001656 __u64 unique = hdr->unique;
1657 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001658
1659 /* We do not access the request again after this point because the underlying
1660 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001661
1662 if (res != NO_STATUS) {
1663 if (res) {
1664 TRACE("[%d] ERROR %d\n", handler->token, res);
1665 }
1666 fuse_status(fuse, unique, res);
1667 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001668 }
1669}
1670
Jeff Brown6249b902012-05-26 14:32:54 -07001671static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001672{
Jeff Brown6249b902012-05-26 14:32:54 -07001673 struct fuse_handler* handler = data;
1674 handle_fuse_requests(handler);
1675 return NULL;
1676}
1677
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001678static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001679 Hashmap* map = context;
1680 hashmapRemove(map, key);
1681 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001682 return true;
1683}
1684
1685static bool remove_int_to_null(void *key, void *value, void *context) {
1686 Hashmap* map = context;
1687 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001688 return true;
1689}
1690
1691static int read_package_list(struct fuse *fuse) {
1692 pthread_mutex_lock(&fuse->lock);
1693
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001694 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1695 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001696
1697 FILE* file = fopen(kPackagesListFile, "r");
1698 if (!file) {
1699 ERROR("failed to open package list: %s\n", strerror(errno));
1700 pthread_mutex_unlock(&fuse->lock);
1701 return -1;
1702 }
1703
1704 char buf[512];
1705 while (fgets(buf, sizeof(buf), file) != NULL) {
1706 char package_name[512];
1707 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001708 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001709
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001710 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1711 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001712 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001713
1714 char* token = strtok(gids, ",");
1715 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001716 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001717 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001718 break;
1719 }
1720 token = strtok(NULL, ",");
1721 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001722 }
1723 }
1724
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001725 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001726 hashmapSize(fuse->package_to_appid),
1727 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001728 fclose(file);
1729 pthread_mutex_unlock(&fuse->lock);
1730 return 0;
1731}
1732
1733static void watch_package_list(struct fuse* fuse) {
1734 struct inotify_event *event;
1735 char event_buf[512];
1736
1737 int nfd = inotify_init();
1738 if (nfd < 0) {
1739 ERROR("inotify_init failed: %s\n", strerror(errno));
1740 return;
1741 }
1742
1743 bool active = false;
1744 while (1) {
1745 if (!active) {
1746 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1747 if (res == -1) {
1748 if (errno == ENOENT || errno == EACCES) {
1749 /* Framework may not have created yet, sleep and retry */
1750 ERROR("missing packages.list; retrying\n");
1751 sleep(3);
1752 continue;
1753 } else {
1754 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1755 return;
1756 }
1757 }
1758
1759 /* Watch above will tell us about any future changes, so
1760 * read the current state. */
1761 if (read_package_list(fuse) == -1) {
1762 ERROR("read_package_list failed: %s\n", strerror(errno));
1763 return;
1764 }
1765 active = true;
1766 }
1767
1768 int event_pos = 0;
1769 int res = read(nfd, event_buf, sizeof(event_buf));
1770 if (res < (int) sizeof(*event)) {
1771 if (errno == EINTR)
1772 continue;
1773 ERROR("failed to read inotify event: %s\n", strerror(errno));
1774 return;
1775 }
1776
1777 while (res >= (int) sizeof(*event)) {
1778 int event_size;
1779 event = (struct inotify_event *) (event_buf + event_pos);
1780
1781 TRACE("inotify event: %08x\n", event->mask);
1782 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1783 /* Previously watched file was deleted, probably due to move
1784 * that swapped in new data; re-arm the watch and read. */
1785 active = false;
1786 }
1787
1788 event_size = sizeof(*event) + event->len;
1789 res -= event_size;
1790 event_pos += event_size;
1791 }
1792 }
1793}
1794
Jeff Brown6249b902012-05-26 14:32:54 -07001795static int ignite_fuse(struct fuse* fuse, int num_threads)
1796{
1797 struct fuse_handler* handlers;
1798 int i;
1799
1800 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1801 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001802 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001803 return -ENOMEM;
1804 }
1805
1806 for (i = 0; i < num_threads; i++) {
1807 handlers[i].fuse = fuse;
1808 handlers[i].token = i;
1809 }
1810
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001811 /* When deriving permissions, this thread is used to process inotify events,
1812 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001813 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001814 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001815 pthread_t thread;
1816 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1817 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001818 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001819 goto quit;
1820 }
1821 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001822
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001823 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001824 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001825 } else {
1826 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001827 }
1828
1829 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001830
1831 /* don't bother killing all of the other threads or freeing anything,
1832 * should never get here anyhow */
1833quit:
1834 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001835}
1836
Mike Lockwood4f35e622011-01-12 14:39:44 -05001837static int usage()
1838{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001839 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1840 " -u: specify UID to run as\n"
1841 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001842 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001843 " -t: specify number of threads to use (default %d)\n"
1844 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001845 " -l: derive file permissions based on legacy internal layout\n"
1846 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001847 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001848 return 1;
1849}
1850
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001851static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001852 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1853 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001854 int fd;
1855 char opts[256];
1856 int res;
1857 struct fuse fuse;
1858
1859 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001860 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001861
1862 fd = open("/dev/fuse", O_RDWR);
1863 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001864 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001865 return -1;
1866 }
1867
1868 snprintf(opts, sizeof(opts),
1869 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1870 fd, uid, gid);
1871
Daisuke Okitsu00690852014-11-24 09:37:55 +01001872 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001873 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001874 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1875 goto error;
1876 }
1877
1878 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1879 if (res < 0) {
1880 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001881 goto error;
1882 }
1883
1884 res = setgid(gid);
1885 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001886 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001887 goto error;
1888 }
1889
1890 res = setuid(uid);
1891 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001892 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001893 goto error;
1894 }
1895
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001896 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001897
1898 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001899 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001900
1901 /* we do not attempt to umount the file system here because we are no longer
1902 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001903
1904error:
1905 close(fd);
1906 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001907}
1908
Brian Swetland03ee9472010-08-12 18:01:08 -07001909int main(int argc, char **argv)
1910{
Brian Swetland03ee9472010-08-12 18:01:08 -07001911 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001912 const char *source_path = NULL;
1913 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001914 uid_t uid = 0;
1915 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001916 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001917 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001918 derive_t derive = DERIVE_NONE;
1919 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001920 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001921 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001922 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001923
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001924 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001925 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001926 switch (opt) {
1927 case 'u':
1928 uid = strtoul(optarg, NULL, 10);
1929 break;
1930 case 'g':
1931 gid = strtoul(optarg, NULL, 10);
1932 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001933 case 'w':
1934 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001935 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001936 case 't':
1937 num_threads = strtoul(optarg, NULL, 10);
1938 break;
1939 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001940 derive = DERIVE_UNIFIED;
1941 break;
1942 case 'l':
1943 derive = DERIVE_LEGACY;
1944 break;
1945 case 's':
1946 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001947 break;
1948 case '?':
1949 default:
1950 return usage();
1951 }
1952 }
1953
1954 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001955 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001956 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001957 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001958 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001959 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001960 } else if (!uid) {
1961 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001962 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001963 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001964 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001965 ERROR("too many arguments\n");
1966 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001967 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001968 }
1969
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001970 if (!source_path) {
1971 ERROR("no source path specified\n");
1972 return usage();
1973 }
1974 if (!dest_path) {
1975 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001976 return usage();
1977 }
Jeff Brown26567352012-05-25 13:27:43 -07001978 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001979 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001980 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001981 }
Jeff Brown6249b902012-05-26 14:32:54 -07001982 if (num_threads < 1) {
1983 ERROR("number of threads must be at least 1\n");
1984 return usage();
1985 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001986 if (split_perms && derive == DERIVE_NONE) {
1987 ERROR("cannot split permissions without deriving\n");
1988 return usage();
1989 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001990
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001991 rlim.rlim_cur = 8192;
1992 rlim.rlim_max = 8192;
1993 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1994 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1995 }
1996
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001997 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1998 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1999 sleep(1);
2000 }
2001
Jeff Sharkeye93a0512013-10-08 10:14:24 -07002002 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07002003 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07002004}