blob: 844ca656cd7faeb139a8a45d3f7e22ac73456c3a [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>
23#include <limits.h>
24#include <linux/fuse.h>
25#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070029#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070030#include <sys/mount.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070031#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070032#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040033#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080034#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070035#include <sys/uio.h>
36#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070037
Jeff Sharkey44d63422013-09-12 09:44:48 -070038#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070039#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070040#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070042
Brian Swetlandb14a2c62010-08-12 18:21:12 -070043#include <private/android_filesystem_config.h>
44
Brian Swetland03ee9472010-08-12 18:01:08 -070045/* README
46 *
47 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070048 *
Brian Swetland03ee9472010-08-12 18:01:08 -070049 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070050 * directory permissions (all files are given fixed owner, group, and
51 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070052 * changeable, symlinks and hardlinks are not createable, etc.
53 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070054 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070055 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070056 * It must be run as root, but will drop to requested UID/GID as soon as it
57 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070058 *
59 * Things I believe to be true:
60 *
61 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
62 * CREAT) must bump that node's refcount
63 * - don't forget that FORGET can forget multiple references (req->nlookup)
64 * - if an op that returns a fuse_entry fails writing the reply to the
65 * kernel, you must rollback the refcount to reflect the reference the
66 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070067 *
68 * This daemon can also derive custom filesystem permissions based on directory
69 * structure when requested. These custom permissions support several features:
70 *
71 * - Apps can access their own files in /Android/data/com.example/ without
72 * requiring any additional GIDs.
73 * - Separate permissions for protecting directories like Pictures and Music.
74 * - Multi-user separation on the same physical device.
75 *
76 * The derived permissions look like this:
77 *
78 * rwxrwx--x root:sdcard_rw /
79 * rwxrwx--- root:sdcard_pics /Pictures
80 * rwxrwx--- root:sdcard_av /Music
81 *
82 * rwxrwx--x root:sdcard_rw /Android
83 * rwxrwx--x root:sdcard_rw /Android/data
84 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
85 * rwxrwx--x root:sdcard_rw /Android/obb/
86 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
87 *
88 * rwxrwx--- root:sdcard_all /Android/user
89 * rwxrwx--x root:sdcard_rw /Android/user/10
90 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070091 */
92
93#define FUSE_TRACE 0
94
95#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070096#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070097#else
98#define TRACE(x...) do {} while (0)
99#endif
100
Elliott Hughes300d5642014-07-08 13:53:26 -0700101#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -0700102
103#define FUSE_UNKNOWN_INO 0xffffffff
104
Jeff Brown84715842012-05-25 14:07:47 -0700105/* Maximum number of bytes to write in one request. */
106#define MAX_WRITE (256 * 1024)
107
108/* Maximum number of bytes to read in one request. */
109#define MAX_READ (128 * 1024)
110
111/* Largest possible request.
112 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
113 * the largest possible data payload. */
114#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
115
Jeff Brown6249b902012-05-26 14:32:54 -0700116/* Default number of threads. */
117#define DEFAULT_NUM_THREADS 2
118
119/* Pseudo-error constant used to indicate that no fuse status is needed
120 * or that a reply has already been written. */
121#define NO_STATUS 1
122
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700123/* Path to system-provided mapping of package name to appIds */
124static const char* const kPackagesListFile = "/data/system/packages.list";
125
126/* Supplementary groups to execute with */
127static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
128
129/* Permission mode for a specific node. Controls how file permissions
130 * are derived for children nodes. */
131typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700132 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700133 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700134 /* This node is one level above a normal root; used for legacy layouts
135 * which use the first level to represent user_id. */
136 PERM_LEGACY_PRE_ROOT,
137 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700138 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700139 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700140 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700141 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700142 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700143 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700144 PERM_ANDROID_OBB,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700145 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700146 PERM_ANDROID_USER,
147} perm_t;
148
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700149/* Permissions structure to derive */
150typedef enum {
151 DERIVE_NONE,
152 DERIVE_LEGACY,
153 DERIVE_UNIFIED,
154} derive_t;
155
Brian Swetland03ee9472010-08-12 18:01:08 -0700156struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700157 int fd;
158};
159
160struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700161 DIR *d;
162};
163
164struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700165 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700166 __u64 nid;
167 __u64 gen;
168
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700169 /* State derived based on current position in hierarchy. */
170 perm_t perm;
171 userid_t userid;
172 uid_t uid;
173 gid_t gid;
174 mode_t mode;
175
Paul Eastham11ccdb32010-10-14 11:04:26 -0700176 struct node *next; /* per-dir sibling list */
177 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700178 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700179
Jeff Brown6249b902012-05-26 14:32:54 -0700180 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700181 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800182 /* If non-null, this is the real name of the file in the underlying storage.
183 * This may differ from the field "name" only by case.
184 * strlen(actual_name) will always equal strlen(name), so it is safe to use
185 * namelen for both fields.
186 */
187 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700188
189 /* If non-null, an exact underlying path that should be grafted into this
190 * position. Used to support things like OBB. */
191 char* graft_path;
192 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700193};
194
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700195static int str_hash(void *key) {
196 return hashmapHash(key, strlen(key));
197}
198
Jeff Sharkey44d63422013-09-12 09:44:48 -0700199/** Test if two string keys are equal ignoring case */
200static bool str_icase_equals(void *keyA, void *keyB) {
201 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700202}
203
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700204static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800205 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700206}
207
208static bool int_equals(void *keyA, void *keyB) {
209 return keyA == keyB;
210}
211
Jeff Brown7729d242012-05-25 15:35:28 -0700212/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700213struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700214 pthread_mutex_t lock;
215
Brian Swetland03ee9472010-08-12 18:01:08 -0700216 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700217 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700218 derive_t derive;
219 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700220 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700221 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700222 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700223
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700224 Hashmap* package_to_appid;
225 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700226};
227
Jeff Brown7729d242012-05-25 15:35:28 -0700228/* Private data used by a single fuse handler. */
229struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700230 struct fuse* fuse;
231 int token;
232
Jeff Brown7729d242012-05-25 15:35:28 -0700233 /* To save memory, we never use the contents of the request buffer and the read
234 * buffer at the same time. This allows us to share the underlying storage. */
235 union {
236 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800237 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700238 };
239};
Brian Swetland03ee9472010-08-12 18:01:08 -0700240
Jeff Brown6249b902012-05-26 14:32:54 -0700241static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700242{
Jeff Brown6249b902012-05-26 14:32:54 -0700243 return (void *) (uintptr_t) nid;
244}
Brian Swetland03ee9472010-08-12 18:01:08 -0700245
Jeff Brown6249b902012-05-26 14:32:54 -0700246static inline __u64 ptr_to_id(void *ptr)
247{
248 return (__u64) (uintptr_t) ptr;
249}
Brian Swetland03ee9472010-08-12 18:01:08 -0700250
Jeff Brown6249b902012-05-26 14:32:54 -0700251static void acquire_node_locked(struct node* node)
252{
253 node->refcount++;
254 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
255}
256
257static void remove_node_from_parent_locked(struct node* node);
258
259static void release_node_locked(struct node* node)
260{
261 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
262 if (node->refcount > 0) {
263 node->refcount--;
264 if (!node->refcount) {
265 TRACE("DESTROY %p (%s)\n", node, node->name);
266 remove_node_from_parent_locked(node);
267
268 /* TODO: remove debugging - poison memory */
269 memset(node->name, 0xef, node->namelen);
270 free(node->name);
271 free(node->actual_name);
272 memset(node, 0xfc, sizeof(*node));
273 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800274 }
Jeff Brown6249b902012-05-26 14:32:54 -0700275 } else {
276 ERROR("Zero refcnt %p\n", node);
277 }
278}
279
280static void add_node_to_parent_locked(struct node *node, struct node *parent) {
281 node->parent = parent;
282 node->next = parent->child;
283 parent->child = node;
284 acquire_node_locked(parent);
285}
286
287static void remove_node_from_parent_locked(struct node* node)
288{
289 if (node->parent) {
290 if (node->parent->child == node) {
291 node->parent->child = node->parent->child->next;
292 } else {
293 struct node *node2;
294 node2 = node->parent->child;
295 while (node2->next != node)
296 node2 = node2->next;
297 node2->next = node->next;
298 }
299 release_node_locked(node->parent);
300 node->parent = NULL;
301 node->next = NULL;
302 }
303}
304
305/* Gets the absolute path to a node into the provided buffer.
306 *
307 * Populates 'buf' with the path and returns the length of the path on success,
308 * or returns -1 if the path is too long for the provided buffer.
309 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700310static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
311 const char* name;
312 size_t namelen;
313 if (node->graft_path) {
314 name = node->graft_path;
315 namelen = node->graft_pathlen;
316 } else if (node->actual_name) {
317 name = node->actual_name;
318 namelen = node->namelen;
319 } else {
320 name = node->name;
321 namelen = node->namelen;
322 }
323
Jeff Brown6249b902012-05-26 14:32:54 -0700324 if (bufsize < namelen + 1) {
325 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700326 }
327
Jeff Brown6249b902012-05-26 14:32:54 -0700328 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700329 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700330 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
331 if (pathlen < 0) {
332 return -1;
333 }
334 buf[pathlen++] = '/';
335 }
336
Jeff Brown6249b902012-05-26 14:32:54 -0700337 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
338 return pathlen + namelen;
339}
340
341/* Finds the absolute path of a file within a given directory.
342 * Performs a case-insensitive search for the file and sets the buffer to the path
343 * of the first matching file. If 'search' is zero or if no match is found, sets
344 * the buffer to the path that the file would have, assuming the name were case-sensitive.
345 *
346 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
347 * or returns NULL if the path is too long for the provided buffer.
348 */
349static char* find_file_within(const char* path, const char* name,
350 char* buf, size_t bufsize, int search)
351{
352 size_t pathlen = strlen(path);
353 size_t namelen = strlen(name);
354 size_t childlen = pathlen + namelen + 1;
355 char* actual;
356
357 if (bufsize <= childlen) {
358 return NULL;
359 }
360
361 memcpy(buf, path, pathlen);
362 buf[pathlen] = '/';
363 actual = buf + pathlen + 1;
364 memcpy(actual, name, namelen + 1);
365
366 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800367 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700368 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800369 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700370 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700371 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800372 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800373 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700374 if (!strcasecmp(entry->d_name, name)) {
375 /* we have a match - replace the name, don't need to copy the null again */
376 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800377 break;
378 }
379 }
380 closedir(dir);
381 }
Jeff Brown6249b902012-05-26 14:32:54 -0700382 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800383}
384
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700385static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800386{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700387 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700388 attr->size = s->st_size;
389 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400390 attr->atime = s->st_atime;
391 attr->mtime = s->st_mtime;
392 attr->ctime = s->st_ctime;
393 attr->atimensec = s->st_atime_nsec;
394 attr->mtimensec = s->st_mtime_nsec;
395 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700396 attr->mode = s->st_mode;
397 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700398
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700399 attr->uid = node->uid;
400 attr->gid = node->gid;
401
402 /* Filter requested mode based on underlying file, and
403 * pass through file type. */
404 int owner_mode = s->st_mode & 0700;
405 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
406 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
407}
408
Jeff Sharkey44d63422013-09-12 09:44:48 -0700409static int touch(char* path, mode_t mode) {
410 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
411 if (fd == -1) {
412 if (errno == EEXIST) {
413 return 0;
414 } else {
415 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
416 return -1;
417 }
418 }
419 close(fd);
420 return 0;
421}
422
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700423static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
424 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700425 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700426
427 /* By default, each node inherits from its parent */
428 node->perm = PERM_INHERIT;
429 node->userid = parent->userid;
430 node->uid = parent->uid;
431 node->gid = parent->gid;
432 node->mode = parent->mode;
433
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700434 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700435 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700436 }
437
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700438 /* Derive custom permissions based on parent and current node */
439 switch (parent->perm) {
440 case PERM_INHERIT:
441 /* Already inherited above */
442 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700443 case PERM_LEGACY_PRE_ROOT:
444 /* Legacy internal layout places users at top level */
445 node->perm = PERM_ROOT;
446 node->userid = strtoul(node->name, NULL, 10);
447 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700448 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700449 /* Assume masked off by default. */
450 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700451 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700452 /* App-specific directories inside; let anyone traverse */
453 node->perm = PERM_ANDROID;
454 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700455 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700456 if (!strcasecmp(node->name, "DCIM")
457 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700458 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700459 } else if (!strcasecmp(node->name, "Alarms")
460 || !strcasecmp(node->name, "Movies")
461 || !strcasecmp(node->name, "Music")
462 || !strcasecmp(node->name, "Notifications")
463 || !strcasecmp(node->name, "Podcasts")
464 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700465 node->gid = AID_SDCARD_AV;
466 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700467 }
468 break;
469 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700470 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700471 /* App-specific directories inside; let anyone traverse */
472 node->perm = PERM_ANDROID_DATA;
473 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700474 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700475 /* App-specific directories inside; let anyone traverse */
476 node->perm = PERM_ANDROID_OBB;
477 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700478 /* Single OBB directory is always shared */
479 node->graft_path = fuse->obbpath;
480 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700481 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700482 /* User directories must only be accessible to system, protected
483 * by sdcard_all. Zygote will bind mount the appropriate user-
484 * specific path. */
485 node->perm = PERM_ANDROID_USER;
486 node->gid = AID_SDCARD_ALL;
487 node->mode = 0770;
488 }
489 break;
490 case PERM_ANDROID_DATA:
491 case PERM_ANDROID_OBB:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800492 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700493 if (appid != 0) {
494 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700495 }
496 node->mode = 0770;
497 break;
498 case PERM_ANDROID_USER:
499 /* Root of a secondary user */
500 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700501 node->userid = strtoul(node->name, NULL, 10);
502 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700503 node->mode = 0771;
504 break;
505 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700506}
507
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700508/* Return if the calling UID holds sdcard_rw. */
509static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700510 /* No additional permissions enforcement */
511 if (fuse->derive == DERIVE_NONE) {
512 return true;
513 }
514
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700515 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800516 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700517}
518
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700519/* Kernel has already enforced everything we returned through
520 * derive_permissions_locked(), so this is used to lock down access
521 * even further, such as enforcing that apps hold sdcard_rw. */
522static bool check_caller_access_to_name(struct fuse* fuse,
523 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700524 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700525 /* Always block security-sensitive files at root */
526 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700527 if (!strcasecmp(name, "autorun.inf")
528 || !strcasecmp(name, ".android_secure")
529 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700530 return false;
531 }
532 }
533
534 /* No additional permissions enforcement */
535 if (fuse->derive == DERIVE_NONE) {
536 return true;
537 }
538
Jeff Sharkey44d63422013-09-12 09:44:48 -0700539 /* Root always has access; access for any other UIDs should always
540 * be controlled through packages.list. */
541 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700542 return true;
543 }
544
545 /* If asking to write, verify that caller either owns the
546 * parent or holds sdcard_rw. */
547 if (mode & W_OK) {
548 if (parent_node && hdr->uid == parent_node->uid) {
549 return true;
550 }
551
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700552 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700553 }
554
555 /* No extra permissions to enforce */
556 return true;
557}
558
559static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700560 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
561 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700562}
563
Jeff Brown6249b902012-05-26 14:32:54 -0700564struct node *create_node_locked(struct fuse* fuse,
565 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700566{
567 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700568 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700569
Paul Eastham11ccdb32010-10-14 11:04:26 -0700570 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700571 if (!node) {
572 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700573 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700574 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700575 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700576 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700577 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700578 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700579 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700580 if (strcmp(name, actual_name)) {
581 node->actual_name = malloc(namelen + 1);
582 if (!node->actual_name) {
583 free(node->name);
584 free(node);
585 return NULL;
586 }
587 memcpy(node->actual_name, actual_name, namelen + 1);
588 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700589 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700590 node->nid = ptr_to_id(node);
591 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700592
593 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700594 acquire_node_locked(node);
595 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700596 return node;
597}
598
Jeff Brown6249b902012-05-26 14:32:54 -0700599static int rename_node_locked(struct node *node, const char *name,
600 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700601{
Jeff Brown6249b902012-05-26 14:32:54 -0700602 size_t namelen = strlen(name);
603 int need_actual_name = strcmp(name, actual_name);
604
605 /* make the storage bigger without actually changing the name
606 * in case an error occurs part way */
607 if (namelen > node->namelen) {
608 char* new_name = realloc(node->name, namelen + 1);
609 if (!new_name) {
610 return -ENOMEM;
611 }
612 node->name = new_name;
613 if (need_actual_name && node->actual_name) {
614 char* new_actual_name = realloc(node->actual_name, namelen + 1);
615 if (!new_actual_name) {
616 return -ENOMEM;
617 }
618 node->actual_name = new_actual_name;
619 }
620 }
621
622 /* update the name, taking care to allocate storage before overwriting the old name */
623 if (need_actual_name) {
624 if (!node->actual_name) {
625 node->actual_name = malloc(namelen + 1);
626 if (!node->actual_name) {
627 return -ENOMEM;
628 }
629 }
630 memcpy(node->actual_name, actual_name, namelen + 1);
631 } else {
632 free(node->actual_name);
633 node->actual_name = NULL;
634 }
635 memcpy(node->name, name, namelen + 1);
636 node->namelen = namelen;
637 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700638}
639
Jeff Brown6249b902012-05-26 14:32:54 -0700640static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700641{
Jeff Brown6249b902012-05-26 14:32:54 -0700642 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700643 return &fuse->root;
644 } else {
645 return id_to_ptr(nid);
646 }
647}
648
Jeff Brown6249b902012-05-26 14:32:54 -0700649static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
650 char* buf, size_t bufsize)
651{
652 struct node* node = lookup_node_by_id_locked(fuse, nid);
653 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
654 node = NULL;
655 }
656 return node;
657}
658
659static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700660{
661 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700662 /* use exact string comparison, nodes that differ by case
663 * must be considered distinct even if they refer to the same
664 * underlying file as otherwise operations such as "mv x x"
665 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700666 if (!strcmp(name, node->name)) {
667 return node;
668 }
669 }
670 return 0;
671}
672
Jeff Brown6249b902012-05-26 14:32:54 -0700673static struct node* acquire_or_create_child_locked(
674 struct fuse* fuse, struct node* parent,
675 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700676{
Jeff Brown6249b902012-05-26 14:32:54 -0700677 struct node* child = lookup_child_by_name_locked(parent, name);
678 if (child) {
679 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800680 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700681 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800682 }
Jeff Brown6249b902012-05-26 14:32:54 -0700683 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700684}
685
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700686static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700687 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700688 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700689
Jeff Brown6249b902012-05-26 14:32:54 -0700690 fuse->fd = fd;
691 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700692 fuse->derive = derive;
693 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700694 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700695
Jeff Brown6249b902012-05-26 14:32:54 -0700696 memset(&fuse->root, 0, sizeof(fuse->root));
697 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
698 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700699 fuse->root.namelen = strlen(source_path);
700 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700701 fuse->root.userid = 0;
702 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700703
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700704 /* Set up root node for various modes of operation */
705 switch (derive) {
706 case DERIVE_NONE:
707 /* Traditional behavior that treats entire device as being accessible
708 * to sdcard_rw, and no permissions are derived. */
709 fuse->root.perm = PERM_ROOT;
710 fuse->root.mode = 0775;
711 fuse->root.gid = AID_SDCARD_RW;
712 break;
713 case DERIVE_LEGACY:
714 /* Legacy behavior used to support internal multiuser layout which
715 * places user_id at the top directory level, with the actual roots
716 * just below that. Shared OBB path is also at top level. */
717 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
718 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700719 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700720 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700721 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
722 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700723 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700724 break;
725 case DERIVE_UNIFIED:
726 /* Unified multiuser layout which places secondary user_id under
727 * /Android/user and shared OBB path under /Android/obb. */
728 fuse->root.perm = PERM_ROOT;
729 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700730 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700731 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700732 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
733 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
734 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700735 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700736}
737
Jeff Brown6249b902012-05-26 14:32:54 -0700738static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700739{
740 struct fuse_out_header hdr;
741 hdr.len = sizeof(hdr);
742 hdr.error = err;
743 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700744 write(fuse->fd, &hdr, sizeof(hdr));
745}
746
Jeff Brown6249b902012-05-26 14:32:54 -0700747static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700748{
749 struct fuse_out_header hdr;
750 struct iovec vec[2];
751 int res;
752
753 hdr.len = len + sizeof(hdr);
754 hdr.error = 0;
755 hdr.unique = unique;
756
757 vec[0].iov_base = &hdr;
758 vec[0].iov_len = sizeof(hdr);
759 vec[1].iov_base = data;
760 vec[1].iov_len = len;
761
762 res = writev(fuse->fd, vec, 2);
763 if (res < 0) {
764 ERROR("*** REPLY FAILED *** %d\n", errno);
765 }
766}
767
Jeff Brown6249b902012-05-26 14:32:54 -0700768static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
769 struct node* parent, const char* name, const char* actual_name,
770 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700771{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700772 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700773 struct fuse_entry_out out;
774 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700775
Jeff Brown6249b902012-05-26 14:32:54 -0700776 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700777 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700778 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700779
Jeff Brown6249b902012-05-26 14:32:54 -0700780 pthread_mutex_lock(&fuse->lock);
781 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
782 if (!node) {
783 pthread_mutex_unlock(&fuse->lock);
784 return -ENOMEM;
785 }
786 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700787 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700788 out.attr_valid = 10;
789 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700790 out.nodeid = node->nid;
791 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700792 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700793 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700794 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700795}
796
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700797static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700798 const char* path)
799{
800 struct fuse_attr_out out;
801 struct stat s;
802
803 if (lstat(path, &s) < 0) {
804 return -errno;
805 }
806 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700807 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700808 out.attr_valid = 10;
809 fuse_reply(fuse, unique, &out, sizeof(out));
810 return NO_STATUS;
811}
812
813static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700814 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700815{
Jeff Brown6249b902012-05-26 14:32:54 -0700816 struct node* parent_node;
817 char parent_path[PATH_MAX];
818 char child_path[PATH_MAX];
819 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700820
Jeff Brown6249b902012-05-26 14:32:54 -0700821 pthread_mutex_lock(&fuse->lock);
822 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
823 parent_path, sizeof(parent_path));
824 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
825 parent_node ? parent_node->name : "?");
826 pthread_mutex_unlock(&fuse->lock);
827
828 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
829 child_path, sizeof(child_path), 1))) {
830 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700831 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700832 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700833 return -EACCES;
834 }
835
Jeff Brown6249b902012-05-26 14:32:54 -0700836 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700837}
838
Jeff Brown6249b902012-05-26 14:32:54 -0700839static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700840 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
841{
Jeff Brown6249b902012-05-26 14:32:54 -0700842 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700843
Jeff Brown6249b902012-05-26 14:32:54 -0700844 pthread_mutex_lock(&fuse->lock);
845 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
846 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
847 hdr->nodeid, node ? node->name : "?");
848 if (node) {
849 __u64 n = req->nlookup;
850 while (n--) {
851 release_node_locked(node);
852 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700853 }
Jeff Brown6249b902012-05-26 14:32:54 -0700854 pthread_mutex_unlock(&fuse->lock);
855 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700856}
857
Jeff Brown6249b902012-05-26 14:32:54 -0700858static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700859 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
860{
Jeff Brown6249b902012-05-26 14:32:54 -0700861 struct node* node;
862 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700863
Jeff Brown6249b902012-05-26 14:32:54 -0700864 pthread_mutex_lock(&fuse->lock);
865 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
866 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
867 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
868 pthread_mutex_unlock(&fuse->lock);
869
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700870 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700871 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700872 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700873 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700874 return -EACCES;
875 }
876
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700877 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700878}
879
Jeff Brown6249b902012-05-26 14:32:54 -0700880static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
882{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700883 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700884 struct node* node;
885 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700887
Jeff Brown6249b902012-05-26 14:32:54 -0700888 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700889 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700890 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
891 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
892 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
893 pthread_mutex_unlock(&fuse->lock);
894
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700895 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700896 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700898 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700899 return -EACCES;
900 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700901
Jeff Brown6249b902012-05-26 14:32:54 -0700902 /* XXX: incomplete implementation on purpose.
903 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700904
Jeff Brown6249b902012-05-26 14:32:54 -0700905 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
906 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700907 }
908
909 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
910 * are both set, then set it to the current time. Else, set it to the
911 * time specified in the request. Same goes for mtime. Use utimensat(2)
912 * as it allows ATIME and MTIME to be changed independently, and has
913 * nanosecond resolution which fuse also has.
914 */
915 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
916 times[0].tv_nsec = UTIME_OMIT;
917 times[1].tv_nsec = UTIME_OMIT;
918 if (req->valid & FATTR_ATIME) {
919 if (req->valid & FATTR_ATIME_NOW) {
920 times[0].tv_nsec = UTIME_NOW;
921 } else {
922 times[0].tv_sec = req->atime;
923 times[0].tv_nsec = req->atimensec;
924 }
925 }
926 if (req->valid & FATTR_MTIME) {
927 if (req->valid & FATTR_MTIME_NOW) {
928 times[1].tv_nsec = UTIME_NOW;
929 } else {
930 times[1].tv_sec = req->mtime;
931 times[1].tv_nsec = req->mtimensec;
932 }
933 }
Jeff Brown6249b902012-05-26 14:32:54 -0700934 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
935 handler->token, path, times[0].tv_sec, times[1].tv_sec);
936 if (utimensat(-1, path, times, 0) < 0) {
937 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700938 }
939 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700940 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941}
942
Jeff Brown6249b902012-05-26 14:32:54 -0700943static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700944 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
945{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700946 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700947 struct node* parent_node;
948 char parent_path[PATH_MAX];
949 char child_path[PATH_MAX];
950 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951
Jeff Brown6249b902012-05-26 14:32:54 -0700952 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700953 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700954 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
955 parent_path, sizeof(parent_path));
956 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
957 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
958 pthread_mutex_unlock(&fuse->lock);
959
960 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
961 child_path, sizeof(child_path), 1))) {
962 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700963 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700964 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700965 return -EACCES;
966 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700968 if (mknod(child_path, mode, req->rdev) < 0) {
969 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970 }
Jeff Brown6249b902012-05-26 14:32:54 -0700971 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700972}
973
Jeff Brown6249b902012-05-26 14:32:54 -0700974static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700975 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
976{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700977 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700978 struct node* parent_node;
979 char parent_path[PATH_MAX];
980 char child_path[PATH_MAX];
981 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700982
Jeff Brown6249b902012-05-26 14:32:54 -0700983 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700984 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700985 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
986 parent_path, sizeof(parent_path));
987 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
988 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
989 pthread_mutex_unlock(&fuse->lock);
990
991 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
992 child_path, sizeof(child_path), 1))) {
993 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700994 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700995 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700996 return -EACCES;
997 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700998 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700999 if (mkdir(child_path, mode) < 0) {
1000 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001002
1003 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1004 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1005 char nomedia[PATH_MAX];
1006 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1007 if (touch(nomedia, 0664) != 0) {
1008 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1009 return -ENOENT;
1010 }
1011 }
1012 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1013 char nomedia[PATH_MAX];
1014 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1015 if (touch(nomedia, 0664) != 0) {
1016 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1017 return -ENOENT;
1018 }
1019 }
1020
Jeff Brown6249b902012-05-26 14:32:54 -07001021 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001022}
1023
Jeff Brown6249b902012-05-26 14:32:54 -07001024static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001025 const struct fuse_in_header* hdr, const char* name)
1026{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001027 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001028 struct node* parent_node;
1029 char parent_path[PATH_MAX];
1030 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001031
Jeff Brown6249b902012-05-26 14:32:54 -07001032 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001033 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001034 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1035 parent_path, sizeof(parent_path));
1036 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1037 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1038 pthread_mutex_unlock(&fuse->lock);
1039
1040 if (!parent_node || !find_file_within(parent_path, name,
1041 child_path, sizeof(child_path), 1)) {
1042 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001043 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001044 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001045 return -EACCES;
1046 }
Jeff Brown6249b902012-05-26 14:32:54 -07001047 if (unlink(child_path) < 0) {
1048 return -errno;
1049 }
1050 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001051}
1052
Jeff Brown6249b902012-05-26 14:32:54 -07001053static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001054 const struct fuse_in_header* hdr, const char* name)
1055{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001056 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001057 struct node* parent_node;
1058 char parent_path[PATH_MAX];
1059 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001060
Jeff Brown6249b902012-05-26 14:32:54 -07001061 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001062 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001063 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1064 parent_path, sizeof(parent_path));
1065 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1066 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1067 pthread_mutex_unlock(&fuse->lock);
1068
1069 if (!parent_node || !find_file_within(parent_path, name,
1070 child_path, sizeof(child_path), 1)) {
1071 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001072 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001073 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001074 return -EACCES;
1075 }
Jeff Brown6249b902012-05-26 14:32:54 -07001076 if (rmdir(child_path) < 0) {
1077 return -errno;
1078 }
1079 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001080}
1081
Jeff Brown6249b902012-05-26 14:32:54 -07001082static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001083 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001084 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001085{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001086 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001087 struct node* old_parent_node;
1088 struct node* new_parent_node;
1089 struct node* child_node;
1090 char old_parent_path[PATH_MAX];
1091 char new_parent_path[PATH_MAX];
1092 char old_child_path[PATH_MAX];
1093 char new_child_path[PATH_MAX];
1094 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001095 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001096
Jeff Brown6249b902012-05-26 14:32:54 -07001097 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001098 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001099 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1100 old_parent_path, sizeof(old_parent_path));
1101 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1102 new_parent_path, sizeof(new_parent_path));
1103 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1104 old_name, new_name,
1105 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1106 req->newdir, new_parent_node ? new_parent_node->name : "?");
1107 if (!old_parent_node || !new_parent_node) {
1108 res = -ENOENT;
1109 goto lookup_error;
1110 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001111 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001112 res = -EACCES;
1113 goto lookup_error;
1114 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001115 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001116 res = -EACCES;
1117 goto lookup_error;
1118 }
Jeff Brown6249b902012-05-26 14:32:54 -07001119 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1120 if (!child_node || get_node_path_locked(child_node,
1121 old_child_path, sizeof(old_child_path)) < 0) {
1122 res = -ENOENT;
1123 goto lookup_error;
1124 }
1125 acquire_node_locked(child_node);
1126 pthread_mutex_unlock(&fuse->lock);
1127
1128 /* Special case for renaming a file where destination is same path
1129 * differing only by case. In this case we don't want to look for a case
1130 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1131 */
1132 int search = old_parent_node != new_parent_node
1133 || strcasecmp(old_name, new_name);
1134 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1135 new_child_path, sizeof(new_child_path), search))) {
1136 res = -ENOENT;
1137 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001138 }
1139
Jeff Brown6249b902012-05-26 14:32:54 -07001140 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1141 res = rename(old_child_path, new_child_path);
1142 if (res < 0) {
1143 res = -errno;
1144 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001145 }
1146
Jeff Brown6249b902012-05-26 14:32:54 -07001147 pthread_mutex_lock(&fuse->lock);
1148 res = rename_node_locked(child_node, new_name, new_actual_name);
1149 if (!res) {
1150 remove_node_from_parent_locked(child_node);
1151 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001152 }
Jeff Brown6249b902012-05-26 14:32:54 -07001153 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001154
Jeff Brown6249b902012-05-26 14:32:54 -07001155io_error:
1156 pthread_mutex_lock(&fuse->lock);
1157done:
1158 release_node_locked(child_node);
1159lookup_error:
1160 pthread_mutex_unlock(&fuse->lock);
1161 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001162}
1163
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001164static int open_flags_to_access_mode(int open_flags) {
1165 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1166 return R_OK;
1167 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1168 return W_OK;
1169 } else {
1170 /* Probably O_RDRW, but treat as default to be safe */
1171 return R_OK | W_OK;
1172 }
1173}
1174
Jeff Brown6249b902012-05-26 14:32:54 -07001175static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001176 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1177{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001178 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001179 struct node* node;
1180 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001181 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001182 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001183
Jeff Brown6249b902012-05-26 14:32:54 -07001184 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001185 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001186 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1187 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1188 req->flags, hdr->nodeid, node ? node->name : "?");
1189 pthread_mutex_unlock(&fuse->lock);
1190
1191 if (!node) {
1192 return -ENOENT;
1193 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001194 if (!check_caller_access_to_node(fuse, hdr, node,
1195 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001196 return -EACCES;
1197 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198 h = malloc(sizeof(*h));
1199 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001200 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001201 }
Jeff Brown6249b902012-05-26 14:32:54 -07001202 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001203 h->fd = open(path, req->flags);
1204 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001206 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 }
1208 out.fh = ptr_to_id(h);
1209 out.open_flags = 0;
1210 out.padding = 0;
1211 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001212 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001213}
1214
Jeff Brown6249b902012-05-26 14:32:54 -07001215static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001216 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1217{
1218 struct handle *h = id_to_ptr(req->fh);
1219 __u64 unique = hdr->unique;
1220 __u32 size = req->size;
1221 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001222 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001223 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001224
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1226 * overlaps the request buffer and will clobber data in the request. This
1227 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001228
1229 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1230 h, h->fd, size, offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001231 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001232 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001233 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001234 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001235 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001236 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001238 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001239 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001240}
1241
Jeff Brown6249b902012-05-26 14:32:54 -07001242static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001243 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1244 const void* buffer)
1245{
1246 struct fuse_write_out out;
1247 struct handle *h = id_to_ptr(req->fh);
1248 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001249 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001250
Arpad Horvath49e93442014-02-18 10:18:25 +01001251 if (req->flags & O_DIRECT) {
1252 memcpy(aligned_buffer, buffer, req->size);
1253 buffer = (const __u8*) aligned_buffer;
1254 }
Jeff Brown6249b902012-05-26 14:32:54 -07001255
1256 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1257 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001258 res = pwrite64(h->fd, buffer, req->size, req->offset);
1259 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001260 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001261 }
1262 out.size = res;
1263 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001264 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001265}
1266
Jeff Brown6249b902012-05-26 14:32:54 -07001267static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001268 const struct fuse_in_header* hdr)
1269{
Jeff Brown6249b902012-05-26 14:32:54 -07001270 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001271 struct statfs stat;
1272 struct fuse_statfs_out out;
1273 int res;
1274
Jeff Brown6249b902012-05-26 14:32:54 -07001275 pthread_mutex_lock(&fuse->lock);
1276 TRACE("[%d] STATFS\n", handler->token);
1277 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1278 pthread_mutex_unlock(&fuse->lock);
1279 if (res < 0) {
1280 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001281 }
Jeff Brown6249b902012-05-26 14:32:54 -07001282 if (statfs(fuse->root.name, &stat) < 0) {
1283 return -errno;
1284 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001285 memset(&out, 0, sizeof(out));
1286 out.st.blocks = stat.f_blocks;
1287 out.st.bfree = stat.f_bfree;
1288 out.st.bavail = stat.f_bavail;
1289 out.st.files = stat.f_files;
1290 out.st.ffree = stat.f_ffree;
1291 out.st.bsize = stat.f_bsize;
1292 out.st.namelen = stat.f_namelen;
1293 out.st.frsize = stat.f_frsize;
1294 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001295 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001296}
1297
Jeff Brown6249b902012-05-26 14:32:54 -07001298static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001299 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1300{
1301 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001302
1303 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001304 close(h->fd);
1305 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001306 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001307}
1308
Jeff Brown6249b902012-05-26 14:32:54 -07001309static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001310 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1311{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001312 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1313 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001314
Elliott Hughesf6d67372014-07-08 14:38:26 -07001315 int fd = -1;
1316 if (is_dir) {
1317 struct dirhandle *dh = id_to_ptr(req->fh);
1318 fd = dirfd(dh->d);
1319 } else {
1320 struct handle *h = id_to_ptr(req->fh);
1321 fd = h->fd;
1322 }
1323
1324 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1325 is_dir ? "FSYNCDIR" : "FSYNC",
1326 id_to_ptr(req->fh), fd, is_data_sync);
1327 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1328 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001329 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001330 }
Jeff Brown6249b902012-05-26 14:32:54 -07001331 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001332}
1333
Jeff Brown6249b902012-05-26 14:32:54 -07001334static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001335 const struct fuse_in_header* hdr)
1336{
Jeff Brown6249b902012-05-26 14:32:54 -07001337 TRACE("[%d] FLUSH\n", handler->token);
1338 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001339}
1340
Jeff Brown6249b902012-05-26 14:32:54 -07001341static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001342 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1343{
Jeff Brown6249b902012-05-26 14:32:54 -07001344 struct node* node;
1345 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001347 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348
Jeff Brown6249b902012-05-26 14:32:54 -07001349 pthread_mutex_lock(&fuse->lock);
1350 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1351 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1352 hdr->nodeid, node ? node->name : "?");
1353 pthread_mutex_unlock(&fuse->lock);
1354
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001355 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001356 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001357 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001358 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001359 return -EACCES;
1360 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001361 h = malloc(sizeof(*h));
1362 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001363 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001364 }
Jeff Brown6249b902012-05-26 14:32:54 -07001365 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001366 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001367 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001368 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001369 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001370 }
1371 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001372 out.open_flags = 0;
1373 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001374 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001375 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001376}
1377
Jeff Brown6249b902012-05-26 14:32:54 -07001378static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001379 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1380{
1381 char buffer[8192];
1382 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1383 struct dirent *de;
1384 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001385
1386 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001387 if (req->offset == 0) {
1388 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001389 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001390 rewinddir(h->d);
1391 }
1392 de = readdir(h->d);
1393 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001394 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001395 }
1396 fde->ino = FUSE_UNKNOWN_INO;
1397 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1398 fde->off = req->offset + 1;
1399 fde->type = de->d_type;
1400 fde->namelen = strlen(de->d_name);
1401 memcpy(fde->name, de->d_name, fde->namelen + 1);
1402 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001403 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1404 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001405}
1406
Jeff Brown6249b902012-05-26 14:32:54 -07001407static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001408 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1409{
1410 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001411
1412 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001413 closedir(h->d);
1414 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001415 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001416}
1417
Jeff Brown6249b902012-05-26 14:32:54 -07001418static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001419 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1420{
1421 struct fuse_init_out out;
1422
Jeff Brown6249b902012-05-26 14:32:54 -07001423 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1424 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425 out.major = FUSE_KERNEL_VERSION;
1426 out.minor = FUSE_KERNEL_MINOR_VERSION;
1427 out.max_readahead = req->max_readahead;
1428 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1429 out.max_background = 32;
1430 out.congestion_threshold = 32;
1431 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001432 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001433 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001434}
1435
Jeff Brown6249b902012-05-26 14:32:54 -07001436static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001437 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1438{
Brian Swetland03ee9472010-08-12 18:01:08 -07001439 switch (hdr->opcode) {
1440 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001441 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001442 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001443 }
Jeff Brown84715842012-05-25 14:07:47 -07001444
Brian Swetland03ee9472010-08-12 18:01:08 -07001445 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001446 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001447 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001448 }
Jeff Brown84715842012-05-25 14:07:47 -07001449
Brian Swetland03ee9472010-08-12 18:01:08 -07001450 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001451 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001452 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001453 }
Jeff Brown84715842012-05-25 14:07:47 -07001454
Brian Swetland03ee9472010-08-12 18:01:08 -07001455 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001456 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001457 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001458 }
Jeff Brown84715842012-05-25 14:07:47 -07001459
Brian Swetland03ee9472010-08-12 18:01:08 -07001460// case FUSE_READLINK:
1461// case FUSE_SYMLINK:
1462 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001463 const struct fuse_mknod_in *req = data;
1464 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001465 return handle_mknod(fuse, handler, hdr, req, 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_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001469 const struct fuse_mkdir_in *req = data;
1470 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001471 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001472 }
Jeff Brown84715842012-05-25 14:07:47 -07001473
Brian Swetland03ee9472010-08-12 18:01:08 -07001474 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001475 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001476 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001477 }
Jeff Brown84715842012-05-25 14:07:47 -07001478
Brian Swetland03ee9472010-08-12 18:01:08 -07001479 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001480 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001481 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001482 }
Jeff Brown84715842012-05-25 14:07:47 -07001483
Brian Swetland03ee9472010-08-12 18:01:08 -07001484 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001485 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001486 const char *old_name = ((const char*) data) + sizeof(*req);
1487 const char *new_name = old_name + strlen(old_name) + 1;
1488 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001489 }
Jeff Brown84715842012-05-25 14:07:47 -07001490
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001491// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001492 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001493 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001494 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001495 }
Jeff Brown84715842012-05-25 14:07:47 -07001496
Brian Swetland03ee9472010-08-12 18:01:08 -07001497 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001498 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001499 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001500 }
Jeff Brown84715842012-05-25 14:07:47 -07001501
Brian Swetland03ee9472010-08-12 18:01:08 -07001502 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001503 const struct fuse_write_in *req = data;
1504 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001505 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001506 }
Jeff Brown84715842012-05-25 14:07:47 -07001507
Mike Lockwood4553b082010-08-16 14:14:44 -04001508 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001509 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001510 }
Jeff Brown84715842012-05-25 14:07:47 -07001511
Brian Swetland03ee9472010-08-12 18:01:08 -07001512 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001513 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001514 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001515 }
Jeff Brown84715842012-05-25 14:07:47 -07001516
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001517 case FUSE_FSYNC:
1518 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001519 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001520 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001521 }
1522
Brian Swetland03ee9472010-08-12 18:01:08 -07001523// case FUSE_SETXATTR:
1524// case FUSE_GETXATTR:
1525// case FUSE_LISTXATTR:
1526// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001527 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001528 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001529 }
1530
Brian Swetland03ee9472010-08-12 18:01:08 -07001531 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001532 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001533 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001534 }
Jeff Brown84715842012-05-25 14:07:47 -07001535
Brian Swetland03ee9472010-08-12 18:01:08 -07001536 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001537 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001538 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001539 }
Jeff Brown84715842012-05-25 14:07:47 -07001540
Brian Swetland03ee9472010-08-12 18:01:08 -07001541 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001542 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001543 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001544 }
Jeff Brown84715842012-05-25 14:07:47 -07001545
Brian Swetland03ee9472010-08-12 18:01:08 -07001546 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001547 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001548 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001549 }
Jeff Brown84715842012-05-25 14:07:47 -07001550
Brian Swetland03ee9472010-08-12 18:01:08 -07001551 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001552 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1553 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1554 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001555 }
Jeff Brown84715842012-05-25 14:07:47 -07001556 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001557}
1558
Jeff Brown6249b902012-05-26 14:32:54 -07001559static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001560{
Jeff Brown6249b902012-05-26 14:32:54 -07001561 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001562 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001563 ssize_t len = read(fuse->fd,
1564 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001565 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001566 if (errno != EINTR) {
1567 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1568 }
1569 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 }
Jeff Brown84715842012-05-25 14:07:47 -07001571
1572 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001573 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1574 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001575 }
1576
Jeff Brown7729d242012-05-25 15:35:28 -07001577 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001578 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001579 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1580 handler->token, (size_t)len, hdr->len);
1581 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001582 }
1583
Jeff Brown7729d242012-05-25 15:35:28 -07001584 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001585 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001586 __u64 unique = hdr->unique;
1587 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001588
1589 /* We do not access the request again after this point because the underlying
1590 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001591
1592 if (res != NO_STATUS) {
1593 if (res) {
1594 TRACE("[%d] ERROR %d\n", handler->token, res);
1595 }
1596 fuse_status(fuse, unique, res);
1597 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001598 }
1599}
1600
Jeff Brown6249b902012-05-26 14:32:54 -07001601static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001602{
Jeff Brown6249b902012-05-26 14:32:54 -07001603 struct fuse_handler* handler = data;
1604 handle_fuse_requests(handler);
1605 return NULL;
1606}
1607
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001608static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001609 Hashmap* map = context;
1610 hashmapRemove(map, key);
1611 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001612 return true;
1613}
1614
1615static bool remove_int_to_null(void *key, void *value, void *context) {
1616 Hashmap* map = context;
1617 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001618 return true;
1619}
1620
1621static int read_package_list(struct fuse *fuse) {
1622 pthread_mutex_lock(&fuse->lock);
1623
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001624 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1625 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001626
1627 FILE* file = fopen(kPackagesListFile, "r");
1628 if (!file) {
1629 ERROR("failed to open package list: %s\n", strerror(errno));
1630 pthread_mutex_unlock(&fuse->lock);
1631 return -1;
1632 }
1633
1634 char buf[512];
1635 while (fgets(buf, sizeof(buf), file) != NULL) {
1636 char package_name[512];
1637 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001638 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001639
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001640 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1641 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001642 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001643
1644 char* token = strtok(gids, ",");
1645 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001646 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001647 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001648 break;
1649 }
1650 token = strtok(NULL, ",");
1651 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001652 }
1653 }
1654
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001655 TRACE("read_package_list: found %d packages, %d with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001656 hashmapSize(fuse->package_to_appid),
1657 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001658 fclose(file);
1659 pthread_mutex_unlock(&fuse->lock);
1660 return 0;
1661}
1662
1663static void watch_package_list(struct fuse* fuse) {
1664 struct inotify_event *event;
1665 char event_buf[512];
1666
1667 int nfd = inotify_init();
1668 if (nfd < 0) {
1669 ERROR("inotify_init failed: %s\n", strerror(errno));
1670 return;
1671 }
1672
1673 bool active = false;
1674 while (1) {
1675 if (!active) {
1676 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1677 if (res == -1) {
1678 if (errno == ENOENT || errno == EACCES) {
1679 /* Framework may not have created yet, sleep and retry */
1680 ERROR("missing packages.list; retrying\n");
1681 sleep(3);
1682 continue;
1683 } else {
1684 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1685 return;
1686 }
1687 }
1688
1689 /* Watch above will tell us about any future changes, so
1690 * read the current state. */
1691 if (read_package_list(fuse) == -1) {
1692 ERROR("read_package_list failed: %s\n", strerror(errno));
1693 return;
1694 }
1695 active = true;
1696 }
1697
1698 int event_pos = 0;
1699 int res = read(nfd, event_buf, sizeof(event_buf));
1700 if (res < (int) sizeof(*event)) {
1701 if (errno == EINTR)
1702 continue;
1703 ERROR("failed to read inotify event: %s\n", strerror(errno));
1704 return;
1705 }
1706
1707 while (res >= (int) sizeof(*event)) {
1708 int event_size;
1709 event = (struct inotify_event *) (event_buf + event_pos);
1710
1711 TRACE("inotify event: %08x\n", event->mask);
1712 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1713 /* Previously watched file was deleted, probably due to move
1714 * that swapped in new data; re-arm the watch and read. */
1715 active = false;
1716 }
1717
1718 event_size = sizeof(*event) + event->len;
1719 res -= event_size;
1720 event_pos += event_size;
1721 }
1722 }
1723}
1724
Jeff Brown6249b902012-05-26 14:32:54 -07001725static int ignite_fuse(struct fuse* fuse, int num_threads)
1726{
1727 struct fuse_handler* handlers;
1728 int i;
1729
1730 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1731 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001732 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001733 return -ENOMEM;
1734 }
1735
1736 for (i = 0; i < num_threads; i++) {
1737 handlers[i].fuse = fuse;
1738 handlers[i].token = i;
1739 }
1740
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001741 /* When deriving permissions, this thread is used to process inotify events,
1742 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001743 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001744 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001745 pthread_t thread;
1746 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1747 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001748 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001749 goto quit;
1750 }
1751 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001752
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001753 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001754 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001755 } else {
1756 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001757 }
1758
1759 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001760
1761 /* don't bother killing all of the other threads or freeing anything,
1762 * should never get here anyhow */
1763quit:
1764 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001765}
1766
Mike Lockwood4f35e622011-01-12 14:39:44 -05001767static int usage()
1768{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001769 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1770 " -u: specify UID to run as\n"
1771 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001772 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001773 " -t: specify number of threads to use (default %d)\n"
1774 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001775 " -l: derive file permissions based on legacy internal layout\n"
1776 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001777 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001778 return 1;
1779}
1780
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001781static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001782 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1783 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001784 int fd;
1785 char opts[256];
1786 int res;
1787 struct fuse fuse;
1788
1789 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001790 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001791
1792 fd = open("/dev/fuse", O_RDWR);
1793 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001794 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001795 return -1;
1796 }
1797
1798 snprintf(opts, sizeof(opts),
1799 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1800 fd, uid, gid);
1801
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001802 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001803 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001804 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1805 goto error;
1806 }
1807
1808 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1809 if (res < 0) {
1810 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001811 goto error;
1812 }
1813
1814 res = setgid(gid);
1815 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001816 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001817 goto error;
1818 }
1819
1820 res = setuid(uid);
1821 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001822 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001823 goto error;
1824 }
1825
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001826 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001827
1828 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001829 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001830
1831 /* we do not attempt to umount the file system here because we are no longer
1832 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001833
1834error:
1835 close(fd);
1836 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001837}
1838
Brian Swetland03ee9472010-08-12 18:01:08 -07001839int main(int argc, char **argv)
1840{
Brian Swetland03ee9472010-08-12 18:01:08 -07001841 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001842 const char *source_path = NULL;
1843 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001844 uid_t uid = 0;
1845 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001846 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001847 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001848 derive_t derive = DERIVE_NONE;
1849 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001850 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001851 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001852
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001853 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001854 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001855 switch (opt) {
1856 case 'u':
1857 uid = strtoul(optarg, NULL, 10);
1858 break;
1859 case 'g':
1860 gid = strtoul(optarg, NULL, 10);
1861 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001862 case 'w':
1863 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001864 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001865 case 't':
1866 num_threads = strtoul(optarg, NULL, 10);
1867 break;
1868 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001869 derive = DERIVE_UNIFIED;
1870 break;
1871 case 'l':
1872 derive = DERIVE_LEGACY;
1873 break;
1874 case 's':
1875 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001876 break;
1877 case '?':
1878 default:
1879 return usage();
1880 }
1881 }
1882
1883 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001884 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001885 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001886 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001887 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001888 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001889 } else if (!uid) {
1890 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001891 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001892 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001893 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001894 ERROR("too many arguments\n");
1895 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001896 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001897 }
1898
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001899 if (!source_path) {
1900 ERROR("no source path specified\n");
1901 return usage();
1902 }
1903 if (!dest_path) {
1904 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001905 return usage();
1906 }
Jeff Brown26567352012-05-25 13:27:43 -07001907 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001908 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001909 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001910 }
Jeff Brown6249b902012-05-26 14:32:54 -07001911 if (num_threads < 1) {
1912 ERROR("number of threads must be at least 1\n");
1913 return usage();
1914 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001915 if (split_perms && derive == DERIVE_NONE) {
1916 ERROR("cannot split permissions without deriving\n");
1917 return usage();
1918 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001919
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001920 rlim.rlim_cur = 8192;
1921 rlim.rlim_max = 8192;
1922 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1923 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1924 }
1925
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001926 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001927 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001928}