blob: 318d0ee0e6239b328faf016b2a320152d7c634f7 [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 Sharkeydfe0cba2013-07-03 17:08:29 -0700149} perm_t;
150
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700151/* Permissions structure to derive */
152typedef enum {
153 DERIVE_NONE,
154 DERIVE_LEGACY,
155 DERIVE_UNIFIED,
156} derive_t;
157
Brian Swetland03ee9472010-08-12 18:01:08 -0700158struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700159 int fd;
160};
161
162struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700163 DIR *d;
164};
165
166struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700167 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700168 __u64 nid;
169 __u64 gen;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000170 /*
171 * The inode number for this FUSE node. Note that this isn't stable across
172 * multiple invocations of the FUSE daemon.
173 */
174 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700175
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700176 /* State derived based on current position in hierarchy. */
177 perm_t perm;
178 userid_t userid;
179 uid_t uid;
180 gid_t gid;
181 mode_t mode;
182
Paul Eastham11ccdb32010-10-14 11:04:26 -0700183 struct node *next; /* per-dir sibling list */
184 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700185 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700186
Jeff Brown6249b902012-05-26 14:32:54 -0700187 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700188 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800189 /* If non-null, this is the real name of the file in the underlying storage.
190 * This may differ from the field "name" only by case.
191 * strlen(actual_name) will always equal strlen(name), so it is safe to use
192 * namelen for both fields.
193 */
194 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700195
196 /* If non-null, an exact underlying path that should be grafted into this
197 * position. Used to support things like OBB. */
198 char* graft_path;
199 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700200};
201
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700202static int str_hash(void *key) {
203 return hashmapHash(key, strlen(key));
204}
205
Jeff Sharkey44d63422013-09-12 09:44:48 -0700206/** Test if two string keys are equal ignoring case */
207static bool str_icase_equals(void *keyA, void *keyB) {
208 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700209}
210
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700211static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800212 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700213}
214
215static bool int_equals(void *keyA, void *keyB) {
216 return keyA == keyB;
217}
218
Jeff Brown7729d242012-05-25 15:35:28 -0700219/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700220struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700221 pthread_mutex_t lock;
222
Brian Swetland03ee9472010-08-12 18:01:08 -0700223 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700224 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700225 derive_t derive;
226 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700227 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700228 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700229 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700230
Narayan Kamathfaa09352015-01-13 18:21:10 +0000231 /* Used to allocate unique inode numbers for fuse nodes. We use
232 * a simple counter based scheme where inode numbers from deleted
233 * nodes aren't reused. Note that inode allocations are not stable
234 * across multiple invocation of the sdcard daemon, but that shouldn't
235 * be a huge problem in practice.
236 *
237 * Note that we restrict inodes to 32 bit unsigned integers to prevent
238 * truncation on 32 bit processes when unsigned long long stat.st_ino is
239 * assigned to an unsigned long ino_t type in an LP32 process.
240 *
241 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
242 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
243 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
244 * in fs/fuse/inode.c).
245 *
246 * Accesses must be guarded by |lock|.
247 */
248 __u32 inode_ctr;
249
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700250 Hashmap* package_to_appid;
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700251 Hashmap* uid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700252};
253
Jeff Brown7729d242012-05-25 15:35:28 -0700254/* Private data used by a single fuse handler. */
255struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700256 struct fuse* fuse;
257 int token;
258
Jeff Brown7729d242012-05-25 15:35:28 -0700259 /* To save memory, we never use the contents of the request buffer and the read
260 * buffer at the same time. This allows us to share the underlying storage. */
261 union {
262 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800263 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700264 };
265};
Brian Swetland03ee9472010-08-12 18:01:08 -0700266
Jeff Brown6249b902012-05-26 14:32:54 -0700267static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700268{
Jeff Brown6249b902012-05-26 14:32:54 -0700269 return (void *) (uintptr_t) nid;
270}
Brian Swetland03ee9472010-08-12 18:01:08 -0700271
Jeff Brown6249b902012-05-26 14:32:54 -0700272static inline __u64 ptr_to_id(void *ptr)
273{
274 return (__u64) (uintptr_t) ptr;
275}
Brian Swetland03ee9472010-08-12 18:01:08 -0700276
Jeff Brown6249b902012-05-26 14:32:54 -0700277static void acquire_node_locked(struct node* node)
278{
279 node->refcount++;
280 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
281}
282
283static void remove_node_from_parent_locked(struct node* node);
284
285static void release_node_locked(struct node* node)
286{
287 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
288 if (node->refcount > 0) {
289 node->refcount--;
290 if (!node->refcount) {
291 TRACE("DESTROY %p (%s)\n", node, node->name);
292 remove_node_from_parent_locked(node);
293
294 /* TODO: remove debugging - poison memory */
295 memset(node->name, 0xef, node->namelen);
296 free(node->name);
297 free(node->actual_name);
298 memset(node, 0xfc, sizeof(*node));
299 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800300 }
Jeff Brown6249b902012-05-26 14:32:54 -0700301 } else {
302 ERROR("Zero refcnt %p\n", node);
303 }
304}
305
306static void add_node_to_parent_locked(struct node *node, struct node *parent) {
307 node->parent = parent;
308 node->next = parent->child;
309 parent->child = node;
310 acquire_node_locked(parent);
311}
312
313static void remove_node_from_parent_locked(struct node* node)
314{
315 if (node->parent) {
316 if (node->parent->child == node) {
317 node->parent->child = node->parent->child->next;
318 } else {
319 struct node *node2;
320 node2 = node->parent->child;
321 while (node2->next != node)
322 node2 = node2->next;
323 node2->next = node->next;
324 }
325 release_node_locked(node->parent);
326 node->parent = NULL;
327 node->next = NULL;
328 }
329}
330
331/* Gets the absolute path to a node into the provided buffer.
332 *
333 * Populates 'buf' with the path and returns the length of the path on success,
334 * or returns -1 if the path is too long for the provided buffer.
335 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700336static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
337 const char* name;
338 size_t namelen;
339 if (node->graft_path) {
340 name = node->graft_path;
341 namelen = node->graft_pathlen;
342 } else if (node->actual_name) {
343 name = node->actual_name;
344 namelen = node->namelen;
345 } else {
346 name = node->name;
347 namelen = node->namelen;
348 }
349
Jeff Brown6249b902012-05-26 14:32:54 -0700350 if (bufsize < namelen + 1) {
351 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700352 }
353
Jeff Brown6249b902012-05-26 14:32:54 -0700354 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700355 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700356 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
357 if (pathlen < 0) {
358 return -1;
359 }
360 buf[pathlen++] = '/';
361 }
362
Jeff Brown6249b902012-05-26 14:32:54 -0700363 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
364 return pathlen + namelen;
365}
366
367/* Finds the absolute path of a file within a given directory.
368 * Performs a case-insensitive search for the file and sets the buffer to the path
369 * of the first matching file. If 'search' is zero or if no match is found, sets
370 * the buffer to the path that the file would have, assuming the name were case-sensitive.
371 *
372 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
373 * or returns NULL if the path is too long for the provided buffer.
374 */
375static char* find_file_within(const char* path, const char* name,
376 char* buf, size_t bufsize, int search)
377{
378 size_t pathlen = strlen(path);
379 size_t namelen = strlen(name);
380 size_t childlen = pathlen + namelen + 1;
381 char* actual;
382
383 if (bufsize <= childlen) {
384 return NULL;
385 }
386
387 memcpy(buf, path, pathlen);
388 buf[pathlen] = '/';
389 actual = buf + pathlen + 1;
390 memcpy(actual, name, namelen + 1);
391
392 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800393 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700394 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800395 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700396 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700397 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800398 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800399 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700400 if (!strcasecmp(entry->d_name, name)) {
401 /* we have a match - replace the name, don't need to copy the null again */
402 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800403 break;
404 }
405 }
406 closedir(dir);
407 }
Jeff Brown6249b902012-05-26 14:32:54 -0700408 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800409}
410
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700411static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800412{
Narayan Kamathfaa09352015-01-13 18:21:10 +0000413 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700414 attr->size = s->st_size;
415 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800416 attr->atime = s->st_atim.tv_sec;
417 attr->mtime = s->st_mtim.tv_sec;
418 attr->ctime = s->st_ctim.tv_sec;
419 attr->atimensec = s->st_atim.tv_nsec;
420 attr->mtimensec = s->st_mtim.tv_nsec;
421 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700422 attr->mode = s->st_mode;
423 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700424
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700425 attr->uid = node->uid;
426 attr->gid = node->gid;
427
428 /* Filter requested mode based on underlying file, and
429 * pass through file type. */
430 int owner_mode = s->st_mode & 0700;
431 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
432 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
433}
434
Jeff Sharkey44d63422013-09-12 09:44:48 -0700435static int touch(char* path, mode_t mode) {
436 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
437 if (fd == -1) {
438 if (errno == EEXIST) {
439 return 0;
440 } else {
441 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
442 return -1;
443 }
444 }
445 close(fd);
446 return 0;
447}
448
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700449static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
450 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700451 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700452
453 /* By default, each node inherits from its parent */
454 node->perm = PERM_INHERIT;
455 node->userid = parent->userid;
456 node->uid = parent->uid;
457 node->gid = parent->gid;
458 node->mode = parent->mode;
459
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700460 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700461 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700462 }
463
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700464 /* Derive custom permissions based on parent and current node */
465 switch (parent->perm) {
466 case PERM_INHERIT:
467 /* Already inherited above */
468 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700469 case PERM_LEGACY_PRE_ROOT:
470 /* Legacy internal layout places users at top level */
471 node->perm = PERM_ROOT;
472 node->userid = strtoul(node->name, NULL, 10);
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700473 node->gid = multiuser_get_uid(node->userid, AID_SDCARD_R);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700474 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700475 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700476 /* Assume masked off by default. */
477 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* App-specific directories inside; let anyone traverse */
480 node->perm = PERM_ANDROID;
481 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700482 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700483 if (!strcasecmp(node->name, "DCIM")
484 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700485 node->gid = multiuser_get_uid(node->userid, AID_SDCARD_PICS);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700486 } else if (!strcasecmp(node->name, "Alarms")
487 || !strcasecmp(node->name, "Movies")
488 || !strcasecmp(node->name, "Music")
489 || !strcasecmp(node->name, "Notifications")
490 || !strcasecmp(node->name, "Podcasts")
491 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700492 node->gid = multiuser_get_uid(node->userid, AID_SDCARD_AV);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700493 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700494 }
495 break;
496 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700497 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700498 /* App-specific directories inside; let anyone traverse */
499 node->perm = PERM_ANDROID_DATA;
500 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700501 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700502 /* App-specific directories inside; let anyone traverse */
503 node->perm = PERM_ANDROID_OBB;
504 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700505 /* Single OBB directory is always shared */
506 node->graft_path = fuse->obbpath;
507 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700508 } else if (!strcasecmp(node->name, "media")) {
509 /* App-specific directories inside; let anyone traverse */
510 node->perm = PERM_ANDROID_MEDIA;
511 node->mode = 0771;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700512 }
513 break;
514 case PERM_ANDROID_DATA:
515 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700516 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800517 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700518 if (appid != 0) {
519 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700520 }
521 node->mode = 0770;
522 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700523 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700524}
525
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700526/* Return if the calling UID holds sdcard_rw. */
527static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700528 /* No additional permissions enforcement */
529 if (fuse->derive == DERIVE_NONE) {
530 return true;
531 }
532
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700533 return hashmapContainsKey(fuse->uid_with_rw, (void*) (uintptr_t) hdr->uid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700534}
535
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700536/* Kernel has already enforced everything we returned through
537 * derive_permissions_locked(), so this is used to lock down access
538 * even further, such as enforcing that apps hold sdcard_rw. */
539static bool check_caller_access_to_name(struct fuse* fuse,
540 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700541 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700542 /* Always block security-sensitive files at root */
543 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700544 if (!strcasecmp(name, "autorun.inf")
545 || !strcasecmp(name, ".android_secure")
546 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700547 return false;
548 }
549 }
550
551 /* No additional permissions enforcement */
552 if (fuse->derive == DERIVE_NONE) {
553 return true;
554 }
555
Jeff Sharkey44d63422013-09-12 09:44:48 -0700556 /* Root always has access; access for any other UIDs should always
557 * be controlled through packages.list. */
558 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700559 return true;
560 }
561
562 /* If asking to write, verify that caller either owns the
563 * parent or holds sdcard_rw. */
564 if (mode & W_OK) {
565 if (parent_node && hdr->uid == parent_node->uid) {
566 return true;
567 }
568
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700569 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700570 }
571
572 /* No extra permissions to enforce */
573 return true;
574}
575
576static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700577 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
578 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700579}
580
Jeff Brown6249b902012-05-26 14:32:54 -0700581struct node *create_node_locked(struct fuse* fuse,
582 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700583{
584 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700585 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700586
Narayan Kamathfaa09352015-01-13 18:21:10 +0000587 // Detect overflows in the inode counter. "4 billion nodes should be enough
588 // for everybody".
589 if (fuse->inode_ctr == 0) {
590 ERROR("No more inode numbers available");
591 return NULL;
592 }
593
Paul Eastham11ccdb32010-10-14 11:04:26 -0700594 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700595 if (!node) {
596 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700597 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700598 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700599 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700600 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700601 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700602 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700603 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700604 if (strcmp(name, actual_name)) {
605 node->actual_name = malloc(namelen + 1);
606 if (!node->actual_name) {
607 free(node->name);
608 free(node);
609 return NULL;
610 }
611 memcpy(node->actual_name, actual_name, namelen + 1);
612 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700613 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700614 node->nid = ptr_to_id(node);
Narayan Kamathfaa09352015-01-13 18:21:10 +0000615 node->ino = fuse->inode_ctr++;
Jeff Brown6249b902012-05-26 14:32:54 -0700616 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700617
618 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700619 acquire_node_locked(node);
620 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700621 return node;
622}
623
Jeff Brown6249b902012-05-26 14:32:54 -0700624static int rename_node_locked(struct node *node, const char *name,
625 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700626{
Jeff Brown6249b902012-05-26 14:32:54 -0700627 size_t namelen = strlen(name);
628 int need_actual_name = strcmp(name, actual_name);
629
630 /* make the storage bigger without actually changing the name
631 * in case an error occurs part way */
632 if (namelen > node->namelen) {
633 char* new_name = realloc(node->name, namelen + 1);
634 if (!new_name) {
635 return -ENOMEM;
636 }
637 node->name = new_name;
638 if (need_actual_name && node->actual_name) {
639 char* new_actual_name = realloc(node->actual_name, namelen + 1);
640 if (!new_actual_name) {
641 return -ENOMEM;
642 }
643 node->actual_name = new_actual_name;
644 }
645 }
646
647 /* update the name, taking care to allocate storage before overwriting the old name */
648 if (need_actual_name) {
649 if (!node->actual_name) {
650 node->actual_name = malloc(namelen + 1);
651 if (!node->actual_name) {
652 return -ENOMEM;
653 }
654 }
655 memcpy(node->actual_name, actual_name, namelen + 1);
656 } else {
657 free(node->actual_name);
658 node->actual_name = NULL;
659 }
660 memcpy(node->name, name, namelen + 1);
661 node->namelen = namelen;
662 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700663}
664
Jeff Brown6249b902012-05-26 14:32:54 -0700665static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700666{
Jeff Brown6249b902012-05-26 14:32:54 -0700667 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700668 return &fuse->root;
669 } else {
670 return id_to_ptr(nid);
671 }
672}
673
Jeff Brown6249b902012-05-26 14:32:54 -0700674static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
675 char* buf, size_t bufsize)
676{
677 struct node* node = lookup_node_by_id_locked(fuse, nid);
678 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
679 node = NULL;
680 }
681 return node;
682}
683
684static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700685{
686 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700687 /* use exact string comparison, nodes that differ by case
688 * must be considered distinct even if they refer to the same
689 * underlying file as otherwise operations such as "mv x x"
690 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700691 if (!strcmp(name, node->name)) {
692 return node;
693 }
694 }
695 return 0;
696}
697
Jeff Brown6249b902012-05-26 14:32:54 -0700698static struct node* acquire_or_create_child_locked(
699 struct fuse* fuse, struct node* parent,
700 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700701{
Jeff Brown6249b902012-05-26 14:32:54 -0700702 struct node* child = lookup_child_by_name_locked(parent, name);
703 if (child) {
704 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800705 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700706 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800707 }
Jeff Brown6249b902012-05-26 14:32:54 -0700708 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700709}
710
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700711static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700712 gid_t write_gid, userid_t owner_user, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700713 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700714
Jeff Brown6249b902012-05-26 14:32:54 -0700715 fuse->fd = fd;
716 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700717 fuse->derive = derive;
718 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700719 fuse->write_gid = write_gid;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000720 fuse->inode_ctr = 1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700721
Jeff Brown6249b902012-05-26 14:32:54 -0700722 memset(&fuse->root, 0, sizeof(fuse->root));
723 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
724 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700725 fuse->root.namelen = strlen(source_path);
726 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700727 fuse->root.userid = 0;
728 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700729
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700730 /* Set up root node for various modes of operation */
731 switch (derive) {
732 case DERIVE_NONE:
733 /* Traditional behavior that treats entire device as being accessible
734 * to sdcard_rw, and no permissions are derived. */
735 fuse->root.perm = PERM_ROOT;
736 fuse->root.mode = 0775;
737 fuse->root.gid = AID_SDCARD_RW;
738 break;
739 case DERIVE_LEGACY:
740 /* Legacy behavior used to support internal multiuser layout which
741 * places user_id at the top directory level, with the actual roots
742 * just below that. Shared OBB path is also at top level. */
743 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
744 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700745 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700746 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700747 fuse->uid_with_rw = hashmapCreate(128, int_hash, int_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700748 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700749 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700750 break;
751 case DERIVE_UNIFIED:
752 /* Unified multiuser layout which places secondary user_id under
753 * /Android/user and shared OBB path under /Android/obb. */
754 fuse->root.perm = PERM_ROOT;
755 fuse->root.mode = 0771;
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700756 fuse->root.userid = owner_user;
757 fuse->root.gid = multiuser_get_uid(owner_userid, AID_SDCARD_R);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700758 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkeya140afe2015-03-23 16:13:53 -0700759 fuse->uid_with_rw = hashmapCreate(128, int_hash, int_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700760 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
761 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700762 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700763}
764
Jeff Brown6249b902012-05-26 14:32:54 -0700765static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700766{
767 struct fuse_out_header hdr;
768 hdr.len = sizeof(hdr);
769 hdr.error = err;
770 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700771 write(fuse->fd, &hdr, sizeof(hdr));
772}
773
Jeff Brown6249b902012-05-26 14:32:54 -0700774static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700775{
776 struct fuse_out_header hdr;
777 struct iovec vec[2];
778 int res;
779
780 hdr.len = len + sizeof(hdr);
781 hdr.error = 0;
782 hdr.unique = unique;
783
784 vec[0].iov_base = &hdr;
785 vec[0].iov_len = sizeof(hdr);
786 vec[1].iov_base = data;
787 vec[1].iov_len = len;
788
789 res = writev(fuse->fd, vec, 2);
790 if (res < 0) {
791 ERROR("*** REPLY FAILED *** %d\n", errno);
792 }
793}
794
Jeff Brown6249b902012-05-26 14:32:54 -0700795static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
796 struct node* parent, const char* name, const char* actual_name,
797 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700798{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700799 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700800 struct fuse_entry_out out;
801 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700802
Jeff Brown6249b902012-05-26 14:32:54 -0700803 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700804 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700805 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700806
Jeff Brown6249b902012-05-26 14:32:54 -0700807 pthread_mutex_lock(&fuse->lock);
808 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
809 if (!node) {
810 pthread_mutex_unlock(&fuse->lock);
811 return -ENOMEM;
812 }
813 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700814 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700815 out.attr_valid = 10;
816 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700817 out.nodeid = node->nid;
818 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700819 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700820 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700821 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700822}
823
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700824static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700825 const char* path)
826{
827 struct fuse_attr_out out;
828 struct stat s;
829
830 if (lstat(path, &s) < 0) {
831 return -errno;
832 }
833 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700834 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700835 out.attr_valid = 10;
836 fuse_reply(fuse, unique, &out, sizeof(out));
837 return NO_STATUS;
838}
839
840static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700841 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700842{
Jeff Brown6249b902012-05-26 14:32:54 -0700843 struct node* parent_node;
844 char parent_path[PATH_MAX];
845 char child_path[PATH_MAX];
846 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700847
Jeff Brown6249b902012-05-26 14:32:54 -0700848 pthread_mutex_lock(&fuse->lock);
849 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
850 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100851 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700852 parent_node ? parent_node->name : "?");
853 pthread_mutex_unlock(&fuse->lock);
854
855 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
856 child_path, sizeof(child_path), 1))) {
857 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700858 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700859 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700860 return -EACCES;
861 }
862
Jeff Brown6249b902012-05-26 14:32:54 -0700863 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700864}
865
Jeff Brown6249b902012-05-26 14:32:54 -0700866static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700867 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
868{
Jeff Brown6249b902012-05-26 14:32:54 -0700869 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700870
Jeff Brown6249b902012-05-26 14:32:54 -0700871 pthread_mutex_lock(&fuse->lock);
872 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100873 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700874 hdr->nodeid, node ? node->name : "?");
875 if (node) {
876 __u64 n = req->nlookup;
877 while (n--) {
878 release_node_locked(node);
879 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880 }
Jeff Brown6249b902012-05-26 14:32:54 -0700881 pthread_mutex_unlock(&fuse->lock);
882 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883}
884
Jeff Brown6249b902012-05-26 14:32:54 -0700885static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
887{
Jeff Brown6249b902012-05-26 14:32:54 -0700888 struct node* node;
889 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700890
Jeff Brown6249b902012-05-26 14:32:54 -0700891 pthread_mutex_lock(&fuse->lock);
892 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100893 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700894 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
895 pthread_mutex_unlock(&fuse->lock);
896
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700898 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700899 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700900 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700901 return -EACCES;
902 }
903
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700904 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905}
906
Jeff Brown6249b902012-05-26 14:32:54 -0700907static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700908 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
909{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700910 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700911 struct node* node;
912 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700913 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700914
Jeff Brown6249b902012-05-26 14:32:54 -0700915 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700916 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700917 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100918 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700919 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
920 pthread_mutex_unlock(&fuse->lock);
921
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700922 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700923 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700924 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800925
926 if (!(req->valid & FATTR_FH) &&
927 !check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700928 return -EACCES;
929 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700930
Jeff Brown6249b902012-05-26 14:32:54 -0700931 /* XXX: incomplete implementation on purpose.
932 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700933
Elliott Hughes853574d2014-07-31 12:03:03 -0700934 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700935 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700936 }
937
938 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
939 * are both set, then set it to the current time. Else, set it to the
940 * time specified in the request. Same goes for mtime. Use utimensat(2)
941 * as it allows ATIME and MTIME to be changed independently, and has
942 * nanosecond resolution which fuse also has.
943 */
944 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
945 times[0].tv_nsec = UTIME_OMIT;
946 times[1].tv_nsec = UTIME_OMIT;
947 if (req->valid & FATTR_ATIME) {
948 if (req->valid & FATTR_ATIME_NOW) {
949 times[0].tv_nsec = UTIME_NOW;
950 } else {
951 times[0].tv_sec = req->atime;
952 times[0].tv_nsec = req->atimensec;
953 }
954 }
955 if (req->valid & FATTR_MTIME) {
956 if (req->valid & FATTR_MTIME_NOW) {
957 times[1].tv_nsec = UTIME_NOW;
958 } else {
959 times[1].tv_sec = req->mtime;
960 times[1].tv_nsec = req->mtimensec;
961 }
962 }
Jeff Brown6249b902012-05-26 14:32:54 -0700963 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
964 handler->token, path, times[0].tv_sec, times[1].tv_sec);
965 if (utimensat(-1, path, times, 0) < 0) {
966 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967 }
968 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700969 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970}
971
Jeff Brown6249b902012-05-26 14:32:54 -0700972static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700973 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
974{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700975 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700976 struct node* parent_node;
977 char parent_path[PATH_MAX];
978 char child_path[PATH_MAX];
979 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980
Jeff Brown6249b902012-05-26 14:32:54 -0700981 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700982 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700983 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
984 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100985 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700986 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
987 pthread_mutex_unlock(&fuse->lock);
988
989 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
990 child_path, sizeof(child_path), 1))) {
991 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700992 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700993 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700994 return -EACCES;
995 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700997 if (mknod(child_path, mode, req->rdev) < 0) {
998 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700999 }
Jeff Brown6249b902012-05-26 14:32:54 -07001000 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001}
1002
Jeff Brown6249b902012-05-26 14:32:54 -07001003static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001004 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
1005{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001006 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001007 struct node* parent_node;
1008 char parent_path[PATH_MAX];
1009 char child_path[PATH_MAX];
1010 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001011
Jeff Brown6249b902012-05-26 14:32:54 -07001012 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001013 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001014 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1015 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001016 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001017 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
1018 pthread_mutex_unlock(&fuse->lock);
1019
1020 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1021 child_path, sizeof(child_path), 1))) {
1022 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001024 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001025 return -EACCES;
1026 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001027 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001028 if (mkdir(child_path, mode) < 0) {
1029 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001030 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001031
1032 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1033 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1034 char nomedia[PATH_MAX];
1035 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1036 if (touch(nomedia, 0664) != 0) {
1037 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1038 return -ENOENT;
1039 }
1040 }
1041 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1042 char nomedia[PATH_MAX];
1043 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1044 if (touch(nomedia, 0664) != 0) {
1045 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1046 return -ENOENT;
1047 }
1048 }
1049
Jeff Brown6249b902012-05-26 14:32:54 -07001050 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001051}
1052
Jeff Brown6249b902012-05-26 14:32:54 -07001053static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001054 const struct fuse_in_header* hdr, const char* name)
1055{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001056 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001057 struct node* parent_node;
1058 char parent_path[PATH_MAX];
1059 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001060
Jeff Brown6249b902012-05-26 14:32:54 -07001061 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001062 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001063 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1064 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001065 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001066 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1067 pthread_mutex_unlock(&fuse->lock);
1068
1069 if (!parent_node || !find_file_within(parent_path, name,
1070 child_path, sizeof(child_path), 1)) {
1071 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001072 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001073 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001074 return -EACCES;
1075 }
Jeff Brown6249b902012-05-26 14:32:54 -07001076 if (unlink(child_path) < 0) {
1077 return -errno;
1078 }
1079 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001080}
1081
Jeff Brown6249b902012-05-26 14:32:54 -07001082static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001083 const struct fuse_in_header* hdr, const char* name)
1084{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001085 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001086 struct node* parent_node;
1087 char parent_path[PATH_MAX];
1088 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089
Jeff Brown6249b902012-05-26 14:32:54 -07001090 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001091 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001092 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1093 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001094 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001095 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1096 pthread_mutex_unlock(&fuse->lock);
1097
1098 if (!parent_node || !find_file_within(parent_path, name,
1099 child_path, sizeof(child_path), 1)) {
1100 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001101 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001102 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001103 return -EACCES;
1104 }
Jeff Brown6249b902012-05-26 14:32:54 -07001105 if (rmdir(child_path) < 0) {
1106 return -errno;
1107 }
1108 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001109}
1110
Jeff Brown6249b902012-05-26 14:32:54 -07001111static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001112 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001113 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001114{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001115 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001116 struct node* old_parent_node;
1117 struct node* new_parent_node;
1118 struct node* child_node;
1119 char old_parent_path[PATH_MAX];
1120 char new_parent_path[PATH_MAX];
1121 char old_child_path[PATH_MAX];
1122 char new_child_path[PATH_MAX];
1123 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001124 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001125
Jeff Brown6249b902012-05-26 14:32:54 -07001126 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001127 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001128 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1129 old_parent_path, sizeof(old_parent_path));
1130 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1131 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001132 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001133 old_name, new_name,
1134 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1135 req->newdir, new_parent_node ? new_parent_node->name : "?");
1136 if (!old_parent_node || !new_parent_node) {
1137 res = -ENOENT;
1138 goto lookup_error;
1139 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001140 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001141 res = -EACCES;
1142 goto lookup_error;
1143 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001144 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001145 res = -EACCES;
1146 goto lookup_error;
1147 }
Jeff Brown6249b902012-05-26 14:32:54 -07001148 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1149 if (!child_node || get_node_path_locked(child_node,
1150 old_child_path, sizeof(old_child_path)) < 0) {
1151 res = -ENOENT;
1152 goto lookup_error;
1153 }
1154 acquire_node_locked(child_node);
1155 pthread_mutex_unlock(&fuse->lock);
1156
1157 /* Special case for renaming a file where destination is same path
1158 * differing only by case. In this case we don't want to look for a case
1159 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1160 */
1161 int search = old_parent_node != new_parent_node
1162 || strcasecmp(old_name, new_name);
1163 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1164 new_child_path, sizeof(new_child_path), search))) {
1165 res = -ENOENT;
1166 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001167 }
1168
Jeff Brown6249b902012-05-26 14:32:54 -07001169 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1170 res = rename(old_child_path, new_child_path);
1171 if (res < 0) {
1172 res = -errno;
1173 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001174 }
1175
Jeff Brown6249b902012-05-26 14:32:54 -07001176 pthread_mutex_lock(&fuse->lock);
1177 res = rename_node_locked(child_node, new_name, new_actual_name);
1178 if (!res) {
1179 remove_node_from_parent_locked(child_node);
1180 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001181 }
Jeff Brown6249b902012-05-26 14:32:54 -07001182 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183
Jeff Brown6249b902012-05-26 14:32:54 -07001184io_error:
1185 pthread_mutex_lock(&fuse->lock);
1186done:
1187 release_node_locked(child_node);
1188lookup_error:
1189 pthread_mutex_unlock(&fuse->lock);
1190 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001191}
1192
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001193static int open_flags_to_access_mode(int open_flags) {
1194 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1195 return R_OK;
1196 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1197 return W_OK;
1198 } else {
1199 /* Probably O_RDRW, but treat as default to be safe */
1200 return R_OK | W_OK;
1201 }
1202}
1203
Jeff Brown6249b902012-05-26 14:32:54 -07001204static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1206{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001207 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001208 struct node* node;
1209 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001210 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212
Jeff Brown6249b902012-05-26 14:32:54 -07001213 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001214 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001215 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001216 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001217 req->flags, hdr->nodeid, node ? node->name : "?");
1218 pthread_mutex_unlock(&fuse->lock);
1219
1220 if (!node) {
1221 return -ENOENT;
1222 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001223 if (!check_caller_access_to_node(fuse, hdr, node,
1224 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001225 return -EACCES;
1226 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227 h = malloc(sizeof(*h));
1228 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001229 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001230 }
Jeff Brown6249b902012-05-26 14:32:54 -07001231 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001232 h->fd = open(path, req->flags);
1233 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001234 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001235 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001236 }
1237 out.fh = ptr_to_id(h);
1238 out.open_flags = 0;
1239 out.padding = 0;
1240 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001241 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001242}
1243
Jeff Brown6249b902012-05-26 14:32:54 -07001244static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1246{
1247 struct handle *h = id_to_ptr(req->fh);
1248 __u64 unique = hdr->unique;
1249 __u32 size = req->size;
1250 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001251 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001252 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001253
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001254 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1255 * overlaps the request buffer and will clobber data in the request. This
1256 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001257
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001258 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1259 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001260 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001261 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001263 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001264 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001265 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001267 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001268 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269}
1270
Jeff Brown6249b902012-05-26 14:32:54 -07001271static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1273 const void* buffer)
1274{
1275 struct fuse_write_out out;
1276 struct handle *h = id_to_ptr(req->fh);
1277 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001278 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001279
Arpad Horvath49e93442014-02-18 10:18:25 +01001280 if (req->flags & O_DIRECT) {
1281 memcpy(aligned_buffer, buffer, req->size);
1282 buffer = (const __u8*) aligned_buffer;
1283 }
Jeff Brown6249b902012-05-26 14:32:54 -07001284
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001285 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001286 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001287 res = pwrite64(h->fd, buffer, req->size, req->offset);
1288 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001289 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001290 }
1291 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001292 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001293 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001294 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001295}
1296
Jeff Brown6249b902012-05-26 14:32:54 -07001297static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001298 const struct fuse_in_header* hdr)
1299{
Jeff Brown6249b902012-05-26 14:32:54 -07001300 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001301 struct statfs stat;
1302 struct fuse_statfs_out out;
1303 int res;
1304
Jeff Brown6249b902012-05-26 14:32:54 -07001305 pthread_mutex_lock(&fuse->lock);
1306 TRACE("[%d] STATFS\n", handler->token);
1307 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1308 pthread_mutex_unlock(&fuse->lock);
1309 if (res < 0) {
1310 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001311 }
Jeff Brown6249b902012-05-26 14:32:54 -07001312 if (statfs(fuse->root.name, &stat) < 0) {
1313 return -errno;
1314 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001315 memset(&out, 0, sizeof(out));
1316 out.st.blocks = stat.f_blocks;
1317 out.st.bfree = stat.f_bfree;
1318 out.st.bavail = stat.f_bavail;
1319 out.st.files = stat.f_files;
1320 out.st.ffree = stat.f_ffree;
1321 out.st.bsize = stat.f_bsize;
1322 out.st.namelen = stat.f_namelen;
1323 out.st.frsize = stat.f_frsize;
1324 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001325 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326}
1327
Jeff Brown6249b902012-05-26 14:32:54 -07001328static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001329 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1330{
1331 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001332
1333 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001334 close(h->fd);
1335 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001336 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337}
1338
Jeff Brown6249b902012-05-26 14:32:54 -07001339static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001340 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1341{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001342 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1343 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001344
Elliott Hughesf6d67372014-07-08 14:38:26 -07001345 int fd = -1;
1346 if (is_dir) {
1347 struct dirhandle *dh = id_to_ptr(req->fh);
1348 fd = dirfd(dh->d);
1349 } else {
1350 struct handle *h = id_to_ptr(req->fh);
1351 fd = h->fd;
1352 }
1353
1354 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1355 is_dir ? "FSYNCDIR" : "FSYNC",
1356 id_to_ptr(req->fh), fd, is_data_sync);
1357 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1358 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001359 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001360 }
Jeff Brown6249b902012-05-26 14:32:54 -07001361 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362}
1363
Jeff Brown6249b902012-05-26 14:32:54 -07001364static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001365 const struct fuse_in_header* hdr)
1366{
Jeff Brown6249b902012-05-26 14:32:54 -07001367 TRACE("[%d] FLUSH\n", handler->token);
1368 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369}
1370
Jeff Brown6249b902012-05-26 14:32:54 -07001371static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001372 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1373{
Jeff Brown6249b902012-05-26 14:32:54 -07001374 struct node* node;
1375 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378
Jeff Brown6249b902012-05-26 14:32:54 -07001379 pthread_mutex_lock(&fuse->lock);
1380 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001381 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001382 hdr->nodeid, node ? node->name : "?");
1383 pthread_mutex_unlock(&fuse->lock);
1384
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001385 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001386 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001387 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001388 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001389 return -EACCES;
1390 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001391 h = malloc(sizeof(*h));
1392 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001393 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001394 }
Jeff Brown6249b902012-05-26 14:32:54 -07001395 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001396 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001397 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001398 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001399 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001400 }
1401 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001402 out.open_flags = 0;
1403 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001404 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001405 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001406}
1407
Jeff Brown6249b902012-05-26 14:32:54 -07001408static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001409 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1410{
1411 char buffer[8192];
1412 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1413 struct dirent *de;
1414 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001415
1416 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001417 if (req->offset == 0) {
1418 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001419 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 rewinddir(h->d);
1421 }
1422 de = readdir(h->d);
1423 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001424 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425 }
1426 fde->ino = FUSE_UNKNOWN_INO;
1427 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1428 fde->off = req->offset + 1;
1429 fde->type = de->d_type;
1430 fde->namelen = strlen(de->d_name);
1431 memcpy(fde->name, de->d_name, fde->namelen + 1);
1432 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001433 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1434 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001435}
1436
Jeff Brown6249b902012-05-26 14:32:54 -07001437static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001438 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1439{
1440 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001441
1442 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001443 closedir(h->d);
1444 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001445 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001446}
1447
Jeff Brown6249b902012-05-26 14:32:54 -07001448static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001449 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1450{
1451 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001452 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001453
Jeff Brown6249b902012-05-26 14:32:54 -07001454 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1455 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001456
1457 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1458 * defined (fuse version 7.6). The structure is the same from 7.6 through
1459 * 7.22. Beginning with 7.23, the structure increased in size and added
1460 * new parameters.
1461 */
1462 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1463 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1464 req->major, req->minor, FUSE_KERNEL_VERSION);
1465 return -1;
1466 }
1467
1468 out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
1469 fuse_struct_size = sizeof(out);
1470#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1471 /* FUSE_KERNEL_VERSION >= 23. */
1472
1473 /* If the kernel only works on minor revs older than or equal to 22,
1474 * then use the older structure size since this code only uses the 7.22
1475 * version of the structure. */
1476 if (req->minor <= 22) {
1477 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1478 }
1479#endif
1480
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001481 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001482 out.max_readahead = req->max_readahead;
1483 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1484 out.max_background = 32;
1485 out.congestion_threshold = 32;
1486 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001487 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001488 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001489}
1490
Jeff Brown6249b902012-05-26 14:32:54 -07001491static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001492 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1493{
Brian Swetland03ee9472010-08-12 18:01:08 -07001494 switch (hdr->opcode) {
1495 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001496 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001497 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001498 }
Jeff Brown84715842012-05-25 14:07:47 -07001499
Brian Swetland03ee9472010-08-12 18:01:08 -07001500 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001501 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001502 return handle_forget(fuse, handler, hdr, req);
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_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001506 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001507 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001508 }
Jeff Brown84715842012-05-25 14:07:47 -07001509
Brian Swetland03ee9472010-08-12 18:01:08 -07001510 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001511 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001512 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001513 }
Jeff Brown84715842012-05-25 14:07:47 -07001514
Brian Swetland03ee9472010-08-12 18:01:08 -07001515// case FUSE_READLINK:
1516// case FUSE_SYMLINK:
1517 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001518 const struct fuse_mknod_in *req = data;
1519 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001520 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001521 }
Jeff Brown84715842012-05-25 14:07:47 -07001522
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001524 const struct fuse_mkdir_in *req = data;
1525 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001526 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001527 }
Jeff Brown84715842012-05-25 14:07:47 -07001528
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001530 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001531 return handle_unlink(fuse, handler, hdr, name);
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_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001535 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001536 return handle_rmdir(fuse, handler, hdr, name);
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_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001540 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001541 const char *old_name = ((const char*) data) + sizeof(*req);
1542 const char *new_name = old_name + strlen(old_name) + 1;
1543 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001544 }
Jeff Brown84715842012-05-25 14:07:47 -07001545
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001546// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001547 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001548 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001549 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 }
Jeff Brown84715842012-05-25 14:07:47 -07001551
Brian Swetland03ee9472010-08-12 18:01:08 -07001552 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001553 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001554 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001555 }
Jeff Brown84715842012-05-25 14:07:47 -07001556
Brian Swetland03ee9472010-08-12 18:01:08 -07001557 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001558 const struct fuse_write_in *req = data;
1559 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001560 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001561 }
Jeff Brown84715842012-05-25 14:07:47 -07001562
Mike Lockwood4553b082010-08-16 14:14:44 -04001563 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001564 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001565 }
Jeff Brown84715842012-05-25 14:07:47 -07001566
Brian Swetland03ee9472010-08-12 18:01:08 -07001567 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001568 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001569 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 }
Jeff Brown84715842012-05-25 14:07:47 -07001571
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001572 case FUSE_FSYNC:
1573 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001574 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001575 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001576 }
1577
Brian Swetland03ee9472010-08-12 18:01:08 -07001578// case FUSE_SETXATTR:
1579// case FUSE_GETXATTR:
1580// case FUSE_LISTXATTR:
1581// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001582 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001583 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001584 }
1585
Brian Swetland03ee9472010-08-12 18:01:08 -07001586 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001587 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001588 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001589 }
Jeff Brown84715842012-05-25 14:07:47 -07001590
Brian Swetland03ee9472010-08-12 18:01:08 -07001591 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001592 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001593 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001594 }
Jeff Brown84715842012-05-25 14:07:47 -07001595
Brian Swetland03ee9472010-08-12 18:01:08 -07001596 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001597 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001598 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001599 }
Jeff Brown84715842012-05-25 14:07:47 -07001600
Brian Swetland03ee9472010-08-12 18:01:08 -07001601 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001602 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001603 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001604 }
Jeff Brown84715842012-05-25 14:07:47 -07001605
Brian Swetland03ee9472010-08-12 18:01:08 -07001606 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001607 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001608 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1609 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001610 }
Jeff Brown84715842012-05-25 14:07:47 -07001611 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001612}
1613
Jeff Brown6249b902012-05-26 14:32:54 -07001614static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001615{
Jeff Brown6249b902012-05-26 14:32:54 -07001616 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001617 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001618 ssize_t len = read(fuse->fd,
1619 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001620 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001621 if (errno != EINTR) {
1622 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1623 }
1624 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001625 }
Jeff Brown84715842012-05-25 14:07:47 -07001626
1627 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001628 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1629 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001630 }
1631
Jeff Brown7729d242012-05-25 15:35:28 -07001632 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001633 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001634 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1635 handler->token, (size_t)len, hdr->len);
1636 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001637 }
1638
Jeff Brown7729d242012-05-25 15:35:28 -07001639 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001640 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001641 __u64 unique = hdr->unique;
1642 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001643
1644 /* We do not access the request again after this point because the underlying
1645 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001646
1647 if (res != NO_STATUS) {
1648 if (res) {
1649 TRACE("[%d] ERROR %d\n", handler->token, res);
1650 }
1651 fuse_status(fuse, unique, res);
1652 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001653 }
1654}
1655
Jeff Brown6249b902012-05-26 14:32:54 -07001656static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001657{
Jeff Brown6249b902012-05-26 14:32:54 -07001658 struct fuse_handler* handler = data;
1659 handle_fuse_requests(handler);
1660 return NULL;
1661}
1662
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001663static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001664 Hashmap* map = context;
1665 hashmapRemove(map, key);
1666 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001667 return true;
1668}
1669
1670static bool remove_int_to_null(void *key, void *value, void *context) {
1671 Hashmap* map = context;
1672 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001673 return true;
1674}
1675
1676static int read_package_list(struct fuse *fuse) {
1677 pthread_mutex_lock(&fuse->lock);
1678
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001679 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001680 hashmapForEach(fuse->uid_with_rw, remove_int_to_null, fuse->uid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001681
1682 FILE* file = fopen(kPackagesListFile, "r");
1683 if (!file) {
1684 ERROR("failed to open package list: %s\n", strerror(errno));
1685 pthread_mutex_unlock(&fuse->lock);
1686 return -1;
1687 }
1688
1689 char buf[512];
1690 while (fgets(buf, sizeof(buf), file) != NULL) {
1691 char package_name[512];
1692 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001693 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001694
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001695 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1696 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001697 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001698
1699 char* token = strtok(gids, ",");
1700 while (token != NULL) {
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001701 // Current packages.list format is a bit funky; it blends per
1702 // user GID membership into a single per-app line. Here we
1703 // work backwards from the groups to build the per-user UIDs
1704 // that have write permission.
1705 gid_t gid = strtoul(token, NULL, 10);
1706 if (multiuser_get_app_id(gid) == fuse->write_gid) {
1707 uid_t uid = multiuser_get_uid(multiuser_get_user_id(gid), appid);
1708 hashmapPut(fuse->uid_with_rw, (void*) (uintptr_t) uid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001709 break;
1710 }
1711 token = strtok(NULL, ",");
1712 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001713 }
1714 }
1715
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001716 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001717 hashmapSize(fuse->package_to_appid),
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001718 hashmapSize(fuse->uid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001719 fclose(file);
1720 pthread_mutex_unlock(&fuse->lock);
1721 return 0;
1722}
1723
1724static void watch_package_list(struct fuse* fuse) {
1725 struct inotify_event *event;
1726 char event_buf[512];
1727
1728 int nfd = inotify_init();
1729 if (nfd < 0) {
1730 ERROR("inotify_init failed: %s\n", strerror(errno));
1731 return;
1732 }
1733
1734 bool active = false;
1735 while (1) {
1736 if (!active) {
1737 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1738 if (res == -1) {
1739 if (errno == ENOENT || errno == EACCES) {
1740 /* Framework may not have created yet, sleep and retry */
1741 ERROR("missing packages.list; retrying\n");
1742 sleep(3);
1743 continue;
1744 } else {
1745 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1746 return;
1747 }
1748 }
1749
1750 /* Watch above will tell us about any future changes, so
1751 * read the current state. */
1752 if (read_package_list(fuse) == -1) {
1753 ERROR("read_package_list failed: %s\n", strerror(errno));
1754 return;
1755 }
1756 active = true;
1757 }
1758
1759 int event_pos = 0;
1760 int res = read(nfd, event_buf, sizeof(event_buf));
1761 if (res < (int) sizeof(*event)) {
1762 if (errno == EINTR)
1763 continue;
1764 ERROR("failed to read inotify event: %s\n", strerror(errno));
1765 return;
1766 }
1767
1768 while (res >= (int) sizeof(*event)) {
1769 int event_size;
1770 event = (struct inotify_event *) (event_buf + event_pos);
1771
1772 TRACE("inotify event: %08x\n", event->mask);
1773 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1774 /* Previously watched file was deleted, probably due to move
1775 * that swapped in new data; re-arm the watch and read. */
1776 active = false;
1777 }
1778
1779 event_size = sizeof(*event) + event->len;
1780 res -= event_size;
1781 event_pos += event_size;
1782 }
1783 }
1784}
1785
Jeff Brown6249b902012-05-26 14:32:54 -07001786static int ignite_fuse(struct fuse* fuse, int num_threads)
1787{
1788 struct fuse_handler* handlers;
1789 int i;
1790
1791 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1792 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001793 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001794 return -ENOMEM;
1795 }
1796
1797 for (i = 0; i < num_threads; i++) {
1798 handlers[i].fuse = fuse;
1799 handlers[i].token = i;
1800 }
1801
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001802 /* When deriving permissions, this thread is used to process inotify events,
1803 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001804 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001805 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001806 pthread_t thread;
1807 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1808 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001809 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001810 goto quit;
1811 }
1812 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001813
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001814 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001815 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001816 } else {
1817 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001818 }
1819
1820 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001821
1822 /* don't bother killing all of the other threads or freeing anything,
1823 * should never get here anyhow */
1824quit:
1825 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001826}
1827
Mike Lockwood4f35e622011-01-12 14:39:44 -05001828static int usage()
1829{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001830 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1831 " -u: specify UID to run as\n"
1832 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001833 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001834 " -t: specify number of threads to use (default %d)\n"
1835 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001836 " -l: derive file permissions based on legacy internal layout\n"
1837 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001838 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001839 return 1;
1840}
1841
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001842static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001843 gid_t gid, gid_t write_gid, userid_t owner_user, int num_threads, derive_t derive,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001844 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001845 int fd;
1846 char opts[256];
1847 int res;
1848 struct fuse fuse;
1849
1850 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001851 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001852
1853 fd = open("/dev/fuse", O_RDWR);
1854 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001855 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001856 return -1;
1857 }
1858
1859 snprintf(opts, sizeof(opts),
1860 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1861 fd, uid, gid);
1862
Daisuke Okitsu00690852014-11-24 09:37:55 +01001863 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001864 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001865 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1866 goto error;
1867 }
1868
1869 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1870 if (res < 0) {
1871 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001872 goto error;
1873 }
1874
1875 res = setgid(gid);
1876 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001877 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001878 goto error;
1879 }
1880
1881 res = setuid(uid);
1882 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001883 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001884 goto error;
1885 }
1886
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001887 fuse_init(&fuse, fd, source_path, write_gid, owner_user, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001888
1889 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001890 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001891
1892 /* we do not attempt to umount the file system here because we are no longer
1893 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001894
1895error:
1896 close(fd);
1897 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001898}
1899
Brian Swetland03ee9472010-08-12 18:01:08 -07001900int main(int argc, char **argv)
1901{
Brian Swetland03ee9472010-08-12 18:01:08 -07001902 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001903 const char *source_path = NULL;
1904 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001905 uid_t uid = 0;
1906 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001907 gid_t write_gid = AID_SDCARD_RW;
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001908 userid_t owner_user = 0;
Jeff Brown6249b902012-05-26 14:32:54 -07001909 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001910 derive_t derive = DERIVE_NONE;
1911 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001912 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001913 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001914 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001915
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001916 int opt;
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001917 while ((opt = getopt(argc, argv, "u:g:w:o:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001918 switch (opt) {
1919 case 'u':
1920 uid = strtoul(optarg, NULL, 10);
1921 break;
1922 case 'g':
1923 gid = strtoul(optarg, NULL, 10);
1924 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001925 case 'w':
1926 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001927 break;
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001928 case 'o':
1929 owner_user = strtoul(optarg, NULL, 10);
1930 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001931 case 't':
1932 num_threads = strtoul(optarg, NULL, 10);
1933 break;
1934 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001935 derive = DERIVE_UNIFIED;
1936 break;
1937 case 'l':
1938 derive = DERIVE_LEGACY;
1939 break;
1940 case 's':
1941 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001942 break;
1943 case '?':
1944 default:
1945 return usage();
1946 }
1947 }
1948
1949 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001950 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001951 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001952 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001953 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001954 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001955 } else if (!uid) {
1956 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001957 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001958 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001959 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001960 ERROR("too many arguments\n");
1961 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001962 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001963 }
1964
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001965 if (!source_path) {
1966 ERROR("no source path specified\n");
1967 return usage();
1968 }
1969 if (!dest_path) {
1970 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001971 return usage();
1972 }
Jeff Brown26567352012-05-25 13:27:43 -07001973 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001974 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001975 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001976 }
Jeff Brown6249b902012-05-26 14:32:54 -07001977 if (num_threads < 1) {
1978 ERROR("number of threads must be at least 1\n");
1979 return usage();
1980 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001981 if (split_perms && derive == DERIVE_NONE) {
1982 ERROR("cannot split permissions without deriving\n");
1983 return usage();
1984 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001985
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001986 rlim.rlim_cur = 8192;
1987 rlim.rlim_max = 8192;
1988 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1989 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1990 }
1991
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001992 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1993 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1994 sleep(1);
1995 }
1996
Jeff Sharkeya140afe2015-03-23 16:13:53 -07001997 res = run(source_path, dest_path, uid, gid, write_gid, owner_user,
1998 num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001999 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07002000}