blob: 6a9c2eb94b7553192348421e9f94c286255b40f9 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040025#include <sys/statfs.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070026#include <sys/uio.h>
27#include <dirent.h>
Jeff Brown7729d242012-05-25 15:35:28 -070028#include <limits.h>
Mike Lockwood1bedb732011-01-13 13:38:42 -050029#include <ctype.h>
Jeff Brown6249b902012-05-26 14:32:54 -070030#include <pthread.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080031#include <sys/time.h>
32#include <sys/resource.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070033#include <sys/inotify.h>
34
Jeff Sharkey44d63422013-09-12 09:44:48 -070035#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070036#include <cutils/hashmap.h>
37#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070038
Brian Swetlandb14a2c62010-08-12 18:21:12 -070039#include <private/android_filesystem_config.h>
40
Brian Swetland03ee9472010-08-12 18:01:08 -070041#include "fuse.h"
42
43/* README
44 *
45 * What is this?
46 *
47 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
48 * directory permissions (all files are given fixed owner, group, and
49 * permissions at creation, owner, group, and permissions are not
50 * changeable, symlinks and hardlinks are not createable, etc.
51 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070052 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070053 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070054 * It must be run as root, but will drop to requested UID/GID as soon as it
55 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070056 *
57 * Things I believe to be true:
58 *
59 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
60 * CREAT) must bump that node's refcount
61 * - don't forget that FORGET can forget multiple references (req->nlookup)
62 * - if an op that returns a fuse_entry fails writing the reply to the
63 * kernel, you must rollback the refcount to reflect the reference the
64 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070065 *
66 * This daemon can also derive custom filesystem permissions based on directory
67 * structure when requested. These custom permissions support several features:
68 *
69 * - Apps can access their own files in /Android/data/com.example/ without
70 * requiring any additional GIDs.
71 * - Separate permissions for protecting directories like Pictures and Music.
72 * - Multi-user separation on the same physical device.
73 *
74 * The derived permissions look like this:
75 *
76 * rwxrwx--x root:sdcard_rw /
77 * rwxrwx--- root:sdcard_pics /Pictures
78 * rwxrwx--- root:sdcard_av /Music
79 *
80 * rwxrwx--x root:sdcard_rw /Android
81 * rwxrwx--x root:sdcard_rw /Android/data
82 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
83 * rwxrwx--x root:sdcard_rw /Android/obb/
84 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
85 *
86 * rwxrwx--- root:sdcard_all /Android/user
87 * rwxrwx--x root:sdcard_rw /Android/user/10
88 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070089 */
90
91#define FUSE_TRACE 0
92
93#if FUSE_TRACE
94#define TRACE(x...) fprintf(stderr,x)
95#else
96#define TRACE(x...) do {} while (0)
97#endif
98
99#define ERROR(x...) fprintf(stderr,x)
100
101#define FUSE_UNKNOWN_INO 0xffffffff
102
Jeff Brown84715842012-05-25 14:07:47 -0700103/* Maximum number of bytes to write in one request. */
104#define MAX_WRITE (256 * 1024)
105
106/* Maximum number of bytes to read in one request. */
107#define MAX_READ (128 * 1024)
108
109/* Largest possible request.
110 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
111 * the largest possible data payload. */
112#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
113
Jeff Brown6249b902012-05-26 14:32:54 -0700114/* Default number of threads. */
115#define DEFAULT_NUM_THREADS 2
116
117/* Pseudo-error constant used to indicate that no fuse status is needed
118 * or that a reply has already been written. */
119#define NO_STATUS 1
120
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700121/* Path to system-provided mapping of package name to appIds */
122static const char* const kPackagesListFile = "/data/system/packages.list";
123
124/* Supplementary groups to execute with */
125static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
126
127/* Permission mode for a specific node. Controls how file permissions
128 * are derived for children nodes. */
129typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700130 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700131 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700132 /* This node is one level above a normal root; used for legacy layouts
133 * which use the first level to represent user_id. */
134 PERM_LEGACY_PRE_ROOT,
135 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700136 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700137 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700138 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700139 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID_OBB,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_USER,
145} perm_t;
146
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700147/* Permissions structure to derive */
148typedef enum {
149 DERIVE_NONE,
150 DERIVE_LEGACY,
151 DERIVE_UNIFIED,
152} derive_t;
153
Brian Swetland03ee9472010-08-12 18:01:08 -0700154struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700155 int fd;
156};
157
158struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700159 DIR *d;
160};
161
162struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700163 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700164 __u64 nid;
165 __u64 gen;
166
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700167 /* State derived based on current position in hierarchy. */
168 perm_t perm;
169 userid_t userid;
170 uid_t uid;
171 gid_t gid;
172 mode_t mode;
173
Paul Eastham11ccdb32010-10-14 11:04:26 -0700174 struct node *next; /* per-dir sibling list */
175 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700176 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700177
Jeff Brown6249b902012-05-26 14:32:54 -0700178 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700179 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800180 /* If non-null, this is the real name of the file in the underlying storage.
181 * This may differ from the field "name" only by case.
182 * strlen(actual_name) will always equal strlen(name), so it is safe to use
183 * namelen for both fields.
184 */
185 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700186
187 /* If non-null, an exact underlying path that should be grafted into this
188 * position. Used to support things like OBB. */
189 char* graft_path;
190 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700191};
192
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700193static int str_hash(void *key) {
194 return hashmapHash(key, strlen(key));
195}
196
Jeff Sharkey44d63422013-09-12 09:44:48 -0700197/** Test if two string keys are equal ignoring case */
198static bool str_icase_equals(void *keyA, void *keyB) {
199 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700200}
201
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700202static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800203 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700204}
205
206static bool int_equals(void *keyA, void *keyB) {
207 return keyA == keyB;
208}
209
Jeff Brown7729d242012-05-25 15:35:28 -0700210/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700211struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700212 pthread_mutex_t lock;
213
Brian Swetland03ee9472010-08-12 18:01:08 -0700214 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700215 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700216 derive_t derive;
217 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700218 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700219 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700220 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700221
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700222 Hashmap* package_to_appid;
223 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700224};
225
Jeff Brown7729d242012-05-25 15:35:28 -0700226/* Private data used by a single fuse handler. */
227struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700228 struct fuse* fuse;
229 int token;
230
Jeff Brown7729d242012-05-25 15:35:28 -0700231 /* To save memory, we never use the contents of the request buffer and the read
232 * buffer at the same time. This allows us to share the underlying storage. */
233 union {
234 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800235 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700236 };
237};
Brian Swetland03ee9472010-08-12 18:01:08 -0700238
Jeff Brown6249b902012-05-26 14:32:54 -0700239static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700240{
Jeff Brown6249b902012-05-26 14:32:54 -0700241 return (void *) (uintptr_t) nid;
242}
Brian Swetland03ee9472010-08-12 18:01:08 -0700243
Jeff Brown6249b902012-05-26 14:32:54 -0700244static inline __u64 ptr_to_id(void *ptr)
245{
246 return (__u64) (uintptr_t) ptr;
247}
Brian Swetland03ee9472010-08-12 18:01:08 -0700248
Jeff Brown6249b902012-05-26 14:32:54 -0700249static void acquire_node_locked(struct node* node)
250{
251 node->refcount++;
252 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
253}
254
255static void remove_node_from_parent_locked(struct node* node);
256
257static void release_node_locked(struct node* node)
258{
259 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
260 if (node->refcount > 0) {
261 node->refcount--;
262 if (!node->refcount) {
263 TRACE("DESTROY %p (%s)\n", node, node->name);
264 remove_node_from_parent_locked(node);
265
266 /* TODO: remove debugging - poison memory */
267 memset(node->name, 0xef, node->namelen);
268 free(node->name);
269 free(node->actual_name);
270 memset(node, 0xfc, sizeof(*node));
271 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800272 }
Jeff Brown6249b902012-05-26 14:32:54 -0700273 } else {
274 ERROR("Zero refcnt %p\n", node);
275 }
276}
277
278static void add_node_to_parent_locked(struct node *node, struct node *parent) {
279 node->parent = parent;
280 node->next = parent->child;
281 parent->child = node;
282 acquire_node_locked(parent);
283}
284
285static void remove_node_from_parent_locked(struct node* node)
286{
287 if (node->parent) {
288 if (node->parent->child == node) {
289 node->parent->child = node->parent->child->next;
290 } else {
291 struct node *node2;
292 node2 = node->parent->child;
293 while (node2->next != node)
294 node2 = node2->next;
295 node2->next = node->next;
296 }
297 release_node_locked(node->parent);
298 node->parent = NULL;
299 node->next = NULL;
300 }
301}
302
303/* Gets the absolute path to a node into the provided buffer.
304 *
305 * Populates 'buf' with the path and returns the length of the path on success,
306 * or returns -1 if the path is too long for the provided buffer.
307 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700308static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
309 const char* name;
310 size_t namelen;
311 if (node->graft_path) {
312 name = node->graft_path;
313 namelen = node->graft_pathlen;
314 } else if (node->actual_name) {
315 name = node->actual_name;
316 namelen = node->namelen;
317 } else {
318 name = node->name;
319 namelen = node->namelen;
320 }
321
Jeff Brown6249b902012-05-26 14:32:54 -0700322 if (bufsize < namelen + 1) {
323 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700324 }
325
Jeff Brown6249b902012-05-26 14:32:54 -0700326 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700327 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700328 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
329 if (pathlen < 0) {
330 return -1;
331 }
332 buf[pathlen++] = '/';
333 }
334
Jeff Brown6249b902012-05-26 14:32:54 -0700335 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
336 return pathlen + namelen;
337}
338
339/* Finds the absolute path of a file within a given directory.
340 * Performs a case-insensitive search for the file and sets the buffer to the path
341 * of the first matching file. If 'search' is zero or if no match is found, sets
342 * the buffer to the path that the file would have, assuming the name were case-sensitive.
343 *
344 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
345 * or returns NULL if the path is too long for the provided buffer.
346 */
347static char* find_file_within(const char* path, const char* name,
348 char* buf, size_t bufsize, int search)
349{
350 size_t pathlen = strlen(path);
351 size_t namelen = strlen(name);
352 size_t childlen = pathlen + namelen + 1;
353 char* actual;
354
355 if (bufsize <= childlen) {
356 return NULL;
357 }
358
359 memcpy(buf, path, pathlen);
360 buf[pathlen] = '/';
361 actual = buf + pathlen + 1;
362 memcpy(actual, name, namelen + 1);
363
364 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800365 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700366 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800367 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700368 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700369 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800370 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800371 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700372 if (!strcasecmp(entry->d_name, name)) {
373 /* we have a match - replace the name, don't need to copy the null again */
374 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800375 break;
376 }
377 }
378 closedir(dir);
379 }
Jeff Brown6249b902012-05-26 14:32:54 -0700380 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800381}
382
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700383static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800384{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700385 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700386 attr->size = s->st_size;
387 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400388 attr->atime = s->st_atime;
389 attr->mtime = s->st_mtime;
390 attr->ctime = s->st_ctime;
391 attr->atimensec = s->st_atime_nsec;
392 attr->mtimensec = s->st_mtime_nsec;
393 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700394 attr->mode = s->st_mode;
395 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700396
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700397 attr->uid = node->uid;
398 attr->gid = node->gid;
399
400 /* Filter requested mode based on underlying file, and
401 * pass through file type. */
402 int owner_mode = s->st_mode & 0700;
403 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
404 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
405}
406
Jeff Sharkey44d63422013-09-12 09:44:48 -0700407static int touch(char* path, mode_t mode) {
408 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
409 if (fd == -1) {
410 if (errno == EEXIST) {
411 return 0;
412 } else {
413 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
414 return -1;
415 }
416 }
417 close(fd);
418 return 0;
419}
420
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700421static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
422 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700423 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700424
425 /* By default, each node inherits from its parent */
426 node->perm = PERM_INHERIT;
427 node->userid = parent->userid;
428 node->uid = parent->uid;
429 node->gid = parent->gid;
430 node->mode = parent->mode;
431
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700432 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700433 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700434 }
435
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700436 /* Derive custom permissions based on parent and current node */
437 switch (parent->perm) {
438 case PERM_INHERIT:
439 /* Already inherited above */
440 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700441 case PERM_LEGACY_PRE_ROOT:
442 /* Legacy internal layout places users at top level */
443 node->perm = PERM_ROOT;
444 node->userid = strtoul(node->name, NULL, 10);
445 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700446 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700447 /* Assume masked off by default. */
448 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700449 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700450 /* App-specific directories inside; let anyone traverse */
451 node->perm = PERM_ANDROID;
452 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700453 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700454 if (!strcasecmp(node->name, "DCIM")
455 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700456 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700457 } else if (!strcasecmp(node->name, "Alarms")
458 || !strcasecmp(node->name, "Movies")
459 || !strcasecmp(node->name, "Music")
460 || !strcasecmp(node->name, "Notifications")
461 || !strcasecmp(node->name, "Podcasts")
462 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700463 node->gid = AID_SDCARD_AV;
464 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700465 }
466 break;
467 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700468 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700469 /* App-specific directories inside; let anyone traverse */
470 node->perm = PERM_ANDROID_DATA;
471 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700472 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700473 /* App-specific directories inside; let anyone traverse */
474 node->perm = PERM_ANDROID_OBB;
475 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700476 /* Single OBB directory is always shared */
477 node->graft_path = fuse->obbpath;
478 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700479 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700480 /* User directories must only be accessible to system, protected
481 * by sdcard_all. Zygote will bind mount the appropriate user-
482 * specific path. */
483 node->perm = PERM_ANDROID_USER;
484 node->gid = AID_SDCARD_ALL;
485 node->mode = 0770;
486 }
487 break;
488 case PERM_ANDROID_DATA:
489 case PERM_ANDROID_OBB:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800490 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700491 if (appid != 0) {
492 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700493 }
494 node->mode = 0770;
495 break;
496 case PERM_ANDROID_USER:
497 /* Root of a secondary user */
498 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700499 node->userid = strtoul(node->name, NULL, 10);
500 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700501 node->mode = 0771;
502 break;
503 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700504}
505
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700506/* Return if the calling UID holds sdcard_rw. */
507static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700508 /* No additional permissions enforcement */
509 if (fuse->derive == DERIVE_NONE) {
510 return true;
511 }
512
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700513 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800514 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700515}
516
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700517/* Kernel has already enforced everything we returned through
518 * derive_permissions_locked(), so this is used to lock down access
519 * even further, such as enforcing that apps hold sdcard_rw. */
520static bool check_caller_access_to_name(struct fuse* fuse,
521 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700522 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700523 /* Always block security-sensitive files at root */
524 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700525 if (!strcasecmp(name, "autorun.inf")
526 || !strcasecmp(name, ".android_secure")
527 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700528 return false;
529 }
530 }
531
532 /* No additional permissions enforcement */
533 if (fuse->derive == DERIVE_NONE) {
534 return true;
535 }
536
Jeff Sharkey44d63422013-09-12 09:44:48 -0700537 /* Root always has access; access for any other UIDs should always
538 * be controlled through packages.list. */
539 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700540 return true;
541 }
542
543 /* If asking to write, verify that caller either owns the
544 * parent or holds sdcard_rw. */
545 if (mode & W_OK) {
546 if (parent_node && hdr->uid == parent_node->uid) {
547 return true;
548 }
549
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700550 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700551 }
552
553 /* No extra permissions to enforce */
554 return true;
555}
556
557static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700558 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
559 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700560}
561
Jeff Brown6249b902012-05-26 14:32:54 -0700562struct node *create_node_locked(struct fuse* fuse,
563 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700564{
565 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700566 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700567
Paul Eastham11ccdb32010-10-14 11:04:26 -0700568 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700569 if (!node) {
570 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700571 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700572 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700573 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700574 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700575 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700576 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700577 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700578 if (strcmp(name, actual_name)) {
579 node->actual_name = malloc(namelen + 1);
580 if (!node->actual_name) {
581 free(node->name);
582 free(node);
583 return NULL;
584 }
585 memcpy(node->actual_name, actual_name, namelen + 1);
586 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700587 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700588 node->nid = ptr_to_id(node);
589 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700590
591 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700592 acquire_node_locked(node);
593 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700594 return node;
595}
596
Jeff Brown6249b902012-05-26 14:32:54 -0700597static int rename_node_locked(struct node *node, const char *name,
598 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700599{
Jeff Brown6249b902012-05-26 14:32:54 -0700600 size_t namelen = strlen(name);
601 int need_actual_name = strcmp(name, actual_name);
602
603 /* make the storage bigger without actually changing the name
604 * in case an error occurs part way */
605 if (namelen > node->namelen) {
606 char* new_name = realloc(node->name, namelen + 1);
607 if (!new_name) {
608 return -ENOMEM;
609 }
610 node->name = new_name;
611 if (need_actual_name && node->actual_name) {
612 char* new_actual_name = realloc(node->actual_name, namelen + 1);
613 if (!new_actual_name) {
614 return -ENOMEM;
615 }
616 node->actual_name = new_actual_name;
617 }
618 }
619
620 /* update the name, taking care to allocate storage before overwriting the old name */
621 if (need_actual_name) {
622 if (!node->actual_name) {
623 node->actual_name = malloc(namelen + 1);
624 if (!node->actual_name) {
625 return -ENOMEM;
626 }
627 }
628 memcpy(node->actual_name, actual_name, namelen + 1);
629 } else {
630 free(node->actual_name);
631 node->actual_name = NULL;
632 }
633 memcpy(node->name, name, namelen + 1);
634 node->namelen = namelen;
635 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700636}
637
Jeff Brown6249b902012-05-26 14:32:54 -0700638static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700639{
Jeff Brown6249b902012-05-26 14:32:54 -0700640 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700641 return &fuse->root;
642 } else {
643 return id_to_ptr(nid);
644 }
645}
646
Jeff Brown6249b902012-05-26 14:32:54 -0700647static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
648 char* buf, size_t bufsize)
649{
650 struct node* node = lookup_node_by_id_locked(fuse, nid);
651 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
652 node = NULL;
653 }
654 return node;
655}
656
657static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700658{
659 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700660 /* use exact string comparison, nodes that differ by case
661 * must be considered distinct even if they refer to the same
662 * underlying file as otherwise operations such as "mv x x"
663 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700664 if (!strcmp(name, node->name)) {
665 return node;
666 }
667 }
668 return 0;
669}
670
Jeff Brown6249b902012-05-26 14:32:54 -0700671static struct node* acquire_or_create_child_locked(
672 struct fuse* fuse, struct node* parent,
673 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700674{
Jeff Brown6249b902012-05-26 14:32:54 -0700675 struct node* child = lookup_child_by_name_locked(parent, name);
676 if (child) {
677 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800678 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700679 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800680 }
Jeff Brown6249b902012-05-26 14:32:54 -0700681 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700682}
683
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700684static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700685 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700686 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700687
Jeff Brown6249b902012-05-26 14:32:54 -0700688 fuse->fd = fd;
689 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700690 fuse->derive = derive;
691 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700692 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700693
Jeff Brown6249b902012-05-26 14:32:54 -0700694 memset(&fuse->root, 0, sizeof(fuse->root));
695 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
696 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700697 fuse->root.namelen = strlen(source_path);
698 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700699 fuse->root.userid = 0;
700 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700701
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700702 /* Set up root node for various modes of operation */
703 switch (derive) {
704 case DERIVE_NONE:
705 /* Traditional behavior that treats entire device as being accessible
706 * to sdcard_rw, and no permissions are derived. */
707 fuse->root.perm = PERM_ROOT;
708 fuse->root.mode = 0775;
709 fuse->root.gid = AID_SDCARD_RW;
710 break;
711 case DERIVE_LEGACY:
712 /* Legacy behavior used to support internal multiuser layout which
713 * places user_id at the top directory level, with the actual roots
714 * just below that. Shared OBB path is also at top level. */
715 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
716 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700717 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700718 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700719 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
720 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700721 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700722 break;
723 case DERIVE_UNIFIED:
724 /* Unified multiuser layout which places secondary user_id under
725 * /Android/user and shared OBB path under /Android/obb. */
726 fuse->root.perm = PERM_ROOT;
727 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700728 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700729 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700730 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
731 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
732 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700733 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700734}
735
Jeff Brown6249b902012-05-26 14:32:54 -0700736static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700737{
738 struct fuse_out_header hdr;
739 hdr.len = sizeof(hdr);
740 hdr.error = err;
741 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700742 write(fuse->fd, &hdr, sizeof(hdr));
743}
744
Jeff Brown6249b902012-05-26 14:32:54 -0700745static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700746{
747 struct fuse_out_header hdr;
748 struct iovec vec[2];
749 int res;
750
751 hdr.len = len + sizeof(hdr);
752 hdr.error = 0;
753 hdr.unique = unique;
754
755 vec[0].iov_base = &hdr;
756 vec[0].iov_len = sizeof(hdr);
757 vec[1].iov_base = data;
758 vec[1].iov_len = len;
759
760 res = writev(fuse->fd, vec, 2);
761 if (res < 0) {
762 ERROR("*** REPLY FAILED *** %d\n", errno);
763 }
764}
765
Jeff Brown6249b902012-05-26 14:32:54 -0700766static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
767 struct node* parent, const char* name, const char* actual_name,
768 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700769{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700770 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700771 struct fuse_entry_out out;
772 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700773
Jeff Brown6249b902012-05-26 14:32:54 -0700774 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700775 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700776 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700777
Jeff Brown6249b902012-05-26 14:32:54 -0700778 pthread_mutex_lock(&fuse->lock);
779 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
780 if (!node) {
781 pthread_mutex_unlock(&fuse->lock);
782 return -ENOMEM;
783 }
784 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700785 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700786 out.attr_valid = 10;
787 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700788 out.nodeid = node->nid;
789 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700790 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700791 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700792 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700793}
794
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700795static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700796 const char* path)
797{
798 struct fuse_attr_out out;
799 struct stat s;
800
801 if (lstat(path, &s) < 0) {
802 return -errno;
803 }
804 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700805 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700806 out.attr_valid = 10;
807 fuse_reply(fuse, unique, &out, sizeof(out));
808 return NO_STATUS;
809}
810
811static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700812 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700813{
Jeff Brown6249b902012-05-26 14:32:54 -0700814 struct node* parent_node;
815 char parent_path[PATH_MAX];
816 char child_path[PATH_MAX];
817 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700818
Jeff Brown6249b902012-05-26 14:32:54 -0700819 pthread_mutex_lock(&fuse->lock);
820 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
821 parent_path, sizeof(parent_path));
822 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
823 parent_node ? parent_node->name : "?");
824 pthread_mutex_unlock(&fuse->lock);
825
826 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
827 child_path, sizeof(child_path), 1))) {
828 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700829 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700830 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700831 return -EACCES;
832 }
833
Jeff Brown6249b902012-05-26 14:32:54 -0700834 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700835}
836
Jeff Brown6249b902012-05-26 14:32:54 -0700837static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700838 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
839{
Jeff Brown6249b902012-05-26 14:32:54 -0700840 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700841
Jeff Brown6249b902012-05-26 14:32:54 -0700842 pthread_mutex_lock(&fuse->lock);
843 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
844 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
845 hdr->nodeid, node ? node->name : "?");
846 if (node) {
847 __u64 n = req->nlookup;
848 while (n--) {
849 release_node_locked(node);
850 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700851 }
Jeff Brown6249b902012-05-26 14:32:54 -0700852 pthread_mutex_unlock(&fuse->lock);
853 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700854}
855
Jeff Brown6249b902012-05-26 14:32:54 -0700856static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700857 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
858{
Jeff Brown6249b902012-05-26 14:32:54 -0700859 struct node* node;
860 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861
Jeff Brown6249b902012-05-26 14:32:54 -0700862 pthread_mutex_lock(&fuse->lock);
863 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
864 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
865 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
866 pthread_mutex_unlock(&fuse->lock);
867
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700869 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700870 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700871 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700872 return -EACCES;
873 }
874
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700875 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876}
877
Jeff Brown6249b902012-05-26 14:32:54 -0700878static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
880{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700881 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700882 struct node* node;
883 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700884 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885
Jeff Brown6249b902012-05-26 14:32:54 -0700886 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700887 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700888 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
889 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
890 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
891 pthread_mutex_unlock(&fuse->lock);
892
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700893 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700894 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700895 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700896 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700897 return -EACCES;
898 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700899
Jeff Brown6249b902012-05-26 14:32:54 -0700900 /* XXX: incomplete implementation on purpose.
901 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700902
Jeff Brown6249b902012-05-26 14:32:54 -0700903 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
904 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905 }
906
907 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
908 * are both set, then set it to the current time. Else, set it to the
909 * time specified in the request. Same goes for mtime. Use utimensat(2)
910 * as it allows ATIME and MTIME to be changed independently, and has
911 * nanosecond resolution which fuse also has.
912 */
913 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
914 times[0].tv_nsec = UTIME_OMIT;
915 times[1].tv_nsec = UTIME_OMIT;
916 if (req->valid & FATTR_ATIME) {
917 if (req->valid & FATTR_ATIME_NOW) {
918 times[0].tv_nsec = UTIME_NOW;
919 } else {
920 times[0].tv_sec = req->atime;
921 times[0].tv_nsec = req->atimensec;
922 }
923 }
924 if (req->valid & FATTR_MTIME) {
925 if (req->valid & FATTR_MTIME_NOW) {
926 times[1].tv_nsec = UTIME_NOW;
927 } else {
928 times[1].tv_sec = req->mtime;
929 times[1].tv_nsec = req->mtimensec;
930 }
931 }
Jeff Brown6249b902012-05-26 14:32:54 -0700932 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
933 handler->token, path, times[0].tv_sec, times[1].tv_sec);
934 if (utimensat(-1, path, times, 0) < 0) {
935 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700936 }
937 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700938 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700939}
940
Jeff Brown6249b902012-05-26 14:32:54 -0700941static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700942 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
943{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700944 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700945 struct node* parent_node;
946 char parent_path[PATH_MAX];
947 char child_path[PATH_MAX];
948 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700949
Jeff Brown6249b902012-05-26 14:32:54 -0700950 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700951 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700952 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
953 parent_path, sizeof(parent_path));
954 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
955 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
956 pthread_mutex_unlock(&fuse->lock);
957
958 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
959 child_path, sizeof(child_path), 1))) {
960 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700961 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700962 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700963 return -EACCES;
964 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700965 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700966 if (mknod(child_path, mode, req->rdev) < 0) {
967 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700968 }
Jeff Brown6249b902012-05-26 14:32:54 -0700969 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970}
971
Jeff Brown6249b902012-05-26 14:32:54 -0700972static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700973 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
974{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700975 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700976 struct node* parent_node;
977 char parent_path[PATH_MAX];
978 char child_path[PATH_MAX];
979 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980
Jeff Brown6249b902012-05-26 14:32:54 -0700981 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700982 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700983 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
984 parent_path, sizeof(parent_path));
985 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
986 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
987 pthread_mutex_unlock(&fuse->lock);
988
989 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
990 child_path, sizeof(child_path), 1))) {
991 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700992 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700993 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700994 return -EACCES;
995 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700997 if (mkdir(child_path, mode) < 0) {
998 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700999 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001000
1001 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1002 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1003 char nomedia[PATH_MAX];
1004 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1005 if (touch(nomedia, 0664) != 0) {
1006 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1007 return -ENOENT;
1008 }
1009 }
1010 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1011 char nomedia[PATH_MAX];
1012 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1013 if (touch(nomedia, 0664) != 0) {
1014 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1015 return -ENOENT;
1016 }
1017 }
1018
Jeff Brown6249b902012-05-26 14:32:54 -07001019 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001020}
1021
Jeff Brown6249b902012-05-26 14:32:54 -07001022static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001023 const struct fuse_in_header* hdr, const char* name)
1024{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001025 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001026 struct node* parent_node;
1027 char parent_path[PATH_MAX];
1028 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001029
Jeff Brown6249b902012-05-26 14:32:54 -07001030 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001031 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001032 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1033 parent_path, sizeof(parent_path));
1034 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1035 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1036 pthread_mutex_unlock(&fuse->lock);
1037
1038 if (!parent_node || !find_file_within(parent_path, name,
1039 child_path, sizeof(child_path), 1)) {
1040 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001041 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001042 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001043 return -EACCES;
1044 }
Jeff Brown6249b902012-05-26 14:32:54 -07001045 if (unlink(child_path) < 0) {
1046 return -errno;
1047 }
1048 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001049}
1050
Jeff Brown6249b902012-05-26 14:32:54 -07001051static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001052 const struct fuse_in_header* hdr, const char* name)
1053{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001054 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001055 struct node* parent_node;
1056 char parent_path[PATH_MAX];
1057 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001058
Jeff Brown6249b902012-05-26 14:32:54 -07001059 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001060 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001061 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1062 parent_path, sizeof(parent_path));
1063 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1064 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1065 pthread_mutex_unlock(&fuse->lock);
1066
1067 if (!parent_node || !find_file_within(parent_path, name,
1068 child_path, sizeof(child_path), 1)) {
1069 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001070 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001071 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001072 return -EACCES;
1073 }
Jeff Brown6249b902012-05-26 14:32:54 -07001074 if (rmdir(child_path) < 0) {
1075 return -errno;
1076 }
1077 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001078}
1079
Jeff Brown6249b902012-05-26 14:32:54 -07001080static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001081 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001082 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001083{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001084 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001085 struct node* old_parent_node;
1086 struct node* new_parent_node;
1087 struct node* child_node;
1088 char old_parent_path[PATH_MAX];
1089 char new_parent_path[PATH_MAX];
1090 char old_child_path[PATH_MAX];
1091 char new_child_path[PATH_MAX];
1092 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001093 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001094
Jeff Brown6249b902012-05-26 14:32:54 -07001095 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001096 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001097 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1098 old_parent_path, sizeof(old_parent_path));
1099 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1100 new_parent_path, sizeof(new_parent_path));
1101 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1102 old_name, new_name,
1103 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1104 req->newdir, new_parent_node ? new_parent_node->name : "?");
1105 if (!old_parent_node || !new_parent_node) {
1106 res = -ENOENT;
1107 goto lookup_error;
1108 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001109 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001110 res = -EACCES;
1111 goto lookup_error;
1112 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001113 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001114 res = -EACCES;
1115 goto lookup_error;
1116 }
Jeff Brown6249b902012-05-26 14:32:54 -07001117 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1118 if (!child_node || get_node_path_locked(child_node,
1119 old_child_path, sizeof(old_child_path)) < 0) {
1120 res = -ENOENT;
1121 goto lookup_error;
1122 }
1123 acquire_node_locked(child_node);
1124 pthread_mutex_unlock(&fuse->lock);
1125
1126 /* Special case for renaming a file where destination is same path
1127 * differing only by case. In this case we don't want to look for a case
1128 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1129 */
1130 int search = old_parent_node != new_parent_node
1131 || strcasecmp(old_name, new_name);
1132 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1133 new_child_path, sizeof(new_child_path), search))) {
1134 res = -ENOENT;
1135 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001136 }
1137
Jeff Brown6249b902012-05-26 14:32:54 -07001138 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1139 res = rename(old_child_path, new_child_path);
1140 if (res < 0) {
1141 res = -errno;
1142 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001143 }
1144
Jeff Brown6249b902012-05-26 14:32:54 -07001145 pthread_mutex_lock(&fuse->lock);
1146 res = rename_node_locked(child_node, new_name, new_actual_name);
1147 if (!res) {
1148 remove_node_from_parent_locked(child_node);
1149 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150 }
Jeff Brown6249b902012-05-26 14:32:54 -07001151 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001152
Jeff Brown6249b902012-05-26 14:32:54 -07001153io_error:
1154 pthread_mutex_lock(&fuse->lock);
1155done:
1156 release_node_locked(child_node);
1157lookup_error:
1158 pthread_mutex_unlock(&fuse->lock);
1159 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001160}
1161
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001162static int open_flags_to_access_mode(int open_flags) {
1163 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1164 return R_OK;
1165 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1166 return W_OK;
1167 } else {
1168 /* Probably O_RDRW, but treat as default to be safe */
1169 return R_OK | W_OK;
1170 }
1171}
1172
Jeff Brown6249b902012-05-26 14:32:54 -07001173static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001174 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1175{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001176 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001177 struct node* node;
1178 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001179 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001180 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001181
Jeff Brown6249b902012-05-26 14:32:54 -07001182 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001183 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001184 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1185 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1186 req->flags, hdr->nodeid, node ? node->name : "?");
1187 pthread_mutex_unlock(&fuse->lock);
1188
1189 if (!node) {
1190 return -ENOENT;
1191 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001192 if (!check_caller_access_to_node(fuse, hdr, node,
1193 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001194 return -EACCES;
1195 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001196 h = malloc(sizeof(*h));
1197 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001198 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199 }
Jeff Brown6249b902012-05-26 14:32:54 -07001200 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001201 h->fd = open(path, req->flags);
1202 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001204 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205 }
1206 out.fh = ptr_to_id(h);
1207 out.open_flags = 0;
1208 out.padding = 0;
1209 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001210 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211}
1212
Jeff Brown6249b902012-05-26 14:32:54 -07001213static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1215{
1216 struct handle *h = id_to_ptr(req->fh);
1217 __u64 unique = hdr->unique;
1218 __u32 size = req->size;
1219 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001220 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001221 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001222
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001223 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1224 * overlaps the request buffer and will clobber data in the request. This
1225 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001226
1227 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1228 h, h->fd, size, offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001229 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001230 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001231 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001232 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001233 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001234 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001235 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001236 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001237 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001238}
1239
Jeff Brown6249b902012-05-26 14:32:54 -07001240static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001241 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1242 const void* buffer)
1243{
1244 struct fuse_write_out out;
1245 struct handle *h = id_to_ptr(req->fh);
1246 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001247 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
1248
1249 if (req->flags & O_DIRECT) {
1250 memcpy(aligned_buffer, buffer, req->size);
1251 buffer = (const __u8*) aligned_buffer;
1252 }
Jeff Brown6249b902012-05-26 14:32:54 -07001253
1254 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1255 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001256 res = pwrite64(h->fd, buffer, req->size, req->offset);
1257 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001258 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001259 }
1260 out.size = res;
1261 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001262 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001263}
1264
Jeff Brown6249b902012-05-26 14:32:54 -07001265static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266 const struct fuse_in_header* hdr)
1267{
Jeff Brown6249b902012-05-26 14:32:54 -07001268 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269 struct statfs stat;
1270 struct fuse_statfs_out out;
1271 int res;
1272
Jeff Brown6249b902012-05-26 14:32:54 -07001273 pthread_mutex_lock(&fuse->lock);
1274 TRACE("[%d] STATFS\n", handler->token);
1275 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1276 pthread_mutex_unlock(&fuse->lock);
1277 if (res < 0) {
1278 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001279 }
Jeff Brown6249b902012-05-26 14:32:54 -07001280 if (statfs(fuse->root.name, &stat) < 0) {
1281 return -errno;
1282 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283 memset(&out, 0, sizeof(out));
1284 out.st.blocks = stat.f_blocks;
1285 out.st.bfree = stat.f_bfree;
1286 out.st.bavail = stat.f_bavail;
1287 out.st.files = stat.f_files;
1288 out.st.ffree = stat.f_ffree;
1289 out.st.bsize = stat.f_bsize;
1290 out.st.namelen = stat.f_namelen;
1291 out.st.frsize = stat.f_frsize;
1292 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001293 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001294}
1295
Jeff Brown6249b902012-05-26 14:32:54 -07001296static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1298{
1299 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001300
1301 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001302 close(h->fd);
1303 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001304 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001305}
1306
Jeff Brown6249b902012-05-26 14:32:54 -07001307static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1309{
1310 int is_data_sync = req->fsync_flags & 1;
1311 struct handle *h = id_to_ptr(req->fh);
1312 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001313
1314 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
1315 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001316 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
1317 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001318 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001319 }
Jeff Brown6249b902012-05-26 14:32:54 -07001320 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001321}
1322
Jeff Brown6249b902012-05-26 14:32:54 -07001323static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001324 const struct fuse_in_header* hdr)
1325{
Jeff Brown6249b902012-05-26 14:32:54 -07001326 TRACE("[%d] FLUSH\n", handler->token);
1327 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001328}
1329
Jeff Brown6249b902012-05-26 14:32:54 -07001330static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001331 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1332{
Jeff Brown6249b902012-05-26 14:32:54 -07001333 struct node* node;
1334 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001335 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001336 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337
Jeff Brown6249b902012-05-26 14:32:54 -07001338 pthread_mutex_lock(&fuse->lock);
1339 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1340 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1341 hdr->nodeid, node ? node->name : "?");
1342 pthread_mutex_unlock(&fuse->lock);
1343
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001345 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001347 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001348 return -EACCES;
1349 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001350 h = malloc(sizeof(*h));
1351 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001352 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001353 }
Jeff Brown6249b902012-05-26 14:32:54 -07001354 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001356 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001357 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001358 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 }
1360 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001361 out.open_flags = 0;
1362 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001363 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001364 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001365}
1366
Jeff Brown6249b902012-05-26 14:32:54 -07001367static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001368 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1369{
1370 char buffer[8192];
1371 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1372 struct dirent *de;
1373 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001374
1375 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376 if (req->offset == 0) {
1377 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001378 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001379 rewinddir(h->d);
1380 }
1381 de = readdir(h->d);
1382 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001383 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001384 }
1385 fde->ino = FUSE_UNKNOWN_INO;
1386 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1387 fde->off = req->offset + 1;
1388 fde->type = de->d_type;
1389 fde->namelen = strlen(de->d_name);
1390 memcpy(fde->name, de->d_name, fde->namelen + 1);
1391 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001392 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1393 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001394}
1395
Jeff Brown6249b902012-05-26 14:32:54 -07001396static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001397 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1398{
1399 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001400
1401 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001402 closedir(h->d);
1403 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001404 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001405}
1406
Jeff Brown6249b902012-05-26 14:32:54 -07001407static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001408 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1409{
1410 struct fuse_init_out out;
1411
Jeff Brown6249b902012-05-26 14:32:54 -07001412 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1413 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001414 out.major = FUSE_KERNEL_VERSION;
1415 out.minor = FUSE_KERNEL_MINOR_VERSION;
1416 out.max_readahead = req->max_readahead;
1417 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1418 out.max_background = 32;
1419 out.congestion_threshold = 32;
1420 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001421 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001422 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001423}
1424
Jeff Brown6249b902012-05-26 14:32:54 -07001425static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001426 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1427{
Brian Swetland03ee9472010-08-12 18:01:08 -07001428 switch (hdr->opcode) {
1429 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001430 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001431 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001432 }
Jeff Brown84715842012-05-25 14:07:47 -07001433
Brian Swetland03ee9472010-08-12 18:01:08 -07001434 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001435 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001436 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001437 }
Jeff Brown84715842012-05-25 14:07:47 -07001438
Brian Swetland03ee9472010-08-12 18:01:08 -07001439 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001440 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001441 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001442 }
Jeff Brown84715842012-05-25 14:07:47 -07001443
Brian Swetland03ee9472010-08-12 18:01:08 -07001444 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001445 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001446 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001447 }
Jeff Brown84715842012-05-25 14:07:47 -07001448
Brian Swetland03ee9472010-08-12 18:01:08 -07001449// case FUSE_READLINK:
1450// case FUSE_SYMLINK:
1451 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001452 const struct fuse_mknod_in *req = data;
1453 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001454 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001455 }
Jeff Brown84715842012-05-25 14:07:47 -07001456
Brian Swetland03ee9472010-08-12 18:01:08 -07001457 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001458 const struct fuse_mkdir_in *req = data;
1459 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001460 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001461 }
Jeff Brown84715842012-05-25 14:07:47 -07001462
Brian Swetland03ee9472010-08-12 18:01:08 -07001463 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001464 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001465 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001466 }
Jeff Brown84715842012-05-25 14:07:47 -07001467
Brian Swetland03ee9472010-08-12 18:01:08 -07001468 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001469 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001470 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001471 }
Jeff Brown84715842012-05-25 14:07:47 -07001472
Brian Swetland03ee9472010-08-12 18:01:08 -07001473 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001474 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001475 const char *old_name = ((const char*) data) + sizeof(*req);
1476 const char *new_name = old_name + strlen(old_name) + 1;
1477 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001478 }
Jeff Brown84715842012-05-25 14:07:47 -07001479
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001480// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001481 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001482 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001483 return handle_open(fuse, handler, hdr, req);
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_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001487 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001488 return handle_read(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_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001492 const struct fuse_write_in *req = data;
1493 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001494 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001495 }
Jeff Brown84715842012-05-25 14:07:47 -07001496
Mike Lockwood4553b082010-08-16 14:14:44 -04001497 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001498 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001499 }
Jeff Brown84715842012-05-25 14:07:47 -07001500
Brian Swetland03ee9472010-08-12 18:01:08 -07001501 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001502 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001503 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001504 }
Jeff Brown84715842012-05-25 14:07:47 -07001505
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001506 case FUSE_FSYNC:
1507 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001508 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001509 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001510 }
1511
Brian Swetland03ee9472010-08-12 18:01:08 -07001512// case FUSE_SETXATTR:
1513// case FUSE_GETXATTR:
1514// case FUSE_LISTXATTR:
1515// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001516 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001517 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001518 }
1519
Brian Swetland03ee9472010-08-12 18:01:08 -07001520 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001521 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001522 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 }
Jeff Brown84715842012-05-25 14:07:47 -07001524
Brian Swetland03ee9472010-08-12 18:01:08 -07001525 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001526 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001527 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001528 }
Jeff Brown84715842012-05-25 14:07:47 -07001529
Brian Swetland03ee9472010-08-12 18:01:08 -07001530 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001531 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001532 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001533 }
Jeff Brown84715842012-05-25 14:07:47 -07001534
Brian Swetland03ee9472010-08-12 18:01:08 -07001535 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001536 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001537 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001538 }
Jeff Brown84715842012-05-25 14:07:47 -07001539
Brian Swetland03ee9472010-08-12 18:01:08 -07001540 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001541 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1542 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1543 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001544 }
Jeff Brown84715842012-05-25 14:07:47 -07001545 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001546}
1547
Jeff Brown6249b902012-05-26 14:32:54 -07001548static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001549{
Jeff Brown6249b902012-05-26 14:32:54 -07001550 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001551 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001552 ssize_t len = read(fuse->fd,
1553 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001554 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001555 if (errno != EINTR) {
1556 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1557 }
1558 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001559 }
Jeff Brown84715842012-05-25 14:07:47 -07001560
1561 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001562 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1563 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001564 }
1565
Jeff Brown7729d242012-05-25 15:35:28 -07001566 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001567 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001568 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1569 handler->token, (size_t)len, hdr->len);
1570 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001571 }
1572
Jeff Brown7729d242012-05-25 15:35:28 -07001573 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001574 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001575 __u64 unique = hdr->unique;
1576 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001577
1578 /* We do not access the request again after this point because the underlying
1579 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001580
1581 if (res != NO_STATUS) {
1582 if (res) {
1583 TRACE("[%d] ERROR %d\n", handler->token, res);
1584 }
1585 fuse_status(fuse, unique, res);
1586 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001587 }
1588}
1589
Jeff Brown6249b902012-05-26 14:32:54 -07001590static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001591{
Jeff Brown6249b902012-05-26 14:32:54 -07001592 struct fuse_handler* handler = data;
1593 handle_fuse_requests(handler);
1594 return NULL;
1595}
1596
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001597static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001598 Hashmap* map = context;
1599 hashmapRemove(map, key);
1600 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001601 return true;
1602}
1603
1604static bool remove_int_to_null(void *key, void *value, void *context) {
1605 Hashmap* map = context;
1606 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001607 return true;
1608}
1609
1610static int read_package_list(struct fuse *fuse) {
1611 pthread_mutex_lock(&fuse->lock);
1612
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001613 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1614 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001615
1616 FILE* file = fopen(kPackagesListFile, "r");
1617 if (!file) {
1618 ERROR("failed to open package list: %s\n", strerror(errno));
1619 pthread_mutex_unlock(&fuse->lock);
1620 return -1;
1621 }
1622
1623 char buf[512];
1624 while (fgets(buf, sizeof(buf), file) != NULL) {
1625 char package_name[512];
1626 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001627 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001628
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001629 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1630 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001631 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001632
1633 char* token = strtok(gids, ",");
1634 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001635 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001636 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001637 break;
1638 }
1639 token = strtok(NULL, ",");
1640 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001641 }
1642 }
1643
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001644 TRACE("read_package_list: found %d packages, %d with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001645 hashmapSize(fuse->package_to_appid),
1646 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001647 fclose(file);
1648 pthread_mutex_unlock(&fuse->lock);
1649 return 0;
1650}
1651
1652static void watch_package_list(struct fuse* fuse) {
1653 struct inotify_event *event;
1654 char event_buf[512];
1655
1656 int nfd = inotify_init();
1657 if (nfd < 0) {
1658 ERROR("inotify_init failed: %s\n", strerror(errno));
1659 return;
1660 }
1661
1662 bool active = false;
1663 while (1) {
1664 if (!active) {
1665 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1666 if (res == -1) {
1667 if (errno == ENOENT || errno == EACCES) {
1668 /* Framework may not have created yet, sleep and retry */
1669 ERROR("missing packages.list; retrying\n");
1670 sleep(3);
1671 continue;
1672 } else {
1673 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1674 return;
1675 }
1676 }
1677
1678 /* Watch above will tell us about any future changes, so
1679 * read the current state. */
1680 if (read_package_list(fuse) == -1) {
1681 ERROR("read_package_list failed: %s\n", strerror(errno));
1682 return;
1683 }
1684 active = true;
1685 }
1686
1687 int event_pos = 0;
1688 int res = read(nfd, event_buf, sizeof(event_buf));
1689 if (res < (int) sizeof(*event)) {
1690 if (errno == EINTR)
1691 continue;
1692 ERROR("failed to read inotify event: %s\n", strerror(errno));
1693 return;
1694 }
1695
1696 while (res >= (int) sizeof(*event)) {
1697 int event_size;
1698 event = (struct inotify_event *) (event_buf + event_pos);
1699
1700 TRACE("inotify event: %08x\n", event->mask);
1701 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1702 /* Previously watched file was deleted, probably due to move
1703 * that swapped in new data; re-arm the watch and read. */
1704 active = false;
1705 }
1706
1707 event_size = sizeof(*event) + event->len;
1708 res -= event_size;
1709 event_pos += event_size;
1710 }
1711 }
1712}
1713
Jeff Brown6249b902012-05-26 14:32:54 -07001714static int ignite_fuse(struct fuse* fuse, int num_threads)
1715{
1716 struct fuse_handler* handlers;
1717 int i;
1718
1719 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1720 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001721 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001722 return -ENOMEM;
1723 }
1724
1725 for (i = 0; i < num_threads; i++) {
1726 handlers[i].fuse = fuse;
1727 handlers[i].token = i;
1728 }
1729
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001730 /* When deriving permissions, this thread is used to process inotify events,
1731 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001732 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001733 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001734 pthread_t thread;
1735 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1736 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001737 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001738 goto quit;
1739 }
1740 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001741
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001742 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001743 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001744 } else {
1745 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001746 }
1747
1748 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001749
1750 /* don't bother killing all of the other threads or freeing anything,
1751 * should never get here anyhow */
1752quit:
1753 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001754}
1755
Mike Lockwood4f35e622011-01-12 14:39:44 -05001756static int usage()
1757{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001758 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1759 " -u: specify UID to run as\n"
1760 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001761 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001762 " -t: specify number of threads to use (default %d)\n"
1763 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001764 " -l: derive file permissions based on legacy internal layout\n"
1765 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001766 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001767 return 1;
1768}
1769
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001770static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001771 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1772 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001773 int fd;
1774 char opts[256];
1775 int res;
1776 struct fuse fuse;
1777
1778 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001779 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001780
1781 fd = open("/dev/fuse", O_RDWR);
1782 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001783 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001784 return -1;
1785 }
1786
1787 snprintf(opts, sizeof(opts),
1788 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1789 fd, uid, gid);
1790
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001791 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001792 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001793 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1794 goto error;
1795 }
1796
1797 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1798 if (res < 0) {
1799 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001800 goto error;
1801 }
1802
1803 res = setgid(gid);
1804 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001805 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001806 goto error;
1807 }
1808
1809 res = setuid(uid);
1810 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001811 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001812 goto error;
1813 }
1814
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001815 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001816
1817 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001818 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001819
1820 /* we do not attempt to umount the file system here because we are no longer
1821 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001822
1823error:
1824 close(fd);
1825 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001826}
1827
Brian Swetland03ee9472010-08-12 18:01:08 -07001828int main(int argc, char **argv)
1829{
Brian Swetland03ee9472010-08-12 18:01:08 -07001830 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001831 const char *source_path = NULL;
1832 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001833 uid_t uid = 0;
1834 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001835 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001836 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001837 derive_t derive = DERIVE_NONE;
1838 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001839 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001840 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001841
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001842 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001843 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001844 switch (opt) {
1845 case 'u':
1846 uid = strtoul(optarg, NULL, 10);
1847 break;
1848 case 'g':
1849 gid = strtoul(optarg, NULL, 10);
1850 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001851 case 'w':
1852 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001853 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001854 case 't':
1855 num_threads = strtoul(optarg, NULL, 10);
1856 break;
1857 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001858 derive = DERIVE_UNIFIED;
1859 break;
1860 case 'l':
1861 derive = DERIVE_LEGACY;
1862 break;
1863 case 's':
1864 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001865 break;
1866 case '?':
1867 default:
1868 return usage();
1869 }
1870 }
1871
1872 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001873 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001874 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001875 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001876 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001877 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001878 } else if (!uid) {
1879 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001880 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001881 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001882 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001883 ERROR("too many arguments\n");
1884 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001885 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001886 }
1887
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001888 if (!source_path) {
1889 ERROR("no source path specified\n");
1890 return usage();
1891 }
1892 if (!dest_path) {
1893 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001894 return usage();
1895 }
Jeff Brown26567352012-05-25 13:27:43 -07001896 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001897 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001898 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001899 }
Jeff Brown6249b902012-05-26 14:32:54 -07001900 if (num_threads < 1) {
1901 ERROR("number of threads must be at least 1\n");
1902 return usage();
1903 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001904 if (split_perms && derive == DERIVE_NONE) {
1905 ERROR("cannot split permissions without deriving\n");
1906 return usage();
1907 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001908
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001909 rlim.rlim_cur = 8192;
1910 rlim.rlim_max = 8192;
1911 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1912 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1913 }
1914
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001915 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001916 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001917}