blob: 9cfb040856f785ade6d7212621c060e784572541 [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>
Elliott Hughes60281d52014-05-07 14:39:58 -070032#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070033#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040034#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080035#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070036#include <sys/uio.h>
37#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070038
Jeff Sharkey44d63422013-09-12 09:44:48 -070039#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070040#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070041#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070042#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070043
Brian Swetlandb14a2c62010-08-12 18:21:12 -070044#include <private/android_filesystem_config.h>
45
Brian Swetland03ee9472010-08-12 18:01:08 -070046/* README
47 *
48 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070049 *
Brian Swetland03ee9472010-08-12 18:01:08 -070050 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070051 * directory permissions (all files are given fixed owner, group, and
52 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070053 * changeable, symlinks and hardlinks are not createable, etc.
54 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070055 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070056 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070057 * It must be run as root, but will drop to requested UID/GID as soon as it
58 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070059 *
60 * Things I believe to be true:
61 *
62 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
63 * CREAT) must bump that node's refcount
64 * - don't forget that FORGET can forget multiple references (req->nlookup)
65 * - if an op that returns a fuse_entry fails writing the reply to the
66 * kernel, you must rollback the refcount to reflect the reference the
67 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070068 *
69 * This daemon can also derive custom filesystem permissions based on directory
70 * structure when requested. These custom permissions support several features:
71 *
72 * - Apps can access their own files in /Android/data/com.example/ without
73 * requiring any additional GIDs.
74 * - Separate permissions for protecting directories like Pictures and Music.
75 * - Multi-user separation on the same physical device.
76 *
77 * The derived permissions look like this:
78 *
79 * rwxrwx--x root:sdcard_rw /
80 * rwxrwx--- root:sdcard_pics /Pictures
81 * rwxrwx--- root:sdcard_av /Music
82 *
83 * rwxrwx--x root:sdcard_rw /Android
84 * rwxrwx--x root:sdcard_rw /Android/data
85 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
86 * rwxrwx--x root:sdcard_rw /Android/obb/
87 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
88 *
89 * rwxrwx--- root:sdcard_all /Android/user
90 * rwxrwx--x root:sdcard_rw /Android/user/10
91 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070092 */
93
94#define FUSE_TRACE 0
95
96#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070097#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070098#else
99#define TRACE(x...) do {} while (0)
100#endif
101
Elliott Hughes300d5642014-07-08 13:53:26 -0700102#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700103
104#define FUSE_UNKNOWN_INO 0xffffffff
105
Jeff Brown84715842012-05-25 14:07:47 -0700106/* Maximum number of bytes to write in one request. */
107#define MAX_WRITE (256 * 1024)
108
109/* Maximum number of bytes to read in one request. */
110#define MAX_READ (128 * 1024)
111
112/* Largest possible request.
113 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
114 * the largest possible data payload. */
115#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
116
Jeff Brown6249b902012-05-26 14:32:54 -0700117/* Default number of threads. */
118#define DEFAULT_NUM_THREADS 2
119
120/* Pseudo-error constant used to indicate that no fuse status is needed
121 * or that a reply has already been written. */
122#define NO_STATUS 1
123
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700124/* Path to system-provided mapping of package name to appIds */
125static const char* const kPackagesListFile = "/data/system/packages.list";
126
127/* Supplementary groups to execute with */
128static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
129
130/* Permission mode for a specific node. Controls how file permissions
131 * are derived for children nodes. */
132typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700133 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700134 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700135 /* This node is one level above a normal root; used for legacy layouts
136 * which use the first level to represent user_id. */
137 PERM_LEGACY_PRE_ROOT,
138 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700139 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700140 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700141 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700142 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700143 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700144 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700145 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700146 /* This node is "/Android/media" */
147 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700148 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700149 PERM_ANDROID_USER,
150} perm_t;
151
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700152/* Permissions structure to derive */
153typedef enum {
154 DERIVE_NONE,
155 DERIVE_LEGACY,
156 DERIVE_UNIFIED,
157} derive_t;
158
Brian Swetland03ee9472010-08-12 18:01:08 -0700159struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700160 int fd;
161};
162
163struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700164 DIR *d;
165};
166
167struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700168 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700169 __u64 nid;
170 __u64 gen;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000171 /*
172 * The inode number for this FUSE node. Note that this isn't stable across
173 * multiple invocations of the FUSE daemon.
174 */
175 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700176
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700177 /* State derived based on current position in hierarchy. */
178 perm_t perm;
179 userid_t userid;
180 uid_t uid;
181 gid_t gid;
182 mode_t mode;
183
Paul Eastham11ccdb32010-10-14 11:04:26 -0700184 struct node *next; /* per-dir sibling list */
185 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700186 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700187
Jeff Brown6249b902012-05-26 14:32:54 -0700188 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700189 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800190 /* If non-null, this is the real name of the file in the underlying storage.
191 * This may differ from the field "name" only by case.
192 * strlen(actual_name) will always equal strlen(name), so it is safe to use
193 * namelen for both fields.
194 */
195 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700196
197 /* If non-null, an exact underlying path that should be grafted into this
198 * position. Used to support things like OBB. */
199 char* graft_path;
200 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700201};
202
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700203static int str_hash(void *key) {
204 return hashmapHash(key, strlen(key));
205}
206
Jeff Sharkey44d63422013-09-12 09:44:48 -0700207/** Test if two string keys are equal ignoring case */
208static bool str_icase_equals(void *keyA, void *keyB) {
209 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700210}
211
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700212static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800213 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700214}
215
216static bool int_equals(void *keyA, void *keyB) {
217 return keyA == keyB;
218}
219
Jeff Brown7729d242012-05-25 15:35:28 -0700220/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700221struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700222 pthread_mutex_t lock;
223
Brian Swetland03ee9472010-08-12 18:01:08 -0700224 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700225 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700226 derive_t derive;
227 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700228 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700229 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700230 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700231
Narayan Kamathfaa09352015-01-13 18:21:10 +0000232 /* Used to allocate unique inode numbers for fuse nodes. We use
233 * a simple counter based scheme where inode numbers from deleted
234 * nodes aren't reused. Note that inode allocations are not stable
235 * across multiple invocation of the sdcard daemon, but that shouldn't
236 * be a huge problem in practice.
237 *
238 * Note that we restrict inodes to 32 bit unsigned integers to prevent
239 * truncation on 32 bit processes when unsigned long long stat.st_ino is
240 * assigned to an unsigned long ino_t type in an LP32 process.
241 *
242 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
243 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
244 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
245 * in fs/fuse/inode.c).
246 *
247 * Accesses must be guarded by |lock|.
248 */
249 __u32 inode_ctr;
250
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700251 Hashmap* package_to_appid;
252 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700253};
254
Jeff Brown7729d242012-05-25 15:35:28 -0700255/* Private data used by a single fuse handler. */
256struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700257 struct fuse* fuse;
258 int token;
259
Jeff Brown7729d242012-05-25 15:35:28 -0700260 /* To save memory, we never use the contents of the request buffer and the read
261 * buffer at the same time. This allows us to share the underlying storage. */
262 union {
263 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800264 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700265 };
266};
Brian Swetland03ee9472010-08-12 18:01:08 -0700267
Jeff Brown6249b902012-05-26 14:32:54 -0700268static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700269{
Jeff Brown6249b902012-05-26 14:32:54 -0700270 return (void *) (uintptr_t) nid;
271}
Brian Swetland03ee9472010-08-12 18:01:08 -0700272
Jeff Brown6249b902012-05-26 14:32:54 -0700273static inline __u64 ptr_to_id(void *ptr)
274{
275 return (__u64) (uintptr_t) ptr;
276}
Brian Swetland03ee9472010-08-12 18:01:08 -0700277
Jeff Brown6249b902012-05-26 14:32:54 -0700278static void acquire_node_locked(struct node* node)
279{
280 node->refcount++;
281 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
282}
283
284static void remove_node_from_parent_locked(struct node* node);
285
286static void release_node_locked(struct node* node)
287{
288 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
289 if (node->refcount > 0) {
290 node->refcount--;
291 if (!node->refcount) {
292 TRACE("DESTROY %p (%s)\n", node, node->name);
293 remove_node_from_parent_locked(node);
294
295 /* TODO: remove debugging - poison memory */
296 memset(node->name, 0xef, node->namelen);
297 free(node->name);
298 free(node->actual_name);
299 memset(node, 0xfc, sizeof(*node));
300 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800301 }
Jeff Brown6249b902012-05-26 14:32:54 -0700302 } else {
303 ERROR("Zero refcnt %p\n", node);
304 }
305}
306
307static void add_node_to_parent_locked(struct node *node, struct node *parent) {
308 node->parent = parent;
309 node->next = parent->child;
310 parent->child = node;
311 acquire_node_locked(parent);
312}
313
314static void remove_node_from_parent_locked(struct node* node)
315{
316 if (node->parent) {
317 if (node->parent->child == node) {
318 node->parent->child = node->parent->child->next;
319 } else {
320 struct node *node2;
321 node2 = node->parent->child;
322 while (node2->next != node)
323 node2 = node2->next;
324 node2->next = node->next;
325 }
326 release_node_locked(node->parent);
327 node->parent = NULL;
328 node->next = NULL;
329 }
330}
331
332/* Gets the absolute path to a node into the provided buffer.
333 *
334 * Populates 'buf' with the path and returns the length of the path on success,
335 * or returns -1 if the path is too long for the provided buffer.
336 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700337static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
338 const char* name;
339 size_t namelen;
340 if (node->graft_path) {
341 name = node->graft_path;
342 namelen = node->graft_pathlen;
343 } else if (node->actual_name) {
344 name = node->actual_name;
345 namelen = node->namelen;
346 } else {
347 name = node->name;
348 namelen = node->namelen;
349 }
350
Jeff Brown6249b902012-05-26 14:32:54 -0700351 if (bufsize < namelen + 1) {
352 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700353 }
354
Jeff Brown6249b902012-05-26 14:32:54 -0700355 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700356 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700357 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
358 if (pathlen < 0) {
359 return -1;
360 }
361 buf[pathlen++] = '/';
362 }
363
Jeff Brown6249b902012-05-26 14:32:54 -0700364 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
365 return pathlen + namelen;
366}
367
368/* Finds the absolute path of a file within a given directory.
369 * Performs a case-insensitive search for the file and sets the buffer to the path
370 * of the first matching file. If 'search' is zero or if no match is found, sets
371 * the buffer to the path that the file would have, assuming the name were case-sensitive.
372 *
373 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
374 * or returns NULL if the path is too long for the provided buffer.
375 */
376static char* find_file_within(const char* path, const char* name,
377 char* buf, size_t bufsize, int search)
378{
379 size_t pathlen = strlen(path);
380 size_t namelen = strlen(name);
381 size_t childlen = pathlen + namelen + 1;
382 char* actual;
383
384 if (bufsize <= childlen) {
385 return NULL;
386 }
387
388 memcpy(buf, path, pathlen);
389 buf[pathlen] = '/';
390 actual = buf + pathlen + 1;
391 memcpy(actual, name, namelen + 1);
392
393 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800394 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700395 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800396 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700397 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700398 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800399 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800400 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700401 if (!strcasecmp(entry->d_name, name)) {
402 /* we have a match - replace the name, don't need to copy the null again */
403 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800404 break;
405 }
406 }
407 closedir(dir);
408 }
Jeff Brown6249b902012-05-26 14:32:54 -0700409 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800410}
411
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700412static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800413{
Narayan Kamathfaa09352015-01-13 18:21:10 +0000414 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700415 attr->size = s->st_size;
416 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400417 attr->atime = s->st_atime;
418 attr->mtime = s->st_mtime;
419 attr->ctime = s->st_ctime;
420 attr->atimensec = s->st_atime_nsec;
421 attr->mtimensec = s->st_mtime_nsec;
422 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700423 attr->mode = s->st_mode;
424 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700425
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700426 attr->uid = node->uid;
427 attr->gid = node->gid;
428
429 /* Filter requested mode based on underlying file, and
430 * pass through file type. */
431 int owner_mode = s->st_mode & 0700;
432 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
433 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
434}
435
Jeff Sharkey44d63422013-09-12 09:44:48 -0700436static int touch(char* path, mode_t mode) {
437 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
438 if (fd == -1) {
439 if (errno == EEXIST) {
440 return 0;
441 } else {
442 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
443 return -1;
444 }
445 }
446 close(fd);
447 return 0;
448}
449
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700450static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
451 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700452 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700453
454 /* By default, each node inherits from its parent */
455 node->perm = PERM_INHERIT;
456 node->userid = parent->userid;
457 node->uid = parent->uid;
458 node->gid = parent->gid;
459 node->mode = parent->mode;
460
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700461 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700462 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700463 }
464
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700465 /* Derive custom permissions based on parent and current node */
466 switch (parent->perm) {
467 case PERM_INHERIT:
468 /* Already inherited above */
469 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700470 case PERM_LEGACY_PRE_ROOT:
471 /* Legacy internal layout places users at top level */
472 node->perm = PERM_ROOT;
473 node->userid = strtoul(node->name, NULL, 10);
474 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700475 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700476 /* Assume masked off by default. */
477 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* App-specific directories inside; let anyone traverse */
480 node->perm = PERM_ANDROID;
481 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700482 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700483 if (!strcasecmp(node->name, "DCIM")
484 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700485 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700486 } else if (!strcasecmp(node->name, "Alarms")
487 || !strcasecmp(node->name, "Movies")
488 || !strcasecmp(node->name, "Music")
489 || !strcasecmp(node->name, "Notifications")
490 || !strcasecmp(node->name, "Podcasts")
491 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700492 node->gid = AID_SDCARD_AV;
493 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700494 }
495 break;
496 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700497 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700498 /* App-specific directories inside; let anyone traverse */
499 node->perm = PERM_ANDROID_DATA;
500 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700501 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700502 /* App-specific directories inside; let anyone traverse */
503 node->perm = PERM_ANDROID_OBB;
504 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700505 /* Single OBB directory is always shared */
506 node->graft_path = fuse->obbpath;
507 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700508 } else if (!strcasecmp(node->name, "media")) {
509 /* App-specific directories inside; let anyone traverse */
510 node->perm = PERM_ANDROID_MEDIA;
511 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700512 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700513 /* User directories must only be accessible to system, protected
514 * by sdcard_all. Zygote will bind mount the appropriate user-
515 * specific path. */
516 node->perm = PERM_ANDROID_USER;
517 node->gid = AID_SDCARD_ALL;
518 node->mode = 0770;
519 }
520 break;
521 case PERM_ANDROID_DATA:
522 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700523 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800524 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700525 if (appid != 0) {
526 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700527 }
528 node->mode = 0770;
529 break;
530 case PERM_ANDROID_USER:
531 /* Root of a secondary user */
532 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700533 node->userid = strtoul(node->name, NULL, 10);
534 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700535 node->mode = 0771;
536 break;
537 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700538}
539
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700540/* Return if the calling UID holds sdcard_rw. */
541static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700542 /* No additional permissions enforcement */
543 if (fuse->derive == DERIVE_NONE) {
544 return true;
545 }
546
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700547 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800548 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700549}
550
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700551/* Kernel has already enforced everything we returned through
552 * derive_permissions_locked(), so this is used to lock down access
553 * even further, such as enforcing that apps hold sdcard_rw. */
554static bool check_caller_access_to_name(struct fuse* fuse,
555 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700556 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700557 /* Always block security-sensitive files at root */
558 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700559 if (!strcasecmp(name, "autorun.inf")
560 || !strcasecmp(name, ".android_secure")
561 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700562 return false;
563 }
564 }
565
566 /* No additional permissions enforcement */
567 if (fuse->derive == DERIVE_NONE) {
568 return true;
569 }
570
Jeff Sharkey44d63422013-09-12 09:44:48 -0700571 /* Root always has access; access for any other UIDs should always
572 * be controlled through packages.list. */
573 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700574 return true;
575 }
576
577 /* If asking to write, verify that caller either owns the
578 * parent or holds sdcard_rw. */
579 if (mode & W_OK) {
580 if (parent_node && hdr->uid == parent_node->uid) {
581 return true;
582 }
583
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700584 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700585 }
586
587 /* No extra permissions to enforce */
588 return true;
589}
590
591static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700592 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
593 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700594}
595
Jeff Brown6249b902012-05-26 14:32:54 -0700596struct node *create_node_locked(struct fuse* fuse,
597 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700598{
599 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700600 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700601
Narayan Kamathfaa09352015-01-13 18:21:10 +0000602 // Detect overflows in the inode counter. "4 billion nodes should be enough
603 // for everybody".
604 if (fuse->inode_ctr == 0) {
605 ERROR("No more inode numbers available");
606 return NULL;
607 }
608
Paul Eastham11ccdb32010-10-14 11:04:26 -0700609 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700610 if (!node) {
611 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700612 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700613 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700614 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700615 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700616 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700617 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700618 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700619 if (strcmp(name, actual_name)) {
620 node->actual_name = malloc(namelen + 1);
621 if (!node->actual_name) {
622 free(node->name);
623 free(node);
624 return NULL;
625 }
626 memcpy(node->actual_name, actual_name, namelen + 1);
627 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700628 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700629 node->nid = ptr_to_id(node);
Narayan Kamathfaa09352015-01-13 18:21:10 +0000630 node->ino = fuse->inode_ctr++;
Jeff Brown6249b902012-05-26 14:32:54 -0700631 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700632
633 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700634 acquire_node_locked(node);
635 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700636 return node;
637}
638
Jeff Brown6249b902012-05-26 14:32:54 -0700639static int rename_node_locked(struct node *node, const char *name,
640 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700641{
Jeff Brown6249b902012-05-26 14:32:54 -0700642 size_t namelen = strlen(name);
643 int need_actual_name = strcmp(name, actual_name);
644
645 /* make the storage bigger without actually changing the name
646 * in case an error occurs part way */
647 if (namelen > node->namelen) {
648 char* new_name = realloc(node->name, namelen + 1);
649 if (!new_name) {
650 return -ENOMEM;
651 }
652 node->name = new_name;
653 if (need_actual_name && node->actual_name) {
654 char* new_actual_name = realloc(node->actual_name, namelen + 1);
655 if (!new_actual_name) {
656 return -ENOMEM;
657 }
658 node->actual_name = new_actual_name;
659 }
660 }
661
662 /* update the name, taking care to allocate storage before overwriting the old name */
663 if (need_actual_name) {
664 if (!node->actual_name) {
665 node->actual_name = malloc(namelen + 1);
666 if (!node->actual_name) {
667 return -ENOMEM;
668 }
669 }
670 memcpy(node->actual_name, actual_name, namelen + 1);
671 } else {
672 free(node->actual_name);
673 node->actual_name = NULL;
674 }
675 memcpy(node->name, name, namelen + 1);
676 node->namelen = namelen;
677 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700678}
679
Jeff Brown6249b902012-05-26 14:32:54 -0700680static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700681{
Jeff Brown6249b902012-05-26 14:32:54 -0700682 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700683 return &fuse->root;
684 } else {
685 return id_to_ptr(nid);
686 }
687}
688
Jeff Brown6249b902012-05-26 14:32:54 -0700689static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
690 char* buf, size_t bufsize)
691{
692 struct node* node = lookup_node_by_id_locked(fuse, nid);
693 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
694 node = NULL;
695 }
696 return node;
697}
698
699static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700700{
701 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700702 /* use exact string comparison, nodes that differ by case
703 * must be considered distinct even if they refer to the same
704 * underlying file as otherwise operations such as "mv x x"
705 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700706 if (!strcmp(name, node->name)) {
707 return node;
708 }
709 }
710 return 0;
711}
712
Jeff Brown6249b902012-05-26 14:32:54 -0700713static struct node* acquire_or_create_child_locked(
714 struct fuse* fuse, struct node* parent,
715 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700716{
Jeff Brown6249b902012-05-26 14:32:54 -0700717 struct node* child = lookup_child_by_name_locked(parent, name);
718 if (child) {
719 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800720 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700721 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800722 }
Jeff Brown6249b902012-05-26 14:32:54 -0700723 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700724}
725
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700726static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700727 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700728 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700729
Jeff Brown6249b902012-05-26 14:32:54 -0700730 fuse->fd = fd;
731 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700732 fuse->derive = derive;
733 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700734 fuse->write_gid = write_gid;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000735 fuse->inode_ctr = 1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700736
Jeff Brown6249b902012-05-26 14:32:54 -0700737 memset(&fuse->root, 0, sizeof(fuse->root));
738 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
739 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700740 fuse->root.namelen = strlen(source_path);
741 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700742 fuse->root.userid = 0;
743 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700744
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700745 /* Set up root node for various modes of operation */
746 switch (derive) {
747 case DERIVE_NONE:
748 /* Traditional behavior that treats entire device as being accessible
749 * to sdcard_rw, and no permissions are derived. */
750 fuse->root.perm = PERM_ROOT;
751 fuse->root.mode = 0775;
752 fuse->root.gid = AID_SDCARD_RW;
753 break;
754 case DERIVE_LEGACY:
755 /* Legacy behavior used to support internal multiuser layout which
756 * places user_id at the top directory level, with the actual roots
757 * just below that. Shared OBB path is also at top level. */
758 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
759 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700760 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700761 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700762 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
763 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700764 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700765 break;
766 case DERIVE_UNIFIED:
767 /* Unified multiuser layout which places secondary user_id under
768 * /Android/user and shared OBB path under /Android/obb. */
769 fuse->root.perm = PERM_ROOT;
770 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700771 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700772 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700773 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
774 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
775 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700776 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700777}
778
Jeff Brown6249b902012-05-26 14:32:54 -0700779static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700780{
781 struct fuse_out_header hdr;
782 hdr.len = sizeof(hdr);
783 hdr.error = err;
784 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700785 write(fuse->fd, &hdr, sizeof(hdr));
786}
787
Jeff Brown6249b902012-05-26 14:32:54 -0700788static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700789{
790 struct fuse_out_header hdr;
791 struct iovec vec[2];
792 int res;
793
794 hdr.len = len + sizeof(hdr);
795 hdr.error = 0;
796 hdr.unique = unique;
797
798 vec[0].iov_base = &hdr;
799 vec[0].iov_len = sizeof(hdr);
800 vec[1].iov_base = data;
801 vec[1].iov_len = len;
802
803 res = writev(fuse->fd, vec, 2);
804 if (res < 0) {
805 ERROR("*** REPLY FAILED *** %d\n", errno);
806 }
807}
808
Jeff Brown6249b902012-05-26 14:32:54 -0700809static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
810 struct node* parent, const char* name, const char* actual_name,
811 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700812{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700813 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700814 struct fuse_entry_out out;
815 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700816
Jeff Brown6249b902012-05-26 14:32:54 -0700817 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700818 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700819 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700820
Jeff Brown6249b902012-05-26 14:32:54 -0700821 pthread_mutex_lock(&fuse->lock);
822 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
823 if (!node) {
824 pthread_mutex_unlock(&fuse->lock);
825 return -ENOMEM;
826 }
827 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700828 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700829 out.attr_valid = 10;
830 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700831 out.nodeid = node->nid;
832 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700833 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700834 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700835 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700836}
837
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700838static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700839 const char* path)
840{
841 struct fuse_attr_out out;
842 struct stat s;
843
844 if (lstat(path, &s) < 0) {
845 return -errno;
846 }
847 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700848 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700849 out.attr_valid = 10;
850 fuse_reply(fuse, unique, &out, sizeof(out));
851 return NO_STATUS;
852}
853
854static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700855 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700856{
Jeff Brown6249b902012-05-26 14:32:54 -0700857 struct node* parent_node;
858 char parent_path[PATH_MAX];
859 char child_path[PATH_MAX];
860 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700861
Jeff Brown6249b902012-05-26 14:32:54 -0700862 pthread_mutex_lock(&fuse->lock);
863 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
864 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100865 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700866 parent_node ? parent_node->name : "?");
867 pthread_mutex_unlock(&fuse->lock);
868
869 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
870 child_path, sizeof(child_path), 1))) {
871 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700872 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700873 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700874 return -EACCES;
875 }
876
Jeff Brown6249b902012-05-26 14:32:54 -0700877 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700878}
879
Jeff Brown6249b902012-05-26 14:32:54 -0700880static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
882{
Jeff Brown6249b902012-05-26 14:32:54 -0700883 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700884
Jeff Brown6249b902012-05-26 14:32:54 -0700885 pthread_mutex_lock(&fuse->lock);
886 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100887 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700888 hdr->nodeid, node ? node->name : "?");
889 if (node) {
890 __u64 n = req->nlookup;
891 while (n--) {
892 release_node_locked(node);
893 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894 }
Jeff Brown6249b902012-05-26 14:32:54 -0700895 pthread_mutex_unlock(&fuse->lock);
896 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897}
898
Jeff Brown6249b902012-05-26 14:32:54 -0700899static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700900 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
901{
Jeff Brown6249b902012-05-26 14:32:54 -0700902 struct node* node;
903 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700904
Jeff Brown6249b902012-05-26 14:32:54 -0700905 pthread_mutex_lock(&fuse->lock);
906 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100907 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700908 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
909 pthread_mutex_unlock(&fuse->lock);
910
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700911 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700912 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700913 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700914 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700915 return -EACCES;
916 }
917
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700918 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700919}
920
Jeff Brown6249b902012-05-26 14:32:54 -0700921static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700922 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
923{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700924 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700925 struct node* node;
926 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700927 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700928
Jeff Brown6249b902012-05-26 14:32:54 -0700929 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700930 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700931 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100932 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700933 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
934 pthread_mutex_unlock(&fuse->lock);
935
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700936 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700937 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700938 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700939 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700940 return -EACCES;
941 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700942
Jeff Brown6249b902012-05-26 14:32:54 -0700943 /* XXX: incomplete implementation on purpose.
944 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945
Elliott Hughes45685652014-07-31 12:03:03 -0700946 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700947 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948 }
949
950 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
951 * are both set, then set it to the current time. Else, set it to the
952 * time specified in the request. Same goes for mtime. Use utimensat(2)
953 * as it allows ATIME and MTIME to be changed independently, and has
954 * nanosecond resolution which fuse also has.
955 */
956 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
957 times[0].tv_nsec = UTIME_OMIT;
958 times[1].tv_nsec = UTIME_OMIT;
959 if (req->valid & FATTR_ATIME) {
960 if (req->valid & FATTR_ATIME_NOW) {
961 times[0].tv_nsec = UTIME_NOW;
962 } else {
963 times[0].tv_sec = req->atime;
964 times[0].tv_nsec = req->atimensec;
965 }
966 }
967 if (req->valid & FATTR_MTIME) {
968 if (req->valid & FATTR_MTIME_NOW) {
969 times[1].tv_nsec = UTIME_NOW;
970 } else {
971 times[1].tv_sec = req->mtime;
972 times[1].tv_nsec = req->mtimensec;
973 }
974 }
Jeff Brown6249b902012-05-26 14:32:54 -0700975 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
976 handler->token, path, times[0].tv_sec, times[1].tv_sec);
977 if (utimensat(-1, path, times, 0) < 0) {
978 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979 }
980 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700981 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700982}
983
Jeff Brown6249b902012-05-26 14:32:54 -0700984static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700985 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
986{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700987 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700988 struct node* parent_node;
989 char parent_path[PATH_MAX];
990 char child_path[PATH_MAX];
991 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700992
Jeff Brown6249b902012-05-26 14:32:54 -0700993 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700994 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700995 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
996 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100997 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700998 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
999 pthread_mutex_unlock(&fuse->lock);
1000
1001 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1002 child_path, sizeof(child_path), 1))) {
1003 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001004 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001005 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001006 return -EACCES;
1007 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001008 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -07001009 if (mknod(child_path, mode, req->rdev) < 0) {
1010 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001011 }
Jeff Brown6249b902012-05-26 14:32:54 -07001012 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001013}
1014
Jeff Brown6249b902012-05-26 14:32:54 -07001015static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001016 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
1017{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001018 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001019 struct node* parent_node;
1020 char parent_path[PATH_MAX];
1021 char child_path[PATH_MAX];
1022 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023
Jeff Brown6249b902012-05-26 14:32:54 -07001024 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001025 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001026 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1027 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001028 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001029 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
1030 pthread_mutex_unlock(&fuse->lock);
1031
1032 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
1033 child_path, sizeof(child_path), 1))) {
1034 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001035 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001036 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001037 return -EACCES;
1038 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001039 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001040 if (mkdir(child_path, mode) < 0) {
1041 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001043
1044 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1045 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1046 char nomedia[PATH_MAX];
1047 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1048 if (touch(nomedia, 0664) != 0) {
1049 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1050 return -ENOENT;
1051 }
1052 }
1053 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1054 char nomedia[PATH_MAX];
1055 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1056 if (touch(nomedia, 0664) != 0) {
1057 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1058 return -ENOENT;
1059 }
1060 }
1061
Jeff Brown6249b902012-05-26 14:32:54 -07001062 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001063}
1064
Jeff Brown6249b902012-05-26 14:32:54 -07001065static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001066 const struct fuse_in_header* hdr, const char* name)
1067{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001068 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001069 struct node* parent_node;
1070 char parent_path[PATH_MAX];
1071 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001072
Jeff Brown6249b902012-05-26 14:32:54 -07001073 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001074 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001075 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1076 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001077 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001078 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1079 pthread_mutex_unlock(&fuse->lock);
1080
1081 if (!parent_node || !find_file_within(parent_path, name,
1082 child_path, sizeof(child_path), 1)) {
1083 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001084 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001085 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001086 return -EACCES;
1087 }
Jeff Brown6249b902012-05-26 14:32:54 -07001088 if (unlink(child_path) < 0) {
1089 return -errno;
1090 }
1091 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092}
1093
Jeff Brown6249b902012-05-26 14:32:54 -07001094static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001095 const struct fuse_in_header* hdr, const char* name)
1096{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001097 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001098 struct node* parent_node;
1099 char parent_path[PATH_MAX];
1100 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001101
Jeff Brown6249b902012-05-26 14:32:54 -07001102 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001103 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001104 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1105 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001106 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001107 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1108 pthread_mutex_unlock(&fuse->lock);
1109
1110 if (!parent_node || !find_file_within(parent_path, name,
1111 child_path, sizeof(child_path), 1)) {
1112 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001113 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001114 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001115 return -EACCES;
1116 }
Jeff Brown6249b902012-05-26 14:32:54 -07001117 if (rmdir(child_path) < 0) {
1118 return -errno;
1119 }
1120 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001121}
1122
Jeff Brown6249b902012-05-26 14:32:54 -07001123static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001124 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001125 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001126{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001127 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001128 struct node* old_parent_node;
1129 struct node* new_parent_node;
1130 struct node* child_node;
1131 char old_parent_path[PATH_MAX];
1132 char new_parent_path[PATH_MAX];
1133 char old_child_path[PATH_MAX];
1134 char new_child_path[PATH_MAX];
1135 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001136 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001137
Jeff Brown6249b902012-05-26 14:32:54 -07001138 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001139 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001140 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1141 old_parent_path, sizeof(old_parent_path));
1142 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1143 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001144 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001145 old_name, new_name,
1146 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1147 req->newdir, new_parent_node ? new_parent_node->name : "?");
1148 if (!old_parent_node || !new_parent_node) {
1149 res = -ENOENT;
1150 goto lookup_error;
1151 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001152 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001153 res = -EACCES;
1154 goto lookup_error;
1155 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001156 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001157 res = -EACCES;
1158 goto lookup_error;
1159 }
Jeff Brown6249b902012-05-26 14:32:54 -07001160 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1161 if (!child_node || get_node_path_locked(child_node,
1162 old_child_path, sizeof(old_child_path)) < 0) {
1163 res = -ENOENT;
1164 goto lookup_error;
1165 }
1166 acquire_node_locked(child_node);
1167 pthread_mutex_unlock(&fuse->lock);
1168
1169 /* Special case for renaming a file where destination is same path
1170 * differing only by case. In this case we don't want to look for a case
1171 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1172 */
1173 int search = old_parent_node != new_parent_node
1174 || strcasecmp(old_name, new_name);
1175 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1176 new_child_path, sizeof(new_child_path), search))) {
1177 res = -ENOENT;
1178 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001179 }
1180
Jeff Brown6249b902012-05-26 14:32:54 -07001181 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1182 res = rename(old_child_path, new_child_path);
1183 if (res < 0) {
1184 res = -errno;
1185 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001186 }
1187
Jeff Brown6249b902012-05-26 14:32:54 -07001188 pthread_mutex_lock(&fuse->lock);
1189 res = rename_node_locked(child_node, new_name, new_actual_name);
1190 if (!res) {
1191 remove_node_from_parent_locked(child_node);
1192 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 }
Jeff Brown6249b902012-05-26 14:32:54 -07001194 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195
Jeff Brown6249b902012-05-26 14:32:54 -07001196io_error:
1197 pthread_mutex_lock(&fuse->lock);
1198done:
1199 release_node_locked(child_node);
1200lookup_error:
1201 pthread_mutex_unlock(&fuse->lock);
1202 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203}
1204
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001205static int open_flags_to_access_mode(int open_flags) {
1206 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1207 return R_OK;
1208 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1209 return W_OK;
1210 } else {
1211 /* Probably O_RDRW, but treat as default to be safe */
1212 return R_OK | W_OK;
1213 }
1214}
1215
Jeff Brown6249b902012-05-26 14:32:54 -07001216static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1218{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001219 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001220 struct node* node;
1221 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001223 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224
Jeff Brown6249b902012-05-26 14:32:54 -07001225 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001226 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001227 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001228 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001229 req->flags, hdr->nodeid, node ? node->name : "?");
1230 pthread_mutex_unlock(&fuse->lock);
1231
1232 if (!node) {
1233 return -ENOENT;
1234 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001235 if (!check_caller_access_to_node(fuse, hdr, node,
1236 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001237 return -EACCES;
1238 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001239 h = malloc(sizeof(*h));
1240 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001241 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001242 }
Jeff Brown6249b902012-05-26 14:32:54 -07001243 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 h->fd = open(path, req->flags);
1245 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001246 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001247 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248 }
1249 out.fh = ptr_to_id(h);
1250 out.open_flags = 0;
1251 out.padding = 0;
1252 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001253 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001254}
1255
Jeff Brown6249b902012-05-26 14:32:54 -07001256static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001257 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1258{
1259 struct handle *h = id_to_ptr(req->fh);
1260 __u64 unique = hdr->unique;
1261 __u32 size = req->size;
1262 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001263 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001264 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001265
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1267 * overlaps the request buffer and will clobber data in the request. This
1268 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001269
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001270 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1271 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001272 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001273 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001275 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001276 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001277 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001278 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001279 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001280 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001281}
1282
Jeff Brown6249b902012-05-26 14:32:54 -07001283static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001284 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1285 const void* buffer)
1286{
1287 struct fuse_write_out out;
1288 struct handle *h = id_to_ptr(req->fh);
1289 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001290 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001291
Arpad Horvath49e93442014-02-18 10:18:25 +01001292 if (req->flags & O_DIRECT) {
1293 memcpy(aligned_buffer, buffer, req->size);
1294 buffer = (const __u8*) aligned_buffer;
1295 }
Jeff Brown6249b902012-05-26 14:32:54 -07001296
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001297 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001298 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001299 res = pwrite64(h->fd, buffer, req->size, req->offset);
1300 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001301 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001302 }
1303 out.size = res;
1304 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001305 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001306}
1307
Jeff Brown6249b902012-05-26 14:32:54 -07001308static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001309 const struct fuse_in_header* hdr)
1310{
Jeff Brown6249b902012-05-26 14:32:54 -07001311 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001312 struct statfs stat;
1313 struct fuse_statfs_out out;
1314 int res;
1315
Jeff Brown6249b902012-05-26 14:32:54 -07001316 pthread_mutex_lock(&fuse->lock);
1317 TRACE("[%d] STATFS\n", handler->token);
1318 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1319 pthread_mutex_unlock(&fuse->lock);
1320 if (res < 0) {
1321 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001322 }
Jeff Brown6249b902012-05-26 14:32:54 -07001323 if (statfs(fuse->root.name, &stat) < 0) {
1324 return -errno;
1325 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001326 memset(&out, 0, sizeof(out));
1327 out.st.blocks = stat.f_blocks;
1328 out.st.bfree = stat.f_bfree;
1329 out.st.bavail = stat.f_bavail;
1330 out.st.files = stat.f_files;
1331 out.st.ffree = stat.f_ffree;
1332 out.st.bsize = stat.f_bsize;
1333 out.st.namelen = stat.f_namelen;
1334 out.st.frsize = stat.f_frsize;
1335 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001336 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337}
1338
Jeff Brown6249b902012-05-26 14:32:54 -07001339static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001340 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1341{
1342 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001343
1344 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001345 close(h->fd);
1346 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001347 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348}
1349
Jeff Brown6249b902012-05-26 14:32:54 -07001350static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001351 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1352{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001353 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1354 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001355
Elliott Hughesf6d67372014-07-08 14:38:26 -07001356 int fd = -1;
1357 if (is_dir) {
1358 struct dirhandle *dh = id_to_ptr(req->fh);
1359 fd = dirfd(dh->d);
1360 } else {
1361 struct handle *h = id_to_ptr(req->fh);
1362 fd = h->fd;
1363 }
1364
1365 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1366 is_dir ? "FSYNCDIR" : "FSYNC",
1367 id_to_ptr(req->fh), fd, is_data_sync);
1368 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1369 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001370 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001371 }
Jeff Brown6249b902012-05-26 14:32:54 -07001372 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001373}
1374
Jeff Brown6249b902012-05-26 14:32:54 -07001375static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376 const struct fuse_in_header* hdr)
1377{
Jeff Brown6249b902012-05-26 14:32:54 -07001378 TRACE("[%d] FLUSH\n", handler->token);
1379 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380}
1381
Jeff Brown6249b902012-05-26 14:32:54 -07001382static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001383 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1384{
Jeff Brown6249b902012-05-26 14:32:54 -07001385 struct node* node;
1386 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001387 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001389
Jeff Brown6249b902012-05-26 14:32:54 -07001390 pthread_mutex_lock(&fuse->lock);
1391 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001392 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001393 hdr->nodeid, node ? node->name : "?");
1394 pthread_mutex_unlock(&fuse->lock);
1395
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001396 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001397 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001398 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001399 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001400 return -EACCES;
1401 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001402 h = malloc(sizeof(*h));
1403 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001404 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001405 }
Jeff Brown6249b902012-05-26 14:32:54 -07001406 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001407 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001408 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001409 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001410 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001411 }
1412 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001413 out.open_flags = 0;
1414 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001415 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001416 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001417}
1418
Jeff Brown6249b902012-05-26 14:32:54 -07001419static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1421{
1422 char buffer[8192];
1423 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1424 struct dirent *de;
1425 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001426
1427 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001428 if (req->offset == 0) {
1429 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001430 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001431 rewinddir(h->d);
1432 }
1433 de = readdir(h->d);
1434 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001435 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001436 }
1437 fde->ino = FUSE_UNKNOWN_INO;
1438 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1439 fde->off = req->offset + 1;
1440 fde->type = de->d_type;
1441 fde->namelen = strlen(de->d_name);
1442 memcpy(fde->name, de->d_name, fde->namelen + 1);
1443 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001444 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1445 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001446}
1447
Jeff Brown6249b902012-05-26 14:32:54 -07001448static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001449 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1450{
1451 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001452
1453 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001454 closedir(h->d);
1455 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001456 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001457}
1458
Jeff Brown6249b902012-05-26 14:32:54 -07001459static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001460 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1461{
1462 struct fuse_init_out out;
1463
Jeff Brown6249b902012-05-26 14:32:54 -07001464 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1465 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001466 out.major = FUSE_KERNEL_VERSION;
1467 out.minor = FUSE_KERNEL_MINOR_VERSION;
1468 out.max_readahead = req->max_readahead;
1469 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1470 out.max_background = 32;
1471 out.congestion_threshold = 32;
1472 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001473 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001474 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001475}
1476
Jeff Brown6249b902012-05-26 14:32:54 -07001477static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001478 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1479{
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 switch (hdr->opcode) {
1481 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001482 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001483 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001484 }
Jeff Brown84715842012-05-25 14:07:47 -07001485
Brian Swetland03ee9472010-08-12 18:01:08 -07001486 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001487 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001488 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001489 }
Jeff Brown84715842012-05-25 14:07:47 -07001490
Brian Swetland03ee9472010-08-12 18:01:08 -07001491 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001492 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001493 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001494 }
Jeff Brown84715842012-05-25 14:07:47 -07001495
Brian Swetland03ee9472010-08-12 18:01:08 -07001496 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001497 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001498 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001499 }
Jeff Brown84715842012-05-25 14:07:47 -07001500
Brian Swetland03ee9472010-08-12 18:01:08 -07001501// case FUSE_READLINK:
1502// case FUSE_SYMLINK:
1503 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001504 const struct fuse_mknod_in *req = data;
1505 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001506 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001507 }
Jeff Brown84715842012-05-25 14:07:47 -07001508
Brian Swetland03ee9472010-08-12 18:01:08 -07001509 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001510 const struct fuse_mkdir_in *req = data;
1511 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001512 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001513 }
Jeff Brown84715842012-05-25 14:07:47 -07001514
Brian Swetland03ee9472010-08-12 18:01:08 -07001515 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001516 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001517 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001518 }
Jeff Brown84715842012-05-25 14:07:47 -07001519
Brian Swetland03ee9472010-08-12 18:01:08 -07001520 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001521 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001522 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 }
Jeff Brown84715842012-05-25 14:07:47 -07001524
Brian Swetland03ee9472010-08-12 18:01:08 -07001525 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001526 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001527 const char *old_name = ((const char*) data) + sizeof(*req);
1528 const char *new_name = old_name + strlen(old_name) + 1;
1529 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001530 }
Jeff Brown84715842012-05-25 14:07:47 -07001531
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001532// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001533 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001534 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001535 return handle_open(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_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001539 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001540 return handle_read(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_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001544 const struct fuse_write_in *req = data;
1545 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001546 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001547 }
Jeff Brown84715842012-05-25 14:07:47 -07001548
Mike Lockwood4553b082010-08-16 14:14:44 -04001549 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001550 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001551 }
Jeff Brown84715842012-05-25 14:07:47 -07001552
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001554 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001555 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001556 }
Jeff Brown84715842012-05-25 14:07:47 -07001557
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001558 case FUSE_FSYNC:
1559 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001560 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001561 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001562 }
1563
Brian Swetland03ee9472010-08-12 18:01:08 -07001564// case FUSE_SETXATTR:
1565// case FUSE_GETXATTR:
1566// case FUSE_LISTXATTR:
1567// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001568 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001569 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001570 }
1571
Brian Swetland03ee9472010-08-12 18:01:08 -07001572 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001573 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001574 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001575 }
Jeff Brown84715842012-05-25 14:07:47 -07001576
Brian Swetland03ee9472010-08-12 18:01:08 -07001577 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001578 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001579 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001580 }
Jeff Brown84715842012-05-25 14:07:47 -07001581
Brian Swetland03ee9472010-08-12 18:01:08 -07001582 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001583 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001584 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001585 }
Jeff Brown84715842012-05-25 14:07:47 -07001586
Brian Swetland03ee9472010-08-12 18:01:08 -07001587 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001588 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001589 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001590 }
Jeff Brown84715842012-05-25 14:07:47 -07001591
Brian Swetland03ee9472010-08-12 18:01:08 -07001592 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001593 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001594 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1595 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001596 }
Jeff Brown84715842012-05-25 14:07:47 -07001597 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001598}
1599
Jeff Brown6249b902012-05-26 14:32:54 -07001600static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001601{
Jeff Brown6249b902012-05-26 14:32:54 -07001602 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001603 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001604 ssize_t len = read(fuse->fd,
1605 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001606 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001607 if (errno != EINTR) {
1608 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1609 }
1610 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001611 }
Jeff Brown84715842012-05-25 14:07:47 -07001612
1613 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001614 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1615 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001616 }
1617
Jeff Brown7729d242012-05-25 15:35:28 -07001618 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001619 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001620 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1621 handler->token, (size_t)len, hdr->len);
1622 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001623 }
1624
Jeff Brown7729d242012-05-25 15:35:28 -07001625 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001626 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001627 __u64 unique = hdr->unique;
1628 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001629
1630 /* We do not access the request again after this point because the underlying
1631 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001632
1633 if (res != NO_STATUS) {
1634 if (res) {
1635 TRACE("[%d] ERROR %d\n", handler->token, res);
1636 }
1637 fuse_status(fuse, unique, res);
1638 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001639 }
1640}
1641
Jeff Brown6249b902012-05-26 14:32:54 -07001642static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001643{
Jeff Brown6249b902012-05-26 14:32:54 -07001644 struct fuse_handler* handler = data;
1645 handle_fuse_requests(handler);
1646 return NULL;
1647}
1648
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001649static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001650 Hashmap* map = context;
1651 hashmapRemove(map, key);
1652 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001653 return true;
1654}
1655
1656static bool remove_int_to_null(void *key, void *value, void *context) {
1657 Hashmap* map = context;
1658 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001659 return true;
1660}
1661
1662static int read_package_list(struct fuse *fuse) {
1663 pthread_mutex_lock(&fuse->lock);
1664
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001665 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1666 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001667
1668 FILE* file = fopen(kPackagesListFile, "r");
1669 if (!file) {
1670 ERROR("failed to open package list: %s\n", strerror(errno));
1671 pthread_mutex_unlock(&fuse->lock);
1672 return -1;
1673 }
1674
1675 char buf[512];
1676 while (fgets(buf, sizeof(buf), file) != NULL) {
1677 char package_name[512];
1678 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001679 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001680
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001681 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1682 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001683 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001684
1685 char* token = strtok(gids, ",");
1686 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001687 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001688 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001689 break;
1690 }
1691 token = strtok(NULL, ",");
1692 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001693 }
1694 }
1695
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001696 TRACE("read_package_list: found %zu packages, %zu with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001697 hashmapSize(fuse->package_to_appid),
1698 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001699 fclose(file);
1700 pthread_mutex_unlock(&fuse->lock);
1701 return 0;
1702}
1703
1704static void watch_package_list(struct fuse* fuse) {
1705 struct inotify_event *event;
1706 char event_buf[512];
1707
1708 int nfd = inotify_init();
1709 if (nfd < 0) {
1710 ERROR("inotify_init failed: %s\n", strerror(errno));
1711 return;
1712 }
1713
1714 bool active = false;
1715 while (1) {
1716 if (!active) {
1717 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1718 if (res == -1) {
1719 if (errno == ENOENT || errno == EACCES) {
1720 /* Framework may not have created yet, sleep and retry */
1721 ERROR("missing packages.list; retrying\n");
1722 sleep(3);
1723 continue;
1724 } else {
1725 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1726 return;
1727 }
1728 }
1729
1730 /* Watch above will tell us about any future changes, so
1731 * read the current state. */
1732 if (read_package_list(fuse) == -1) {
1733 ERROR("read_package_list failed: %s\n", strerror(errno));
1734 return;
1735 }
1736 active = true;
1737 }
1738
1739 int event_pos = 0;
1740 int res = read(nfd, event_buf, sizeof(event_buf));
1741 if (res < (int) sizeof(*event)) {
1742 if (errno == EINTR)
1743 continue;
1744 ERROR("failed to read inotify event: %s\n", strerror(errno));
1745 return;
1746 }
1747
1748 while (res >= (int) sizeof(*event)) {
1749 int event_size;
1750 event = (struct inotify_event *) (event_buf + event_pos);
1751
1752 TRACE("inotify event: %08x\n", event->mask);
1753 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1754 /* Previously watched file was deleted, probably due to move
1755 * that swapped in new data; re-arm the watch and read. */
1756 active = false;
1757 }
1758
1759 event_size = sizeof(*event) + event->len;
1760 res -= event_size;
1761 event_pos += event_size;
1762 }
1763 }
1764}
1765
Jeff Brown6249b902012-05-26 14:32:54 -07001766static int ignite_fuse(struct fuse* fuse, int num_threads)
1767{
1768 struct fuse_handler* handlers;
1769 int i;
1770
1771 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1772 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001773 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001774 return -ENOMEM;
1775 }
1776
1777 for (i = 0; i < num_threads; i++) {
1778 handlers[i].fuse = fuse;
1779 handlers[i].token = i;
1780 }
1781
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001782 /* When deriving permissions, this thread is used to process inotify events,
1783 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001784 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001785 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001786 pthread_t thread;
1787 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1788 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001789 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001790 goto quit;
1791 }
1792 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001793
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001794 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001795 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001796 } else {
1797 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001798 }
1799
1800 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001801
1802 /* don't bother killing all of the other threads or freeing anything,
1803 * should never get here anyhow */
1804quit:
1805 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001806}
1807
Mike Lockwood4f35e622011-01-12 14:39:44 -05001808static int usage()
1809{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001810 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1811 " -u: specify UID to run as\n"
1812 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001813 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001814 " -t: specify number of threads to use (default %d)\n"
1815 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001816 " -l: derive file permissions based on legacy internal layout\n"
1817 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001818 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001819 return 1;
1820}
1821
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001822static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001823 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1824 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001825 int fd;
1826 char opts[256];
1827 int res;
1828 struct fuse fuse;
1829
1830 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001831 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001832
1833 fd = open("/dev/fuse", O_RDWR);
1834 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001835 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001836 return -1;
1837 }
1838
1839 snprintf(opts, sizeof(opts),
1840 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1841 fd, uid, gid);
1842
Daisuke Okitsua5a4e9e2014-11-24 09:37:55 +01001843 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001844 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001845 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1846 goto error;
1847 }
1848
1849 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1850 if (res < 0) {
1851 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001852 goto error;
1853 }
1854
1855 res = setgid(gid);
1856 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001857 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001858 goto error;
1859 }
1860
1861 res = setuid(uid);
1862 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001863 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001864 goto error;
1865 }
1866
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001867 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001868
1869 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001870 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001871
1872 /* we do not attempt to umount the file system here because we are no longer
1873 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001874
1875error:
1876 close(fd);
1877 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001878}
1879
Brian Swetland03ee9472010-08-12 18:01:08 -07001880int main(int argc, char **argv)
1881{
Brian Swetland03ee9472010-08-12 18:01:08 -07001882 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001883 const char *source_path = NULL;
1884 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001885 uid_t uid = 0;
1886 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001887 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001888 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001889 derive_t derive = DERIVE_NONE;
1890 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001891 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001892 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001893 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001894
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001895 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001896 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001897 switch (opt) {
1898 case 'u':
1899 uid = strtoul(optarg, NULL, 10);
1900 break;
1901 case 'g':
1902 gid = strtoul(optarg, NULL, 10);
1903 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001904 case 'w':
1905 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001906 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001907 case 't':
1908 num_threads = strtoul(optarg, NULL, 10);
1909 break;
1910 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001911 derive = DERIVE_UNIFIED;
1912 break;
1913 case 'l':
1914 derive = DERIVE_LEGACY;
1915 break;
1916 case 's':
1917 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001918 break;
1919 case '?':
1920 default:
1921 return usage();
1922 }
1923 }
1924
1925 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001926 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001927 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001928 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001929 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001930 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001931 } else if (!uid) {
1932 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001933 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001934 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001935 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001936 ERROR("too many arguments\n");
1937 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001938 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001939 }
1940
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001941 if (!source_path) {
1942 ERROR("no source path specified\n");
1943 return usage();
1944 }
1945 if (!dest_path) {
1946 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001947 return usage();
1948 }
Jeff Brown26567352012-05-25 13:27:43 -07001949 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001950 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001951 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001952 }
Jeff Brown6249b902012-05-26 14:32:54 -07001953 if (num_threads < 1) {
1954 ERROR("number of threads must be at least 1\n");
1955 return usage();
1956 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001957 if (split_perms && derive == DERIVE_NONE) {
1958 ERROR("cannot split permissions without deriving\n");
1959 return usage();
1960 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001961
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001962 rlim.rlim_cur = 8192;
1963 rlim.rlim_max = 8192;
1964 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1965 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1966 }
1967
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001968 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1969 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1970 sleep(1);
1971 }
1972
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001973 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001974 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001975}