blob: 893c0dce02d2cb3c320cff9d5a2ae9dfd5b06fed [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070030#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070031#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070032#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070033#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070034#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040035#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080036#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070037#include <sys/uio.h>
38#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039
Jeff Sharkey44d63422013-09-12 09:44:48 -070040#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070042#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070044
Brian Swetlandb14a2c62010-08-12 18:21:12 -070045#include <private/android_filesystem_config.h>
46
Brian Swetland03ee9472010-08-12 18:01:08 -070047/* README
48 *
49 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070050 *
Brian Swetland03ee9472010-08-12 18:01:08 -070051 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070052 * directory permissions (all files are given fixed owner, group, and
53 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070054 * changeable, symlinks and hardlinks are not createable, etc.
55 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070057 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070058 * It must be run as root, but will drop to requested UID/GID as soon as it
59 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070060 *
61 * Things I believe to be true:
62 *
63 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
64 * CREAT) must bump that node's refcount
65 * - don't forget that FORGET can forget multiple references (req->nlookup)
66 * - if an op that returns a fuse_entry fails writing the reply to the
67 * kernel, you must rollback the refcount to reflect the reference the
68 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070069 *
70 * This daemon can also derive custom filesystem permissions based on directory
71 * structure when requested. These custom permissions support several features:
72 *
73 * - Apps can access their own files in /Android/data/com.example/ without
74 * requiring any additional GIDs.
75 * - Separate permissions for protecting directories like Pictures and Music.
76 * - Multi-user separation on the same physical device.
77 *
78 * The derived permissions look like this:
79 *
80 * rwxrwx--x root:sdcard_rw /
81 * rwxrwx--- root:sdcard_pics /Pictures
82 * rwxrwx--- root:sdcard_av /Music
83 *
84 * rwxrwx--x root:sdcard_rw /Android
85 * rwxrwx--x root:sdcard_rw /Android/data
86 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
87 * rwxrwx--x root:sdcard_rw /Android/obb/
88 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
89 *
90 * rwxrwx--- root:sdcard_all /Android/user
91 * rwxrwx--x root:sdcard_rw /Android/user/10
92 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070093 */
94
95#define FUSE_TRACE 0
96
97#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070098#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070099#else
100#define TRACE(x...) do {} while (0)
101#endif
102
Elliott Hughes300d5642014-07-08 13:53:26 -0700103#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700104
105#define FUSE_UNKNOWN_INO 0xffffffff
106
Jeff Brown84715842012-05-25 14:07:47 -0700107/* Maximum number of bytes to write in one request. */
108#define MAX_WRITE (256 * 1024)
109
110/* Maximum number of bytes to read in one request. */
111#define MAX_READ (128 * 1024)
112
113/* Largest possible request.
114 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
115 * the largest possible data payload. */
116#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
117
Jeff Brown6249b902012-05-26 14:32:54 -0700118/* Default number of threads. */
119#define DEFAULT_NUM_THREADS 2
120
121/* Pseudo-error constant used to indicate that no fuse status is needed
122 * or that a reply has already been written. */
123#define NO_STATUS 1
124
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125/* Path to system-provided mapping of package name to appIds */
126static const char* const kPackagesListFile = "/data/system/packages.list";
127
128/* Supplementary groups to execute with */
129static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
130
131/* Permission mode for a specific node. Controls how file permissions
132 * are derived for children nodes. */
133typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700134 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700135 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700136 /* This node is one level above a normal root; used for legacy layouts
137 * which use the first level to represent user_id. */
138 PERM_LEGACY_PRE_ROOT,
139 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700145 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700146 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700147 /* This node is "/Android/media" */
148 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700149 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700150 PERM_ANDROID_USER,
151} perm_t;
152
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700153/* Permissions structure to derive */
154typedef enum {
155 DERIVE_NONE,
156 DERIVE_LEGACY,
157 DERIVE_UNIFIED,
158} derive_t;
159
Brian Swetland03ee9472010-08-12 18:01:08 -0700160struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700161 int fd;
162};
163
164struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700165 DIR *d;
166};
167
168struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700169 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700170 __u64 nid;
171 __u64 gen;
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000172 /*
173 * The inode number for this FUSE node. Note that this isn't stable across
174 * multiple invocations of the FUSE daemon.
175 */
176 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700177
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700178 /* State derived based on current position in hierarchy. */
179 perm_t perm;
180 userid_t userid;
181 uid_t uid;
182 gid_t gid;
183 mode_t mode;
184
Paul Eastham11ccdb32010-10-14 11:04:26 -0700185 struct node *next; /* per-dir sibling list */
186 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700187 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700188
Jeff Brown6249b902012-05-26 14:32:54 -0700189 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700190 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800191 /* If non-null, this is the real name of the file in the underlying storage.
192 * This may differ from the field "name" only by case.
193 * strlen(actual_name) will always equal strlen(name), so it is safe to use
194 * namelen for both fields.
195 */
196 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700197
198 /* If non-null, an exact underlying path that should be grafted into this
199 * position. Used to support things like OBB. */
200 char* graft_path;
201 size_t graft_pathlen;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200202
203 bool deleted;
Brian Swetland03ee9472010-08-12 18:01:08 -0700204};
205
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700206static int str_hash(void *key) {
207 return hashmapHash(key, strlen(key));
208}
209
Jeff Sharkey44d63422013-09-12 09:44:48 -0700210/** Test if two string keys are equal ignoring case */
211static bool str_icase_equals(void *keyA, void *keyB) {
212 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700213}
214
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700215static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800216 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700217}
218
219static bool int_equals(void *keyA, void *keyB) {
220 return keyA == keyB;
221}
222
Jeff Brown7729d242012-05-25 15:35:28 -0700223/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700224struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700225 pthread_mutex_t lock;
226
Brian Swetland03ee9472010-08-12 18:01:08 -0700227 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700228 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700229 derive_t derive;
230 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700231 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700232 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700233 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700234
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000235 /* Used to allocate unique inode numbers for fuse nodes. We use
236 * a simple counter based scheme where inode numbers from deleted
237 * nodes aren't reused. Note that inode allocations are not stable
238 * across multiple invocation of the sdcard daemon, but that shouldn't
239 * be a huge problem in practice.
240 *
241 * Note that we restrict inodes to 32 bit unsigned integers to prevent
242 * truncation on 32 bit processes when unsigned long long stat.st_ino is
243 * assigned to an unsigned long ino_t type in an LP32 process.
244 *
245 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
246 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
247 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
248 * in fs/fuse/inode.c).
249 *
250 * Accesses must be guarded by |lock|.
251 */
252 __u32 inode_ctr;
253
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700254 Hashmap* package_to_appid;
255 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700256};
257
Jeff Brown7729d242012-05-25 15:35:28 -0700258/* Private data used by a single fuse handler. */
259struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700260 struct fuse* fuse;
261 int token;
262
Jeff Brown7729d242012-05-25 15:35:28 -0700263 /* To save memory, we never use the contents of the request buffer and the read
264 * buffer at the same time. This allows us to share the underlying storage. */
265 union {
266 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800267 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700268 };
269};
Brian Swetland03ee9472010-08-12 18:01:08 -0700270
Jeff Brown6249b902012-05-26 14:32:54 -0700271static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700272{
Jeff Brown6249b902012-05-26 14:32:54 -0700273 return (void *) (uintptr_t) nid;
274}
Brian Swetland03ee9472010-08-12 18:01:08 -0700275
Jeff Brown6249b902012-05-26 14:32:54 -0700276static inline __u64 ptr_to_id(void *ptr)
277{
278 return (__u64) (uintptr_t) ptr;
279}
Brian Swetland03ee9472010-08-12 18:01:08 -0700280
Jeff Brown6249b902012-05-26 14:32:54 -0700281static void acquire_node_locked(struct node* node)
282{
283 node->refcount++;
284 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
285}
286
287static void remove_node_from_parent_locked(struct node* node);
288
289static void release_node_locked(struct node* node)
290{
291 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
292 if (node->refcount > 0) {
293 node->refcount--;
294 if (!node->refcount) {
295 TRACE("DESTROY %p (%s)\n", node, node->name);
296 remove_node_from_parent_locked(node);
297
298 /* TODO: remove debugging - poison memory */
299 memset(node->name, 0xef, node->namelen);
300 free(node->name);
301 free(node->actual_name);
302 memset(node, 0xfc, sizeof(*node));
303 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800304 }
Jeff Brown6249b902012-05-26 14:32:54 -0700305 } else {
306 ERROR("Zero refcnt %p\n", node);
307 }
308}
309
310static void add_node_to_parent_locked(struct node *node, struct node *parent) {
311 node->parent = parent;
312 node->next = parent->child;
313 parent->child = node;
314 acquire_node_locked(parent);
315}
316
317static void remove_node_from_parent_locked(struct node* node)
318{
319 if (node->parent) {
320 if (node->parent->child == node) {
321 node->parent->child = node->parent->child->next;
322 } else {
323 struct node *node2;
324 node2 = node->parent->child;
325 while (node2->next != node)
326 node2 = node2->next;
327 node2->next = node->next;
328 }
329 release_node_locked(node->parent);
330 node->parent = NULL;
331 node->next = NULL;
332 }
333}
334
335/* Gets the absolute path to a node into the provided buffer.
336 *
337 * Populates 'buf' with the path and returns the length of the path on success,
338 * or returns -1 if the path is too long for the provided buffer.
339 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700340static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
341 const char* name;
342 size_t namelen;
343 if (node->graft_path) {
344 name = node->graft_path;
345 namelen = node->graft_pathlen;
346 } else if (node->actual_name) {
347 name = node->actual_name;
348 namelen = node->namelen;
349 } else {
350 name = node->name;
351 namelen = node->namelen;
352 }
353
Jeff Brown6249b902012-05-26 14:32:54 -0700354 if (bufsize < namelen + 1) {
355 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700356 }
357
Jeff Brown6249b902012-05-26 14:32:54 -0700358 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700359 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700360 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
361 if (pathlen < 0) {
362 return -1;
363 }
364 buf[pathlen++] = '/';
365 }
366
Jeff Brown6249b902012-05-26 14:32:54 -0700367 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
368 return pathlen + namelen;
369}
370
371/* Finds the absolute path of a file within a given directory.
372 * Performs a case-insensitive search for the file and sets the buffer to the path
373 * of the first matching file. If 'search' is zero or if no match is found, sets
374 * the buffer to the path that the file would have, assuming the name were case-sensitive.
375 *
376 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
377 * or returns NULL if the path is too long for the provided buffer.
378 */
379static char* find_file_within(const char* path, const char* name,
380 char* buf, size_t bufsize, int search)
381{
382 size_t pathlen = strlen(path);
383 size_t namelen = strlen(name);
384 size_t childlen = pathlen + namelen + 1;
385 char* actual;
386
387 if (bufsize <= childlen) {
388 return NULL;
389 }
390
391 memcpy(buf, path, pathlen);
392 buf[pathlen] = '/';
393 actual = buf + pathlen + 1;
394 memcpy(actual, name, namelen + 1);
395
396 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800397 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700398 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800399 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700400 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700401 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800402 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800403 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700404 if (!strcasecmp(entry->d_name, name)) {
405 /* we have a match - replace the name, don't need to copy the null again */
406 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800407 break;
408 }
409 }
410 closedir(dir);
411 }
Jeff Brown6249b902012-05-26 14:32:54 -0700412 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800413}
414
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700415static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800416{
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000417 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700418 attr->size = s->st_size;
419 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800420 attr->atime = s->st_atim.tv_sec;
421 attr->mtime = s->st_mtim.tv_sec;
422 attr->ctime = s->st_ctim.tv_sec;
423 attr->atimensec = s->st_atim.tv_nsec;
424 attr->mtimensec = s->st_mtim.tv_nsec;
425 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700426 attr->mode = s->st_mode;
427 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700428
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700429 attr->uid = node->uid;
430 attr->gid = node->gid;
431
432 /* Filter requested mode based on underlying file, and
433 * pass through file type. */
434 int owner_mode = s->st_mode & 0700;
435 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
436 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
437}
438
Jeff Sharkey44d63422013-09-12 09:44:48 -0700439static int touch(char* path, mode_t mode) {
440 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
441 if (fd == -1) {
442 if (errno == EEXIST) {
443 return 0;
444 } else {
445 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
446 return -1;
447 }
448 }
449 close(fd);
450 return 0;
451}
452
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700453static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
454 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700455 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700456
457 /* By default, each node inherits from its parent */
458 node->perm = PERM_INHERIT;
459 node->userid = parent->userid;
460 node->uid = parent->uid;
461 node->gid = parent->gid;
462 node->mode = parent->mode;
463
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700464 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700465 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700466 }
467
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700468 /* Derive custom permissions based on parent and current node */
469 switch (parent->perm) {
470 case PERM_INHERIT:
471 /* Already inherited above */
472 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700473 case PERM_LEGACY_PRE_ROOT:
474 /* Legacy internal layout places users at top level */
475 node->perm = PERM_ROOT;
476 node->userid = strtoul(node->name, NULL, 10);
477 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700478 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700479 /* Assume masked off by default. */
480 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700481 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700482 /* App-specific directories inside; let anyone traverse */
483 node->perm = PERM_ANDROID;
484 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700485 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700486 if (!strcasecmp(node->name, "DCIM")
487 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700488 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700489 } else if (!strcasecmp(node->name, "Alarms")
490 || !strcasecmp(node->name, "Movies")
491 || !strcasecmp(node->name, "Music")
492 || !strcasecmp(node->name, "Notifications")
493 || !strcasecmp(node->name, "Podcasts")
494 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700495 node->gid = AID_SDCARD_AV;
496 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700497 }
498 break;
499 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700500 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700501 /* App-specific directories inside; let anyone traverse */
502 node->perm = PERM_ANDROID_DATA;
503 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700504 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700505 /* App-specific directories inside; let anyone traverse */
506 node->perm = PERM_ANDROID_OBB;
507 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700508 /* Single OBB directory is always shared */
509 node->graft_path = fuse->obbpath;
510 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700511 } else if (!strcasecmp(node->name, "media")) {
512 /* App-specific directories inside; let anyone traverse */
513 node->perm = PERM_ANDROID_MEDIA;
514 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700515 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700516 /* User directories must only be accessible to system, protected
517 * by sdcard_all. Zygote will bind mount the appropriate user-
518 * specific path. */
519 node->perm = PERM_ANDROID_USER;
520 node->gid = AID_SDCARD_ALL;
521 node->mode = 0770;
522 }
523 break;
524 case PERM_ANDROID_DATA:
525 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700526 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800527 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700528 if (appid != 0) {
529 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700530 }
531 node->mode = 0770;
532 break;
533 case PERM_ANDROID_USER:
534 /* Root of a secondary user */
535 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700536 node->userid = strtoul(node->name, NULL, 10);
537 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700538 node->mode = 0771;
539 break;
540 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700541}
542
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700543/* Return if the calling UID holds sdcard_rw. */
544static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700545 /* No additional permissions enforcement */
546 if (fuse->derive == DERIVE_NONE) {
547 return true;
548 }
549
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700550 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800551 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700552}
553
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700554/* Kernel has already enforced everything we returned through
555 * derive_permissions_locked(), so this is used to lock down access
556 * even further, such as enforcing that apps hold sdcard_rw. */
557static bool check_caller_access_to_name(struct fuse* fuse,
558 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700559 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700560 /* Always block security-sensitive files at root */
561 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700562 if (!strcasecmp(name, "autorun.inf")
563 || !strcasecmp(name, ".android_secure")
564 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700565 return false;
566 }
567 }
568
569 /* No additional permissions enforcement */
570 if (fuse->derive == DERIVE_NONE) {
571 return true;
572 }
573
Jeff Sharkey44d63422013-09-12 09:44:48 -0700574 /* Root always has access; access for any other UIDs should always
575 * be controlled through packages.list. */
576 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700577 return true;
578 }
579
580 /* If asking to write, verify that caller either owns the
581 * parent or holds sdcard_rw. */
582 if (mode & W_OK) {
583 if (parent_node && hdr->uid == parent_node->uid) {
584 return true;
585 }
586
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700587 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700588 }
589
590 /* No extra permissions to enforce */
591 return true;
592}
593
594static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700595 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
596 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700597}
598
Jeff Brown6249b902012-05-26 14:32:54 -0700599struct node *create_node_locked(struct fuse* fuse,
600 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700601{
602 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700603 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700604
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000605 // Detect overflows in the inode counter. "4 billion nodes should be enough
606 // for everybody".
607 if (fuse->inode_ctr == 0) {
608 ERROR("No more inode numbers available");
609 return NULL;
610 }
611
Paul Eastham11ccdb32010-10-14 11:04:26 -0700612 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700613 if (!node) {
614 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700615 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700616 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700617 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700618 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700619 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700620 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700621 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700622 if (strcmp(name, actual_name)) {
623 node->actual_name = malloc(namelen + 1);
624 if (!node->actual_name) {
625 free(node->name);
626 free(node);
627 return NULL;
628 }
629 memcpy(node->actual_name, actual_name, namelen + 1);
630 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700631 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700632 node->nid = ptr_to_id(node);
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000633 node->ino = fuse->inode_ctr++;
Jeff Brown6249b902012-05-26 14:32:54 -0700634 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700635
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200636 node->deleted = false;
637
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700638 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700639 acquire_node_locked(node);
640 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700641 return node;
642}
643
Jeff Brown6249b902012-05-26 14:32:54 -0700644static int rename_node_locked(struct node *node, const char *name,
645 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700646{
Jeff Brown6249b902012-05-26 14:32:54 -0700647 size_t namelen = strlen(name);
648 int need_actual_name = strcmp(name, actual_name);
649
650 /* make the storage bigger without actually changing the name
651 * in case an error occurs part way */
652 if (namelen > node->namelen) {
653 char* new_name = realloc(node->name, namelen + 1);
654 if (!new_name) {
655 return -ENOMEM;
656 }
657 node->name = new_name;
658 if (need_actual_name && node->actual_name) {
659 char* new_actual_name = realloc(node->actual_name, namelen + 1);
660 if (!new_actual_name) {
661 return -ENOMEM;
662 }
663 node->actual_name = new_actual_name;
664 }
665 }
666
667 /* update the name, taking care to allocate storage before overwriting the old name */
668 if (need_actual_name) {
669 if (!node->actual_name) {
670 node->actual_name = malloc(namelen + 1);
671 if (!node->actual_name) {
672 return -ENOMEM;
673 }
674 }
675 memcpy(node->actual_name, actual_name, namelen + 1);
676 } else {
677 free(node->actual_name);
678 node->actual_name = NULL;
679 }
680 memcpy(node->name, name, namelen + 1);
681 node->namelen = namelen;
682 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700683}
684
Jeff Brown6249b902012-05-26 14:32:54 -0700685static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700686{
Jeff Brown6249b902012-05-26 14:32:54 -0700687 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700688 return &fuse->root;
689 } else {
690 return id_to_ptr(nid);
691 }
692}
693
Jeff Brown6249b902012-05-26 14:32:54 -0700694static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
695 char* buf, size_t bufsize)
696{
697 struct node* node = lookup_node_by_id_locked(fuse, nid);
698 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
699 node = NULL;
700 }
701 return node;
702}
703
704static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700705{
706 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700707 /* use exact string comparison, nodes that differ by case
708 * must be considered distinct even if they refer to the same
709 * underlying file as otherwise operations such as "mv x x"
710 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200711 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700712 return node;
713 }
714 }
715 return 0;
716}
717
Jeff Brown6249b902012-05-26 14:32:54 -0700718static struct node* acquire_or_create_child_locked(
719 struct fuse* fuse, struct node* parent,
720 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700721{
Jeff Brown6249b902012-05-26 14:32:54 -0700722 struct node* child = lookup_child_by_name_locked(parent, name);
723 if (child) {
724 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800725 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700726 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800727 }
Jeff Brown6249b902012-05-26 14:32:54 -0700728 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700729}
730
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700731static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700732 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700733 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700734
Jeff Brown6249b902012-05-26 14:32:54 -0700735 fuse->fd = fd;
736 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700737 fuse->derive = derive;
738 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700739 fuse->write_gid = write_gid;
Narayan Kamath5aadceb2015-01-13 18:21:10 +0000740 fuse->inode_ctr = 1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700741
Jeff Brown6249b902012-05-26 14:32:54 -0700742 memset(&fuse->root, 0, sizeof(fuse->root));
743 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
744 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700745 fuse->root.namelen = strlen(source_path);
746 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700747 fuse->root.userid = 0;
748 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700749
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700750 /* Set up root node for various modes of operation */
751 switch (derive) {
752 case DERIVE_NONE:
753 /* Traditional behavior that treats entire device as being accessible
754 * to sdcard_rw, and no permissions are derived. */
755 fuse->root.perm = PERM_ROOT;
756 fuse->root.mode = 0775;
757 fuse->root.gid = AID_SDCARD_RW;
758 break;
759 case DERIVE_LEGACY:
760 /* Legacy behavior used to support internal multiuser layout which
761 * places user_id at the top directory level, with the actual roots
762 * just below that. Shared OBB path is also at top level. */
763 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
764 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700765 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700766 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700767 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
768 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700769 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700770 break;
771 case DERIVE_UNIFIED:
772 /* Unified multiuser layout which places secondary user_id under
773 * /Android/user and shared OBB path under /Android/obb. */
774 fuse->root.perm = PERM_ROOT;
775 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700776 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700777 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700778 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
779 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
780 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700781 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700782}
783
Jeff Brown6249b902012-05-26 14:32:54 -0700784static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700785{
786 struct fuse_out_header hdr;
787 hdr.len = sizeof(hdr);
788 hdr.error = err;
789 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700790 write(fuse->fd, &hdr, sizeof(hdr));
791}
792
Jeff Brown6249b902012-05-26 14:32:54 -0700793static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700794{
795 struct fuse_out_header hdr;
796 struct iovec vec[2];
797 int res;
798
799 hdr.len = len + sizeof(hdr);
800 hdr.error = 0;
801 hdr.unique = unique;
802
803 vec[0].iov_base = &hdr;
804 vec[0].iov_len = sizeof(hdr);
805 vec[1].iov_base = data;
806 vec[1].iov_len = len;
807
808 res = writev(fuse->fd, vec, 2);
809 if (res < 0) {
810 ERROR("*** REPLY FAILED *** %d\n", errno);
811 }
812}
813
Jeff Brown6249b902012-05-26 14:32:54 -0700814static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
815 struct node* parent, const char* name, const char* actual_name,
816 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700817{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700819 struct fuse_entry_out out;
820 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700821
Jeff Brown6249b902012-05-26 14:32:54 -0700822 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700823 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700824 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700825
Jeff Brown6249b902012-05-26 14:32:54 -0700826 pthread_mutex_lock(&fuse->lock);
827 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
828 if (!node) {
829 pthread_mutex_unlock(&fuse->lock);
830 return -ENOMEM;
831 }
832 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700833 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700834 out.attr_valid = 10;
835 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700836 out.nodeid = node->nid;
837 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700838 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700839 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700840 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700841}
842
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700843static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700844 const char* path)
845{
846 struct fuse_attr_out out;
847 struct stat s;
848
849 if (lstat(path, &s) < 0) {
850 return -errno;
851 }
852 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700853 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700854 out.attr_valid = 10;
855 fuse_reply(fuse, unique, &out, sizeof(out));
856 return NO_STATUS;
857}
858
859static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700861{
Jeff Brown6249b902012-05-26 14:32:54 -0700862 struct node* parent_node;
863 char parent_path[PATH_MAX];
864 char child_path[PATH_MAX];
865 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700866
Jeff Brown6249b902012-05-26 14:32:54 -0700867 pthread_mutex_lock(&fuse->lock);
868 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
869 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100870 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700871 parent_node ? parent_node->name : "?");
872 pthread_mutex_unlock(&fuse->lock);
873
874 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
875 child_path, sizeof(child_path), 1))) {
876 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700877 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700878 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700879 return -EACCES;
880 }
881
Jeff Brown6249b902012-05-26 14:32:54 -0700882 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883}
884
Jeff Brown6249b902012-05-26 14:32:54 -0700885static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
887{
Jeff Brown6249b902012-05-26 14:32:54 -0700888 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700889
Jeff Brown6249b902012-05-26 14:32:54 -0700890 pthread_mutex_lock(&fuse->lock);
891 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100892 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700893 hdr->nodeid, node ? node->name : "?");
894 if (node) {
895 __u64 n = req->nlookup;
896 while (n--) {
897 release_node_locked(node);
898 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700899 }
Jeff Brown6249b902012-05-26 14:32:54 -0700900 pthread_mutex_unlock(&fuse->lock);
901 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700902}
903
Jeff Brown6249b902012-05-26 14:32:54 -0700904static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
906{
Jeff Brown6249b902012-05-26 14:32:54 -0700907 struct node* node;
908 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700909
Jeff Brown6249b902012-05-26 14:32:54 -0700910 pthread_mutex_lock(&fuse->lock);
911 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100912 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700913 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
914 pthread_mutex_unlock(&fuse->lock);
915
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700916 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700917 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700918 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700919 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700920 return -EACCES;
921 }
922
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700923 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700924}
925
Jeff Brown6249b902012-05-26 14:32:54 -0700926static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700927 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
928{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700929 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700930 struct node* node;
931 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700932 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700933
Jeff Brown6249b902012-05-26 14:32:54 -0700934 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700935 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700936 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100937 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700938 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
939 pthread_mutex_unlock(&fuse->lock);
940
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700942 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700943 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800944
945 if (!(req->valid & FATTR_FH) &&
946 !check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700947 return -EACCES;
948 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700949
Jeff Brown6249b902012-05-26 14:32:54 -0700950 /* XXX: incomplete implementation on purpose.
951 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952
Elliott Hughes853574d2014-07-31 12:03:03 -0700953 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700954 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700955 }
956
957 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
958 * are both set, then set it to the current time. Else, set it to the
959 * time specified in the request. Same goes for mtime. Use utimensat(2)
960 * as it allows ATIME and MTIME to be changed independently, and has
961 * nanosecond resolution which fuse also has.
962 */
963 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
964 times[0].tv_nsec = UTIME_OMIT;
965 times[1].tv_nsec = UTIME_OMIT;
966 if (req->valid & FATTR_ATIME) {
967 if (req->valid & FATTR_ATIME_NOW) {
968 times[0].tv_nsec = UTIME_NOW;
969 } else {
970 times[0].tv_sec = req->atime;
971 times[0].tv_nsec = req->atimensec;
972 }
973 }
974 if (req->valid & FATTR_MTIME) {
975 if (req->valid & FATTR_MTIME_NOW) {
976 times[1].tv_nsec = UTIME_NOW;
977 } else {
978 times[1].tv_sec = req->mtime;
979 times[1].tv_nsec = req->mtimensec;
980 }
981 }
Jeff Brown6249b902012-05-26 14:32:54 -0700982 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
983 handler->token, path, times[0].tv_sec, times[1].tv_sec);
984 if (utimensat(-1, path, times, 0) < 0) {
985 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700986 }
987 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700988 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700989}
990
Jeff Brown6249b902012-05-26 14:32:54 -0700991static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700992 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
993{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700994 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700995 struct node* parent_node;
996 char parent_path[PATH_MAX];
997 char child_path[PATH_MAX];
998 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700999
Jeff Brown6249b902012-05-26 14:32:54 -07001000 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001001 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001002 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1003 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001004 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001005 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
1006 pthread_mutex_unlock(&fuse->lock);
1007
1008 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1009 child_path, sizeof(child_path), 1))) {
1010 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001011 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001012 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001013 return -EACCES;
1014 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001015 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -07001016 if (mknod(child_path, mode, req->rdev) < 0) {
1017 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 }
Jeff Brown6249b902012-05-26 14:32:54 -07001019 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001020}
1021
Jeff Brown6249b902012-05-26 14:32:54 -07001022static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
1024{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001025 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001026 struct node* parent_node;
1027 char parent_path[PATH_MAX];
1028 char child_path[PATH_MAX];
1029 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001030
Jeff Brown6249b902012-05-26 14:32:54 -07001031 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001032 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001033 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1034 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001035 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001036 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
1037 pthread_mutex_unlock(&fuse->lock);
1038
1039 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1040 child_path, sizeof(child_path), 1))) {
1041 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001043 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001044 return -EACCES;
1045 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001046 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001047 if (mkdir(child_path, mode) < 0) {
1048 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001049 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001050
1051 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1052 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1053 char nomedia[PATH_MAX];
1054 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1055 if (touch(nomedia, 0664) != 0) {
1056 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1057 return -ENOENT;
1058 }
1059 }
1060 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1061 char nomedia[PATH_MAX];
1062 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1063 if (touch(nomedia, 0664) != 0) {
1064 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1065 return -ENOENT;
1066 }
1067 }
1068
Jeff Brown6249b902012-05-26 14:32:54 -07001069 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001070}
1071
Jeff Brown6249b902012-05-26 14:32:54 -07001072static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073 const struct fuse_in_header* hdr, const char* name)
1074{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001075 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001076 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001077 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001078 char parent_path[PATH_MAX];
1079 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001080
Jeff Brown6249b902012-05-26 14:32:54 -07001081 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001082 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001083 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1084 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001085 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001086 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1087 pthread_mutex_unlock(&fuse->lock);
1088
1089 if (!parent_node || !find_file_within(parent_path, name,
1090 child_path, sizeof(child_path), 1)) {
1091 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001093 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001094 return -EACCES;
1095 }
Jeff Brown6249b902012-05-26 14:32:54 -07001096 if (unlink(child_path) < 0) {
1097 return -errno;
1098 }
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001099 pthread_mutex_lock(&fuse->lock);
1100 child_node = lookup_child_by_name_locked(parent_node, name);
1101 if (child_node) {
1102 child_node->deleted = true;
1103 }
1104 pthread_mutex_unlock(&fuse->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001105 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001106}
1107
Jeff Brown6249b902012-05-26 14:32:54 -07001108static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001109 const struct fuse_in_header* hdr, const char* name)
1110{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001111 bool has_rw;
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001112 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001113 struct node* parent_node;
1114 char parent_path[PATH_MAX];
1115 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001116
Jeff Brown6249b902012-05-26 14:32:54 -07001117 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001118 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001119 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1120 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001121 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001122 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1123 pthread_mutex_unlock(&fuse->lock);
1124
1125 if (!parent_node || !find_file_within(parent_path, name,
1126 child_path, sizeof(child_path), 1)) {
1127 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001128 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001129 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001130 return -EACCES;
1131 }
Jeff Brown6249b902012-05-26 14:32:54 -07001132 if (rmdir(child_path) < 0) {
1133 return -errno;
1134 }
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001135 pthread_mutex_lock(&fuse->lock);
1136 child_node = lookup_child_by_name_locked(parent_node, name);
1137 if (child_node) {
1138 child_node->deleted = true;
1139 }
1140 pthread_mutex_unlock(&fuse->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001141 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142}
1143
Jeff Brown6249b902012-05-26 14:32:54 -07001144static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001145 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001146 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001147{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001148 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001149 struct node* old_parent_node;
1150 struct node* new_parent_node;
1151 struct node* child_node;
1152 char old_parent_path[PATH_MAX];
1153 char new_parent_path[PATH_MAX];
1154 char old_child_path[PATH_MAX];
1155 char new_child_path[PATH_MAX];
1156 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001157 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158
Jeff Brown6249b902012-05-26 14:32:54 -07001159 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001160 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001161 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1162 old_parent_path, sizeof(old_parent_path));
1163 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1164 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001165 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001166 old_name, new_name,
1167 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1168 req->newdir, new_parent_node ? new_parent_node->name : "?");
1169 if (!old_parent_node || !new_parent_node) {
1170 res = -ENOENT;
1171 goto lookup_error;
1172 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001173 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001174 res = -EACCES;
1175 goto lookup_error;
1176 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001177 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001178 res = -EACCES;
1179 goto lookup_error;
1180 }
Jeff Brown6249b902012-05-26 14:32:54 -07001181 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1182 if (!child_node || get_node_path_locked(child_node,
1183 old_child_path, sizeof(old_child_path)) < 0) {
1184 res = -ENOENT;
1185 goto lookup_error;
1186 }
1187 acquire_node_locked(child_node);
1188 pthread_mutex_unlock(&fuse->lock);
1189
1190 /* Special case for renaming a file where destination is same path
1191 * differing only by case. In this case we don't want to look for a case
1192 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1193 */
1194 int search = old_parent_node != new_parent_node
1195 || strcasecmp(old_name, new_name);
1196 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1197 new_child_path, sizeof(new_child_path), search))) {
1198 res = -ENOENT;
1199 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001200 }
1201
Jeff Brown6249b902012-05-26 14:32:54 -07001202 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1203 res = rename(old_child_path, new_child_path);
1204 if (res < 0) {
1205 res = -errno;
1206 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 }
1208
Jeff Brown6249b902012-05-26 14:32:54 -07001209 pthread_mutex_lock(&fuse->lock);
1210 res = rename_node_locked(child_node, new_name, new_actual_name);
1211 if (!res) {
1212 remove_node_from_parent_locked(child_node);
1213 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214 }
Jeff Brown6249b902012-05-26 14:32:54 -07001215 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216
Jeff Brown6249b902012-05-26 14:32:54 -07001217io_error:
1218 pthread_mutex_lock(&fuse->lock);
1219done:
1220 release_node_locked(child_node);
1221lookup_error:
1222 pthread_mutex_unlock(&fuse->lock);
1223 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224}
1225
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001226static int open_flags_to_access_mode(int open_flags) {
1227 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1228 return R_OK;
1229 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1230 return W_OK;
1231 } else {
1232 /* Probably O_RDRW, but treat as default to be safe */
1233 return R_OK | W_OK;
1234 }
1235}
1236
Jeff Brown6249b902012-05-26 14:32:54 -07001237static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001238 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1239{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001240 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001241 struct node* node;
1242 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001243 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001245
Jeff Brown6249b902012-05-26 14:32:54 -07001246 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001247 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001248 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001249 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001250 req->flags, hdr->nodeid, node ? node->name : "?");
1251 pthread_mutex_unlock(&fuse->lock);
1252
1253 if (!node) {
1254 return -ENOENT;
1255 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001256 if (!check_caller_access_to_node(fuse, hdr, node,
1257 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001258 return -EACCES;
1259 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001260 h = malloc(sizeof(*h));
1261 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001262 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001263 }
Jeff Brown6249b902012-05-26 14:32:54 -07001264 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001265 h->fd = open(path, req->flags);
1266 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001267 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001268 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 }
1270 out.fh = ptr_to_id(h);
1271 out.open_flags = 0;
1272 out.padding = 0;
1273 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001274 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275}
1276
Jeff Brown6249b902012-05-26 14:32:54 -07001277static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001278 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1279{
1280 struct handle *h = id_to_ptr(req->fh);
1281 __u64 unique = hdr->unique;
1282 __u32 size = req->size;
1283 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001284 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001285 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001286
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001287 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1288 * overlaps the request buffer and will clobber data in the request. This
1289 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001290
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001291 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1292 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001293 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001294 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001295 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001296 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001298 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001299 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001300 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001301 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001302}
1303
Jeff Brown6249b902012-05-26 14:32:54 -07001304static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001305 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1306 const void* buffer)
1307{
1308 struct fuse_write_out out;
1309 struct handle *h = id_to_ptr(req->fh);
1310 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001311 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001312
Arpad Horvath49e93442014-02-18 10:18:25 +01001313 if (req->flags & O_DIRECT) {
1314 memcpy(aligned_buffer, buffer, req->size);
1315 buffer = (const __u8*) aligned_buffer;
1316 }
Jeff Brown6249b902012-05-26 14:32:54 -07001317
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001318 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001319 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001320 res = pwrite64(h->fd, buffer, req->size, req->offset);
1321 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001322 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001323 }
1324 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001325 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001327 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001328}
1329
Jeff Brown6249b902012-05-26 14:32:54 -07001330static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001331 const struct fuse_in_header* hdr)
1332{
Jeff Brown6249b902012-05-26 14:32:54 -07001333 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001334 struct statfs stat;
1335 struct fuse_statfs_out out;
1336 int res;
1337
Jeff Brown6249b902012-05-26 14:32:54 -07001338 pthread_mutex_lock(&fuse->lock);
1339 TRACE("[%d] STATFS\n", handler->token);
1340 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1341 pthread_mutex_unlock(&fuse->lock);
1342 if (res < 0) {
1343 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344 }
Jeff Brown6249b902012-05-26 14:32:54 -07001345 if (statfs(fuse->root.name, &stat) < 0) {
1346 return -errno;
1347 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348 memset(&out, 0, sizeof(out));
1349 out.st.blocks = stat.f_blocks;
1350 out.st.bfree = stat.f_bfree;
1351 out.st.bavail = stat.f_bavail;
1352 out.st.files = stat.f_files;
1353 out.st.ffree = stat.f_ffree;
1354 out.st.bsize = stat.f_bsize;
1355 out.st.namelen = stat.f_namelen;
1356 out.st.frsize = stat.f_frsize;
1357 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001358 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359}
1360
Jeff Brown6249b902012-05-26 14:32:54 -07001361static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1363{
1364 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001365
1366 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 close(h->fd);
1368 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001369 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001370}
1371
Jeff Brown6249b902012-05-26 14:32:54 -07001372static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001373 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1374{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001375 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1376 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001377
Elliott Hughesf6d67372014-07-08 14:38:26 -07001378 int fd = -1;
1379 if (is_dir) {
1380 struct dirhandle *dh = id_to_ptr(req->fh);
1381 fd = dirfd(dh->d);
1382 } else {
1383 struct handle *h = id_to_ptr(req->fh);
1384 fd = h->fd;
1385 }
1386
1387 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1388 is_dir ? "FSYNCDIR" : "FSYNC",
1389 id_to_ptr(req->fh), fd, is_data_sync);
1390 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1391 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001392 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001393 }
Jeff Brown6249b902012-05-26 14:32:54 -07001394 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001395}
1396
Jeff Brown6249b902012-05-26 14:32:54 -07001397static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001398 const struct fuse_in_header* hdr)
1399{
Jeff Brown6249b902012-05-26 14:32:54 -07001400 TRACE("[%d] FLUSH\n", handler->token);
1401 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001402}
1403
Jeff Brown6249b902012-05-26 14:32:54 -07001404static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001405 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1406{
Jeff Brown6249b902012-05-26 14:32:54 -07001407 struct node* node;
1408 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001409 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001410 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001411
Jeff Brown6249b902012-05-26 14:32:54 -07001412 pthread_mutex_lock(&fuse->lock);
1413 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001414 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001415 hdr->nodeid, node ? node->name : "?");
1416 pthread_mutex_unlock(&fuse->lock);
1417
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001418 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001419 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001421 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001422 return -EACCES;
1423 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001424 h = malloc(sizeof(*h));
1425 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001426 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001427 }
Jeff Brown6249b902012-05-26 14:32:54 -07001428 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001429 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001430 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001431 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001432 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001433 }
1434 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001435 out.open_flags = 0;
1436 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001437 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001438 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001439}
1440
Jeff Brown6249b902012-05-26 14:32:54 -07001441static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001442 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1443{
1444 char buffer[8192];
1445 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1446 struct dirent *de;
1447 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001448
1449 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001450 if (req->offset == 0) {
1451 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001452 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001453 rewinddir(h->d);
1454 }
1455 de = readdir(h->d);
1456 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001457 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001458 }
1459 fde->ino = FUSE_UNKNOWN_INO;
1460 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1461 fde->off = req->offset + 1;
1462 fde->type = de->d_type;
1463 fde->namelen = strlen(de->d_name);
1464 memcpy(fde->name, de->d_name, fde->namelen + 1);
1465 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001466 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1467 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001468}
1469
Jeff Brown6249b902012-05-26 14:32:54 -07001470static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001471 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1472{
1473 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001474
1475 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001476 closedir(h->d);
1477 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001478 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001479}
1480
Jeff Brown6249b902012-05-26 14:32:54 -07001481static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001482 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1483{
1484 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001485 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001486
Jeff Brown6249b902012-05-26 14:32:54 -07001487 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1488 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001489
1490 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1491 * defined (fuse version 7.6). The structure is the same from 7.6 through
1492 * 7.22. Beginning with 7.23, the structure increased in size and added
1493 * new parameters.
1494 */
1495 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1496 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1497 req->major, req->minor, FUSE_KERNEL_VERSION);
1498 return -1;
1499 }
1500
1501 out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
1502 fuse_struct_size = sizeof(out);
1503#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1504 /* FUSE_KERNEL_VERSION >= 23. */
1505
1506 /* If the kernel only works on minor revs older than or equal to 22,
1507 * then use the older structure size since this code only uses the 7.22
1508 * version of the structure. */
1509 if (req->minor <= 22) {
1510 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1511 }
1512#endif
1513
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001514 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001515 out.max_readahead = req->max_readahead;
1516 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1517 out.max_background = 32;
1518 out.congestion_threshold = 32;
1519 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001520 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001521 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001522}
1523
Jeff Brown6249b902012-05-26 14:32:54 -07001524static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001525 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1526{
Brian Swetland03ee9472010-08-12 18:01:08 -07001527 switch (hdr->opcode) {
1528 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001529 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001530 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001531 }
Jeff Brown84715842012-05-25 14:07:47 -07001532
Brian Swetland03ee9472010-08-12 18:01:08 -07001533 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001534 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001535 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001536 }
Jeff Brown84715842012-05-25 14:07:47 -07001537
Brian Swetland03ee9472010-08-12 18:01:08 -07001538 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001539 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001540 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001541 }
Jeff Brown84715842012-05-25 14:07:47 -07001542
Brian Swetland03ee9472010-08-12 18:01:08 -07001543 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001544 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001545 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001546 }
Jeff Brown84715842012-05-25 14:07:47 -07001547
Brian Swetland03ee9472010-08-12 18:01:08 -07001548// case FUSE_READLINK:
1549// case FUSE_SYMLINK:
1550 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001551 const struct fuse_mknod_in *req = data;
1552 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001553 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001554 }
Jeff Brown84715842012-05-25 14:07:47 -07001555
Brian Swetland03ee9472010-08-12 18:01:08 -07001556 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001557 const struct fuse_mkdir_in *req = data;
1558 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001559 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001560 }
Jeff Brown84715842012-05-25 14:07:47 -07001561
Brian Swetland03ee9472010-08-12 18:01:08 -07001562 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001563 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001564 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001565 }
Jeff Brown84715842012-05-25 14:07:47 -07001566
Brian Swetland03ee9472010-08-12 18:01:08 -07001567 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001568 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001569 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 }
Jeff Brown84715842012-05-25 14:07:47 -07001571
Brian Swetland03ee9472010-08-12 18:01:08 -07001572 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001573 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001574 const char *old_name = ((const char*) data) + sizeof(*req);
1575 const char *new_name = old_name + strlen(old_name) + 1;
1576 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001577 }
Jeff Brown84715842012-05-25 14:07:47 -07001578
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001579// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001580 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001581 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001582 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001583 }
Jeff Brown84715842012-05-25 14:07:47 -07001584
Brian Swetland03ee9472010-08-12 18:01:08 -07001585 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001586 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001587 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001588 }
Jeff Brown84715842012-05-25 14:07:47 -07001589
Brian Swetland03ee9472010-08-12 18:01:08 -07001590 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001591 const struct fuse_write_in *req = data;
1592 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001593 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001594 }
Jeff Brown84715842012-05-25 14:07:47 -07001595
Mike Lockwood4553b082010-08-16 14:14:44 -04001596 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001597 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001598 }
Jeff Brown84715842012-05-25 14:07:47 -07001599
Brian Swetland03ee9472010-08-12 18:01:08 -07001600 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001601 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001602 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001603 }
Jeff Brown84715842012-05-25 14:07:47 -07001604
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001605 case FUSE_FSYNC:
1606 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001607 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001608 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001609 }
1610
Brian Swetland03ee9472010-08-12 18:01:08 -07001611// case FUSE_SETXATTR:
1612// case FUSE_GETXATTR:
1613// case FUSE_LISTXATTR:
1614// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001615 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001616 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001617 }
1618
Brian Swetland03ee9472010-08-12 18:01:08 -07001619 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001620 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001621 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001622 }
Jeff Brown84715842012-05-25 14:07:47 -07001623
Brian Swetland03ee9472010-08-12 18:01:08 -07001624 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001625 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001626 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001627 }
Jeff Brown84715842012-05-25 14:07:47 -07001628
Brian Swetland03ee9472010-08-12 18:01:08 -07001629 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001630 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001631 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001632 }
Jeff Brown84715842012-05-25 14:07:47 -07001633
Brian Swetland03ee9472010-08-12 18:01:08 -07001634 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001635 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001636 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001637 }
Jeff Brown84715842012-05-25 14:07:47 -07001638
Brian Swetland03ee9472010-08-12 18:01:08 -07001639 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001640 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001641 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1642 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001643 }
Jeff Brown84715842012-05-25 14:07:47 -07001644 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001645}
1646
Jeff Brown6249b902012-05-26 14:32:54 -07001647static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001648{
Jeff Brown6249b902012-05-26 14:32:54 -07001649 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001650 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001651 ssize_t len = read(fuse->fd,
1652 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001653 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001654 if (errno != EINTR) {
1655 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1656 }
1657 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001658 }
Jeff Brown84715842012-05-25 14:07:47 -07001659
1660 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001661 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1662 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001663 }
1664
Jeff Brown7729d242012-05-25 15:35:28 -07001665 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001666 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001667 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1668 handler->token, (size_t)len, hdr->len);
1669 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001670 }
1671
Jeff Brown7729d242012-05-25 15:35:28 -07001672 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001673 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001674 __u64 unique = hdr->unique;
1675 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001676
1677 /* We do not access the request again after this point because the underlying
1678 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001679
1680 if (res != NO_STATUS) {
1681 if (res) {
1682 TRACE("[%d] ERROR %d\n", handler->token, res);
1683 }
1684 fuse_status(fuse, unique, res);
1685 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001686 }
1687}
1688
Jeff Brown6249b902012-05-26 14:32:54 -07001689static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001690{
Jeff Brown6249b902012-05-26 14:32:54 -07001691 struct fuse_handler* handler = data;
1692 handle_fuse_requests(handler);
1693 return NULL;
1694}
1695
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001696static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001697 Hashmap* map = context;
1698 hashmapRemove(map, key);
1699 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001700 return true;
1701}
1702
1703static bool remove_int_to_null(void *key, void *value, void *context) {
1704 Hashmap* map = context;
1705 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001706 return true;
1707}
1708
1709static int read_package_list(struct fuse *fuse) {
1710 pthread_mutex_lock(&fuse->lock);
1711
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001712 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1713 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001714
1715 FILE* file = fopen(kPackagesListFile, "r");
1716 if (!file) {
1717 ERROR("failed to open package list: %s\n", strerror(errno));
1718 pthread_mutex_unlock(&fuse->lock);
1719 return -1;
1720 }
1721
1722 char buf[512];
1723 while (fgets(buf, sizeof(buf), file) != NULL) {
1724 char package_name[512];
1725 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001726 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001727
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001728 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1729 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001730 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001731
1732 char* token = strtok(gids, ",");
1733 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001734 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001735 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001736 break;
1737 }
1738 token = strtok(NULL, ",");
1739 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001740 }
1741 }
1742
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001743 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001744 hashmapSize(fuse->package_to_appid),
1745 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001746 fclose(file);
1747 pthread_mutex_unlock(&fuse->lock);
1748 return 0;
1749}
1750
1751static void watch_package_list(struct fuse* fuse) {
1752 struct inotify_event *event;
1753 char event_buf[512];
1754
1755 int nfd = inotify_init();
1756 if (nfd < 0) {
1757 ERROR("inotify_init failed: %s\n", strerror(errno));
1758 return;
1759 }
1760
1761 bool active = false;
1762 while (1) {
1763 if (!active) {
1764 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1765 if (res == -1) {
1766 if (errno == ENOENT || errno == EACCES) {
1767 /* Framework may not have created yet, sleep and retry */
1768 ERROR("missing packages.list; retrying\n");
1769 sleep(3);
1770 continue;
1771 } else {
1772 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1773 return;
1774 }
1775 }
1776
1777 /* Watch above will tell us about any future changes, so
1778 * read the current state. */
1779 if (read_package_list(fuse) == -1) {
1780 ERROR("read_package_list failed: %s\n", strerror(errno));
1781 return;
1782 }
1783 active = true;
1784 }
1785
1786 int event_pos = 0;
1787 int res = read(nfd, event_buf, sizeof(event_buf));
1788 if (res < (int) sizeof(*event)) {
1789 if (errno == EINTR)
1790 continue;
1791 ERROR("failed to read inotify event: %s\n", strerror(errno));
1792 return;
1793 }
1794
1795 while (res >= (int) sizeof(*event)) {
1796 int event_size;
1797 event = (struct inotify_event *) (event_buf + event_pos);
1798
1799 TRACE("inotify event: %08x\n", event->mask);
1800 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1801 /* Previously watched file was deleted, probably due to move
1802 * that swapped in new data; re-arm the watch and read. */
1803 active = false;
1804 }
1805
1806 event_size = sizeof(*event) + event->len;
1807 res -= event_size;
1808 event_pos += event_size;
1809 }
1810 }
1811}
1812
Jeff Brown6249b902012-05-26 14:32:54 -07001813static int ignite_fuse(struct fuse* fuse, int num_threads)
1814{
1815 struct fuse_handler* handlers;
1816 int i;
1817
1818 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1819 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001820 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001821 return -ENOMEM;
1822 }
1823
1824 for (i = 0; i < num_threads; i++) {
1825 handlers[i].fuse = fuse;
1826 handlers[i].token = i;
1827 }
1828
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001829 /* When deriving permissions, this thread is used to process inotify events,
1830 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001831 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001832 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001833 pthread_t thread;
1834 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1835 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001836 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001837 goto quit;
1838 }
1839 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001840
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001841 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001842 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001843 } else {
1844 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001845 }
1846
1847 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001848
1849 /* don't bother killing all of the other threads or freeing anything,
1850 * should never get here anyhow */
1851quit:
1852 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001853}
1854
Mike Lockwood4f35e622011-01-12 14:39:44 -05001855static int usage()
1856{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001857 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1858 " -u: specify UID to run as\n"
1859 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001860 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001861 " -t: specify number of threads to use (default %d)\n"
1862 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001863 " -l: derive file permissions based on legacy internal layout\n"
1864 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001865 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001866 return 1;
1867}
1868
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001869static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001870 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1871 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001872 int fd;
1873 char opts[256];
1874 int res;
1875 struct fuse fuse;
1876
1877 /* cleanup from previous instance, if necessary */
William Roberts4555b692015-04-23 18:10:51 -07001878 umount2(dest_path, MNT_DETACH);
Jeff Brown26567352012-05-25 13:27:43 -07001879
1880 fd = open("/dev/fuse", O_RDWR);
1881 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001882 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001883 return -1;
1884 }
1885
1886 snprintf(opts, sizeof(opts),
1887 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1888 fd, uid, gid);
1889
Johan Redestig55cc5e52015-01-24 19:00:39 +01001890 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1891 MS_NOATIME, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001892 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001893 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1894 goto error;
1895 }
1896
1897 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1898 if (res < 0) {
1899 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001900 goto error;
1901 }
1902
1903 res = setgid(gid);
1904 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001905 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001906 goto error;
1907 }
1908
1909 res = setuid(uid);
1910 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001911 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001912 goto error;
1913 }
1914
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001915 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001916
1917 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001918 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001919
1920 /* we do not attempt to umount the file system here because we are no longer
1921 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001922
1923error:
1924 close(fd);
1925 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001926}
1927
Brian Swetland03ee9472010-08-12 18:01:08 -07001928int main(int argc, char **argv)
1929{
Brian Swetland03ee9472010-08-12 18:01:08 -07001930 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001931 const char *source_path = NULL;
1932 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001933 uid_t uid = 0;
1934 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001935 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001936 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001937 derive_t derive = DERIVE_NONE;
1938 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001939 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001940 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001941 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001942
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001943 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001944 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001945 switch (opt) {
1946 case 'u':
1947 uid = strtoul(optarg, NULL, 10);
1948 break;
1949 case 'g':
1950 gid = strtoul(optarg, NULL, 10);
1951 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001952 case 'w':
1953 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001954 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001955 case 't':
1956 num_threads = strtoul(optarg, NULL, 10);
1957 break;
1958 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001959 derive = DERIVE_UNIFIED;
1960 break;
1961 case 'l':
1962 derive = DERIVE_LEGACY;
1963 break;
1964 case 's':
1965 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001966 break;
1967 case '?':
1968 default:
1969 return usage();
1970 }
1971 }
1972
1973 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001974 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001975 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001976 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001977 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001978 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001979 } else if (!uid) {
1980 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001981 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001982 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001983 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001984 ERROR("too many arguments\n");
1985 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001986 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001987 }
1988
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001989 if (!source_path) {
1990 ERROR("no source path specified\n");
1991 return usage();
1992 }
1993 if (!dest_path) {
1994 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001995 return usage();
1996 }
Jeff Brown26567352012-05-25 13:27:43 -07001997 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001998 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001999 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07002000 }
Jeff Brown6249b902012-05-26 14:32:54 -07002001 if (num_threads < 1) {
2002 ERROR("number of threads must be at least 1\n");
2003 return usage();
2004 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07002005 if (split_perms && derive == DERIVE_NONE) {
2006 ERROR("cannot split permissions without deriving\n");
2007 return usage();
2008 }
Brian Swetland03ee9472010-08-12 18:01:08 -07002009
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08002010 rlim.rlim_cur = 8192;
2011 rlim.rlim_max = 8192;
2012 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
2013 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
2014 }
2015
Nick Kralevich8d28fa72014-07-24 17:05:59 -07002016 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
2017 ERROR("installd fs upgrade not yet complete. Waiting...\n");
2018 sleep(1);
2019 }
2020
Jeff Sharkeye93a0512013-10-08 10:14:24 -07002021 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07002022 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07002023}