blob: 7baad6395eb142e4bd68560a99596249f7ff02ce [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 Sharkey977a9f32013-08-12 20:23:49 -0700142 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700143 PERM_ANDROID_USER,
144} perm_t;
145
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700146/* Permissions structure to derive */
147typedef enum {
148 DERIVE_NONE,
149 DERIVE_LEGACY,
150 DERIVE_UNIFIED,
151} derive_t;
152
Brian Swetland03ee9472010-08-12 18:01:08 -0700153struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700154 int fd;
155};
156
157struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700158 DIR *d;
159};
160
161struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700162 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700163 __u64 nid;
164 __u64 gen;
165
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700166 /* State derived based on current position in hierarchy. */
167 perm_t perm;
168 userid_t userid;
169 uid_t uid;
170 gid_t gid;
171 mode_t mode;
172
Paul Eastham11ccdb32010-10-14 11:04:26 -0700173 struct node *next; /* per-dir sibling list */
174 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700175 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700176
Jeff Brown6249b902012-05-26 14:32:54 -0700177 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700178 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800179 /* If non-null, this is the real name of the file in the underlying storage.
180 * This may differ from the field "name" only by case.
181 * strlen(actual_name) will always equal strlen(name), so it is safe to use
182 * namelen for both fields.
183 */
184 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700185
186 /* If non-null, an exact underlying path that should be grafted into this
187 * position. Used to support things like OBB. */
188 char* graft_path;
189 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700190};
191
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700192static int str_hash(void *key) {
193 return hashmapHash(key, strlen(key));
194}
195
Jeff Sharkey44d63422013-09-12 09:44:48 -0700196/** Test if two string keys are equal ignoring case */
197static bool str_icase_equals(void *keyA, void *keyB) {
198 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700199}
200
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700201static int int_hash(void *key) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800202 return (int) (uintptr_t) key;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700203}
204
205static bool int_equals(void *keyA, void *keyB) {
206 return keyA == keyB;
207}
208
Jeff Brown7729d242012-05-25 15:35:28 -0700209/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700210struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700211 pthread_mutex_t lock;
212
Brian Swetland03ee9472010-08-12 18:01:08 -0700213 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700214 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700215 derive_t derive;
216 bool split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700217 gid_t write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700218 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700219 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700220
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700221 Hashmap* package_to_appid;
222 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700223};
224
Jeff Brown7729d242012-05-25 15:35:28 -0700225/* Private data used by a single fuse handler. */
226struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700227 struct fuse* fuse;
228 int token;
229
Jeff Brown7729d242012-05-25 15:35:28 -0700230 /* To save memory, we never use the contents of the request buffer and the read
231 * buffer at the same time. This allows us to share the underlying storage. */
232 union {
233 __u8 request_buffer[MAX_REQUEST_SIZE];
Arpad Horvath80b435a2014-02-14 16:42:27 -0800234 __u8 read_buffer[MAX_READ + PAGESIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700235 };
236};
Brian Swetland03ee9472010-08-12 18:01:08 -0700237
Jeff Brown6249b902012-05-26 14:32:54 -0700238static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700239{
Jeff Brown6249b902012-05-26 14:32:54 -0700240 return (void *) (uintptr_t) nid;
241}
Brian Swetland03ee9472010-08-12 18:01:08 -0700242
Jeff Brown6249b902012-05-26 14:32:54 -0700243static inline __u64 ptr_to_id(void *ptr)
244{
245 return (__u64) (uintptr_t) ptr;
246}
Brian Swetland03ee9472010-08-12 18:01:08 -0700247
Jeff Brown6249b902012-05-26 14:32:54 -0700248static void acquire_node_locked(struct node* node)
249{
250 node->refcount++;
251 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
252}
253
254static void remove_node_from_parent_locked(struct node* node);
255
256static void release_node_locked(struct node* node)
257{
258 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
259 if (node->refcount > 0) {
260 node->refcount--;
261 if (!node->refcount) {
262 TRACE("DESTROY %p (%s)\n", node, node->name);
263 remove_node_from_parent_locked(node);
264
265 /* TODO: remove debugging - poison memory */
266 memset(node->name, 0xef, node->namelen);
267 free(node->name);
268 free(node->actual_name);
269 memset(node, 0xfc, sizeof(*node));
270 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800271 }
Jeff Brown6249b902012-05-26 14:32:54 -0700272 } else {
273 ERROR("Zero refcnt %p\n", node);
274 }
275}
276
277static void add_node_to_parent_locked(struct node *node, struct node *parent) {
278 node->parent = parent;
279 node->next = parent->child;
280 parent->child = node;
281 acquire_node_locked(parent);
282}
283
284static void remove_node_from_parent_locked(struct node* node)
285{
286 if (node->parent) {
287 if (node->parent->child == node) {
288 node->parent->child = node->parent->child->next;
289 } else {
290 struct node *node2;
291 node2 = node->parent->child;
292 while (node2->next != node)
293 node2 = node2->next;
294 node2->next = node->next;
295 }
296 release_node_locked(node->parent);
297 node->parent = NULL;
298 node->next = NULL;
299 }
300}
301
302/* Gets the absolute path to a node into the provided buffer.
303 *
304 * Populates 'buf' with the path and returns the length of the path on success,
305 * or returns -1 if the path is too long for the provided buffer.
306 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700307static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
308 const char* name;
309 size_t namelen;
310 if (node->graft_path) {
311 name = node->graft_path;
312 namelen = node->graft_pathlen;
313 } else if (node->actual_name) {
314 name = node->actual_name;
315 namelen = node->namelen;
316 } else {
317 name = node->name;
318 namelen = node->namelen;
319 }
320
Jeff Brown6249b902012-05-26 14:32:54 -0700321 if (bufsize < namelen + 1) {
322 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700323 }
324
Jeff Brown6249b902012-05-26 14:32:54 -0700325 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700326 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700327 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
328 if (pathlen < 0) {
329 return -1;
330 }
331 buf[pathlen++] = '/';
332 }
333
Jeff Brown6249b902012-05-26 14:32:54 -0700334 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
335 return pathlen + namelen;
336}
337
338/* Finds the absolute path of a file within a given directory.
339 * Performs a case-insensitive search for the file and sets the buffer to the path
340 * of the first matching file. If 'search' is zero or if no match is found, sets
341 * the buffer to the path that the file would have, assuming the name were case-sensitive.
342 *
343 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
344 * or returns NULL if the path is too long for the provided buffer.
345 */
346static char* find_file_within(const char* path, const char* name,
347 char* buf, size_t bufsize, int search)
348{
349 size_t pathlen = strlen(path);
350 size_t namelen = strlen(name);
351 size_t childlen = pathlen + namelen + 1;
352 char* actual;
353
354 if (bufsize <= childlen) {
355 return NULL;
356 }
357
358 memcpy(buf, path, pathlen);
359 buf[pathlen] = '/';
360 actual = buf + pathlen + 1;
361 memcpy(actual, name, namelen + 1);
362
363 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800364 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700365 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800366 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700367 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700368 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800369 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800370 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700371 if (!strcasecmp(entry->d_name, name)) {
372 /* we have a match - replace the name, don't need to copy the null again */
373 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800374 break;
375 }
376 }
377 closedir(dir);
378 }
Jeff Brown6249b902012-05-26 14:32:54 -0700379 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800380}
381
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700382static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800383{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700384 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700385 attr->size = s->st_size;
386 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400387 attr->atime = s->st_atime;
388 attr->mtime = s->st_mtime;
389 attr->ctime = s->st_ctime;
390 attr->atimensec = s->st_atime_nsec;
391 attr->mtimensec = s->st_mtime_nsec;
392 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700393 attr->mode = s->st_mode;
394 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700395
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700396 attr->uid = node->uid;
397 attr->gid = node->gid;
398
399 /* Filter requested mode based on underlying file, and
400 * pass through file type. */
401 int owner_mode = s->st_mode & 0700;
402 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
403 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
404}
405
Jeff Sharkey44d63422013-09-12 09:44:48 -0700406static int touch(char* path, mode_t mode) {
407 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
408 if (fd == -1) {
409 if (errno == EEXIST) {
410 return 0;
411 } else {
412 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
413 return -1;
414 }
415 }
416 close(fd);
417 return 0;
418}
419
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700420static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
421 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700422 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700423
424 /* By default, each node inherits from its parent */
425 node->perm = PERM_INHERIT;
426 node->userid = parent->userid;
427 node->uid = parent->uid;
428 node->gid = parent->gid;
429 node->mode = parent->mode;
430
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700431 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700432 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700433 }
434
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700435 /* Derive custom permissions based on parent and current node */
436 switch (parent->perm) {
437 case PERM_INHERIT:
438 /* Already inherited above */
439 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700440 case PERM_LEGACY_PRE_ROOT:
441 /* Legacy internal layout places users at top level */
442 node->perm = PERM_ROOT;
443 node->userid = strtoul(node->name, NULL, 10);
444 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700445 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700446 /* Assume masked off by default. */
447 node->mode = 0770;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700448 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700449 /* App-specific directories inside; let anyone traverse */
450 node->perm = PERM_ANDROID;
451 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700452 } else if (fuse->split_perms) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700453 if (!strcasecmp(node->name, "DCIM")
454 || !strcasecmp(node->name, "Pictures")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700455 node->gid = AID_SDCARD_PICS;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700456 } else if (!strcasecmp(node->name, "Alarms")
457 || !strcasecmp(node->name, "Movies")
458 || !strcasecmp(node->name, "Music")
459 || !strcasecmp(node->name, "Notifications")
460 || !strcasecmp(node->name, "Podcasts")
461 || !strcasecmp(node->name, "Ringtones")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700462 node->gid = AID_SDCARD_AV;
463 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700464 }
465 break;
466 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700467 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700468 /* App-specific directories inside; let anyone traverse */
469 node->perm = PERM_ANDROID_DATA;
470 node->mode = 0771;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700471 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700472 /* App-specific directories inside; let anyone traverse */
473 node->perm = PERM_ANDROID_OBB;
474 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700475 /* Single OBB directory is always shared */
476 node->graft_path = fuse->obbpath;
477 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 } else if (!strcasecmp(node->name, "user")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* User directories must only be accessible to system, protected
480 * by sdcard_all. Zygote will bind mount the appropriate user-
481 * specific path. */
482 node->perm = PERM_ANDROID_USER;
483 node->gid = AID_SDCARD_ALL;
484 node->mode = 0770;
485 }
486 break;
487 case PERM_ANDROID_DATA:
488 case PERM_ANDROID_OBB:
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800489 appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700490 if (appid != 0) {
491 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700492 }
493 node->mode = 0770;
494 break;
495 case PERM_ANDROID_USER:
496 /* Root of a secondary user */
497 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700498 node->userid = strtoul(node->name, NULL, 10);
499 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700500 node->mode = 0771;
501 break;
502 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700503}
504
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700505/* Return if the calling UID holds sdcard_rw. */
506static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700507 /* No additional permissions enforcement */
508 if (fuse->derive == DERIVE_NONE) {
509 return true;
510 }
511
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700512 appid_t appid = multiuser_get_app_id(hdr->uid);
Elliott Hughes5d9fe772014-02-05 17:50:35 -0800513 return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700514}
515
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700516/* Kernel has already enforced everything we returned through
517 * derive_permissions_locked(), so this is used to lock down access
518 * even further, such as enforcing that apps hold sdcard_rw. */
519static bool check_caller_access_to_name(struct fuse* fuse,
520 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700521 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700522 /* Always block security-sensitive files at root */
523 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700524 if (!strcasecmp(name, "autorun.inf")
525 || !strcasecmp(name, ".android_secure")
526 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700527 return false;
528 }
529 }
530
531 /* No additional permissions enforcement */
532 if (fuse->derive == DERIVE_NONE) {
533 return true;
534 }
535
Jeff Sharkey44d63422013-09-12 09:44:48 -0700536 /* Root always has access; access for any other UIDs should always
537 * be controlled through packages.list. */
538 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700539 return true;
540 }
541
542 /* If asking to write, verify that caller either owns the
543 * parent or holds sdcard_rw. */
544 if (mode & W_OK) {
545 if (parent_node && hdr->uid == parent_node->uid) {
546 return true;
547 }
548
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700549 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700550 }
551
552 /* No extra permissions to enforce */
553 return true;
554}
555
556static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700557 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
558 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700559}
560
Jeff Brown6249b902012-05-26 14:32:54 -0700561struct node *create_node_locked(struct fuse* fuse,
562 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700563{
564 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700565 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700566
Paul Eastham11ccdb32010-10-14 11:04:26 -0700567 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700568 if (!node) {
569 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700570 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700571 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700572 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700573 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700574 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700575 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700576 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700577 if (strcmp(name, actual_name)) {
578 node->actual_name = malloc(namelen + 1);
579 if (!node->actual_name) {
580 free(node->name);
581 free(node);
582 return NULL;
583 }
584 memcpy(node->actual_name, actual_name, namelen + 1);
585 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700586 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700587 node->nid = ptr_to_id(node);
588 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700589
590 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700591 acquire_node_locked(node);
592 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700593 return node;
594}
595
Jeff Brown6249b902012-05-26 14:32:54 -0700596static int rename_node_locked(struct node *node, const char *name,
597 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700598{
Jeff Brown6249b902012-05-26 14:32:54 -0700599 size_t namelen = strlen(name);
600 int need_actual_name = strcmp(name, actual_name);
601
602 /* make the storage bigger without actually changing the name
603 * in case an error occurs part way */
604 if (namelen > node->namelen) {
605 char* new_name = realloc(node->name, namelen + 1);
606 if (!new_name) {
607 return -ENOMEM;
608 }
609 node->name = new_name;
610 if (need_actual_name && node->actual_name) {
611 char* new_actual_name = realloc(node->actual_name, namelen + 1);
612 if (!new_actual_name) {
613 return -ENOMEM;
614 }
615 node->actual_name = new_actual_name;
616 }
617 }
618
619 /* update the name, taking care to allocate storage before overwriting the old name */
620 if (need_actual_name) {
621 if (!node->actual_name) {
622 node->actual_name = malloc(namelen + 1);
623 if (!node->actual_name) {
624 return -ENOMEM;
625 }
626 }
627 memcpy(node->actual_name, actual_name, namelen + 1);
628 } else {
629 free(node->actual_name);
630 node->actual_name = NULL;
631 }
632 memcpy(node->name, name, namelen + 1);
633 node->namelen = namelen;
634 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700635}
636
Jeff Brown6249b902012-05-26 14:32:54 -0700637static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700638{
Jeff Brown6249b902012-05-26 14:32:54 -0700639 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700640 return &fuse->root;
641 } else {
642 return id_to_ptr(nid);
643 }
644}
645
Jeff Brown6249b902012-05-26 14:32:54 -0700646static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
647 char* buf, size_t bufsize)
648{
649 struct node* node = lookup_node_by_id_locked(fuse, nid);
650 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
651 node = NULL;
652 }
653 return node;
654}
655
656static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700657{
658 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700659 /* use exact string comparison, nodes that differ by case
660 * must be considered distinct even if they refer to the same
661 * underlying file as otherwise operations such as "mv x x"
662 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700663 if (!strcmp(name, node->name)) {
664 return node;
665 }
666 }
667 return 0;
668}
669
Jeff Brown6249b902012-05-26 14:32:54 -0700670static struct node* acquire_or_create_child_locked(
671 struct fuse* fuse, struct node* parent,
672 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700673{
Jeff Brown6249b902012-05-26 14:32:54 -0700674 struct node* child = lookup_child_by_name_locked(parent, name);
675 if (child) {
676 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800677 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700678 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800679 }
Jeff Brown6249b902012-05-26 14:32:54 -0700680 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700681}
682
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700683static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700684 gid_t write_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700685 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700686
Jeff Brown6249b902012-05-26 14:32:54 -0700687 fuse->fd = fd;
688 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700689 fuse->derive = derive;
690 fuse->split_perms = split_perms;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700691 fuse->write_gid = write_gid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700692
Jeff Brown6249b902012-05-26 14:32:54 -0700693 memset(&fuse->root, 0, sizeof(fuse->root));
694 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
695 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700696 fuse->root.namelen = strlen(source_path);
697 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700698 fuse->root.userid = 0;
699 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700700
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700701 /* Set up root node for various modes of operation */
702 switch (derive) {
703 case DERIVE_NONE:
704 /* Traditional behavior that treats entire device as being accessible
705 * to sdcard_rw, and no permissions are derived. */
706 fuse->root.perm = PERM_ROOT;
707 fuse->root.mode = 0775;
708 fuse->root.gid = AID_SDCARD_RW;
709 break;
710 case DERIVE_LEGACY:
711 /* Legacy behavior used to support internal multiuser layout which
712 * places user_id at the top directory level, with the actual roots
713 * just below that. Shared OBB path is also at top level. */
714 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
715 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700716 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700717 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700718 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
719 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700720 fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid());
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700721 break;
722 case DERIVE_UNIFIED:
723 /* Unified multiuser layout which places secondary user_id under
724 * /Android/user and shared OBB path under /Android/obb. */
725 fuse->root.perm = PERM_ROOT;
726 fuse->root.mode = 0771;
Jeff Sharkeye93a0512013-10-08 10:14:24 -0700727 fuse->root.gid = AID_SDCARD_R;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700728 fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700729 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
730 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
731 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700732 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700733}
734
Jeff Brown6249b902012-05-26 14:32:54 -0700735static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700736{
737 struct fuse_out_header hdr;
738 hdr.len = sizeof(hdr);
739 hdr.error = err;
740 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700741 write(fuse->fd, &hdr, sizeof(hdr));
742}
743
Jeff Brown6249b902012-05-26 14:32:54 -0700744static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700745{
746 struct fuse_out_header hdr;
747 struct iovec vec[2];
748 int res;
749
750 hdr.len = len + sizeof(hdr);
751 hdr.error = 0;
752 hdr.unique = unique;
753
754 vec[0].iov_base = &hdr;
755 vec[0].iov_len = sizeof(hdr);
756 vec[1].iov_base = data;
757 vec[1].iov_len = len;
758
759 res = writev(fuse->fd, vec, 2);
760 if (res < 0) {
761 ERROR("*** REPLY FAILED *** %d\n", errno);
762 }
763}
764
Jeff Brown6249b902012-05-26 14:32:54 -0700765static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
766 struct node* parent, const char* name, const char* actual_name,
767 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700768{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700769 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700770 struct fuse_entry_out out;
771 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700772
Jeff Brown6249b902012-05-26 14:32:54 -0700773 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700774 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700775 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700776
Jeff Brown6249b902012-05-26 14:32:54 -0700777 pthread_mutex_lock(&fuse->lock);
778 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
779 if (!node) {
780 pthread_mutex_unlock(&fuse->lock);
781 return -ENOMEM;
782 }
783 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700784 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700785 out.attr_valid = 10;
786 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700787 out.nodeid = node->nid;
788 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700789 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700790 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700791 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700792}
793
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700794static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700795 const char* path)
796{
797 struct fuse_attr_out out;
798 struct stat s;
799
800 if (lstat(path, &s) < 0) {
801 return -errno;
802 }
803 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700804 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700805 out.attr_valid = 10;
806 fuse_reply(fuse, unique, &out, sizeof(out));
807 return NO_STATUS;
808}
809
810static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700811 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700812{
Jeff Brown6249b902012-05-26 14:32:54 -0700813 struct node* parent_node;
814 char parent_path[PATH_MAX];
815 char child_path[PATH_MAX];
816 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700817
Jeff Brown6249b902012-05-26 14:32:54 -0700818 pthread_mutex_lock(&fuse->lock);
819 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
820 parent_path, sizeof(parent_path));
821 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
822 parent_node ? parent_node->name : "?");
823 pthread_mutex_unlock(&fuse->lock);
824
825 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
826 child_path, sizeof(child_path), 1))) {
827 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700828 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700829 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700830 return -EACCES;
831 }
832
Jeff Brown6249b902012-05-26 14:32:54 -0700833 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700834}
835
Jeff Brown6249b902012-05-26 14:32:54 -0700836static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700837 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
838{
Jeff Brown6249b902012-05-26 14:32:54 -0700839 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700840
Jeff Brown6249b902012-05-26 14:32:54 -0700841 pthread_mutex_lock(&fuse->lock);
842 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
843 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
844 hdr->nodeid, node ? node->name : "?");
845 if (node) {
846 __u64 n = req->nlookup;
847 while (n--) {
848 release_node_locked(node);
849 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700850 }
Jeff Brown6249b902012-05-26 14:32:54 -0700851 pthread_mutex_unlock(&fuse->lock);
852 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700853}
854
Jeff Brown6249b902012-05-26 14:32:54 -0700855static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700856 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
857{
Jeff Brown6249b902012-05-26 14:32:54 -0700858 struct node* node;
859 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860
Jeff Brown6249b902012-05-26 14:32:54 -0700861 pthread_mutex_lock(&fuse->lock);
862 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
863 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
864 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
865 pthread_mutex_unlock(&fuse->lock);
866
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700867 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700868 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700869 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700870 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700871 return -EACCES;
872 }
873
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700874 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700875}
876
Jeff Brown6249b902012-05-26 14:32:54 -0700877static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700878 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
879{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700880 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700881 struct node* node;
882 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700884
Jeff Brown6249b902012-05-26 14:32:54 -0700885 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700886 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700887 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
888 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
889 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
890 pthread_mutex_unlock(&fuse->lock);
891
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700892 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700893 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700895 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700896 return -EACCES;
897 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700898
Jeff Brown6249b902012-05-26 14:32:54 -0700899 /* XXX: incomplete implementation on purpose.
900 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700901
Jeff Brown6249b902012-05-26 14:32:54 -0700902 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
903 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700904 }
905
906 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
907 * are both set, then set it to the current time. Else, set it to the
908 * time specified in the request. Same goes for mtime. Use utimensat(2)
909 * as it allows ATIME and MTIME to be changed independently, and has
910 * nanosecond resolution which fuse also has.
911 */
912 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
913 times[0].tv_nsec = UTIME_OMIT;
914 times[1].tv_nsec = UTIME_OMIT;
915 if (req->valid & FATTR_ATIME) {
916 if (req->valid & FATTR_ATIME_NOW) {
917 times[0].tv_nsec = UTIME_NOW;
918 } else {
919 times[0].tv_sec = req->atime;
920 times[0].tv_nsec = req->atimensec;
921 }
922 }
923 if (req->valid & FATTR_MTIME) {
924 if (req->valid & FATTR_MTIME_NOW) {
925 times[1].tv_nsec = UTIME_NOW;
926 } else {
927 times[1].tv_sec = req->mtime;
928 times[1].tv_nsec = req->mtimensec;
929 }
930 }
Jeff Brown6249b902012-05-26 14:32:54 -0700931 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
932 handler->token, path, times[0].tv_sec, times[1].tv_sec);
933 if (utimensat(-1, path, times, 0) < 0) {
934 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700935 }
936 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700937 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700938}
939
Jeff Brown6249b902012-05-26 14:32:54 -0700940static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
942{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700943 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700944 struct node* parent_node;
945 char parent_path[PATH_MAX];
946 char child_path[PATH_MAX];
947 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948
Jeff Brown6249b902012-05-26 14:32:54 -0700949 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700950 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700951 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
952 parent_path, sizeof(parent_path));
953 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
954 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
955 pthread_mutex_unlock(&fuse->lock);
956
957 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
958 child_path, sizeof(child_path), 1))) {
959 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700960 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700961 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700962 return -EACCES;
963 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700964 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700965 if (mknod(child_path, mode, req->rdev) < 0) {
966 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967 }
Jeff Brown6249b902012-05-26 14:32:54 -0700968 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969}
970
Jeff Brown6249b902012-05-26 14:32:54 -0700971static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700972 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
973{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700974 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700975 struct node* parent_node;
976 char parent_path[PATH_MAX];
977 char child_path[PATH_MAX];
978 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979
Jeff Brown6249b902012-05-26 14:32:54 -0700980 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700981 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700982 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
983 parent_path, sizeof(parent_path));
984 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
985 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
986 pthread_mutex_unlock(&fuse->lock);
987
988 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
989 child_path, sizeof(child_path), 1))) {
990 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700991 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700992 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700993 return -EACCES;
994 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700995 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700996 if (mkdir(child_path, mode) < 0) {
997 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700998 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700999
1000 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
1001 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
1002 char nomedia[PATH_MAX];
1003 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
1004 if (touch(nomedia, 0664) != 0) {
1005 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1006 return -ENOENT;
1007 }
1008 }
1009 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
1010 char nomedia[PATH_MAX];
1011 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath);
1012 if (touch(nomedia, 0664) != 0) {
1013 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
1014 return -ENOENT;
1015 }
1016 }
1017
Jeff Brown6249b902012-05-26 14:32:54 -07001018 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001019}
1020
Jeff Brown6249b902012-05-26 14:32:54 -07001021static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001022 const struct fuse_in_header* hdr, const char* name)
1023{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001024 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001025 struct node* parent_node;
1026 char parent_path[PATH_MAX];
1027 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001028
Jeff Brown6249b902012-05-26 14:32:54 -07001029 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001030 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001031 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1032 parent_path, sizeof(parent_path));
1033 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1034 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1035 pthread_mutex_unlock(&fuse->lock);
1036
1037 if (!parent_node || !find_file_within(parent_path, name,
1038 child_path, sizeof(child_path), 1)) {
1039 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001040 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001041 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001042 return -EACCES;
1043 }
Jeff Brown6249b902012-05-26 14:32:54 -07001044 if (unlink(child_path) < 0) {
1045 return -errno;
1046 }
1047 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001048}
1049
Jeff Brown6249b902012-05-26 14:32:54 -07001050static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001051 const struct fuse_in_header* hdr, const char* name)
1052{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001053 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001054 struct node* parent_node;
1055 char parent_path[PATH_MAX];
1056 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001057
Jeff Brown6249b902012-05-26 14:32:54 -07001058 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001059 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001060 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1061 parent_path, sizeof(parent_path));
1062 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1063 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1064 pthread_mutex_unlock(&fuse->lock);
1065
1066 if (!parent_node || !find_file_within(parent_path, name,
1067 child_path, sizeof(child_path), 1)) {
1068 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001069 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001070 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001071 return -EACCES;
1072 }
Jeff Brown6249b902012-05-26 14:32:54 -07001073 if (rmdir(child_path) < 0) {
1074 return -errno;
1075 }
1076 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001077}
1078
Jeff Brown6249b902012-05-26 14:32:54 -07001079static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001080 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001081 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001082{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001083 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001084 struct node* old_parent_node;
1085 struct node* new_parent_node;
1086 struct node* child_node;
1087 char old_parent_path[PATH_MAX];
1088 char new_parent_path[PATH_MAX];
1089 char old_child_path[PATH_MAX];
1090 char new_child_path[PATH_MAX];
1091 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001093
Jeff Brown6249b902012-05-26 14:32:54 -07001094 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001095 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001096 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1097 old_parent_path, sizeof(old_parent_path));
1098 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1099 new_parent_path, sizeof(new_parent_path));
1100 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1101 old_name, new_name,
1102 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1103 req->newdir, new_parent_node ? new_parent_node->name : "?");
1104 if (!old_parent_node || !new_parent_node) {
1105 res = -ENOENT;
1106 goto lookup_error;
1107 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001108 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001109 res = -EACCES;
1110 goto lookup_error;
1111 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001112 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001113 res = -EACCES;
1114 goto lookup_error;
1115 }
Jeff Brown6249b902012-05-26 14:32:54 -07001116 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1117 if (!child_node || get_node_path_locked(child_node,
1118 old_child_path, sizeof(old_child_path)) < 0) {
1119 res = -ENOENT;
1120 goto lookup_error;
1121 }
1122 acquire_node_locked(child_node);
1123 pthread_mutex_unlock(&fuse->lock);
1124
1125 /* Special case for renaming a file where destination is same path
1126 * differing only by case. In this case we don't want to look for a case
1127 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1128 */
1129 int search = old_parent_node != new_parent_node
1130 || strcasecmp(old_name, new_name);
1131 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1132 new_child_path, sizeof(new_child_path), search))) {
1133 res = -ENOENT;
1134 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001135 }
1136
Jeff Brown6249b902012-05-26 14:32:54 -07001137 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1138 res = rename(old_child_path, new_child_path);
1139 if (res < 0) {
1140 res = -errno;
1141 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142 }
1143
Jeff Brown6249b902012-05-26 14:32:54 -07001144 pthread_mutex_lock(&fuse->lock);
1145 res = rename_node_locked(child_node, new_name, new_actual_name);
1146 if (!res) {
1147 remove_node_from_parent_locked(child_node);
1148 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001149 }
Jeff Brown6249b902012-05-26 14:32:54 -07001150 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001151
Jeff Brown6249b902012-05-26 14:32:54 -07001152io_error:
1153 pthread_mutex_lock(&fuse->lock);
1154done:
1155 release_node_locked(child_node);
1156lookup_error:
1157 pthread_mutex_unlock(&fuse->lock);
1158 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001159}
1160
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001161static int open_flags_to_access_mode(int open_flags) {
1162 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1163 return R_OK;
1164 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1165 return W_OK;
1166 } else {
1167 /* Probably O_RDRW, but treat as default to be safe */
1168 return R_OK | W_OK;
1169 }
1170}
1171
Jeff Brown6249b902012-05-26 14:32:54 -07001172static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001173 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1174{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001175 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001176 struct node* node;
1177 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001178 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001179 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001180
Jeff Brown6249b902012-05-26 14:32:54 -07001181 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001182 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001183 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1184 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1185 req->flags, hdr->nodeid, node ? node->name : "?");
1186 pthread_mutex_unlock(&fuse->lock);
1187
1188 if (!node) {
1189 return -ENOENT;
1190 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001191 if (!check_caller_access_to_node(fuse, hdr, node,
1192 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001193 return -EACCES;
1194 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195 h = malloc(sizeof(*h));
1196 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001197 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001198 }
Jeff Brown6249b902012-05-26 14:32:54 -07001199 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001200 h->fd = open(path, req->flags);
1201 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001202 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001203 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001204 }
1205 out.fh = ptr_to_id(h);
1206 out.open_flags = 0;
1207 out.padding = 0;
1208 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001209 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001210}
1211
Jeff Brown6249b902012-05-26 14:32:54 -07001212static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001213 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1214{
1215 struct handle *h = id_to_ptr(req->fh);
1216 __u64 unique = hdr->unique;
1217 __u32 size = req->size;
1218 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001219 int res;
Arpad Horvath80b435a2014-02-14 16:42:27 -08001220 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001221
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1223 * overlaps the request buffer and will clobber data in the request. This
1224 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001225
1226 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1227 h, h->fd, size, offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001228 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001229 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001230 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001231 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001232 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001233 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001234 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001235 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001236 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237}
1238
Jeff Brown6249b902012-05-26 14:32:54 -07001239static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001240 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1241 const void* buffer)
1242{
1243 struct fuse_write_out out;
1244 struct handle *h = id_to_ptr(req->fh);
1245 int res;
Arpad Horvath49e93442014-02-18 10:18:25 +01001246 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001247
Arpad Horvath49e93442014-02-18 10:18:25 +01001248 if (req->flags & O_DIRECT) {
1249 memcpy(aligned_buffer, buffer, req->size);
1250 buffer = (const __u8*) aligned_buffer;
1251 }
Jeff Brown6249b902012-05-26 14:32:54 -07001252
1253 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1254 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001255 res = pwrite64(h->fd, buffer, req->size, req->offset);
1256 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001257 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001258 }
1259 out.size = res;
1260 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001261 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262}
1263
Jeff Brown6249b902012-05-26 14:32:54 -07001264static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001265 const struct fuse_in_header* hdr)
1266{
Jeff Brown6249b902012-05-26 14:32:54 -07001267 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001268 struct statfs stat;
1269 struct fuse_statfs_out out;
1270 int res;
1271
Jeff Brown6249b902012-05-26 14:32:54 -07001272 pthread_mutex_lock(&fuse->lock);
1273 TRACE("[%d] STATFS\n", handler->token);
1274 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1275 pthread_mutex_unlock(&fuse->lock);
1276 if (res < 0) {
1277 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001278 }
Jeff Brown6249b902012-05-26 14:32:54 -07001279 if (statfs(fuse->root.name, &stat) < 0) {
1280 return -errno;
1281 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001282 memset(&out, 0, sizeof(out));
1283 out.st.blocks = stat.f_blocks;
1284 out.st.bfree = stat.f_bfree;
1285 out.st.bavail = stat.f_bavail;
1286 out.st.files = stat.f_files;
1287 out.st.ffree = stat.f_ffree;
1288 out.st.bsize = stat.f_bsize;
1289 out.st.namelen = stat.f_namelen;
1290 out.st.frsize = stat.f_frsize;
1291 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001292 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001293}
1294
Jeff Brown6249b902012-05-26 14:32:54 -07001295static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001296 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1297{
1298 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001299
1300 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001301 close(h->fd);
1302 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001303 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001304}
1305
Jeff Brown6249b902012-05-26 14:32:54 -07001306static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001307 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1308{
1309 int is_data_sync = req->fsync_flags & 1;
1310 struct handle *h = id_to_ptr(req->fh);
1311 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001312
1313 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
1314 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001315 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
1316 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001317 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001318 }
Jeff Brown6249b902012-05-26 14:32:54 -07001319 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001320}
1321
Jeff Brown6249b902012-05-26 14:32:54 -07001322static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001323 const struct fuse_in_header* hdr)
1324{
Jeff Brown6249b902012-05-26 14:32:54 -07001325 TRACE("[%d] FLUSH\n", handler->token);
1326 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001327}
1328
Jeff Brown6249b902012-05-26 14:32:54 -07001329static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001330 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1331{
Jeff Brown6249b902012-05-26 14:32:54 -07001332 struct node* node;
1333 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001334 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001335 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001336
Jeff Brown6249b902012-05-26 14:32:54 -07001337 pthread_mutex_lock(&fuse->lock);
1338 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1339 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1340 hdr->nodeid, node ? node->name : "?");
1341 pthread_mutex_unlock(&fuse->lock);
1342
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001343 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001344 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001345 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001346 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001347 return -EACCES;
1348 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349 h = malloc(sizeof(*h));
1350 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001351 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001352 }
Jeff Brown6249b902012-05-26 14:32:54 -07001353 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001355 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001356 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001357 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001358 }
1359 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001360 out.open_flags = 0;
1361 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001363 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001364}
1365
Jeff Brown6249b902012-05-26 14:32:54 -07001366static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1368{
1369 char buffer[8192];
1370 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1371 struct dirent *de;
1372 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001373
1374 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 if (req->offset == 0) {
1376 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001377 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378 rewinddir(h->d);
1379 }
1380 de = readdir(h->d);
1381 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001382 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001383 }
1384 fde->ino = FUSE_UNKNOWN_INO;
1385 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1386 fde->off = req->offset + 1;
1387 fde->type = de->d_type;
1388 fde->namelen = strlen(de->d_name);
1389 memcpy(fde->name, de->d_name, fde->namelen + 1);
1390 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001391 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1392 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001393}
1394
Jeff Brown6249b902012-05-26 14:32:54 -07001395static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001396 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1397{
1398 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001399
1400 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001401 closedir(h->d);
1402 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001403 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001404}
1405
Jeff Brown6249b902012-05-26 14:32:54 -07001406static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001407 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1408{
1409 struct fuse_init_out out;
1410
Jeff Brown6249b902012-05-26 14:32:54 -07001411 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1412 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001413 out.major = FUSE_KERNEL_VERSION;
1414 out.minor = FUSE_KERNEL_MINOR_VERSION;
1415 out.max_readahead = req->max_readahead;
1416 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1417 out.max_background = 32;
1418 out.congestion_threshold = 32;
1419 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001420 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001421 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001422}
1423
Jeff Brown6249b902012-05-26 14:32:54 -07001424static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1426{
Brian Swetland03ee9472010-08-12 18:01:08 -07001427 switch (hdr->opcode) {
1428 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001429 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001430 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001431 }
Jeff Brown84715842012-05-25 14:07:47 -07001432
Brian Swetland03ee9472010-08-12 18:01:08 -07001433 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001434 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001435 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001436 }
Jeff Brown84715842012-05-25 14:07:47 -07001437
Brian Swetland03ee9472010-08-12 18:01:08 -07001438 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001439 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001440 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001441 }
Jeff Brown84715842012-05-25 14:07:47 -07001442
Brian Swetland03ee9472010-08-12 18:01:08 -07001443 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001444 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001445 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001446 }
Jeff Brown84715842012-05-25 14:07:47 -07001447
Brian Swetland03ee9472010-08-12 18:01:08 -07001448// case FUSE_READLINK:
1449// case FUSE_SYMLINK:
1450 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001451 const struct fuse_mknod_in *req = data;
1452 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001453 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001454 }
Jeff Brown84715842012-05-25 14:07:47 -07001455
Brian Swetland03ee9472010-08-12 18:01:08 -07001456 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001457 const struct fuse_mkdir_in *req = data;
1458 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001459 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001460 }
Jeff Brown84715842012-05-25 14:07:47 -07001461
Brian Swetland03ee9472010-08-12 18:01:08 -07001462 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001463 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001464 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001465 }
Jeff Brown84715842012-05-25 14:07:47 -07001466
Brian Swetland03ee9472010-08-12 18:01:08 -07001467 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001468 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001469 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001470 }
Jeff Brown84715842012-05-25 14:07:47 -07001471
Brian Swetland03ee9472010-08-12 18:01:08 -07001472 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001473 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001474 const char *old_name = ((const char*) data) + sizeof(*req);
1475 const char *new_name = old_name + strlen(old_name) + 1;
1476 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001477 }
Jeff Brown84715842012-05-25 14:07:47 -07001478
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001479// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001480 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001481 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001482 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001483 }
Jeff Brown84715842012-05-25 14:07:47 -07001484
Brian Swetland03ee9472010-08-12 18:01:08 -07001485 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001486 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001487 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001488 }
Jeff Brown84715842012-05-25 14:07:47 -07001489
Brian Swetland03ee9472010-08-12 18:01:08 -07001490 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001491 const struct fuse_write_in *req = data;
1492 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001493 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001494 }
Jeff Brown84715842012-05-25 14:07:47 -07001495
Mike Lockwood4553b082010-08-16 14:14:44 -04001496 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001497 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001498 }
Jeff Brown84715842012-05-25 14:07:47 -07001499
Brian Swetland03ee9472010-08-12 18:01:08 -07001500 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001501 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001502 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001503 }
Jeff Brown84715842012-05-25 14:07:47 -07001504
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001505 case FUSE_FSYNC:
1506 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001507 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001508 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001509 }
1510
Brian Swetland03ee9472010-08-12 18:01:08 -07001511// case FUSE_SETXATTR:
1512// case FUSE_GETXATTR:
1513// case FUSE_LISTXATTR:
1514// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001515 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001516 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001517 }
1518
Brian Swetland03ee9472010-08-12 18:01:08 -07001519 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001520 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001521 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001522 }
Jeff Brown84715842012-05-25 14:07:47 -07001523
Brian Swetland03ee9472010-08-12 18:01:08 -07001524 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001525 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001526 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001527 }
Jeff Brown84715842012-05-25 14:07:47 -07001528
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001530 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001531 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001532 }
Jeff Brown84715842012-05-25 14:07:47 -07001533
Brian Swetland03ee9472010-08-12 18:01:08 -07001534 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001535 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001536 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001537 }
Jeff Brown84715842012-05-25 14:07:47 -07001538
Brian Swetland03ee9472010-08-12 18:01:08 -07001539 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001540 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1541 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1542 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001543 }
Jeff Brown84715842012-05-25 14:07:47 -07001544 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001545}
1546
Jeff Brown6249b902012-05-26 14:32:54 -07001547static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001548{
Jeff Brown6249b902012-05-26 14:32:54 -07001549 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001550 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001551 ssize_t len = read(fuse->fd,
1552 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001554 if (errno != EINTR) {
1555 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1556 }
1557 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001558 }
Jeff Brown84715842012-05-25 14:07:47 -07001559
1560 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001561 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1562 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001563 }
1564
Jeff Brown7729d242012-05-25 15:35:28 -07001565 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001566 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001567 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1568 handler->token, (size_t)len, hdr->len);
1569 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001570 }
1571
Jeff Brown7729d242012-05-25 15:35:28 -07001572 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001573 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001574 __u64 unique = hdr->unique;
1575 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001576
1577 /* We do not access the request again after this point because the underlying
1578 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001579
1580 if (res != NO_STATUS) {
1581 if (res) {
1582 TRACE("[%d] ERROR %d\n", handler->token, res);
1583 }
1584 fuse_status(fuse, unique, res);
1585 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001586 }
1587}
1588
Jeff Brown6249b902012-05-26 14:32:54 -07001589static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001590{
Jeff Brown6249b902012-05-26 14:32:54 -07001591 struct fuse_handler* handler = data;
1592 handle_fuse_requests(handler);
1593 return NULL;
1594}
1595
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001596static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001597 Hashmap* map = context;
1598 hashmapRemove(map, key);
1599 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001600 return true;
1601}
1602
1603static bool remove_int_to_null(void *key, void *value, void *context) {
1604 Hashmap* map = context;
1605 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001606 return true;
1607}
1608
1609static int read_package_list(struct fuse *fuse) {
1610 pthread_mutex_lock(&fuse->lock);
1611
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001612 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1613 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001614
1615 FILE* file = fopen(kPackagesListFile, "r");
1616 if (!file) {
1617 ERROR("failed to open package list: %s\n", strerror(errno));
1618 pthread_mutex_unlock(&fuse->lock);
1619 return -1;
1620 }
1621
1622 char buf[512];
1623 while (fgets(buf, sizeof(buf), file) != NULL) {
1624 char package_name[512];
1625 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001626 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001627
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001628 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1629 char* package_name_dup = strdup(package_name);
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001630 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001631
1632 char* token = strtok(gids, ",");
1633 while (token != NULL) {
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001634 if (strtoul(token, NULL, 10) == fuse->write_gid) {
Elliott Hughes5d9fe772014-02-05 17:50:35 -08001635 hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001636 break;
1637 }
1638 token = strtok(NULL, ",");
1639 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001640 }
1641 }
1642
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001643 TRACE("read_package_list: found %d packages, %d with write_gid\n",
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001644 hashmapSize(fuse->package_to_appid),
1645 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001646 fclose(file);
1647 pthread_mutex_unlock(&fuse->lock);
1648 return 0;
1649}
1650
1651static void watch_package_list(struct fuse* fuse) {
1652 struct inotify_event *event;
1653 char event_buf[512];
1654
1655 int nfd = inotify_init();
1656 if (nfd < 0) {
1657 ERROR("inotify_init failed: %s\n", strerror(errno));
1658 return;
1659 }
1660
1661 bool active = false;
1662 while (1) {
1663 if (!active) {
1664 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1665 if (res == -1) {
1666 if (errno == ENOENT || errno == EACCES) {
1667 /* Framework may not have created yet, sleep and retry */
1668 ERROR("missing packages.list; retrying\n");
1669 sleep(3);
1670 continue;
1671 } else {
1672 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1673 return;
1674 }
1675 }
1676
1677 /* Watch above will tell us about any future changes, so
1678 * read the current state. */
1679 if (read_package_list(fuse) == -1) {
1680 ERROR("read_package_list failed: %s\n", strerror(errno));
1681 return;
1682 }
1683 active = true;
1684 }
1685
1686 int event_pos = 0;
1687 int res = read(nfd, event_buf, sizeof(event_buf));
1688 if (res < (int) sizeof(*event)) {
1689 if (errno == EINTR)
1690 continue;
1691 ERROR("failed to read inotify event: %s\n", strerror(errno));
1692 return;
1693 }
1694
1695 while (res >= (int) sizeof(*event)) {
1696 int event_size;
1697 event = (struct inotify_event *) (event_buf + event_pos);
1698
1699 TRACE("inotify event: %08x\n", event->mask);
1700 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1701 /* Previously watched file was deleted, probably due to move
1702 * that swapped in new data; re-arm the watch and read. */
1703 active = false;
1704 }
1705
1706 event_size = sizeof(*event) + event->len;
1707 res -= event_size;
1708 event_pos += event_size;
1709 }
1710 }
1711}
1712
Jeff Brown6249b902012-05-26 14:32:54 -07001713static int ignite_fuse(struct fuse* fuse, int num_threads)
1714{
1715 struct fuse_handler* handlers;
1716 int i;
1717
1718 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1719 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001720 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001721 return -ENOMEM;
1722 }
1723
1724 for (i = 0; i < num_threads; i++) {
1725 handlers[i].fuse = fuse;
1726 handlers[i].token = i;
1727 }
1728
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001729 /* When deriving permissions, this thread is used to process inotify events,
1730 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001731 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001732 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001733 pthread_t thread;
1734 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1735 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001736 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001737 goto quit;
1738 }
1739 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001740
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001741 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001742 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001743 } else {
1744 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001745 }
1746
1747 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001748
1749 /* don't bother killing all of the other threads or freeing anything,
1750 * should never get here anyhow */
1751quit:
1752 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001753}
1754
Mike Lockwood4f35e622011-01-12 14:39:44 -05001755static int usage()
1756{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001757 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1758 " -u: specify UID to run as\n"
1759 " -g: specify GID to run as\n"
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001760 " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001761 " -t: specify number of threads to use (default %d)\n"
1762 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001763 " -l: derive file permissions based on legacy internal layout\n"
1764 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001765 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001766 return 1;
1767}
1768
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001769static int run(const char* source_path, const char* dest_path, uid_t uid,
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001770 gid_t gid, gid_t write_gid, int num_threads, derive_t derive,
1771 bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001772 int fd;
1773 char opts[256];
1774 int res;
1775 struct fuse fuse;
1776
1777 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001778 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001779
1780 fd = open("/dev/fuse", O_RDWR);
1781 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001782 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001783 return -1;
1784 }
1785
1786 snprintf(opts, sizeof(opts),
1787 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1788 fd, uid, gid);
1789
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001790 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001791 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001792 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1793 goto error;
1794 }
1795
1796 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1797 if (res < 0) {
1798 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001799 goto error;
1800 }
1801
1802 res = setgid(gid);
1803 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001804 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001805 goto error;
1806 }
1807
1808 res = setuid(uid);
1809 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001810 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001811 goto error;
1812 }
1813
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001814 fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001815
1816 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001817 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001818
1819 /* we do not attempt to umount the file system here because we are no longer
1820 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001821
1822error:
1823 close(fd);
1824 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001825}
1826
Brian Swetland03ee9472010-08-12 18:01:08 -07001827int main(int argc, char **argv)
1828{
Brian Swetland03ee9472010-08-12 18:01:08 -07001829 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001830 const char *source_path = NULL;
1831 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001832 uid_t uid = 0;
1833 gid_t gid = 0;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001834 gid_t write_gid = AID_SDCARD_RW;
Jeff Brown6249b902012-05-26 14:32:54 -07001835 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001836 derive_t derive = DERIVE_NONE;
1837 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001838 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001839 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001840
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001841 int opt;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001842 while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001843 switch (opt) {
1844 case 'u':
1845 uid = strtoul(optarg, NULL, 10);
1846 break;
1847 case 'g':
1848 gid = strtoul(optarg, NULL, 10);
1849 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001850 case 'w':
1851 write_gid = strtoul(optarg, NULL, 10);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001852 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001853 case 't':
1854 num_threads = strtoul(optarg, NULL, 10);
1855 break;
1856 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001857 derive = DERIVE_UNIFIED;
1858 break;
1859 case 'l':
1860 derive = DERIVE_LEGACY;
1861 break;
1862 case 's':
1863 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001864 break;
1865 case '?':
1866 default:
1867 return usage();
1868 }
1869 }
1870
1871 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001872 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001873 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001874 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001875 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001876 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001877 } else if (!uid) {
1878 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001879 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001880 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001881 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001882 ERROR("too many arguments\n");
1883 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001884 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001885 }
1886
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001887 if (!source_path) {
1888 ERROR("no source path specified\n");
1889 return usage();
1890 }
1891 if (!dest_path) {
1892 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001893 return usage();
1894 }
Jeff Brown26567352012-05-25 13:27:43 -07001895 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001896 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001897 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001898 }
Jeff Brown6249b902012-05-26 14:32:54 -07001899 if (num_threads < 1) {
1900 ERROR("number of threads must be at least 1\n");
1901 return usage();
1902 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001903 if (split_perms && derive == DERIVE_NONE) {
1904 ERROR("cannot split permissions without deriving\n");
1905 return usage();
1906 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001907
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001908 rlim.rlim_cur = 8192;
1909 rlim.rlim_max = 8192;
1910 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1911 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1912 }
1913
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001914 res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001915 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001916}