blob: a48906a5417a3ae92b31f5fc3feda5a52692d65c [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070032#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070033#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070034#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040035#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080036#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070037#include <sys/uio.h>
38#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039
Jeff Sharkey44d63422013-09-12 09:44:48 -070040#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070042#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070044
Brian Swetlandb14a2c62010-08-12 18:21:12 -070045#include <private/android_filesystem_config.h>
46
Brian Swetland03ee9472010-08-12 18:01:08 -070047/* README
48 *
49 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070050 *
Brian Swetland03ee9472010-08-12 18:01:08 -070051 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070052 * directory permissions (all files are given fixed owner, group, and
53 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070054 * changeable, symlinks and hardlinks are not createable, etc.
55 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070057 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070058 * It must be run as root, but will drop to requested UID/GID as soon as it
59 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070060 *
61 * Things I believe to be true:
62 *
63 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
64 * CREAT) must bump that node's refcount
65 * - don't forget that FORGET can forget multiple references (req->nlookup)
66 * - if an op that returns a fuse_entry fails writing the reply to the
67 * kernel, you must rollback the refcount to reflect the reference the
68 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070069 *
70 * This daemon can also derive custom filesystem permissions based on directory
71 * structure when requested. These custom permissions support several features:
72 *
73 * - Apps can access their own files in /Android/data/com.example/ without
74 * requiring any additional GIDs.
75 * - Separate permissions for protecting directories like Pictures and Music.
76 * - Multi-user separation on the same physical device.
77 *
78 * The derived permissions look like this:
79 *
80 * rwxrwx--x root:sdcard_rw /
81 * rwxrwx--- root:sdcard_pics /Pictures
82 * rwxrwx--- root:sdcard_av /Music
83 *
84 * rwxrwx--x root:sdcard_rw /Android
85 * rwxrwx--x root:sdcard_rw /Android/data
86 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
87 * rwxrwx--x root:sdcard_rw /Android/obb/
88 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
89 *
90 * rwxrwx--- root:sdcard_all /Android/user
91 * rwxrwx--x root:sdcard_rw /Android/user/10
92 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070093 */
94
95#define FUSE_TRACE 0
96
97#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070098#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070099#else
100#define TRACE(x...) do {} while (0)
101#endif
102
Elliott Hughes300d5642014-07-08 13:53:26 -0700103#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700104
105#define FUSE_UNKNOWN_INO 0xffffffff
106
Jeff Brown84715842012-05-25 14:07:47 -0700107/* Maximum number of bytes to write in one request. */
108#define MAX_WRITE (256 * 1024)
109
110/* Maximum number of bytes to read in one request. */
111#define MAX_READ (128 * 1024)
112
113/* Largest possible request.
114 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
115 * the largest possible data payload. */
116#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
117
Jeff Brown6249b902012-05-26 14:32:54 -0700118/* Default number of threads. */
119#define DEFAULT_NUM_THREADS 2
120
121/* Pseudo-error constant used to indicate that no fuse status is needed
122 * or that a reply has already been written. */
123#define NO_STATUS 1
124
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125/* Path to system-provided mapping of package name to appIds */
126static const char* const kPackagesListFile = "/data/system/packages.list";
127
128/* Supplementary groups to execute with */
129static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
130
131/* Permission mode for a specific node. Controls how file permissions
132 * are derived for children nodes. */
133typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700134 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700135 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700136 /* This node is one level above a normal root; used for legacy layouts
137 * which use the first level to represent user_id. */
138 PERM_LEGACY_PRE_ROOT,
139 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700145 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700146 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700147 /* This node is "/Android/media" */
148 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700149 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700150 PERM_ANDROID_USER,
151} perm_t;
152
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700153/* Permissions structure to derive */
154typedef enum {
155 DERIVE_NONE,
156 DERIVE_LEGACY,
157 DERIVE_UNIFIED,
158} derive_t;
159
Brian Swetland03ee9472010-08-12 18:01:08 -0700160struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700161 int fd;
162};
163
164struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700165 DIR *d;
166};
167
168struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700169 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700170 __u64 nid;
171 __u64 gen;
172
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700173 /* State derived based on current position in hierarchy. */
174 perm_t perm;
175 userid_t userid;
176 uid_t uid;
177 gid_t gid;
178 mode_t mode;
179
Paul Eastham11ccdb32010-10-14 11:04:26 -0700180 struct node *next; /* per-dir sibling list */
181 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700182 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700183
Jeff Brown6249b902012-05-26 14:32:54 -0700184 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700185 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800186 /* If non-null, this is the real name of the file in the underlying storage.
187 * This may differ from the field "name" only by case.
188 * strlen(actual_name) will always equal strlen(name), so it is safe to use
189 * namelen for both fields.
190 */
191 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700192
193 /* If non-null, an exact underlying path that should be grafted into this
194 * position. Used to support things like OBB. */
195 char* graft_path;
196 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700197};
198
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700199static int str_hash(void *key) {
200 return hashmapHash(key, strlen(key));
201}
202
Jeff Sharkey44d63422013-09-12 09:44:48 -0700203/** Test if two string keys are equal ignoring case */
204static bool str_icase_equals(void *keyA, void *keyB) {
205 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700206}
207
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700208static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800209 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700210}
211
212static bool int_equals(void *keyA, void *keyB) {
213 return keyA == keyB;
214}
215
Jeff Brown7729d242012-05-25 15:35:28 -0700216/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700217struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700218 pthread_mutex_t lock;
219
Brian Swetland03ee9472010-08-12 18:01:08 -0700220 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700221 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700222 derive_t derive;
223 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700224 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700225 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700226 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700227
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700228 Hashmap* package_to_appid;
229 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700230};
231
Jeff Brown7729d242012-05-25 15:35:28 -0700232/* Private data used by a single fuse handler. */
233struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700234 struct fuse* fuse;
235 int token;
236
Jeff Brown7729d242012-05-25 15:35:28 -0700237 /* To save memory, we never use the contents of the request buffer and the read
238 * buffer at the same time. This allows us to share the underlying storage. */
239 union {
240 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800241 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700242 };
243};
Brian Swetland03ee9472010-08-12 18:01:08 -0700244
Jeff Brown6249b902012-05-26 14:32:54 -0700245static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700246{
Jeff Brown6249b902012-05-26 14:32:54 -0700247 return (void *) (uintptr_t) nid;
248}
Brian Swetland03ee9472010-08-12 18:01:08 -0700249
Jeff Brown6249b902012-05-26 14:32:54 -0700250static inline __u64 ptr_to_id(void *ptr)
251{
252 return (__u64) (uintptr_t) ptr;
253}
Brian Swetland03ee9472010-08-12 18:01:08 -0700254
Jeff Brown6249b902012-05-26 14:32:54 -0700255static void acquire_node_locked(struct node* node)
256{
257 node->refcount++;
258 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
259}
260
261static void remove_node_from_parent_locked(struct node* node);
262
263static void release_node_locked(struct node* node)
264{
265 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
266 if (node->refcount > 0) {
267 node->refcount--;
268 if (!node->refcount) {
269 TRACE("DESTROY %p (%s)\n", node, node->name);
270 remove_node_from_parent_locked(node);
271
272 /* TODO: remove debugging - poison memory */
273 memset(node->name, 0xef, node->namelen);
274 free(node->name);
275 free(node->actual_name);
276 memset(node, 0xfc, sizeof(*node));
277 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800278 }
Jeff Brown6249b902012-05-26 14:32:54 -0700279 } else {
280 ERROR("Zero refcnt %p\n", node);
281 }
282}
283
284static void add_node_to_parent_locked(struct node *node, struct node *parent) {
285 node->parent = parent;
286 node->next = parent->child;
287 parent->child = node;
288 acquire_node_locked(parent);
289}
290
291static void remove_node_from_parent_locked(struct node* node)
292{
293 if (node->parent) {
294 if (node->parent->child == node) {
295 node->parent->child = node->parent->child->next;
296 } else {
297 struct node *node2;
298 node2 = node->parent->child;
299 while (node2->next != node)
300 node2 = node2->next;
301 node2->next = node->next;
302 }
303 release_node_locked(node->parent);
304 node->parent = NULL;
305 node->next = NULL;
306 }
307}
308
309/* Gets the absolute path to a node into the provided buffer.
310 *
311 * Populates 'buf' with the path and returns the length of the path on success,
312 * or returns -1 if the path is too long for the provided buffer.
313 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700314static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
315 const char* name;
316 size_t namelen;
317 if (node->graft_path) {
318 name = node->graft_path;
319 namelen = node->graft_pathlen;
320 } else if (node->actual_name) {
321 name = node->actual_name;
322 namelen = node->namelen;
323 } else {
324 name = node->name;
325 namelen = node->namelen;
326 }
327
Jeff Brown6249b902012-05-26 14:32:54 -0700328 if (bufsize < namelen + 1) {
329 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700330 }
331
Jeff Brown6249b902012-05-26 14:32:54 -0700332 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700333 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700334 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
335 if (pathlen < 0) {
336 return -1;
337 }
338 buf[pathlen++] = '/';
339 }
340
Jeff Brown6249b902012-05-26 14:32:54 -0700341 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
342 return pathlen + namelen;
343}
344
345/* Finds the absolute path of a file within a given directory.
346 * Performs a case-insensitive search for the file and sets the buffer to the path
347 * of the first matching file. If 'search' is zero or if no match is found, sets
348 * the buffer to the path that the file would have, assuming the name were case-sensitive.
349 *
350 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
351 * or returns NULL if the path is too long for the provided buffer.
352 */
353static char* find_file_within(const char* path, const char* name,
354 char* buf, size_t bufsize, int search)
355{
356 size_t pathlen = strlen(path);
357 size_t namelen = strlen(name);
358 size_t childlen = pathlen + namelen + 1;
359 char* actual;
360
361 if (bufsize <= childlen) {
362 return NULL;
363 }
364
365 memcpy(buf, path, pathlen);
366 buf[pathlen] = '/';
367 actual = buf + pathlen + 1;
368 memcpy(actual, name, namelen + 1);
369
370 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800371 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700372 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800373 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700374 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700375 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800376 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800377 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700378 if (!strcasecmp(entry->d_name, name)) {
379 /* we have a match - replace the name, don't need to copy the null again */
380 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800381 break;
382 }
383 }
384 closedir(dir);
385 }
Jeff Brown6249b902012-05-26 14:32:54 -0700386 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800387}
388
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700389static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800390{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700391 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700392 attr->size = s->st_size;
393 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800394 attr->atime = s->st_atim.tv_sec;
395 attr->mtime = s->st_mtim.tv_sec;
396 attr->ctime = s->st_ctim.tv_sec;
397 attr->atimensec = s->st_atim.tv_nsec;
398 attr->mtimensec = s->st_mtim.tv_nsec;
399 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700400 attr->mode = s->st_mode;
401 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700402
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700403 attr->uid = node->uid;
404 attr->gid = node->gid;
405
406 /* Filter requested mode based on underlying file, and
407 * pass through file type. */
408 int owner_mode = s->st_mode & 0700;
409 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
410 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
411}
412
Jeff Sharkey44d63422013-09-12 09:44:48 -0700413static int touch(char* path, mode_t mode) {
414 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
415 if (fd == -1) {
416 if (errno == EEXIST) {
417 return 0;
418 } else {
419 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
420 return -1;
421 }
422 }
423 close(fd);
424 return 0;
425}
426
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700427static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
428 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700429 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700430
431 /* By default, each node inherits from its parent */
432 node->perm = PERM_INHERIT;
433 node->userid = parent->userid;
434 node->uid = parent->uid;
435 node->gid = parent->gid;
436 node->mode = parent->mode;
437
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700438 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700439 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700440 }
441
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700442 /* Derive custom permissions based on parent and current node */
443 switch (parent->perm) {
444 case PERM_INHERIT:
445 /* Already inherited above */
446 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700447 case PERM_LEGACY_PRE_ROOT:
448 /* Legacy internal layout places users at top level */
449 node->perm = PERM_ROOT;
450 node->userid = strtoul(node->name, NULL, 10);
451 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700452 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700453 /* Assume masked off by default. */
454 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700455 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700456 /* App-specific directories inside; let anyone traverse */
457 node->perm = PERM_ANDROID;
458 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700459 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700460 if (!strcasecmp(node->name, "DCIM")
461 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700462 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700463 } else if (!strcasecmp(node->name, "Alarms")
464 || !strcasecmp(node->name, "Movies")
465 || !strcasecmp(node->name, "Music")
466 || !strcasecmp(node->name, "Notifications")
467 || !strcasecmp(node->name, "Podcasts")
468 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700469 node->gid = AID_SDCARD_AV;
470 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700471 }
472 break;
473 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700474 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700475 /* App-specific directories inside; let anyone traverse */
476 node->perm = PERM_ANDROID_DATA;
477 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* App-specific directories inside; let anyone traverse */
480 node->perm = PERM_ANDROID_OBB;
481 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700482 /* Single OBB directory is always shared */
483 node->graft_path = fuse->obbpath;
484 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700485 } else if (!strcasecmp(node->name, "media")) {
486 /* App-specific directories inside; let anyone traverse */
487 node->perm = PERM_ANDROID_MEDIA;
488 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700489 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700490 /* User directories must only be accessible to system, protected
491 * by sdcard_all. Zygote will bind mount the appropriate user-
492 * specific path. */
493 node->perm = PERM_ANDROID_USER;
494 node->gid = AID_SDCARD_ALL;
495 node->mode = 0770;
496 }
497 break;
498 case PERM_ANDROID_DATA:
499 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700500 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800501 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700502 if (appid != 0) {
503 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700504 }
505 node->mode = 0770;
506 break;
507 case PERM_ANDROID_USER:
508 /* Root of a secondary user */
509 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700510 node->userid = strtoul(node->name, NULL, 10);
511 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700512 node->mode = 0771;
513 break;
514 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700515}
516
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700517/* Return if the calling UID holds sdcard_rw. */
518static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700519 /* No additional permissions enforcement */
520 if (fuse->derive == DERIVE_NONE) {
521 return true;
522 }
523
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700524 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800525 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700526}
527
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700528/* Kernel has already enforced everything we returned through
529 * derive_permissions_locked(), so this is used to lock down access
530 * even further, such as enforcing that apps hold sdcard_rw. */
531static bool check_caller_access_to_name(struct fuse* fuse,
532 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700533 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700534 /* Always block security-sensitive files at root */
535 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700536 if (!strcasecmp(name, "autorun.inf")
537 || !strcasecmp(name, ".android_secure")
538 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700539 return false;
540 }
541 }
542
543 /* No additional permissions enforcement */
544 if (fuse->derive == DERIVE_NONE) {
545 return true;
546 }
547
Jeff Sharkey44d63422013-09-12 09:44:48 -0700548 /* Root always has access; access for any other UIDs should always
549 * be controlled through packages.list. */
550 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700551 return true;
552 }
553
554 /* If asking to write, verify that caller either owns the
555 * parent or holds sdcard_rw. */
556 if (mode & W_OK) {
557 if (parent_node && hdr->uid == parent_node->uid) {
558 return true;
559 }
560
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700561 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700562 }
563
564 /* No extra permissions to enforce */
565 return true;
566}
567
568static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700569 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
570 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700571}
572
Jeff Brown6249b902012-05-26 14:32:54 -0700573struct node *create_node_locked(struct fuse* fuse,
574 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700575{
576 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700577 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700578
Paul Eastham11ccdb32010-10-14 11:04:26 -0700579 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700580 if (!node) {
581 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700582 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700583 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700584 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700585 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700586 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700587 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700588 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700589 if (strcmp(name, actual_name)) {
590 node->actual_name = malloc(namelen + 1);
591 if (!node->actual_name) {
592 free(node->name);
593 free(node);
594 return NULL;
595 }
596 memcpy(node->actual_name, actual_name, namelen + 1);
597 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700598 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700599 node->nid = ptr_to_id(node);
600 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700601
602 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700603 acquire_node_locked(node);
604 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700605 return node;
606}
607
Jeff Brown6249b902012-05-26 14:32:54 -0700608static int rename_node_locked(struct node *node, const char *name,
609 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700610{
Jeff Brown6249b902012-05-26 14:32:54 -0700611 size_t namelen = strlen(name);
612 int need_actual_name = strcmp(name, actual_name);
613
614 /* make the storage bigger without actually changing the name
615 * in case an error occurs part way */
616 if (namelen > node->namelen) {
617 char* new_name = realloc(node->name, namelen + 1);
618 if (!new_name) {
619 return -ENOMEM;
620 }
621 node->name = new_name;
622 if (need_actual_name && node->actual_name) {
623 char* new_actual_name = realloc(node->actual_name, namelen + 1);
624 if (!new_actual_name) {
625 return -ENOMEM;
626 }
627 node->actual_name = new_actual_name;
628 }
629 }
630
631 /* update the name, taking care to allocate storage before overwriting the old name */
632 if (need_actual_name) {
633 if (!node->actual_name) {
634 node->actual_name = malloc(namelen + 1);
635 if (!node->actual_name) {
636 return -ENOMEM;
637 }
638 }
639 memcpy(node->actual_name, actual_name, namelen + 1);
640 } else {
641 free(node->actual_name);
642 node->actual_name = NULL;
643 }
644 memcpy(node->name, name, namelen + 1);
645 node->namelen = namelen;
646 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700647}
648
Jeff Brown6249b902012-05-26 14:32:54 -0700649static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700650{
Jeff Brown6249b902012-05-26 14:32:54 -0700651 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700652 return &fuse->root;
653 } else {
654 return id_to_ptr(nid);
655 }
656}
657
Jeff Brown6249b902012-05-26 14:32:54 -0700658static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
659 char* buf, size_t bufsize)
660{
661 struct node* node = lookup_node_by_id_locked(fuse, nid);
662 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
663 node = NULL;
664 }
665 return node;
666}
667
668static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700669{
670 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700671 /* use exact string comparison, nodes that differ by case
672 * must be considered distinct even if they refer to the same
673 * underlying file as otherwise operations such as "mv x x"
674 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700675 if (!strcmp(name, node->name)) {
676 return node;
677 }
678 }
679 return 0;
680}
681
Jeff Brown6249b902012-05-26 14:32:54 -0700682static struct node* acquire_or_create_child_locked(
683 struct fuse* fuse, struct node* parent,
684 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700685{
Jeff Brown6249b902012-05-26 14:32:54 -0700686 struct node* child = lookup_child_by_name_locked(parent, name);
687 if (child) {
688 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800689 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700690 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800691 }
Jeff Brown6249b902012-05-26 14:32:54 -0700692 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700693}
694
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700695static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700696 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700697 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700698
Jeff Brown6249b902012-05-26 14:32:54 -0700699 fuse->fd = fd;
700 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700701 fuse->derive = derive;
702 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700703 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700704
Jeff Brown6249b902012-05-26 14:32:54 -0700705 memset(&fuse->root, 0, sizeof(fuse->root));
706 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
707 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700708 fuse->root.namelen = strlen(source_path);
709 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700710 fuse->root.userid = 0;
711 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700712
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700713 /* Set up root node for various modes of operation */
714 switch (derive) {
715 case DERIVE_NONE:
716 /* Traditional behavior that treats entire device as being accessible
717 * to sdcard_rw, and no permissions are derived. */
718 fuse->root.perm = PERM_ROOT;
719 fuse->root.mode = 0775;
720 fuse->root.gid = AID_SDCARD_RW;
721 break;
722 case DERIVE_LEGACY:
723 /* Legacy behavior used to support internal multiuser layout which
724 * places user_id at the top directory level, with the actual roots
725 * just below that. Shared OBB path is also at top level. */
726 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
727 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700728 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700729 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700730 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
731 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700732 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700733 break;
734 case DERIVE_UNIFIED:
735 /* Unified multiuser layout which places secondary user_id under
736 * /Android/user and shared OBB path under /Android/obb. */
737 fuse->root.perm = PERM_ROOT;
738 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700739 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700740 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700741 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
742 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
743 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700744 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700745}
746
Jeff Brown6249b902012-05-26 14:32:54 -0700747static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700748{
749 struct fuse_out_header hdr;
750 hdr.len = sizeof(hdr);
751 hdr.error = err;
752 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700753 write(fuse->fd, &hdr, sizeof(hdr));
754}
755
Jeff Brown6249b902012-05-26 14:32:54 -0700756static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700757{
758 struct fuse_out_header hdr;
759 struct iovec vec[2];
760 int res;
761
762 hdr.len = len + sizeof(hdr);
763 hdr.error = 0;
764 hdr.unique = unique;
765
766 vec[0].iov_base = &hdr;
767 vec[0].iov_len = sizeof(hdr);
768 vec[1].iov_base = data;
769 vec[1].iov_len = len;
770
771 res = writev(fuse->fd, vec, 2);
772 if (res < 0) {
773 ERROR("*** REPLY FAILED *** %d\n", errno);
774 }
775}
776
Jeff Brown6249b902012-05-26 14:32:54 -0700777static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
778 struct node* parent, const char* name, const char* actual_name,
779 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700780{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700781 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700782 struct fuse_entry_out out;
783 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700784
Jeff Brown6249b902012-05-26 14:32:54 -0700785 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700786 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700787 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700788
Jeff Brown6249b902012-05-26 14:32:54 -0700789 pthread_mutex_lock(&fuse->lock);
790 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
791 if (!node) {
792 pthread_mutex_unlock(&fuse->lock);
793 return -ENOMEM;
794 }
795 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700796 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700797 out.attr_valid = 10;
798 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700799 out.nodeid = node->nid;
800 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700801 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700802 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700803 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700804}
805
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700806static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700807 const char* path)
808{
809 struct fuse_attr_out out;
810 struct stat s;
811
812 if (lstat(path, &s) < 0) {
813 return -errno;
814 }
815 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700816 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700817 out.attr_valid = 10;
818 fuse_reply(fuse, unique, &out, sizeof(out));
819 return NO_STATUS;
820}
821
822static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700823 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700824{
Jeff Brown6249b902012-05-26 14:32:54 -0700825 struct node* parent_node;
826 char parent_path[PATH_MAX];
827 char child_path[PATH_MAX];
828 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700829
Jeff Brown6249b902012-05-26 14:32:54 -0700830 pthread_mutex_lock(&fuse->lock);
831 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
832 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100833 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700834 parent_node ? parent_node->name : "?");
835 pthread_mutex_unlock(&fuse->lock);
836
837 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
838 child_path, sizeof(child_path), 1))) {
839 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700840 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700841 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700842 return -EACCES;
843 }
844
Jeff Brown6249b902012-05-26 14:32:54 -0700845 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700846}
847
Jeff Brown6249b902012-05-26 14:32:54 -0700848static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700849 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
850{
Jeff Brown6249b902012-05-26 14:32:54 -0700851 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700852
Jeff Brown6249b902012-05-26 14:32:54 -0700853 pthread_mutex_lock(&fuse->lock);
854 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100855 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700856 hdr->nodeid, node ? node->name : "?");
857 if (node) {
858 __u64 n = req->nlookup;
859 while (n--) {
860 release_node_locked(node);
861 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700862 }
Jeff Brown6249b902012-05-26 14:32:54 -0700863 pthread_mutex_unlock(&fuse->lock);
864 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865}
866
Jeff Brown6249b902012-05-26 14:32:54 -0700867static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
869{
Jeff Brown6249b902012-05-26 14:32:54 -0700870 struct node* node;
871 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700872
Jeff Brown6249b902012-05-26 14:32:54 -0700873 pthread_mutex_lock(&fuse->lock);
874 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100875 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700876 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
877 pthread_mutex_unlock(&fuse->lock);
878
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700880 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700882 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700883 return -EACCES;
884 }
885
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700886 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700887}
888
Jeff Brown6249b902012-05-26 14:32:54 -0700889static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700890 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
891{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700892 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700893 struct node* node;
894 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700895 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700896
Jeff Brown6249b902012-05-26 14:32:54 -0700897 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700898 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700899 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100900 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700901 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
902 pthread_mutex_unlock(&fuse->lock);
903
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700904 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700905 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700906 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800907
908 if (!(req->valid & FATTR_FH) &&
909 !check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700910 return -EACCES;
911 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700912
Jeff Brown6249b902012-05-26 14:32:54 -0700913 /* XXX: incomplete implementation on purpose.
914 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700915
Elliott Hughes853574d2014-07-31 12:03:03 -0700916 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700917 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700918 }
919
920 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
921 * are both set, then set it to the current time. Else, set it to the
922 * time specified in the request. Same goes for mtime. Use utimensat(2)
923 * as it allows ATIME and MTIME to be changed independently, and has
924 * nanosecond resolution which fuse also has.
925 */
926 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
927 times[0].tv_nsec = UTIME_OMIT;
928 times[1].tv_nsec = UTIME_OMIT;
929 if (req->valid & FATTR_ATIME) {
930 if (req->valid & FATTR_ATIME_NOW) {
931 times[0].tv_nsec = UTIME_NOW;
932 } else {
933 times[0].tv_sec = req->atime;
934 times[0].tv_nsec = req->atimensec;
935 }
936 }
937 if (req->valid & FATTR_MTIME) {
938 if (req->valid & FATTR_MTIME_NOW) {
939 times[1].tv_nsec = UTIME_NOW;
940 } else {
941 times[1].tv_sec = req->mtime;
942 times[1].tv_nsec = req->mtimensec;
943 }
944 }
Jeff Brown6249b902012-05-26 14:32:54 -0700945 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
946 handler->token, path, times[0].tv_sec, times[1].tv_sec);
947 if (utimensat(-1, path, times, 0) < 0) {
948 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700949 }
950 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700951 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952}
953
Jeff Brown6249b902012-05-26 14:32:54 -0700954static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700955 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
956{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700957 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700958 struct node* parent_node;
959 char parent_path[PATH_MAX];
960 char child_path[PATH_MAX];
961 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700962
Jeff Brown6249b902012-05-26 14:32:54 -0700963 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700964 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700965 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
966 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100967 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700968 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
969 pthread_mutex_unlock(&fuse->lock);
970
971 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
972 child_path, sizeof(child_path), 1))) {
973 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700975 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700976 return -EACCES;
977 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700978 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700979 if (mknod(child_path, mode, req->rdev) < 0) {
980 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700981 }
Jeff Brown6249b902012-05-26 14:32:54 -0700982 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700983}
984
Jeff Brown6249b902012-05-26 14:32:54 -0700985static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700986 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
987{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700988 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700989 struct node* parent_node;
990 char parent_path[PATH_MAX];
991 char child_path[PATH_MAX];
992 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700993
Jeff Brown6249b902012-05-26 14:32:54 -0700994 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700995 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700996 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
997 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100998 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700999 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
1000 pthread_mutex_unlock(&fuse->lock);
1001
1002 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1003 child_path, sizeof(child_path), 1))) {
1004 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001006 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001007 return -EACCES;
1008 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001009 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001010 if (mkdir(child_path, mode) < 0) {
1011 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001012 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001013
1014 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1015 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1016 char nomedia[PATH_MAX];
1017 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1018 if (touch(nomedia, 0664) != 0) {
1019 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1020 return -ENOENT;
1021 }
1022 }
1023 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1024 char nomedia[PATH_MAX];
1025 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1026 if (touch(nomedia, 0664) != 0) {
1027 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1028 return -ENOENT;
1029 }
1030 }
1031
Jeff Brown6249b902012-05-26 14:32:54 -07001032 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001033}
1034
Jeff Brown6249b902012-05-26 14:32:54 -07001035static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001036 const struct fuse_in_header* hdr, const char* name)
1037{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001038 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001039 struct node* parent_node;
1040 char parent_path[PATH_MAX];
1041 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042
Jeff Brown6249b902012-05-26 14:32:54 -07001043 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001044 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001045 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1046 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001047 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001048 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1049 pthread_mutex_unlock(&fuse->lock);
1050
1051 if (!parent_node || !find_file_within(parent_path, name,
1052 child_path, sizeof(child_path), 1)) {
1053 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001054 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001055 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001056 return -EACCES;
1057 }
Jeff Brown6249b902012-05-26 14:32:54 -07001058 if (unlink(child_path) < 0) {
1059 return -errno;
1060 }
1061 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001062}
1063
Jeff Brown6249b902012-05-26 14:32:54 -07001064static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001065 const struct fuse_in_header* hdr, const char* name)
1066{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001067 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001068 struct node* parent_node;
1069 char parent_path[PATH_MAX];
1070 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001071
Jeff Brown6249b902012-05-26 14:32:54 -07001072 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001073 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001074 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1075 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001076 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001077 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1078 pthread_mutex_unlock(&fuse->lock);
1079
1080 if (!parent_node || !find_file_within(parent_path, name,
1081 child_path, sizeof(child_path), 1)) {
1082 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001083 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001084 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001085 return -EACCES;
1086 }
Jeff Brown6249b902012-05-26 14:32:54 -07001087 if (rmdir(child_path) < 0) {
1088 return -errno;
1089 }
1090 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001091}
1092
Jeff Brown6249b902012-05-26 14:32:54 -07001093static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001094 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001095 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001096{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001097 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001098 struct node* old_parent_node;
1099 struct node* new_parent_node;
1100 struct node* child_node;
1101 char old_parent_path[PATH_MAX];
1102 char new_parent_path[PATH_MAX];
1103 char old_child_path[PATH_MAX];
1104 char new_child_path[PATH_MAX];
1105 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001106 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001107
Jeff Brown6249b902012-05-26 14:32:54 -07001108 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001109 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001110 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1111 old_parent_path, sizeof(old_parent_path));
1112 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1113 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001114 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001115 old_name, new_name,
1116 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1117 req->newdir, new_parent_node ? new_parent_node->name : "?");
1118 if (!old_parent_node || !new_parent_node) {
1119 res = -ENOENT;
1120 goto lookup_error;
1121 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001122 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001123 res = -EACCES;
1124 goto lookup_error;
1125 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001126 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001127 res = -EACCES;
1128 goto lookup_error;
1129 }
Jeff Brown6249b902012-05-26 14:32:54 -07001130 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1131 if (!child_node || get_node_path_locked(child_node,
1132 old_child_path, sizeof(old_child_path)) < 0) {
1133 res = -ENOENT;
1134 goto lookup_error;
1135 }
1136 acquire_node_locked(child_node);
1137 pthread_mutex_unlock(&fuse->lock);
1138
1139 /* Special case for renaming a file where destination is same path
1140 * differing only by case. In this case we don't want to look for a case
1141 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1142 */
1143 int search = old_parent_node != new_parent_node
1144 || strcasecmp(old_name, new_name);
1145 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1146 new_child_path, sizeof(new_child_path), search))) {
1147 res = -ENOENT;
1148 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001149 }
1150
Jeff Brown6249b902012-05-26 14:32:54 -07001151 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1152 res = rename(old_child_path, new_child_path);
1153 if (res < 0) {
1154 res = -errno;
1155 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156 }
1157
Jeff Brown6249b902012-05-26 14:32:54 -07001158 pthread_mutex_lock(&fuse->lock);
1159 res = rename_node_locked(child_node, new_name, new_actual_name);
1160 if (!res) {
1161 remove_node_from_parent_locked(child_node);
1162 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001163 }
Jeff Brown6249b902012-05-26 14:32:54 -07001164 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165
Jeff Brown6249b902012-05-26 14:32:54 -07001166io_error:
1167 pthread_mutex_lock(&fuse->lock);
1168done:
1169 release_node_locked(child_node);
1170lookup_error:
1171 pthread_mutex_unlock(&fuse->lock);
1172 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001173}
1174
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001175static int open_flags_to_access_mode(int open_flags) {
1176 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1177 return R_OK;
1178 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1179 return W_OK;
1180 } else {
1181 /* Probably O_RDRW, but treat as default to be safe */
1182 return R_OK | W_OK;
1183 }
1184}
1185
Jeff Brown6249b902012-05-26 14:32:54 -07001186static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001187 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1188{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001189 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001190 struct node* node;
1191 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001192 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001194
Jeff Brown6249b902012-05-26 14:32:54 -07001195 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001196 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001197 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001198 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001199 req->flags, hdr->nodeid, node ? node->name : "?");
1200 pthread_mutex_unlock(&fuse->lock);
1201
1202 if (!node) {
1203 return -ENOENT;
1204 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001205 if (!check_caller_access_to_node(fuse, hdr, node,
1206 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001207 return -EACCES;
1208 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209 h = malloc(sizeof(*h));
1210 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001211 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 }
Jeff Brown6249b902012-05-26 14:32:54 -07001213 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214 h->fd = open(path, req->flags);
1215 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001217 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001218 }
1219 out.fh = ptr_to_id(h);
1220 out.open_flags = 0;
1221 out.padding = 0;
1222 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001223 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224}
1225
Jeff Brown6249b902012-05-26 14:32:54 -07001226static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1228{
1229 struct handle *h = id_to_ptr(req->fh);
1230 __u64 unique = hdr->unique;
1231 __u32 size = req->size;
1232 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001233 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001234 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001235
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001236 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1237 * overlaps the request buffer and will clobber data in the request. This
1238 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001239
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001240 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1241 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001242 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001243 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001245 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001246 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001247 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001249 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001250 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001251}
1252
Jeff Brown6249b902012-05-26 14:32:54 -07001253static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001254 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1255 const void* buffer)
1256{
1257 struct fuse_write_out out;
1258 struct handle *h = id_to_ptr(req->fh);
1259 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001260 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001261
Arpad Horvath49e93442014-02-18 10:18:25 +01001262 if (req->flags & O_DIRECT) {
1263 memcpy(aligned_buffer, buffer, req->size);
1264 buffer = (const __u8*) aligned_buffer;
1265 }
Jeff Brown6249b902012-05-26 14:32:54 -07001266
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001267 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001268 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 res = pwrite64(h->fd, buffer, req->size, req->offset);
1270 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001271 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272 }
1273 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001274 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001276 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001277}
1278
Jeff Brown6249b902012-05-26 14:32:54 -07001279static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001280 const struct fuse_in_header* hdr)
1281{
Jeff Brown6249b902012-05-26 14:32:54 -07001282 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283 struct statfs stat;
1284 struct fuse_statfs_out out;
1285 int res;
1286
Jeff Brown6249b902012-05-26 14:32:54 -07001287 pthread_mutex_lock(&fuse->lock);
1288 TRACE("[%d] STATFS\n", handler->token);
1289 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1290 pthread_mutex_unlock(&fuse->lock);
1291 if (res < 0) {
1292 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001293 }
Jeff Brown6249b902012-05-26 14:32:54 -07001294 if (statfs(fuse->root.name, &stat) < 0) {
1295 return -errno;
1296 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 memset(&out, 0, sizeof(out));
1298 out.st.blocks = stat.f_blocks;
1299 out.st.bfree = stat.f_bfree;
1300 out.st.bavail = stat.f_bavail;
1301 out.st.files = stat.f_files;
1302 out.st.ffree = stat.f_ffree;
1303 out.st.bsize = stat.f_bsize;
1304 out.st.namelen = stat.f_namelen;
1305 out.st.frsize = stat.f_frsize;
1306 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001307 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308}
1309
Jeff Brown6249b902012-05-26 14:32:54 -07001310static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001311 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1312{
1313 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001314
1315 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001316 close(h->fd);
1317 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001318 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001319}
1320
Jeff Brown6249b902012-05-26 14:32:54 -07001321static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001322 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1323{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001324 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1325 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001326
Elliott Hughesf6d67372014-07-08 14:38:26 -07001327 int fd = -1;
1328 if (is_dir) {
1329 struct dirhandle *dh = id_to_ptr(req->fh);
1330 fd = dirfd(dh->d);
1331 } else {
1332 struct handle *h = id_to_ptr(req->fh);
1333 fd = h->fd;
1334 }
1335
1336 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1337 is_dir ? "FSYNCDIR" : "FSYNC",
1338 id_to_ptr(req->fh), fd, is_data_sync);
1339 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1340 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001341 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001342 }
Jeff Brown6249b902012-05-26 14:32:54 -07001343 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344}
1345
Jeff Brown6249b902012-05-26 14:32:54 -07001346static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001347 const struct fuse_in_header* hdr)
1348{
Jeff Brown6249b902012-05-26 14:32:54 -07001349 TRACE("[%d] FLUSH\n", handler->token);
1350 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001351}
1352
Jeff Brown6249b902012-05-26 14:32:54 -07001353static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1355{
Jeff Brown6249b902012-05-26 14:32:54 -07001356 struct node* node;
1357 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001358 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001360
Jeff Brown6249b902012-05-26 14:32:54 -07001361 pthread_mutex_lock(&fuse->lock);
1362 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001363 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001364 hdr->nodeid, node ? node->name : "?");
1365 pthread_mutex_unlock(&fuse->lock);
1366
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001368 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001370 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001371 return -EACCES;
1372 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001373 h = malloc(sizeof(*h));
1374 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001375 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376 }
Jeff Brown6249b902012-05-26 14:32:54 -07001377 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001379 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001381 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001382 }
1383 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001384 out.open_flags = 0;
1385 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001386 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001387 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388}
1389
Jeff Brown6249b902012-05-26 14:32:54 -07001390static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001391 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1392{
1393 char buffer[8192];
1394 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1395 struct dirent *de;
1396 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001397
1398 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001399 if (req->offset == 0) {
1400 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001401 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001402 rewinddir(h->d);
1403 }
1404 de = readdir(h->d);
1405 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001406 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001407 }
1408 fde->ino = FUSE_UNKNOWN_INO;
1409 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1410 fde->off = req->offset + 1;
1411 fde->type = de->d_type;
1412 fde->namelen = strlen(de->d_name);
1413 memcpy(fde->name, de->d_name, fde->namelen + 1);
1414 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001415 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1416 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001417}
1418
Jeff Brown6249b902012-05-26 14:32:54 -07001419static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1421{
1422 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001423
1424 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425 closedir(h->d);
1426 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001427 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001428}
1429
Jeff Brown6249b902012-05-26 14:32:54 -07001430static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001431 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1432{
1433 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001434 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001435
Jeff Brown6249b902012-05-26 14:32:54 -07001436 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1437 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001438
1439 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1440 * defined (fuse version 7.6). The structure is the same from 7.6 through
1441 * 7.22. Beginning with 7.23, the structure increased in size and added
1442 * new parameters.
1443 */
1444 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1445 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1446 req->major, req->minor, FUSE_KERNEL_VERSION);
1447 return -1;
1448 }
1449
1450 out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
1451 fuse_struct_size = sizeof(out);
1452#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1453 /* FUSE_KERNEL_VERSION >= 23. */
1454
1455 /* If the kernel only works on minor revs older than or equal to 22,
1456 * then use the older structure size since this code only uses the 7.22
1457 * version of the structure. */
1458 if (req->minor <= 22) {
1459 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1460 }
1461#endif
1462
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001463 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001464 out.max_readahead = req->max_readahead;
1465 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1466 out.max_background = 32;
1467 out.congestion_threshold = 32;
1468 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001469 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001470 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001471}
1472
Jeff Brown6249b902012-05-26 14:32:54 -07001473static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001474 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1475{
Brian Swetland03ee9472010-08-12 18:01:08 -07001476 switch (hdr->opcode) {
1477 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001478 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001479 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 }
Jeff Brown84715842012-05-25 14:07:47 -07001481
Brian Swetland03ee9472010-08-12 18:01:08 -07001482 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001483 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001484 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001485 }
Jeff Brown84715842012-05-25 14:07:47 -07001486
Brian Swetland03ee9472010-08-12 18:01:08 -07001487 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001488 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001489 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001490 }
Jeff Brown84715842012-05-25 14:07:47 -07001491
Brian Swetland03ee9472010-08-12 18:01:08 -07001492 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001493 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001494 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001495 }
Jeff Brown84715842012-05-25 14:07:47 -07001496
Brian Swetland03ee9472010-08-12 18:01:08 -07001497// case FUSE_READLINK:
1498// case FUSE_SYMLINK:
1499 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001500 const struct fuse_mknod_in *req = data;
1501 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001502 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001503 }
Jeff Brown84715842012-05-25 14:07:47 -07001504
Brian Swetland03ee9472010-08-12 18:01:08 -07001505 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001506 const struct fuse_mkdir_in *req = data;
1507 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001508 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001509 }
Jeff Brown84715842012-05-25 14:07:47 -07001510
Brian Swetland03ee9472010-08-12 18:01:08 -07001511 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001512 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001513 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001514 }
Jeff Brown84715842012-05-25 14:07:47 -07001515
Brian Swetland03ee9472010-08-12 18:01:08 -07001516 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001517 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001518 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001519 }
Jeff Brown84715842012-05-25 14:07:47 -07001520
Brian Swetland03ee9472010-08-12 18:01:08 -07001521 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001522 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001523 const char *old_name = ((const char*) data) + sizeof(*req);
1524 const char *new_name = old_name + strlen(old_name) + 1;
1525 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001526 }
Jeff Brown84715842012-05-25 14:07:47 -07001527
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001528// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001530 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001531 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001532 }
Jeff Brown84715842012-05-25 14:07:47 -07001533
Brian Swetland03ee9472010-08-12 18:01:08 -07001534 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001535 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001536 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001537 }
Jeff Brown84715842012-05-25 14:07:47 -07001538
Brian Swetland03ee9472010-08-12 18:01:08 -07001539 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001540 const struct fuse_write_in *req = data;
1541 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001542 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001543 }
Jeff Brown84715842012-05-25 14:07:47 -07001544
Mike Lockwood4553b082010-08-16 14:14:44 -04001545 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001546 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001547 }
Jeff Brown84715842012-05-25 14:07:47 -07001548
Brian Swetland03ee9472010-08-12 18:01:08 -07001549 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001550 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001551 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001552 }
Jeff Brown84715842012-05-25 14:07:47 -07001553
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001554 case FUSE_FSYNC:
1555 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001556 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001557 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001558 }
1559
Brian Swetland03ee9472010-08-12 18:01:08 -07001560// case FUSE_SETXATTR:
1561// case FUSE_GETXATTR:
1562// case FUSE_LISTXATTR:
1563// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001564 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001565 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001566 }
1567
Brian Swetland03ee9472010-08-12 18:01:08 -07001568 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001569 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001570 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001571 }
Jeff Brown84715842012-05-25 14:07:47 -07001572
Brian Swetland03ee9472010-08-12 18:01:08 -07001573 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001574 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001575 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001576 }
Jeff Brown84715842012-05-25 14:07:47 -07001577
Brian Swetland03ee9472010-08-12 18:01:08 -07001578 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001579 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001580 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001581 }
Jeff Brown84715842012-05-25 14:07:47 -07001582
Brian Swetland03ee9472010-08-12 18:01:08 -07001583 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001584 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001585 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001586 }
Jeff Brown84715842012-05-25 14:07:47 -07001587
Brian Swetland03ee9472010-08-12 18:01:08 -07001588 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001589 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001590 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1591 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001592 }
Jeff Brown84715842012-05-25 14:07:47 -07001593 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001594}
1595
Jeff Brown6249b902012-05-26 14:32:54 -07001596static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001597{
Jeff Brown6249b902012-05-26 14:32:54 -07001598 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001599 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001600 ssize_t len = read(fuse->fd,
1601 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001602 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001603 if (errno != EINTR) {
1604 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1605 }
1606 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001607 }
Jeff Brown84715842012-05-25 14:07:47 -07001608
1609 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001610 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1611 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001612 }
1613
Jeff Brown7729d242012-05-25 15:35:28 -07001614 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001615 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001616 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1617 handler->token, (size_t)len, hdr->len);
1618 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001619 }
1620
Jeff Brown7729d242012-05-25 15:35:28 -07001621 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001622 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001623 __u64 unique = hdr->unique;
1624 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001625
1626 /* We do not access the request again after this point because the underlying
1627 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001628
1629 if (res != NO_STATUS) {
1630 if (res) {
1631 TRACE("[%d] ERROR %d\n", handler->token, res);
1632 }
1633 fuse_status(fuse, unique, res);
1634 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001635 }
1636}
1637
Jeff Brown6249b902012-05-26 14:32:54 -07001638static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001639{
Jeff Brown6249b902012-05-26 14:32:54 -07001640 struct fuse_handler* handler = data;
1641 handle_fuse_requests(handler);
1642 return NULL;
1643}
1644
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001645static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001646 Hashmap* map = context;
1647 hashmapRemove(map, key);
1648 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001649 return true;
1650}
1651
1652static bool remove_int_to_null(void *key, void *value, void *context) {
1653 Hashmap* map = context;
1654 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001655 return true;
1656}
1657
1658static int read_package_list(struct fuse *fuse) {
1659 pthread_mutex_lock(&fuse->lock);
1660
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001661 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1662 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001663
1664 FILE* file = fopen(kPackagesListFile, "r");
1665 if (!file) {
1666 ERROR("failed to open package list: %s\n", strerror(errno));
1667 pthread_mutex_unlock(&fuse->lock);
1668 return -1;
1669 }
1670
1671 char buf[512];
1672 while (fgets(buf, sizeof(buf), file) != NULL) {
1673 char package_name[512];
1674 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001675 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001676
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001677 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1678 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001679 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001680
1681 char* token = strtok(gids, ",");
1682 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001683 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001684 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001685 break;
1686 }
1687 token = strtok(NULL, ",");
1688 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001689 }
1690 }
1691
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001692 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001693 hashmapSize(fuse->package_to_appid),
1694 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001695 fclose(file);
1696 pthread_mutex_unlock(&fuse->lock);
1697 return 0;
1698}
1699
1700static void watch_package_list(struct fuse* fuse) {
1701 struct inotify_event *event;
1702 char event_buf[512];
1703
1704 int nfd = inotify_init();
1705 if (nfd < 0) {
1706 ERROR("inotify_init failed: %s\n", strerror(errno));
1707 return;
1708 }
1709
1710 bool active = false;
1711 while (1) {
1712 if (!active) {
1713 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1714 if (res == -1) {
1715 if (errno == ENOENT || errno == EACCES) {
1716 /* Framework may not have created yet, sleep and retry */
1717 ERROR("missing packages.list; retrying\n");
1718 sleep(3);
1719 continue;
1720 } else {
1721 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1722 return;
1723 }
1724 }
1725
1726 /* Watch above will tell us about any future changes, so
1727 * read the current state. */
1728 if (read_package_list(fuse) == -1) {
1729 ERROR("read_package_list failed: %s\n", strerror(errno));
1730 return;
1731 }
1732 active = true;
1733 }
1734
1735 int event_pos = 0;
1736 int res = read(nfd, event_buf, sizeof(event_buf));
1737 if (res < (int) sizeof(*event)) {
1738 if (errno == EINTR)
1739 continue;
1740 ERROR("failed to read inotify event: %s\n", strerror(errno));
1741 return;
1742 }
1743
1744 while (res >= (int) sizeof(*event)) {
1745 int event_size;
1746 event = (struct inotify_event *) (event_buf + event_pos);
1747
1748 TRACE("inotify event: %08x\n", event->mask);
1749 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1750 /* Previously watched file was deleted, probably due to move
1751 * that swapped in new data; re-arm the watch and read. */
1752 active = false;
1753 }
1754
1755 event_size = sizeof(*event) + event->len;
1756 res -= event_size;
1757 event_pos += event_size;
1758 }
1759 }
1760}
1761
Jeff Brown6249b902012-05-26 14:32:54 -07001762static int ignite_fuse(struct fuse* fuse, int num_threads)
1763{
1764 struct fuse_handler* handlers;
1765 int i;
1766
1767 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1768 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001769 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001770 return -ENOMEM;
1771 }
1772
1773 for (i = 0; i < num_threads; i++) {
1774 handlers[i].fuse = fuse;
1775 handlers[i].token = i;
1776 }
1777
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001778 /* When deriving permissions, this thread is used to process inotify events,
1779 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001780 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001781 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001782 pthread_t thread;
1783 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1784 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001785 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001786 goto quit;
1787 }
1788 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001789
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001790 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001791 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001792 } else {
1793 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001794 }
1795
1796 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001797
1798 /* don't bother killing all of the other threads or freeing anything,
1799 * should never get here anyhow */
1800quit:
1801 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001802}
1803
Mike Lockwood4f35e622011-01-12 14:39:44 -05001804static int usage()
1805{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001806 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1807 " -u: specify UID to run as\n"
1808 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001809 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001810 " -t: specify number of threads to use (default %d)\n"
1811 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001812 " -l: derive file permissions based on legacy internal layout\n"
1813 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001814 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001815 return 1;
1816}
1817
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001818static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001819 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1820 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001821 int fd;
1822 char opts[256];
1823 int res;
1824 struct fuse fuse;
1825
1826 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001827 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001828
1829 fd = open("/dev/fuse", O_RDWR);
1830 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001831 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001832 return -1;
1833 }
1834
1835 snprintf(opts, sizeof(opts),
1836 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1837 fd, uid, gid);
1838
Daisuke Okitsu00690852014-11-24 09:37:55 +01001839 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001840 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001841 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1842 goto error;
1843 }
1844
1845 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1846 if (res < 0) {
1847 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001848 goto error;
1849 }
1850
1851 res = setgid(gid);
1852 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001853 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001854 goto error;
1855 }
1856
1857 res = setuid(uid);
1858 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001859 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001860 goto error;
1861 }
1862
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001863 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001864
1865 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001866 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001867
1868 /* we do not attempt to umount the file system here because we are no longer
1869 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001870
1871error:
1872 close(fd);
1873 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001874}
1875
Brian Swetland03ee9472010-08-12 18:01:08 -07001876int main(int argc, char **argv)
1877{
Brian Swetland03ee9472010-08-12 18:01:08 -07001878 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001879 const char *source_path = NULL;
1880 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001881 uid_t uid = 0;
1882 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001883 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001884 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001885 derive_t derive = DERIVE_NONE;
1886 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001887 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001888 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001889 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001890
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001891 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001892 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001893 switch (opt) {
1894 case 'u':
1895 uid = strtoul(optarg, NULL, 10);
1896 break;
1897 case 'g':
1898 gid = strtoul(optarg, NULL, 10);
1899 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001900 case 'w':
1901 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001902 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001903 case 't':
1904 num_threads = strtoul(optarg, NULL, 10);
1905 break;
1906 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001907 derive = DERIVE_UNIFIED;
1908 break;
1909 case 'l':
1910 derive = DERIVE_LEGACY;
1911 break;
1912 case 's':
1913 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001914 break;
1915 case '?':
1916 default:
1917 return usage();
1918 }
1919 }
1920
1921 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001922 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001923 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001924 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001925 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001926 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001927 } else if (!uid) {
1928 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001929 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001930 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001931 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001932 ERROR("too many arguments\n");
1933 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001934 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001935 }
1936
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001937 if (!source_path) {
1938 ERROR("no source path specified\n");
1939 return usage();
1940 }
1941 if (!dest_path) {
1942 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001943 return usage();
1944 }
Jeff Brown26567352012-05-25 13:27:43 -07001945 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001946 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001947 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001948 }
Jeff Brown6249b902012-05-26 14:32:54 -07001949 if (num_threads < 1) {
1950 ERROR("number of threads must be at least 1\n");
1951 return usage();
1952 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001953 if (split_perms && derive == DERIVE_NONE) {
1954 ERROR("cannot split permissions without deriving\n");
1955 return usage();
1956 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001957
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001958 rlim.rlim_cur = 8192;
1959 rlim.rlim_max = 8192;
1960 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1961 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1962 }
1963
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001964 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1965 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1966 sleep(1);
1967 }
1968
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001969 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001970 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001971}