blob: 3a164ed6255d852a53183f3c63b794c4e898af89 [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 Hughes60281d52014-05-07 14:39:58 -070017#include <ctype.h>
18#include <dirent.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <limits.h>
22#include <linux/fuse.h>
23#include <pthread.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070027#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070028#include <sys/mount.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070029#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070030#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040031#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080032#include <sys/time.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070033#include <sys/uio.h>
34#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070035
Jeff Sharkey44d63422013-09-12 09:44:48 -070036#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070037#include <cutils/hashmap.h>
38#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070039
Brian Swetlandb14a2c62010-08-12 18:21:12 -070040#include <private/android_filesystem_config.h>
41
Brian Swetland03ee9472010-08-12 18:01:08 -070042/* README
43 *
44 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070045 *
Brian Swetland03ee9472010-08-12 18:01:08 -070046 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070047 * directory permissions (all files are given fixed owner, group, and
48 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070049 * changeable, symlinks and hardlinks are not createable, etc.
50 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070051 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070052 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070053 * It must be run as root, but will drop to requested UID/GID as soon as it
54 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070055 *
56 * Things I believe to be true:
57 *
58 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
59 * CREAT) must bump that node's refcount
60 * - don't forget that FORGET can forget multiple references (req->nlookup)
61 * - if an op that returns a fuse_entry fails writing the reply to the
62 * kernel, you must rollback the refcount to reflect the reference the
63 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070064 *
65 * This daemon can also derive custom filesystem permissions based on directory
66 * structure when requested. These custom permissions support several features:
67 *
68 * - Apps can access their own files in /Android/data/com.example/ without
69 * requiring any additional GIDs.
70 * - Separate permissions for protecting directories like Pictures and Music.
71 * - Multi-user separation on the same physical device.
72 *
73 * The derived permissions look like this:
74 *
75 * rwxrwx--x root:sdcard_rw /
76 * rwxrwx--- root:sdcard_pics /Pictures
77 * rwxrwx--- root:sdcard_av /Music
78 *
79 * rwxrwx--x root:sdcard_rw /Android
80 * rwxrwx--x root:sdcard_rw /Android/data
81 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
82 * rwxrwx--x root:sdcard_rw /Android/obb/
83 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
84 *
85 * rwxrwx--- root:sdcard_all /Android/user
86 * rwxrwx--x root:sdcard_rw /Android/user/10
87 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070088 */
89
90#define FUSE_TRACE 0
91
92#if FUSE_TRACE
93#define TRACE(x...) fprintf(stderr,x)
94#else
95#define TRACE(x...) do {} while (0)
96#endif
97
98#define ERROR(x...) fprintf(stderr,x)
99
100#define FUSE_UNKNOWN_INO 0xffffffff
101
Jeff Brown84715842012-05-25 14:07:47 -0700102/* Maximum number of bytes to write in one request. */
103#define MAX_WRITE (256 * 1024)
104
105/* Maximum number of bytes to read in one request. */
106#define MAX_READ (128 * 1024)
107
108/* Largest possible request.
109 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
110 * the largest possible data payload. */
111#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
112
Jeff Brown6249b902012-05-26 14:32:54 -0700113/* Default number of threads. */
114#define DEFAULT_NUM_THREADS 2
115
116/* Pseudo-error constant used to indicate that no fuse status is needed
117 * or that a reply has already been written. */
118#define NO_STATUS 1
119
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700120/* Path to system-provided mapping of package name to appIds */
121static const char* const kPackagesListFile = "/data/system/packages.list";
122
123/* Supplementary groups to execute with */
124static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
125
126/* Permission mode for a specific node. Controls how file permissions
127 * are derived for children nodes. */
128typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700129 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700130 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700131 /* This node is one level above a normal root; used for legacy layouts
132 * which use the first level to represent user_id. */
133 PERM_LEGACY_PRE_ROOT,
134 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700135 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700136 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700137 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700138 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700139 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700140 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700141 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700142 /* This node is "/Android/media" */
143 PERM_ANDROID_MEDIA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700144 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700145 PERM_ANDROID_USER,
146} perm_t;
147
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700148/* Permissions structure to derive */
149typedef enum {
150 DERIVE_NONE,
151 DERIVE_LEGACY,
152 DERIVE_UNIFIED,
153} derive_t;
154
Brian Swetland03ee9472010-08-12 18:01:08 -0700155struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700156 int fd;
157};
158
159struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700160 DIR *d;
161};
162
163struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700164 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700165 __u64 nid;
166 __u64 gen;
167
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700168 /* State derived based on current position in hierarchy. */
169 perm_t perm;
170 userid_t userid;
171 uid_t uid;
172 gid_t gid;
173 mode_t mode;
174
Paul Eastham11ccdb32010-10-14 11:04:26 -0700175 struct node *next; /* per-dir sibling list */
176 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700177 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700178
Jeff Brown6249b902012-05-26 14:32:54 -0700179 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700180 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800181 /* If non-null, this is the real name of the file in the underlying storage.
182 * This may differ from the field "name" only by case.
183 * strlen(actual_name) will always equal strlen(name), so it is safe to use
184 * namelen for both fields.
185 */
186 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700187
188 /* If non-null, an exact underlying path that should be grafted into this
189 * position. Used to support things like OBB. */
190 char* graft_path;
191 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700192};
193
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700194static int str_hash(void *key) {
195 return hashmapHash(key, strlen(key));
196}
197
Jeff Sharkey44d63422013-09-12 09:44:48 -0700198/** Test if two string keys are equal ignoring case */
199static bool str_icase_equals(void *keyA, void *keyB) {
200 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700201}
202
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700203static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800204 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700205}
206
207static bool int_equals(void *keyA, void *keyB) {
208 return keyA == keyB;
209}
210
Jeff Brown7729d242012-05-25 15:35:28 -0700211/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700212struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700213 pthread_mutex_t lock;
214
Brian Swetland03ee9472010-08-12 18:01:08 -0700215 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700216 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700217 derive_t derive;
218 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700219 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700220 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700221 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700222
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700223 Hashmap* package_to_appid;
224 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700225};
226
Jeff Brown7729d242012-05-25 15:35:28 -0700227/* Private data used by a single fuse handler. */
228struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700229 struct fuse* fuse;
230 int token;
231
Jeff Brown7729d242012-05-25 15:35:28 -0700232 /* To save memory, we never use the contents of the request buffer and the read
233 * buffer at the same time. This allows us to share the underlying storage. */
234 union {
235 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800236 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700237 };
238};
Brian Swetland03ee9472010-08-12 18:01:08 -0700239
Jeff Brown6249b902012-05-26 14:32:54 -0700240static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700241{
Jeff Brown6249b902012-05-26 14:32:54 -0700242 return (void *) (uintptr_t) nid;
243}
Brian Swetland03ee9472010-08-12 18:01:08 -0700244
Jeff Brown6249b902012-05-26 14:32:54 -0700245static inline __u64 ptr_to_id(void *ptr)
246{
247 return (__u64) (uintptr_t) ptr;
248}
Brian Swetland03ee9472010-08-12 18:01:08 -0700249
Jeff Brown6249b902012-05-26 14:32:54 -0700250static void acquire_node_locked(struct node* node)
251{
252 node->refcount++;
253 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
254}
255
256static void remove_node_from_parent_locked(struct node* node);
257
258static void release_node_locked(struct node* node)
259{
260 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
261 if (node->refcount > 0) {
262 node->refcount--;
263 if (!node->refcount) {
264 TRACE("DESTROY %p (%s)\n", node, node->name);
265 remove_node_from_parent_locked(node);
266
267 /* TODO: remove debugging - poison memory */
268 memset(node->name, 0xef, node->namelen);
269 free(node->name);
270 free(node->actual_name);
271 memset(node, 0xfc, sizeof(*node));
272 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800273 }
Jeff Brown6249b902012-05-26 14:32:54 -0700274 } else {
275 ERROR("Zero refcnt %p\n", node);
276 }
277}
278
279static void add_node_to_parent_locked(struct node *node, struct node *parent) {
280 node->parent = parent;
281 node->next = parent->child;
282 parent->child = node;
283 acquire_node_locked(parent);
284}
285
286static void remove_node_from_parent_locked(struct node* node)
287{
288 if (node->parent) {
289 if (node->parent->child == node) {
290 node->parent->child = node->parent->child->next;
291 } else {
292 struct node *node2;
293 node2 = node->parent->child;
294 while (node2->next != node)
295 node2 = node2->next;
296 node2->next = node->next;
297 }
298 release_node_locked(node->parent);
299 node->parent = NULL;
300 node->next = NULL;
301 }
302}
303
304/* Gets the absolute path to a node into the provided buffer.
305 *
306 * Populates 'buf' with the path and returns the length of the path on success,
307 * or returns -1 if the path is too long for the provided buffer.
308 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700309static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
310 const char* name;
311 size_t namelen;
312 if (node->graft_path) {
313 name = node->graft_path;
314 namelen = node->graft_pathlen;
315 } else if (node->actual_name) {
316 name = node->actual_name;
317 namelen = node->namelen;
318 } else {
319 name = node->name;
320 namelen = node->namelen;
321 }
322
Jeff Brown6249b902012-05-26 14:32:54 -0700323 if (bufsize < namelen + 1) {
324 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700325 }
326
Jeff Brown6249b902012-05-26 14:32:54 -0700327 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700328 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700329 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
330 if (pathlen < 0) {
331 return -1;
332 }
333 buf[pathlen++] = '/';
334 }
335
Jeff Brown6249b902012-05-26 14:32:54 -0700336 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
337 return pathlen + namelen;
338}
339
340/* Finds the absolute path of a file within a given directory.
341 * Performs a case-insensitive search for the file and sets the buffer to the path
342 * of the first matching file. If 'search' is zero or if no match is found, sets
343 * the buffer to the path that the file would have, assuming the name were case-sensitive.
344 *
345 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
346 * or returns NULL if the path is too long for the provided buffer.
347 */
348static char* find_file_within(const char* path, const char* name,
349 char* buf, size_t bufsize, int search)
350{
351 size_t pathlen = strlen(path);
352 size_t namelen = strlen(name);
353 size_t childlen = pathlen + namelen + 1;
354 char* actual;
355
356 if (bufsize <= childlen) {
357 return NULL;
358 }
359
360 memcpy(buf, path, pathlen);
361 buf[pathlen] = '/';
362 actual = buf + pathlen + 1;
363 memcpy(actual, name, namelen + 1);
364
365 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800366 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700367 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800368 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700369 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700370 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800371 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800372 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700373 if (!strcasecmp(entry->d_name, name)) {
374 /* we have a match - replace the name, don't need to copy the null again */
375 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800376 break;
377 }
378 }
379 closedir(dir);
380 }
Jeff Brown6249b902012-05-26 14:32:54 -0700381 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800382}
383
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700384static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800385{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700386 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700387 attr->size = s->st_size;
388 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400389 attr->atime = s->st_atime;
390 attr->mtime = s->st_mtime;
391 attr->ctime = s->st_ctime;
392 attr->atimensec = s->st_atime_nsec;
393 attr->mtimensec = s->st_mtime_nsec;
394 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700395 attr->mode = s->st_mode;
396 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700397
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700398 attr->uid = node->uid;
399 attr->gid = node->gid;
400
401 /* Filter requested mode based on underlying file, and
402 * pass through file type. */
403 int owner_mode = s->st_mode & 0700;
404 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
405 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
406}
407
Jeff Sharkey44d63422013-09-12 09:44:48 -0700408static int touch(char* path, mode_t mode) {
409 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
410 if (fd == -1) {
411 if (errno == EEXIST) {
412 return 0;
413 } else {
414 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
415 return -1;
416 }
417 }
418 close(fd);
419 return 0;
420}
421
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700422static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
423 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700424 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700425
426 /* By default, each node inherits from its parent */
427 node->perm = PERM_INHERIT;
428 node->userid = parent->userid;
429 node->uid = parent->uid;
430 node->gid = parent->gid;
431 node->mode = parent->mode;
432
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700433 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700434 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700435 }
436
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700437 /* Derive custom permissions based on parent and current node */
438 switch (parent->perm) {
439 case PERM_INHERIT:
440 /* Already inherited above */
441 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700442 case PERM_LEGACY_PRE_ROOT:
443 /* Legacy internal layout places users at top level */
444 node->perm = PERM_ROOT;
445 node->userid = strtoul(node->name, NULL, 10);
446 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700447 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700448 /* Assume masked off by default. */
449 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700450 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700451 /* App-specific directories inside; let anyone traverse */
452 node->perm = PERM_ANDROID;
453 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700454 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700455 if (!strcasecmp(node->name, "DCIM")
456 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700457 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700458 } else if (!strcasecmp(node->name, "Alarms")
459 || !strcasecmp(node->name, "Movies")
460 || !strcasecmp(node->name, "Music")
461 || !strcasecmp(node->name, "Notifications")
462 || !strcasecmp(node->name, "Podcasts")
463 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700464 node->gid = AID_SDCARD_AV;
465 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700466 }
467 break;
468 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700469 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700470 /* App-specific directories inside; let anyone traverse */
471 node->perm = PERM_ANDROID_DATA;
472 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700473 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700474 /* App-specific directories inside; let anyone traverse */
475 node->perm = PERM_ANDROID_OBB;
476 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700477 /* Single OBB directory is always shared */
478 node->graft_path = fuse->obbpath;
479 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700480 } else if (!strcasecmp(node->name, "media")) {
481 /* App-specific directories inside; let anyone traverse */
482 node->perm = PERM_ANDROID_MEDIA;
483 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700484 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700485 /* User directories must only be accessible to system, protected
486 * by sdcard_all. Zygote will bind mount the appropriate user-
487 * specific path. */
488 node->perm = PERM_ANDROID_USER;
489 node->gid = AID_SDCARD_ALL;
490 node->mode = 0770;
491 }
492 break;
493 case PERM_ANDROID_DATA:
494 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700495 case PERM_ANDROID_MEDIA:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800496 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700497 if (appid != 0) {
498 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700499 }
500 node->mode = 0770;
501 break;
502 case PERM_ANDROID_USER:
503 /* Root of a secondary user */
504 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700505 node->userid = strtoul(node->name, NULL, 10);
506 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700507 node->mode = 0771;
508 break;
509 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700510}
511
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700512/* Return if the calling UID holds sdcard_rw. */
513static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700514 /* No additional permissions enforcement */
515 if (fuse->derive == DERIVE_NONE) {
516 return true;
517 }
518
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700519 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800520 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700521}
522
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700523/* Kernel has already enforced everything we returned through
524 * derive_permissions_locked(), so this is used to lock down access
525 * even further, such as enforcing that apps hold sdcard_rw. */
526static bool check_caller_access_to_name(struct fuse* fuse,
527 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700528 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700529 /* Always block security-sensitive files at root */
530 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700531 if (!strcasecmp(name, "autorun.inf")
532 || !strcasecmp(name, ".android_secure")
533 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700534 return false;
535 }
536 }
537
538 /* No additional permissions enforcement */
539 if (fuse->derive == DERIVE_NONE) {
540 return true;
541 }
542
Jeff Sharkey44d63422013-09-12 09:44:48 -0700543 /* Root always has access; access for any other UIDs should always
544 * be controlled through packages.list. */
545 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700546 return true;
547 }
548
549 /* If asking to write, verify that caller either owns the
550 * parent or holds sdcard_rw. */
551 if (mode & W_OK) {
552 if (parent_node && hdr->uid == parent_node->uid) {
553 return true;
554 }
555
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700556 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700557 }
558
559 /* No extra permissions to enforce */
560 return true;
561}
562
563static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700564 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
565 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700566}
567
Jeff Brown6249b902012-05-26 14:32:54 -0700568struct node *create_node_locked(struct fuse* fuse,
569 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700570{
571 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700572 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700573
Paul Eastham11ccdb32010-10-14 11:04:26 -0700574 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700575 if (!node) {
576 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700577 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700578 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700579 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700580 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700581 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700582 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700583 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700584 if (strcmp(name, actual_name)) {
585 node->actual_name = malloc(namelen + 1);
586 if (!node->actual_name) {
587 free(node->name);
588 free(node);
589 return NULL;
590 }
591 memcpy(node->actual_name, actual_name, namelen + 1);
592 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700593 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700594 node->nid = ptr_to_id(node);
595 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700596
597 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700598 acquire_node_locked(node);
599 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700600 return node;
601}
602
Jeff Brown6249b902012-05-26 14:32:54 -0700603static int rename_node_locked(struct node *node, const char *name,
604 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700605{
Jeff Brown6249b902012-05-26 14:32:54 -0700606 size_t namelen = strlen(name);
607 int need_actual_name = strcmp(name, actual_name);
608
609 /* make the storage bigger without actually changing the name
610 * in case an error occurs part way */
611 if (namelen > node->namelen) {
612 char* new_name = realloc(node->name, namelen + 1);
613 if (!new_name) {
614 return -ENOMEM;
615 }
616 node->name = new_name;
617 if (need_actual_name && node->actual_name) {
618 char* new_actual_name = realloc(node->actual_name, namelen + 1);
619 if (!new_actual_name) {
620 return -ENOMEM;
621 }
622 node->actual_name = new_actual_name;
623 }
624 }
625
626 /* update the name, taking care to allocate storage before overwriting the old name */
627 if (need_actual_name) {
628 if (!node->actual_name) {
629 node->actual_name = malloc(namelen + 1);
630 if (!node->actual_name) {
631 return -ENOMEM;
632 }
633 }
634 memcpy(node->actual_name, actual_name, namelen + 1);
635 } else {
636 free(node->actual_name);
637 node->actual_name = NULL;
638 }
639 memcpy(node->name, name, namelen + 1);
640 node->namelen = namelen;
641 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700642}
643
Jeff Brown6249b902012-05-26 14:32:54 -0700644static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700645{
Jeff Brown6249b902012-05-26 14:32:54 -0700646 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700647 return &fuse->root;
648 } else {
649 return id_to_ptr(nid);
650 }
651}
652
Jeff Brown6249b902012-05-26 14:32:54 -0700653static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
654 char* buf, size_t bufsize)
655{
656 struct node* node = lookup_node_by_id_locked(fuse, nid);
657 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
658 node = NULL;
659 }
660 return node;
661}
662
663static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700664{
665 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700666 /* use exact string comparison, nodes that differ by case
667 * must be considered distinct even if they refer to the same
668 * underlying file as otherwise operations such as "mv x x"
669 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700670 if (!strcmp(name, node->name)) {
671 return node;
672 }
673 }
674 return 0;
675}
676
Jeff Brown6249b902012-05-26 14:32:54 -0700677static struct node* acquire_or_create_child_locked(
678 struct fuse* fuse, struct node* parent,
679 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700680{
Jeff Brown6249b902012-05-26 14:32:54 -0700681 struct node* child = lookup_child_by_name_locked(parent, name);
682 if (child) {
683 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800684 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700685 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800686 }
Jeff Brown6249b902012-05-26 14:32:54 -0700687 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700688}
689
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700690static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700691 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700692 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700693
Jeff Brown6249b902012-05-26 14:32:54 -0700694 fuse->fd = fd;
695 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700696 fuse->derive = derive;
697 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700698 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700699
Jeff Brown6249b902012-05-26 14:32:54 -0700700 memset(&fuse->root, 0, sizeof(fuse->root));
701 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
702 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700703 fuse->root.namelen = strlen(source_path);
704 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700705 fuse->root.userid = 0;
706 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700707
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700708 /* Set up root node for various modes of operation */
709 switch (derive) {
710 case DERIVE_NONE:
711 /* Traditional behavior that treats entire device as being accessible
712 * to sdcard_rw, and no permissions are derived. */
713 fuse->root.perm = PERM_ROOT;
714 fuse->root.mode = 0775;
715 fuse->root.gid = AID_SDCARD_RW;
716 break;
717 case DERIVE_LEGACY:
718 /* Legacy behavior used to support internal multiuser layout which
719 * places user_id at the top directory level, with the actual roots
720 * just below that. Shared OBB path is also at top level. */
721 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
722 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700723 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700724 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700725 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
726 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700727 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700728 break;
729 case DERIVE_UNIFIED:
730 /* Unified multiuser layout which places secondary user_id under
731 * /Android/user and shared OBB path under /Android/obb. */
732 fuse->root.perm = PERM_ROOT;
733 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700734 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700735 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700736 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
737 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
738 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700739 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700740}
741
Jeff Brown6249b902012-05-26 14:32:54 -0700742static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700743{
744 struct fuse_out_header hdr;
745 hdr.len = sizeof(hdr);
746 hdr.error = err;
747 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700748 write(fuse->fd, &hdr, sizeof(hdr));
749}
750
Jeff Brown6249b902012-05-26 14:32:54 -0700751static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700752{
753 struct fuse_out_header hdr;
754 struct iovec vec[2];
755 int res;
756
757 hdr.len = len + sizeof(hdr);
758 hdr.error = 0;
759 hdr.unique = unique;
760
761 vec[0].iov_base = &hdr;
762 vec[0].iov_len = sizeof(hdr);
763 vec[1].iov_base = data;
764 vec[1].iov_len = len;
765
766 res = writev(fuse->fd, vec, 2);
767 if (res < 0) {
768 ERROR("*** REPLY FAILED *** %d\n", errno);
769 }
770}
771
Jeff Brown6249b902012-05-26 14:32:54 -0700772static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
773 struct node* parent, const char* name, const char* actual_name,
774 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700775{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700776 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700777 struct fuse_entry_out out;
778 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700779
Jeff Brown6249b902012-05-26 14:32:54 -0700780 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700781 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700782 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700783
Jeff Brown6249b902012-05-26 14:32:54 -0700784 pthread_mutex_lock(&fuse->lock);
785 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
786 if (!node) {
787 pthread_mutex_unlock(&fuse->lock);
788 return -ENOMEM;
789 }
790 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700791 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700792 out.attr_valid = 10;
793 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700794 out.nodeid = node->nid;
795 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700796 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700797 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700798 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700799}
800
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700801static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700802 const char* path)
803{
804 struct fuse_attr_out out;
805 struct stat s;
806
807 if (lstat(path, &s) < 0) {
808 return -errno;
809 }
810 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700811 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700812 out.attr_valid = 10;
813 fuse_reply(fuse, unique, &out, sizeof(out));
814 return NO_STATUS;
815}
816
817static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700819{
Jeff Brown6249b902012-05-26 14:32:54 -0700820 struct node* parent_node;
821 char parent_path[PATH_MAX];
822 char child_path[PATH_MAX];
823 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700824
Jeff Brown6249b902012-05-26 14:32:54 -0700825 pthread_mutex_lock(&fuse->lock);
826 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
827 parent_path, sizeof(parent_path));
828 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
829 parent_node ? parent_node->name : "?");
830 pthread_mutex_unlock(&fuse->lock);
831
832 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
833 child_path, sizeof(child_path), 1))) {
834 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700835 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700836 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700837 return -EACCES;
838 }
839
Jeff Brown6249b902012-05-26 14:32:54 -0700840 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700841}
842
Jeff Brown6249b902012-05-26 14:32:54 -0700843static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700844 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
845{
Jeff Brown6249b902012-05-26 14:32:54 -0700846 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700847
Jeff Brown6249b902012-05-26 14:32:54 -0700848 pthread_mutex_lock(&fuse->lock);
849 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
850 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
851 hdr->nodeid, node ? node->name : "?");
852 if (node) {
853 __u64 n = req->nlookup;
854 while (n--) {
855 release_node_locked(node);
856 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700857 }
Jeff Brown6249b902012-05-26 14:32:54 -0700858 pthread_mutex_unlock(&fuse->lock);
859 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860}
861
Jeff Brown6249b902012-05-26 14:32:54 -0700862static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700863 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
864{
Jeff Brown6249b902012-05-26 14:32:54 -0700865 struct node* node;
866 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700867
Jeff Brown6249b902012-05-26 14:32:54 -0700868 pthread_mutex_lock(&fuse->lock);
869 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
870 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
871 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
872 pthread_mutex_unlock(&fuse->lock);
873
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700874 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700875 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700877 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700878 return -EACCES;
879 }
880
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700881 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882}
883
Jeff Brown6249b902012-05-26 14:32:54 -0700884static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
886{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700887 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700888 struct node* node;
889 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700890 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700891
Jeff Brown6249b902012-05-26 14:32:54 -0700892 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700893 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700894 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
895 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
896 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
897 pthread_mutex_unlock(&fuse->lock);
898
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700899 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700900 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700901 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700902 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700903 return -EACCES;
904 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700905
Jeff Brown6249b902012-05-26 14:32:54 -0700906 /* XXX: incomplete implementation on purpose.
907 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700908
Jeff Brown6249b902012-05-26 14:32:54 -0700909 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
910 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700911 }
912
913 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
914 * are both set, then set it to the current time. Else, set it to the
915 * time specified in the request. Same goes for mtime. Use utimensat(2)
916 * as it allows ATIME and MTIME to be changed independently, and has
917 * nanosecond resolution which fuse also has.
918 */
919 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
920 times[0].tv_nsec = UTIME_OMIT;
921 times[1].tv_nsec = UTIME_OMIT;
922 if (req->valid & FATTR_ATIME) {
923 if (req->valid & FATTR_ATIME_NOW) {
924 times[0].tv_nsec = UTIME_NOW;
925 } else {
926 times[0].tv_sec = req->atime;
927 times[0].tv_nsec = req->atimensec;
928 }
929 }
930 if (req->valid & FATTR_MTIME) {
931 if (req->valid & FATTR_MTIME_NOW) {
932 times[1].tv_nsec = UTIME_NOW;
933 } else {
934 times[1].tv_sec = req->mtime;
935 times[1].tv_nsec = req->mtimensec;
936 }
937 }
Jeff Brown6249b902012-05-26 14:32:54 -0700938 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
939 handler->token, path, times[0].tv_sec, times[1].tv_sec);
940 if (utimensat(-1, path, times, 0) < 0) {
941 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700942 }
943 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700944 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945}
946
Jeff Brown6249b902012-05-26 14:32:54 -0700947static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
949{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700950 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700951 struct node* parent_node;
952 char parent_path[PATH_MAX];
953 char child_path[PATH_MAX];
954 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700955
Jeff Brown6249b902012-05-26 14:32:54 -0700956 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700957 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700958 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
959 parent_path, sizeof(parent_path));
960 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
961 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
962 pthread_mutex_unlock(&fuse->lock);
963
964 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
965 child_path, sizeof(child_path), 1))) {
966 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700968 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700969 return -EACCES;
970 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700971 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700972 if (mknod(child_path, mode, req->rdev) < 0) {
973 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974 }
Jeff Brown6249b902012-05-26 14:32:54 -0700975 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700976}
977
Jeff Brown6249b902012-05-26 14:32:54 -0700978static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
980{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700981 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700982 struct node* parent_node;
983 char parent_path[PATH_MAX];
984 char child_path[PATH_MAX];
985 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700986
Jeff Brown6249b902012-05-26 14:32:54 -0700987 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700988 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700989 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
990 parent_path, sizeof(parent_path));
991 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
992 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
993 pthread_mutex_unlock(&fuse->lock);
994
995 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
996 child_path, sizeof(child_path), 1))) {
997 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700998 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700999 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001000 return -EACCES;
1001 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001002 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001003 if (mkdir(child_path, mode) < 0) {
1004 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005 }
Jeff Sharkey44d63422013-09-12 09:44:48 -07001006
1007 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1008 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1009 char nomedia[PATH_MAX];
1010 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1011 if (touch(nomedia, 0664) != 0) {
1012 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1013 return -ENOENT;
1014 }
1015 }
1016 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1017 char nomedia[PATH_MAX];
1018 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1019 if (touch(nomedia, 0664) != 0) {
1020 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1021 return -ENOENT;
1022 }
1023 }
1024
Jeff Brown6249b902012-05-26 14:32:54 -07001025 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026}
1027
Jeff Brown6249b902012-05-26 14:32:54 -07001028static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001029 const struct fuse_in_header* hdr, const char* name)
1030{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001031 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001032 struct node* parent_node;
1033 char parent_path[PATH_MAX];
1034 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001035
Jeff Brown6249b902012-05-26 14:32:54 -07001036 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001037 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001038 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1039 parent_path, sizeof(parent_path));
1040 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1041 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1042 pthread_mutex_unlock(&fuse->lock);
1043
1044 if (!parent_node || !find_file_within(parent_path, name,
1045 child_path, sizeof(child_path), 1)) {
1046 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001047 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001048 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001049 return -EACCES;
1050 }
Jeff Brown6249b902012-05-26 14:32:54 -07001051 if (unlink(child_path) < 0) {
1052 return -errno;
1053 }
1054 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001055}
1056
Jeff Brown6249b902012-05-26 14:32:54 -07001057static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001058 const struct fuse_in_header* hdr, const char* name)
1059{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001060 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001061 struct node* parent_node;
1062 char parent_path[PATH_MAX];
1063 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001064
Jeff Brown6249b902012-05-26 14:32:54 -07001065 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001066 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001067 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1068 parent_path, sizeof(parent_path));
1069 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1070 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1071 pthread_mutex_unlock(&fuse->lock);
1072
1073 if (!parent_node || !find_file_within(parent_path, name,
1074 child_path, sizeof(child_path), 1)) {
1075 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001076 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001077 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001078 return -EACCES;
1079 }
Jeff Brown6249b902012-05-26 14:32:54 -07001080 if (rmdir(child_path) < 0) {
1081 return -errno;
1082 }
1083 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001084}
1085
Jeff Brown6249b902012-05-26 14:32:54 -07001086static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001087 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001088 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001090 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001091 struct node* old_parent_node;
1092 struct node* new_parent_node;
1093 struct node* child_node;
1094 char old_parent_path[PATH_MAX];
1095 char new_parent_path[PATH_MAX];
1096 char old_child_path[PATH_MAX];
1097 char new_child_path[PATH_MAX];
1098 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001099 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001100
Jeff Brown6249b902012-05-26 14:32:54 -07001101 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001102 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001103 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1104 old_parent_path, sizeof(old_parent_path));
1105 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1106 new_parent_path, sizeof(new_parent_path));
1107 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1108 old_name, new_name,
1109 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1110 req->newdir, new_parent_node ? new_parent_node->name : "?");
1111 if (!old_parent_node || !new_parent_node) {
1112 res = -ENOENT;
1113 goto lookup_error;
1114 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001115 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001116 res = -EACCES;
1117 goto lookup_error;
1118 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001119 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001120 res = -EACCES;
1121 goto lookup_error;
1122 }
Jeff Brown6249b902012-05-26 14:32:54 -07001123 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1124 if (!child_node || get_node_path_locked(child_node,
1125 old_child_path, sizeof(old_child_path)) < 0) {
1126 res = -ENOENT;
1127 goto lookup_error;
1128 }
1129 acquire_node_locked(child_node);
1130 pthread_mutex_unlock(&fuse->lock);
1131
1132 /* Special case for renaming a file where destination is same path
1133 * differing only by case. In this case we don't want to look for a case
1134 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1135 */
1136 int search = old_parent_node != new_parent_node
1137 || strcasecmp(old_name, new_name);
1138 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1139 new_child_path, sizeof(new_child_path), search))) {
1140 res = -ENOENT;
1141 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142 }
1143
Jeff Brown6249b902012-05-26 14:32:54 -07001144 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1145 res = rename(old_child_path, new_child_path);
1146 if (res < 0) {
1147 res = -errno;
1148 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001149 }
1150
Jeff Brown6249b902012-05-26 14:32:54 -07001151 pthread_mutex_lock(&fuse->lock);
1152 res = rename_node_locked(child_node, new_name, new_actual_name);
1153 if (!res) {
1154 remove_node_from_parent_locked(child_node);
1155 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001156 }
Jeff Brown6249b902012-05-26 14:32:54 -07001157 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001158
Jeff Brown6249b902012-05-26 14:32:54 -07001159io_error:
1160 pthread_mutex_lock(&fuse->lock);
1161done:
1162 release_node_locked(child_node);
1163lookup_error:
1164 pthread_mutex_unlock(&fuse->lock);
1165 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001166}
1167
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001168static int open_flags_to_access_mode(int open_flags) {
1169 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1170 return R_OK;
1171 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1172 return W_OK;
1173 } else {
1174 /* Probably O_RDRW, but treat as default to be safe */
1175 return R_OK | W_OK;
1176 }
1177}
1178
Jeff Brown6249b902012-05-26 14:32:54 -07001179static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001180 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1181{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001182 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001183 struct node* node;
1184 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001185 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001186 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001187
Jeff Brown6249b902012-05-26 14:32:54 -07001188 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001189 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001190 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1191 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1192 req->flags, hdr->nodeid, node ? node->name : "?");
1193 pthread_mutex_unlock(&fuse->lock);
1194
1195 if (!node) {
1196 return -ENOENT;
1197 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001198 if (!check_caller_access_to_node(fuse, hdr, node,
1199 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001200 return -EACCES;
1201 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001202 h = malloc(sizeof(*h));
1203 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001204 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205 }
Jeff Brown6249b902012-05-26 14:32:54 -07001206 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 h->fd = open(path, req->flags);
1208 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001209 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001210 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001211 }
1212 out.fh = ptr_to_id(h);
1213 out.open_flags = 0;
1214 out.padding = 0;
1215 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001216 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001217}
1218
Jeff Brown6249b902012-05-26 14:32:54 -07001219static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001220 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1221{
1222 struct handle *h = id_to_ptr(req->fh);
1223 __u64 unique = hdr->unique;
1224 __u32 size = req->size;
1225 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001226 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001227 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001228
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001229 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1230 * overlaps the request buffer and will clobber data in the request. This
1231 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001232
1233 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1234 h, h->fd, size, offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001235 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001236 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001238 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001239 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001240 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001241 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001242 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001243 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244}
1245
Jeff Brown6249b902012-05-26 14:32:54 -07001246static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001247 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1248 const void* buffer)
1249{
1250 struct fuse_write_out out;
1251 struct handle *h = id_to_ptr(req->fh);
1252 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001253 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001254
Arpad Horvath49e93442014-02-18 10:18:25 +01001255 if (req->flags & O_DIRECT) {
1256 memcpy(aligned_buffer, buffer, req->size);
1257 buffer = (const __u8*) aligned_buffer;
1258 }
Jeff Brown6249b902012-05-26 14:32:54 -07001259
1260 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1261 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262 res = pwrite64(h->fd, buffer, req->size, req->offset);
1263 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001264 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001265 }
1266 out.size = res;
1267 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001268 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001269}
1270
Jeff Brown6249b902012-05-26 14:32:54 -07001271static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272 const struct fuse_in_header* hdr)
1273{
Jeff Brown6249b902012-05-26 14:32:54 -07001274 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275 struct statfs stat;
1276 struct fuse_statfs_out out;
1277 int res;
1278
Jeff Brown6249b902012-05-26 14:32:54 -07001279 pthread_mutex_lock(&fuse->lock);
1280 TRACE("[%d] STATFS\n", handler->token);
1281 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1282 pthread_mutex_unlock(&fuse->lock);
1283 if (res < 0) {
1284 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001285 }
Jeff Brown6249b902012-05-26 14:32:54 -07001286 if (statfs(fuse->root.name, &stat) < 0) {
1287 return -errno;
1288 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001289 memset(&out, 0, sizeof(out));
1290 out.st.blocks = stat.f_blocks;
1291 out.st.bfree = stat.f_bfree;
1292 out.st.bavail = stat.f_bavail;
1293 out.st.files = stat.f_files;
1294 out.st.ffree = stat.f_ffree;
1295 out.st.bsize = stat.f_bsize;
1296 out.st.namelen = stat.f_namelen;
1297 out.st.frsize = stat.f_frsize;
1298 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001299 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001300}
1301
Jeff Brown6249b902012-05-26 14:32:54 -07001302static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001303 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1304{
1305 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001306
1307 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001308 close(h->fd);
1309 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001310 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001311}
1312
Jeff Brown6249b902012-05-26 14:32:54 -07001313static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001314 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1315{
1316 int is_data_sync = req->fsync_flags & 1;
1317 struct handle *h = id_to_ptr(req->fh);
1318 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001319
1320 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
1321 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001322 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
1323 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001324 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001325 }
Jeff Brown6249b902012-05-26 14:32:54 -07001326 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001327}
1328
Jeff Brown6249b902012-05-26 14:32:54 -07001329static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001330 const struct fuse_in_header* hdr)
1331{
Jeff Brown6249b902012-05-26 14:32:54 -07001332 TRACE("[%d] FLUSH\n", handler->token);
1333 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001334}
1335
Jeff Brown6249b902012-05-26 14:32:54 -07001336static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1338{
Jeff Brown6249b902012-05-26 14:32:54 -07001339 struct node* node;
1340 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001342 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001343
Jeff Brown6249b902012-05-26 14:32:54 -07001344 pthread_mutex_lock(&fuse->lock);
1345 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1346 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1347 hdr->nodeid, node ? node->name : "?");
1348 pthread_mutex_unlock(&fuse->lock);
1349
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001350 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001351 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001352 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001353 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001354 return -EACCES;
1355 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001356 h = malloc(sizeof(*h));
1357 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001358 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 }
Jeff Brown6249b902012-05-26 14:32:54 -07001360 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001361 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001362 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001363 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001364 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001365 }
1366 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001367 out.open_flags = 0;
1368 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001370 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001371}
1372
Jeff Brown6249b902012-05-26 14:32:54 -07001373static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001374 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1375{
1376 char buffer[8192];
1377 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1378 struct dirent *de;
1379 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001380
1381 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001382 if (req->offset == 0) {
1383 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001384 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001385 rewinddir(h->d);
1386 }
1387 de = readdir(h->d);
1388 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001389 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001390 }
1391 fde->ino = FUSE_UNKNOWN_INO;
1392 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1393 fde->off = req->offset + 1;
1394 fde->type = de->d_type;
1395 fde->namelen = strlen(de->d_name);
1396 memcpy(fde->name, de->d_name, fde->namelen + 1);
1397 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001398 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1399 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001400}
1401
Jeff Brown6249b902012-05-26 14:32:54 -07001402static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001403 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1404{
1405 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001406
1407 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001408 closedir(h->d);
1409 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001410 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001411}
1412
Jeff Brown6249b902012-05-26 14:32:54 -07001413static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001414 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1415{
1416 struct fuse_init_out out;
1417
Jeff Brown6249b902012-05-26 14:32:54 -07001418 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1419 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 out.major = FUSE_KERNEL_VERSION;
1421 out.minor = FUSE_KERNEL_MINOR_VERSION;
1422 out.max_readahead = req->max_readahead;
1423 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1424 out.max_background = 32;
1425 out.congestion_threshold = 32;
1426 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001427 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001428 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001429}
1430
Jeff Brown6249b902012-05-26 14:32:54 -07001431static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001432 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1433{
Brian Swetland03ee9472010-08-12 18:01:08 -07001434 switch (hdr->opcode) {
1435 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001436 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001437 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001438 }
Jeff Brown84715842012-05-25 14:07:47 -07001439
Brian Swetland03ee9472010-08-12 18:01:08 -07001440 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001441 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001442 return handle_forget(fuse, handler, hdr, req);
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_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001446 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001447 return handle_getattr(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_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001451 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001452 return handle_setattr(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_READLINK:
1456// case FUSE_SYMLINK:
1457 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001458 const struct fuse_mknod_in *req = data;
1459 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001460 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001461 }
Jeff Brown84715842012-05-25 14:07:47 -07001462
Brian Swetland03ee9472010-08-12 18:01:08 -07001463 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001464 const struct fuse_mkdir_in *req = data;
1465 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001466 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001467 }
Jeff Brown84715842012-05-25 14:07:47 -07001468
Brian Swetland03ee9472010-08-12 18:01:08 -07001469 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001470 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001471 return handle_unlink(fuse, handler, hdr, 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_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001475 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001476 return handle_rmdir(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_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001480 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001481 const char *old_name = ((const char*) data) + sizeof(*req);
1482 const char *new_name = old_name + strlen(old_name) + 1;
1483 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001484 }
Jeff Brown84715842012-05-25 14:07:47 -07001485
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001486// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001487 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001488 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001489 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001490 }
Jeff Brown84715842012-05-25 14:07:47 -07001491
Brian Swetland03ee9472010-08-12 18:01:08 -07001492 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001493 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001494 return handle_read(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_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001498 const struct fuse_write_in *req = data;
1499 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001500 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001501 }
Jeff Brown84715842012-05-25 14:07:47 -07001502
Mike Lockwood4553b082010-08-16 14:14:44 -04001503 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001504 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001505 }
Jeff Brown84715842012-05-25 14:07:47 -07001506
Brian Swetland03ee9472010-08-12 18:01:08 -07001507 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001508 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001509 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001510 }
Jeff Brown84715842012-05-25 14:07:47 -07001511
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001512 case FUSE_FSYNC:
1513 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001514 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001515 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001516 }
1517
Brian Swetland03ee9472010-08-12 18:01:08 -07001518// case FUSE_SETXATTR:
1519// case FUSE_GETXATTR:
1520// case FUSE_LISTXATTR:
1521// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001522 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001523 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001524 }
1525
Brian Swetland03ee9472010-08-12 18:01:08 -07001526 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001527 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001528 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 }
Jeff Brown84715842012-05-25 14:07:47 -07001530
Brian Swetland03ee9472010-08-12 18:01:08 -07001531 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001532 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001533 return handle_readdir(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_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001537 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001538 return handle_releasedir(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_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001542 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001543 return handle_init(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 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001547 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1548 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1549 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 }
Jeff Brown84715842012-05-25 14:07:47 -07001551 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001552}
1553
Jeff Brown6249b902012-05-26 14:32:54 -07001554static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001555{
Jeff Brown6249b902012-05-26 14:32:54 -07001556 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001557 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001558 ssize_t len = read(fuse->fd,
1559 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001560 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001561 if (errno != EINTR) {
1562 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1563 }
1564 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001565 }
Jeff Brown84715842012-05-25 14:07:47 -07001566
1567 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001568 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1569 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001570 }
1571
Jeff Brown7729d242012-05-25 15:35:28 -07001572 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001573 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001574 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1575 handler->token, (size_t)len, hdr->len);
1576 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001577 }
1578
Jeff Brown7729d242012-05-25 15:35:28 -07001579 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001580 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001581 __u64 unique = hdr->unique;
1582 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001583
1584 /* We do not access the request again after this point because the underlying
1585 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001586
1587 if (res != NO_STATUS) {
1588 if (res) {
1589 TRACE("[%d] ERROR %d\n", handler->token, res);
1590 }
1591 fuse_status(fuse, unique, res);
1592 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001593 }
1594}
1595
Jeff Brown6249b902012-05-26 14:32:54 -07001596static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001597{
Jeff Brown6249b902012-05-26 14:32:54 -07001598 struct fuse_handler* handler = data;
1599 handle_fuse_requests(handler);
1600 return NULL;
1601}
1602
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001603static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001604 Hashmap* map = context;
1605 hashmapRemove(map, key);
1606 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001607 return true;
1608}
1609
1610static bool remove_int_to_null(void *key, void *value, void *context) {
1611 Hashmap* map = context;
1612 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001613 return true;
1614}
1615
1616static int read_package_list(struct fuse *fuse) {
1617 pthread_mutex_lock(&fuse->lock);
1618
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001619 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1620 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001621
1622 FILE* file = fopen(kPackagesListFile, "r");
1623 if (!file) {
1624 ERROR("failed to open package list: %s\n", strerror(errno));
1625 pthread_mutex_unlock(&fuse->lock);
1626 return -1;
1627 }
1628
1629 char buf[512];
1630 while (fgets(buf, sizeof(buf), file) != NULL) {
1631 char package_name[512];
1632 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001633 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001634
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001635 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1636 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001637 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001638
1639 char* token = strtok(gids, ",");
1640 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001641 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001642 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001643 break;
1644 }
1645 token = strtok(NULL, ",");
1646 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001647 }
1648 }
1649
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001650 TRACE("read_package_list: found %d packages, %d with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001651 hashmapSize(fuse->package_to_appid),
1652 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001653 fclose(file);
1654 pthread_mutex_unlock(&fuse->lock);
1655 return 0;
1656}
1657
1658static void watch_package_list(struct fuse* fuse) {
1659 struct inotify_event *event;
1660 char event_buf[512];
1661
1662 int nfd = inotify_init();
1663 if (nfd < 0) {
1664 ERROR("inotify_init failed: %s\n", strerror(errno));
1665 return;
1666 }
1667
1668 bool active = false;
1669 while (1) {
1670 if (!active) {
1671 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1672 if (res == -1) {
1673 if (errno == ENOENT || errno == EACCES) {
1674 /* Framework may not have created yet, sleep and retry */
1675 ERROR("missing packages.list; retrying\n");
1676 sleep(3);
1677 continue;
1678 } else {
1679 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1680 return;
1681 }
1682 }
1683
1684 /* Watch above will tell us about any future changes, so
1685 * read the current state. */
1686 if (read_package_list(fuse) == -1) {
1687 ERROR("read_package_list failed: %s\n", strerror(errno));
1688 return;
1689 }
1690 active = true;
1691 }
1692
1693 int event_pos = 0;
1694 int res = read(nfd, event_buf, sizeof(event_buf));
1695 if (res < (int) sizeof(*event)) {
1696 if (errno == EINTR)
1697 continue;
1698 ERROR("failed to read inotify event: %s\n", strerror(errno));
1699 return;
1700 }
1701
1702 while (res >= (int) sizeof(*event)) {
1703 int event_size;
1704 event = (struct inotify_event *) (event_buf + event_pos);
1705
1706 TRACE("inotify event: %08x\n", event->mask);
1707 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1708 /* Previously watched file was deleted, probably due to move
1709 * that swapped in new data; re-arm the watch and read. */
1710 active = false;
1711 }
1712
1713 event_size = sizeof(*event) + event->len;
1714 res -= event_size;
1715 event_pos += event_size;
1716 }
1717 }
1718}
1719
Jeff Brown6249b902012-05-26 14:32:54 -07001720static int ignite_fuse(struct fuse* fuse, int num_threads)
1721{
1722 struct fuse_handler* handlers;
1723 int i;
1724
1725 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1726 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001727 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001728 return -ENOMEM;
1729 }
1730
1731 for (i = 0; i < num_threads; i++) {
1732 handlers[i].fuse = fuse;
1733 handlers[i].token = i;
1734 }
1735
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001736 /* When deriving permissions, this thread is used to process inotify events,
1737 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001738 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001739 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001740 pthread_t thread;
1741 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1742 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001743 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001744 goto quit;
1745 }
1746 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001747
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001748 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001749 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001750 } else {
1751 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001752 }
1753
1754 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001755
1756 /* don't bother killing all of the other threads or freeing anything,
1757 * should never get here anyhow */
1758quit:
1759 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001760}
1761
Mike Lockwood4f35e622011-01-12 14:39:44 -05001762static int usage()
1763{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001764 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1765 " -u: specify UID to run as\n"
1766 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001767 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001768 " -t: specify number of threads to use (default %d)\n"
1769 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001770 " -l: derive file permissions based on legacy internal layout\n"
1771 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001772 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001773 return 1;
1774}
1775
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001776static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001777 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1778 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001779 int fd;
1780 char opts[256];
1781 int res;
1782 struct fuse fuse;
1783
1784 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001785 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001786
1787 fd = open("/dev/fuse", O_RDWR);
1788 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001789 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001790 return -1;
1791 }
1792
1793 snprintf(opts, sizeof(opts),
1794 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1795 fd, uid, gid);
1796
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001797 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001798 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001799 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1800 goto error;
1801 }
1802
1803 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1804 if (res < 0) {
1805 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001806 goto error;
1807 }
1808
1809 res = setgid(gid);
1810 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001811 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001812 goto error;
1813 }
1814
1815 res = setuid(uid);
1816 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001817 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001818 goto error;
1819 }
1820
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001821 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001822
1823 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001824 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001825
1826 /* we do not attempt to umount the file system here because we are no longer
1827 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001828
1829error:
1830 close(fd);
1831 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001832}
1833
Brian Swetland03ee9472010-08-12 18:01:08 -07001834int main(int argc, char **argv)
1835{
Brian Swetland03ee9472010-08-12 18:01:08 -07001836 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001837 const char *source_path = NULL;
1838 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001839 uid_t uid = 0;
1840 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001841 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001842 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001843 derive_t derive = DERIVE_NONE;
1844 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001845 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001846 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001847
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001848 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001849 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001850 switch (opt) {
1851 case 'u':
1852 uid = strtoul(optarg, NULL, 10);
1853 break;
1854 case 'g':
1855 gid = strtoul(optarg, NULL, 10);
1856 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001857 case 'w':
1858 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001859 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001860 case 't':
1861 num_threads = strtoul(optarg, NULL, 10);
1862 break;
1863 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001864 derive = DERIVE_UNIFIED;
1865 break;
1866 case 'l':
1867 derive = DERIVE_LEGACY;
1868 break;
1869 case 's':
1870 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001871 break;
1872 case '?':
1873 default:
1874 return usage();
1875 }
1876 }
1877
1878 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001879 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001880 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001881 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001882 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001883 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001884 } else if (!uid) {
1885 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001886 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001887 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001888 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001889 ERROR("too many arguments\n");
1890 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001891 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001892 }
1893
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001894 if (!source_path) {
1895 ERROR("no source path specified\n");
1896 return usage();
1897 }
1898 if (!dest_path) {
1899 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001900 return usage();
1901 }
Jeff Brown26567352012-05-25 13:27:43 -07001902 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001903 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001904 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001905 }
Jeff Brown6249b902012-05-26 14:32:54 -07001906 if (num_threads < 1) {
1907 ERROR("number of threads must be at least 1\n");
1908 return usage();
1909 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001910 if (split_perms && derive == DERIVE_NONE) {
1911 ERROR("cannot split permissions without deriving\n");
1912 return usage();
1913 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001914
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001915 rlim.rlim_cur = 8192;
1916 rlim.rlim_max = 8192;
1917 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1918 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1919 }
1920
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001921 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001922 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001923}